1 /*****************************************************************************
2 
3   Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4   more contributor license agreements.  See the NOTICE file distributed
5   with this work for additional information regarding copyright ownership.
6   Accellera licenses this file to you under the Apache License, Version 2.0
7   (the "License"); you may not use this file except in compliance with the
8   License.  You may obtain a copy of the License at
9 
10     http://www.apache.org/licenses/LICENSE-2.0
11 
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15   implied.  See the License for the specific language governing
16   permissions and limitations under the License.
17 
18  *****************************************************************************/
19 
20 #ifndef TLM_CORE_TLM2_TLM_ARRAY_H_INCLUDED_
21 #define TLM_CORE_TLM2_TLM_ARRAY_H_INCLUDED_
22 
23 #include <vector>
24 
25 #if defined(_MSC_VER) && !defined(SC_WIN_DLL_WARN)
26 #pragma warning(push)
27 #pragma warning(disable: 4251) // DLL import for std::string,vector
28 #endif
29 
30 namespace tlm {
31 
32 //
33 // To the LRM writer: the below class is an artifact of the tlm_generic_payload
34 //                    implementation and not part of the core TLM standard
35 //
36 
37 
38 // This implements a lean and fast array class that supports array expansion on
39 // request. The class is primarily used in the tlm_generic_payload class for
40 // storing the pointers to the extensions.
41 //
42 // Individual array elements can be accessed through the [] operators, and the
43 // array length is returned by the size() method.
44 //
45 // The size can be dynamically expanded using the expand(uint) method. There
46 // is no shrinking mechanism implemented, because the extension mechanism
47 // does not require this feature. Bear in mind that calling the expand method
48 // may invalidate all direct pointers into the array.
49 
50 
51 //the tlm_array shall always be used with T=tlm_extension_base*
52 template <typename T>
53 class tlm_array
54   : private std::vector<T>
55 {
56     typedef std::vector<T>                base_type;
57     typedef typename base_type::size_type size_type;
58 public:
59 
60     // constructor:
61     tlm_array(size_type size = 0)
base_type(size)62         : base_type(size)
63         , m_entries()
64     {
65         //m_entries.reserve(size); // optional
66     }
67 
68     // copy constructor:
69     // tlm_array(const tlm_array& orig) = default;
70 
71     // destructor:
72     // ~tlm_array() = default;
73 
74     // operators for dereferencing:
75     using base_type::operator[];
76 
77     // array size:
78     using base_type::size;
79 
80     // expand the array if needed:
expand(size_type new_size)81     void expand(size_type new_size)
82     {
83         if (new_size > size())
84         {
85             base_type::resize(new_size);
86             //m_entries.reserve(new_size); // optional
87         }
88     }
89 
90     static const char* const kind_string;
kind()91     const char* kind() const { return kind_string; }
92 
93     //this function shall get a pointer to a array slot
94     // it stores this slot in a cache of active slots
insert_in_cache(T * p)95     void insert_in_cache(T* p)
96     {
97         //sc_assert( (p-&(*this)[0]) < size() );
98         m_entries.push_back( p-&(*this)[0] );
99     }
100 
101     //this functions clears all active slots of the array
free_entire_cache()102     void free_entire_cache()
103     {
104         while(m_entries.size())
105         {
106             if ((*this)[m_entries.back()])      //we make sure no one cleared the slot manually
107               (*this)[m_entries.back()]->free();//...and then we call free on the content of the slot
108             (*this)[m_entries.back()]=0;        //afterwards we set the slot to NULL
109             m_entries.pop_back();
110         }
111     }
112 
113 protected:
114     std::vector<size_type> m_entries;
115 };
116 
117 template <typename T>
118 const char* const tlm_array<T>::kind_string = "tlm_array";
119 
120 #if defined(_MSC_VER) && !defined(SC_WIN_DLL_WARN)
121 #pragma warning(pop)
122 #endif
123 
124 } // namespace tlm
125 
126 #endif /* TLM_CORE_TLM2_TLM_ARRAY_H_INCLUDED_ */
127