tlm_array.h revision 12027:1eb7dc7aa10b
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_ARRAY_H__
21#define __TLM_ARRAY_H__
22
23#include <systemc>
24#include <exception>
25// unused for the time being: #include <cassert>
26
27namespace tlm {
28
29//
30// To the LRM writer: the below class is an artifact of the tlm_generic_payload
31//                    implementation and not part of the core TLM standard
32//
33
34
35// This implements a lean and fast array class that supports array expansion on
36// request. The class is primarily used in the tlm_generic_payload class for
37// storing the pointers to the extensions.
38//
39// Individual array elements can be accessed through the [] operators, and the
40// array length is returned by the size() method.
41//
42// The size can be dynamically expanded using the expand(uint) method. There
43// is no shrinking mechanism implemented, because the extension mechanism
44// does not require this feature. Bear in mind that calling the expand method
45// may invalidate all direct pointers into the array.
46
47
48//the tlm_array shall always be used with T=tlm_extension_base*
49template <typename T>
50class tlm_array
51  : private std::vector<T>
52{
53    typedef std::vector<T>                base_type;
54    typedef typename base_type::size_type size_type;
55public:
56
57    // constructor:
58    tlm_array(size_type size = 0, T const & default_value = T() )
59        : base_type(size,default_value)
60        , m_entries()
61        , m_default(default_value)
62    {
63        //m_entries.reserve(size); // optional
64    }
65
66    // copy constructor:
67    // tlm_array(const tlm_array& orig) = default;
68
69    // destructor:
70    // ~tlm_array() = default;
71
72    // operators for dereferencing:
73    using base_type::operator[];
74
75    // array size:
76    using base_type::size;
77
78    // expand the array if needed:
79    void expand(size_type new_size)
80    {
81        if (new_size > size())
82        {
83            base_type::resize(new_size);
84            //m_entries.reserve(new_size); // optional
85        }
86    }
87
88    static const char* const kind_string;
89    const char* kind() const { return kind_string; }
90
91    //this function shall get a pointer to a array slot
92    // it stores this slot in a cache of active slots
93    void insert_in_cache(T* p)
94    {
95        //assert( (p-&(*this)[0]) < size() );
96        m_entries.push_back( p-&(*this)[0] );
97    }
98
99    //this functions clears all active slots of the array
100    void free_entire_cache()
101    {
102        while(m_entries.size())
103        {
104            if ((*this)[m_entries.back()])      //we make sure no one cleared the slot manually
105              (*this)[m_entries.back()]->free();//...and then we call free on the content of the slot
106            (*this)[m_entries.back()]=0;        //afterwards we set the slot to NULL
107            m_entries.pop_back();
108        }
109    }
110
111protected:
112    std::vector<size_type> m_entries;
113    T m_default;
114
115    // disabled:
116    tlm_array& operator=(const tlm_array<T>&);
117};
118
119
120template <typename T>
121const char* const tlm_array<T>::kind_string = "tlm_array";
122
123} // namespace tlm
124
125#endif /* __TLM_ARRAY_H__ */
126