SystemC  2.3.2
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 
62 #include "sysc/kernel/sc_cmnhdr.h"
65 #include "sysc/utils/sc_hash.h"
66 
67 
68 namespace sc_core {
69  class sc_process_b;
70 }
71 
72 using sc_core::default_ptr_hash_fn; // To keep HP aCC happy.
73 
74 namespace sc_dt
75 {
76 
77 // classes defined in this module
78 class sc_without_context;
79 template <class T> class sc_global;
80 template <class T> class sc_context;
81 
82 
90 
91 
98 template <class T>
99 class sc_global
100 {
101 
102  sc_global();
103 
104  void update();
105 
106 public:
107 
108  static sc_global<T>* instance();
109 
110  const T*& value_ptr();
111 
112 private:
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 
129 {
132 };
133 
134 
141 template <class T>
142 class sc_context
143 {
144  // disabled
145  sc_context( const sc_context<T>& );
146  void* operator new( std::size_t );
147 
148 public:
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 
159 private:
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 
176 template <class T>
178 
179 template <class T>
180 inline
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 
189 template <class T>
190 inline
191 void
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 
209 template <class T>
210 inline
213 {
214  if( m_instance == 0 )
215  {
216  m_instance = new sc_global<T>;
217  }
218  return m_instance;
219 }
220 
221 
222 template <class T>
223 inline
224 const T*&
226 {
227  update();
228  return m_value_ptr;
229 }
230 
231 
238 template <class T>
239 inline
240 sc_context<T>::sc_context( const T& value_, sc_context_begin begin_ )
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 
252 template <class T>
253 inline
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 
264 template <class T>
265 inline
266 void
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  {
276  SC_REPORT_ERROR( sc_core::SC_ID_CONTEXT_BEGIN_FAILED_, 0 );
277  }
278 }
279 
280 template <class T>
281 inline
282 void
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  {
292  SC_REPORT_ERROR( sc_core::SC_ID_CONTEXT_END_FAILED_, 0 );
293  }
294 }
295 
296 
297 template <class T>
298 inline
299 const T&
301 {
302  return *sc_global<T>::instance()->value_ptr();
303 }
304 
305 template <class T>
306 inline
307 const T&
309 {
310  return m_value;
311 }
312 
313 } // namespace sc_dt
314 
315 
316 #endif
317 
318 // Taf!
Report ids for the datatypes/fx code.
sc_process_b * sc_get_current_process_b()
static sc_global< T > * instance()
Definition: sc_context.h:212
Empty class that is used for its type only.
Definition: sc_context.h:89
static const T & default_value()
Definition: sc_context.h:300
Template context class; co-routine safe.
Definition: sc_context.h:80
Template global variable class; singleton; co-routine safe.
Definition: sc_context.h:79
sc_clock period is zero sc_clock low time is zero sc_fifo< T > cannot have more than one writer bind interface to port failed complete binding failed remove port failed insert primitive channel failed sc_signal< T > cannot have more than one driver resolved port not bound to resolved signal sc_semaphore requires an initial value
SC_API unsigned default_ptr_hash_fn(const void *)
const T & value() const
Definition: sc_context.h:308
Definition of the simulation context class.
#define SC_REPORT_ERROR(msg_type, msg)
Definition: sc_report.h:223
sc_process_b sc_process_b
Definition: sc_process.h:458
#define SC_API
Definition: sc_cmnhdr.h:168
sc_context_begin
Enumeration of context begin options.
Definition: sc_context.h:128