Changeset 287 in Sophya


Ignore:
Timestamp:
Apr 30, 1999, 8:59:28 PM (26 years ago)
Author:
ansari
Message:

ajoute const smart pointers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/BaseTools/rcptr.h

    r282 r287  
    66// This implements a very simple "smart pointer" with reference counting.
    77// Many refinements are possible
    8 //  - const propagation
    9 // ...
    108
    119// Principle :  use RCPtr<T> instead of T*
     
    1715//    can be passed by reference, copied, etc
    1816// the object is destroyed when the last Xp is destroyed.
     17
     18// The class RCCPtr<X> is a const smart pointer, to be used like
     19// a X const *.
     20
     21// Eric Aubourg  1999   CEA/DAPNIA/SPP
     22
     23
     24template <class T> class RCCPtr;
    1925
    2026template <class T>
     
    3339  }
    3440
    35   RCPtr<T>& operator = (RCPtr const& other) {
     41  RCPtr<T>& operator = (RCPtr<T> const& other) {
    3642    (*cnt)--;
    3743    if (!*cnt) {
     
    5965  T* x;                  // the object we are referring
    6066  int* cnt;              // how many smart pointers ?
     67  friend class RCCPtr<T>;
     68};
     69
     70
     71// A const smart pointer
     72template <class T>
     73class RCCPtr {
     74public:
     75  RCCPtr(T const* obj=0) {
     76    x = obj;
     77    cnt = new int;
     78    *cnt = 1;
     79  }
     80 
     81  RCCPtr(RCCPtr<T> const& other) {
     82    x = other.x;
     83    cnt = other.cnt;
     84    (*cnt)++;
     85  }
     86
     87  RCCPtr(RCPtr<T> const& other) {
     88    x = other.x;
     89    cnt = other.cnt;
     90    (*cnt)++;
     91  }
     92
     93  RCCPtr<T>& operator = (RCCPtr<T> const& other) {
     94    (*cnt)--;
     95    if (!*cnt) {
     96      delete (T*) x;
     97      delete cnt;
     98    }
     99    x = other.x;
     100    cnt = other.cnt;
     101    (*cnt)++;
     102    return *this;
     103  }
     104 
     105  ~RCCPtr() {
     106    (*cnt)--;
     107    if (!*cnt) {
     108      delete (T*) x;
     109      delete cnt;
     110    }
     111  }
     112
     113  T const* operator->() {return x;}
     114  T const& operator*()  {return *x;}
     115
     116private:
     117  T const * x;           // the object we are referring
     118  int* cnt;              // how many smart pointers ?
    61119};
    62120
Note: See TracChangeset for help on using the changeset viewer.