TLM-2.0  2.0.3
Accellera TLM-2.0 proof-of-concept library
tlm_array.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  The following code is derived, directly or indirectly, from the SystemC
4  source code Copyright (c) 1996-2014 by all Contributors.
5  All Rights reserved.
6 
7  The contents of this file are subject to the restrictions and limitations
8  set forth in the SystemC Open Source License (the "License");
9  You may not use this file except in compliance with such restrictions and
10  limitations. You may obtain instructions on how to receive a copy of the
11  License at http://www.accellera.org/. Software distributed by Contributors
12  under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
13  ANY KIND, either express or implied. See the License for the specific
14  language governing rights and limitations under the License.
15 
16  Author: Olaf Scheufen
17 *****************************************************************************/
18 
19 #ifndef __TLM_ARRAY_H__
20 #define __TLM_ARRAY_H__
21 
22 #include <systemc>
23 #include <exception>
24 // unused for the time being: #include <cassert>
25 
26 namespace tlm {
27 
28 //
29 // To the LRM writer: the below class is an artifact of the tlm_generic_payload
30 // implementation and not part of the core TLM standard
31 //
32 
33 
34 // This implements a lean and fast array class that supports array expansion on
35 // request. The class is primarily used in the tlm_generic_payload class for
36 // storing the pointers to the extensions.
37 //
38 // Individual array elements can be accessed through the [] operators, and the
39 // array length is returned by the size() method.
40 //
41 // The size can be dynamically expanded using the expand(uint) method. There
42 // is no shrinking mechanism implemented, because the extension mechanism
43 // does not require this feature. Bear in mind that calling the expand method
44 // may invalidate all direct pointers into the array.
45 
46 
47 //the tlm_array shall always be used with T=tlm_extension_base*
48 template <typename T>
49 class tlm_array
50  : private std::vector<T>
51 {
52  typedef std::vector<T> base_type;
53  typedef typename base_type::size_type size_type;
54 public:
55 
56  // constructor:
57  tlm_array(size_type size = 0, T const & default_value = T() )
58  : base_type(size,default_value)
59  , m_entries()
60  , m_default(default_value)
61  {
62  //m_entries.reserve(size); // optional
63  }
64 
65  // copy constructor:
66  // tlm_array(const tlm_array& orig) = default;
67 
68  // destructor:
69  // ~tlm_array() = default;
70 
71  // operators for dereferencing:
72  using base_type::operator[];
73 
74  // array size:
75  using base_type::size;
76 
77  // expand the array if needed:
78  void expand(size_type new_size)
79  {
80  if (new_size > size())
81  {
82  base_type::resize(new_size);
83  //m_entries.reserve(new_size); // optional
84  }
85  }
86 
87  static const char* const kind_string;
88  const char* kind() const { return kind_string; }
89 
90  //this function shall get a pointer to a array slot
91  // it stores this slot in a cache of active slots
92  void insert_in_cache(T* p)
93  {
94  //assert( (p-&(*this)[0]) < size() );
95  m_entries.push_back( p-&(*this)[0] );
96  }
97 
98  //this functions clears all active slots of the array
100  {
101  while(m_entries.size())
102  {
103  if ((*this)[m_entries.back()]) //we make sure no one cleared the slot manually
104  (*this)[m_entries.back()]->free();//...and then we call free on the content of the slot
105  (*this)[m_entries.back()]=0; //afterwards we set the slot to NULL
106  m_entries.pop_back();
107  }
108  }
109 
110 protected:
111  std::vector<size_type> m_entries;
113 
114  // disabled:
116 };
117 
118 
119 template <typename T>
120 const char* const tlm_array<T>::kind_string = "tlm_array";
121 
122 } // namespace tlm
123 
124 #endif /* __TLM_ARRAY_H__ */
void expand(size_type new_size)
Definition: tlm_array.h:78
void free_entire_cache()
Definition: tlm_array.h:99
void insert_in_cache(T *p)
Definition: tlm_array.h:92
const char * kind() const
Definition: tlm_array.h:88
static const char *const kind_string
Definition: tlm_array.h:87
std::vector< size_type > m_entries
Definition: tlm_array.h:111
tlm_array & operator=(const tlm_array< T > &)
tlm_array(size_type size=0, T const &default_value=T())
Definition: tlm_array.h:57