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

scv_shared_ptr.h

Go to the documentation of this file.
00001 //  -*- C++ -*- <this line is for emacs to recognize it as C++ code>
00002 /*****************************************************************************
00003 
00004   The following code is derived, directly or indirectly, from the SystemC
00005   source code Copyright (c) 1996-2002 by all Contributors.
00006   All Rights reserved.
00007 
00008   The contents of this file are subject to the restrictions and limitations
00009   set forth in the SystemC Open Source License Version 2.3 (the "License");
00010   You may not use this file except in compliance with such restrictions and
00011   limitations. You may obtain instructions on how to receive a copy of the
00012   License at http://www.systemc.org/. Software distributed by Contributors
00013   under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
00014   ANY KIND, either express or implied. See the License for the specific
00015   language governing rights and limitations under the License.
00016 
00017  *****************************************************************************/
00018 
00019 /*****************************************************************************
00020 
00021   scv_shared_ptr.h -- 
00022   A wrapper class/template to provide a safe handle to an underlying object.
00023 
00024   A safe handle is one with automatic reference counting so that
00025   even when multiple C++ threads are sharing the underlying object,
00026   the system knows when to destroy the underlying object and reclaim
00027   the memory.
00028 
00029   Original Authors (Cadence Design Systems, Inc):
00030   Norris Ip, Dean Shea, John Rose, Jasvinder Singh, William Paulsen,
00031   John Pierce, Rachida Kebichi, Ted Elkind, David Bailey
00032   2002-09-23
00033 
00034  *****************************************************************************/
00035 
00036 /*****************************************************************************
00037 
00038   MODIFICATION LOG - modifiers, enter your name, affiliation, date and
00039   changes you are making here.
00040 
00041       Name, Affiliation, Date:
00042   Description of Modification:
00043 
00044  *****************************************************************************/
00045 
00046 #ifndef INCLUDED_SCV_SHARED_PTR_H
00047 #define INCLUDED_SCV_SHARED_PTR_H
00048 
00049 #include <assert.h>
00050 
00051 #ifdef SCV_USE_IOSTREAM_H
00052 # include <iostream.h>
00053 #else
00054 # include <iostream>
00055 #endif
00056 
00057 #define LOCAL_MEMORY_MANAGEMENT \
00058   static int *_createCountP() { return new int(); } \
00059   static void _freeCountP(int *countP) { delete countP; }
00060 
00061 template<typename T>
00062 class scv_shared_ptr {
00063 public:
00064   //
00065   // constructor (user-provided underlying object)
00066   //
00067   // It is the user's responsibility to make sure that no other code
00068   // is accessing the underlying object "core" except through safe handles.
00069   //
00070   // Typical usage:
00071   //   scv_shared_ptr<my_packet> packet(new my_packet());
00072   //
00073   scv_shared_ptr(T *coreP=NULL) : _coreP(coreP), _countP(NULL) {
00074     if (_coreP) { _countP = _createCountP(); *_countP = 1; }
00075   }
00076 
00077   scv_shared_ptr(T *coreP, int *countP) : _coreP(coreP), _countP(countP) {
00078     if (_countP) { ++(*_countP); }
00079   }
00080 
00081   //
00082   // constructor (copy)
00083   //
00084   // The new safe handle refers to the same underlying object as the
00085   // safe handle in the argument.
00086   //
00087   // If the argument is NULL, it is a place holder for future assignments.
00088   //
00089   scv_shared_ptr(const scv_shared_ptr<T>& t) 
00090     : _coreP(t._coreP), _countP(t._countP) { if (_countP) ++(*_countP); }
00091 
00092   //
00093   // destructor
00094   //
00095   ~scv_shared_ptr() {
00096     if (_coreP) { --(*_countP); if (*_countP == 0) { delete _coreP; _freeCountP(_countP); } }
00097   }
00098 
00099   template<typename T2>
00100   operator scv_shared_ptr<T2> () { return scv_shared_ptr<T2>(_coreP,_countP); }
00101 
00102   //
00103   // assignment
00104   //
00105   // The current safe handle becomes a wrapper to the underlying object 
00106   // of the safe handle in the argument. The reference count of the 
00107   // original underlying object is decrement, and the original underlying object
00108   // is destroyed if the reference count become zero.
00109   //
00110   scv_shared_ptr& operator=(const scv_shared_ptr& t) {
00111     if (t._countP != _countP) {
00112       if (t._coreP) ++(*t._countP);
00113       if (_coreP) {
00114   --(*_countP);
00115         if (*_countP == 0) { delete _coreP; _freeCountP(_countP); }
00116       }
00117       _coreP = t._coreP;
00118       _countP = t._countP;
00119     }
00120     return *this;
00121   }
00122 
00123   bool compare(const scv_shared_ptr& other) const {
00124     return this->_coreP == other._coreP;
00125   }
00126 
00127   bool isNull() const {
00128     return this->_coreP == NULL;
00129   }
00130 
00131   //
00132   // access to the underlying object
00133   //
00134   T& operator*() { 
00135     if (_coreP) return *_coreP;
00136     assert(0);
00137     return *(T*)0; // will cause segmentation fault when this error occurs.
00138   }
00139   const T& operator*() const {
00140     if (_coreP) return *_coreP;
00141     assert(0);
00142     return *(T*)0; // will cause segmentation fault when this error occurs.
00143   }
00144   T *operator->() {
00145     if (_coreP) return _coreP;
00146     assert(0);
00147     return NULL ;
00148   }
00149   const T *operator->() const {
00150     if (_coreP) return _coreP;
00151     assert(0);
00152     return NULL ;
00153   }
00154 #if 0
00155   friend ostream& operator<<(ostream& os, const scv_shared_ptr& a) {
00156     os << *a._coreP << endl;
00157     return os;
00158   }
00159 #endif
00160 
00161 private:
00162   T *_coreP;
00163   int *_countP;
00164 
00165   LOCAL_MEMORY_MANAGEMENT
00166 };
00167 
00168 #undef LOCAL_MEMORY_MANAGEMENT
00169 
00170 template <class T>
00171 bool operator==(const scv_shared_ptr<T>& a, const scv_shared_ptr<T>& b) {
00172   return a.compare(b);
00173 }
00174 template <class T>
00175 bool operator!=(const scv_shared_ptr<T>& a, const scv_shared_ptr<T>& b) {
00176   return !(a==b);
00177 }
00178 template <class T>
00179 ostream& operator<<(ostream& os, const scv_shared_ptr<T>& a) {
00180     //os << *a._coreP << endl;
00181     os << *a << endl;
00182     return os;
00183 }
00184 
00185 #endif // INCLUDED_SCV_SHARED_PTR_H

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