Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

sc_context.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002 
00003   The following code is derived, directly or indirectly, from the SystemC
00004   source code Copyright (c) 1996-2004 by all Contributors.
00005   All Rights reserved.
00006 
00007   The contents of this file are subject to the restrictions and limitations
00008   set forth in the SystemC Open Source License Version 2.3 (the "License");
00009   You may not use this file except in compliance with such restrictions and
00010   limitations. You may obtain instructions on how to receive a copy of the
00011   License at http://www.systemc.org/. Software distributed by Contributors
00012   under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
00013   ANY KIND, either express or implied. See the License for the specific
00014   language governing rights and limitations under the License.
00015 
00016  *****************************************************************************/
00017 
00018 /*****************************************************************************
00019 
00020   sc_context.h - 
00021 
00022   Original Author: Martin Janssen, Synopsys, Inc.
00023 
00024  *****************************************************************************/
00025 
00026 /*****************************************************************************
00027 
00028   MODIFICATION LOG - modifiers, enter your name, affiliation, date and
00029   changes you are making here.
00030 
00031       Name, Affiliation, Date:
00032   Description of Modification:
00033 
00034  *****************************************************************************/
00035 
00036 #ifndef SC_CONTEXT_H
00037 #define SC_CONTEXT_H
00038 
00039 
00040 #include "systemc/datatypes/fx/sc_fx_ids.h"
00041 #include "systemc/kernel/sc_simcontext.h"
00042 #include "systemc/utils/sc_hash.h"
00043 
00044 
00045 class sc_process_b;
00046 
00047 
00048 namespace sc_dt
00049 {
00050 
00051 // classes defined in this module
00052 class sc_without_context;
00053 template <class T> class sc_global;
00054 template <class T> class sc_context;
00055 
00056 
00057 // ----------------------------------------------------------------------------
00058 //  CLASS : sc_without_context
00059 //
00060 //  Empty class that is used for its type only.
00061 // ----------------------------------------------------------------------------
00062 
00063 class sc_without_context {};
00064 
00065 
00066 // ----------------------------------------------------------------------------
00067 //  TEMPLATE CLASS : sc_global
00068 //
00069 //  Template global variable class; singleton; co-routine safe.
00070 // ----------------------------------------------------------------------------
00071 
00072 template <class T>
00073 class sc_global
00074 {
00075 
00076     sc_global();
00077 
00078     void update();
00079 
00080 public:
00081 
00082     static sc_global<T>* instance();
00083 
00084     const T*& value_ptr();
00085 
00086 private:
00087 
00088     static sc_global<T>* m_instance;
00089 
00090     sc_phash<const sc_process_b*,const T*> m_map;
00091     const sc_process_b*                    m_proc;
00092     const T*                               m_value_ptr;
00093 
00094 };
00095 
00096 
00097 // ----------------------------------------------------------------------------
00098 //  ENUM : sc_context_begin
00099 //
00100 //  Enumeration of context begin options.
00101 // ----------------------------------------------------------------------------
00102 
00103 enum sc_context_begin
00104 {
00105     SC_NOW,
00106     SC_LATER
00107 };
00108 
00109 
00110 // ----------------------------------------------------------------------------
00111 //  CLASS : sc_context
00112 //
00113 //  Template context class; co-routine safe.
00114 // ----------------------------------------------------------------------------
00115 
00116 template <class T>
00117 class sc_context
00118 {
00119     sc_context( const sc_context<T>& );
00120     void* operator new( size_t );
00121 
00122 public:
00123 
00124     explicit sc_context( const T&, sc_context_begin = SC_NOW );
00125     ~sc_context();
00126 
00127     void begin();
00128     void end();
00129 
00130     static const T& default_value();
00131     const T& value() const;
00132 
00133 private:
00134 
00135     const T   m_value;
00136     const T*& m_def_value_ptr;
00137     const T*  m_old_value_ptr;
00138 };
00139 
00140 
00141 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
00142 
00143 // ----------------------------------------------------------------------------
00144 //  TEMPLATE CLASS : sc_global
00145 //
00146 //  Template global variable class; singleton; co-routine safe.
00147 // ----------------------------------------------------------------------------
00148 
00149 template <class T>
00150 sc_global<T>* sc_global<T>::m_instance = 0;
00151 
00152 
00153 template <class T>
00154 inline
00155 sc_global<T>::sc_global()
00156 : m_proc( reinterpret_cast<const sc_process_b*>( -1 ) ), m_value_ptr( 0 )
00157 {}
00158 
00159 
00160 template <class T>
00161 inline
00162 void
00163 sc_global<T>::update()
00164 {
00165     const sc_process_b* p = sc_get_curr_process_handle();
00166     if( p != m_proc )
00167     {
00168         const T* vp = m_map[p];
00169         if( vp == 0 )
00170         {
00171             vp = new T( sc_without_context() );
00172             m_map.insert( p, vp );
00173         }
00174         m_proc = p;
00175         m_value_ptr = vp;
00176     }
00177 }
00178 
00179 
00180 template <class T>
00181 inline
00182 sc_global<T>*
00183 sc_global<T>::instance()
00184 {
00185     if( m_instance == 0 )
00186     {
00187         m_instance = new sc_global<T>;
00188     }
00189     return m_instance;
00190 }
00191 
00192 
00193 template <class T>
00194 inline
00195 const T*&
00196 sc_global<T>::value_ptr()
00197 {
00198     update();
00199     return m_value_ptr;
00200 }
00201 
00202 
00203 // ----------------------------------------------------------------------------
00204 //  CLASS : sc_context
00205 //
00206 //  Template context class; co-routine safe.
00207 // ----------------------------------------------------------------------------
00208 
00209 template <class T>
00210 inline
00211 sc_context<T>::sc_context( const sc_context<T>& )
00212 : m_value(),
00213   m_def_value_ptr( sc_global<T>::instance()->value_ptr() ),
00214   m_old_value_ptr( 0 )
00215 {
00216     // this constructor should never be called
00217     SC_REPORT_FATAL( SC_ID_INTERNAL_ERROR_, "should never be called" );
00218 }
00219 
00220 template <class T>
00221 inline
00222 void*
00223 sc_context<T>::operator new( size_t )
00224 {
00225     // this method should never be called
00226     SC_REPORT_FATAL( SC_ID_INTERNAL_ERROR_, "should never be called" );
00227 }
00228 
00229 
00230 template <class T>
00231 inline
00232 sc_context<T>::sc_context( const T& value_, sc_context_begin begin )
00233 : m_value( value_ ),
00234   m_def_value_ptr( sc_global<T>::instance()->value_ptr() ),
00235   m_old_value_ptr( 0 )
00236 {
00237     if( begin == SC_NOW )
00238     {
00239   m_old_value_ptr = m_def_value_ptr;
00240   m_def_value_ptr = &m_value;
00241     }
00242 }
00243 
00244 template <class T>
00245 inline
00246 sc_context<T>::~sc_context()
00247 {
00248     if( m_old_value_ptr != 0 )
00249     {
00250         m_def_value_ptr = m_old_value_ptr;
00251   m_old_value_ptr = 0;
00252     }
00253 }
00254 
00255 
00256 template <class T>
00257 inline
00258 void
00259 sc_context<T>::begin()
00260 {
00261     if( m_old_value_ptr == 0 )
00262     {
00263   m_old_value_ptr = m_def_value_ptr;
00264   m_def_value_ptr = &m_value;
00265     }
00266     else
00267     {
00268         SC_REPORT_ERROR( SC_ID_CONTEXT_BEGIN_FAILED_, 0 );
00269     }
00270 }
00271 
00272 template <class T>
00273 inline
00274 void
00275 sc_context<T>::end()
00276 {
00277     if( m_old_value_ptr != 0 )
00278     {
00279         m_def_value_ptr = m_old_value_ptr;
00280   m_old_value_ptr = 0;
00281     }
00282     else
00283     {
00284         SC_REPORT_ERROR( SC_ID_CONTEXT_END_FAILED_, 0 );
00285     }
00286 }
00287 
00288 
00289 template <class T>
00290 inline
00291 const T&
00292 sc_context<T>::default_value()
00293 {
00294     return *sc_global<T>::instance()->value_ptr();
00295 }
00296 
00297 template <class T>
00298 inline
00299 const T&
00300 sc_context<T>::value() const
00301 {
00302     return m_value;
00303 }
00304 
00305 } // namespace sc_dt
00306 
00307 
00308 #endif
00309 
00310 // Taf!

Generated on Fri Jan 14 08:29:00 2005 for SystemC2.1beta11(excludingMSLib)(IncludingSCV)\nProvidedby:www.openverificationfoundation.org by doxygen1.2.18