SystemC 3.0.0
Accellera SystemC proof-of-concept library
sc_context.h
Go to the documentation of this file.
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/*****************************************************************************
21
22 sc_context.h -
23
24 Original Author: Martin Janssen, Synopsys, Inc.
25
26 *****************************************************************************/
27
28/*****************************************************************************
29
30 MODIFICATION LOG - modifiers, enter your name, affiliation, date and
31 changes you are making here.
32
33 Name, Affiliation, Date:
34 Description of Modification:
35
36 *****************************************************************************/
37
38// $Log: sc_context.h,v $
39// Revision 1.2 2011/08/24 22:05:43 acg
40// Torsten Maehne: initialization changes to remove warnings.
41//
42// Revision 1.1.1.1 2006/12/15 20:20:04 acg
43// SystemC 2.3
44//
45// Revision 1.5 2006/05/26 20:36:52 acg
46// Andy Goodrich: added a using for sc_core::default_ptr_hash_fn to keep HP
47// aCC happy.
48//
49// Revision 1.4 2006/03/21 00:00:31 acg
50// Andy Goodrich: changed name of sc_get_current_process_base() to be
51// sc_get_current_process_b() since its returning an sc_process_b instance.
52//
53// Revision 1.3 2006/01/13 18:53:57 acg
54// Andy Goodrich: added $Log command so that CVS comments are reproduced in
55// the source.
56//
57
58#ifndef SC_CONTEXT_H
59#define SC_CONTEXT_H
60
61
65#include "sysc/utils/sc_hash.h"
66
67
68namespace sc_core {
69 class sc_process_b;
70}
71
72using sc_core::default_ptr_hash_fn; // To keep HP aCC happy.
73
74namespace sc_dt
75{
76
77// classes defined in this module
78class sc_without_context;
79template <class T> class sc_global;
80template <class T> class sc_context;
81
82
83// ----------------------------------------------------------------------------
84// CLASS : sc_without_context
85//
86// Empty class that is used for its type only.
87// ----------------------------------------------------------------------------
88
90
91
92// ----------------------------------------------------------------------------
93// TEMPLATE CLASS : sc_global
94//
95// Template global variable class; singleton; co-routine safe.
96// ----------------------------------------------------------------------------
97
98template <class T>
100{
101
102 sc_global();
103
104 void update();
105
106public:
107
108 static sc_global<T>* instance();
109
110 const T*& value_ptr();
111
112private:
113 static sc_global<T>* m_instance;
114
116 void* m_proc; // context (current process or NULL)
117 const T* m_value_ptr;
118
119};
120
121
122// ----------------------------------------------------------------------------
123// ENUM : sc_context_begin
124//
125// Enumeration of context begin options.
126// ----------------------------------------------------------------------------
127
129{
133
134
135// ----------------------------------------------------------------------------
136// CLASS : sc_context
137//
138// Template context class; co-routine safe.
139// ----------------------------------------------------------------------------
140
141template <class T>
143{
144 // disabled
145 sc_context( const sc_context<T>& );
146 void* operator new( std::size_t );
147
148public:
149
150 explicit sc_context( const T&, sc_context_begin = SC_NOW );
151 ~sc_context();
152
153 void begin();
154 void end();
155
156 static const T& default_value();
157 const T& value() const;
158
159private:
160 sc_context& operator=(const sc_context&) /* = delete */;
161
162 const T m_value;
163 const T*& m_def_value_ptr;
164 const T* m_old_value_ptr;
165};
166
167
168// IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
169
170// ----------------------------------------------------------------------------
171// TEMPLATE CLASS : sc_global
172//
173// Template global variable class; singleton; co-routine safe.
174// ----------------------------------------------------------------------------
175
176template <class T>
178
179template <class T>
180inline
182 : m_map()
183 // use &m_instance as unique "non-process" key (NULL denotes 'sc_main' context)
184 , m_proc( &m_instance )
185 , m_value_ptr( 0 )
186{}
187
188
189template <class T>
190inline
191void
192sc_global<T>::update()
193{
195 if( p != m_proc )
196 {
197 const T* vp = m_map[p];
198 if( vp == 0 )
199 {
200 vp = new T( sc_without_context() );
201 m_map.insert( p, vp );
202 }
203 m_proc = p;
204 m_value_ptr = vp;
205 }
206}
207
208
209template <class T>
210inline
211sc_global<T>*
213{
214 if( m_instance == 0 )
215 {
216 m_instance = new sc_global<T>;
217 }
218 return m_instance;
219}
220
221
222template <class T>
223inline
224const T*&
226{
227 update();
228 return m_value_ptr;
229}
230
231
232// ----------------------------------------------------------------------------
233// CLASS : sc_context
234//
235// Template context class; co-routine safe.
236// ----------------------------------------------------------------------------
237
238template <class T>
239inline
241: m_value( value_ ),
242 m_def_value_ptr( sc_global<T>::instance()->value_ptr() ),
243 m_old_value_ptr( 0 )
244{
245 if( begin_ == SC_NOW )
246 {
247 m_old_value_ptr = m_def_value_ptr;
248 m_def_value_ptr = &m_value;
249 }
250}
251
252template <class T>
253inline
255{
256 if( m_old_value_ptr != 0 )
257 {
258 m_def_value_ptr = m_old_value_ptr;
259 m_old_value_ptr = 0;
260 }
261}
262
263
264template <class T>
265inline
266void
268{
269 if( m_old_value_ptr == 0 )
270 {
271 m_old_value_ptr = m_def_value_ptr;
272 m_def_value_ptr = &m_value;
273 }
274 else
275 {
277 }
278}
279
280template <class T>
281inline
282void
284{
285 if( m_old_value_ptr != 0 )
286 {
287 m_def_value_ptr = m_old_value_ptr;
288 m_old_value_ptr = 0;
289 }
290 else
291 {
293 }
294}
295
296
297template <class T>
298inline
299const T&
301{
302 return *sc_global<T>::instance()->value_ptr();
303}
304
305template <class T>
306inline
307const T&
309{
310 return m_value;
311}
312
313} // namespace sc_dt
314
315
316#endif
317
318// Taf!
#define SC_API
Definition: sc_cmnhdr.h:148
#define SC_REPORT_ERROR(msg_type, msg)
Definition: sc_report.h:217
const char SC_ID_CONTEXT_BEGIN_FAILED_[]
SC_API unsigned default_ptr_hash_fn(const void *)
const char SC_ID_CONTEXT_END_FAILED_[]
sc_process_b * sc_get_current_process_b()
sc_context_begin
Definition: sc_context.h:129
@ SC_NOW
Definition: sc_context.h:130
@ SC_LATER
Definition: sc_context.h:131
static sc_global< T > * instance()
Definition: sc_context.h:212
const T *& value_ptr()
Definition: sc_context.h:225
static const T & default_value()
Definition: sc_context.h:300
const T & value() const
Definition: sc_context.h:308