MINT2
old_counted_ptr.h
Go to the documentation of this file.
1 // author: Jonas Rademacker (Jonas.Rademacker@bristol.ac.uk)
2 // status: Mon 9 Feb 2009 19:17:56 GMT
3 /*
4  * counted_ptr - simple reference counted pointer.
5  *
6  * The is a non-intrusive implementation that allocates an additional
7  * int and pointer for every counted object.
8  */
9 
10 /* Got this originally from:
11  http://ootips.org/yonat/4dev/counted_ptr.h
12 
13  applied a few changes ('signed' with //jr)
14  o removed friend that's specialisation of the own class (with Y)
15  o added conversion to bool (well, to void*, apparently that's safer)
16  o completely changed the way the pointer is stored - now
17  as a pointer to void; this facilitates conversions of
18  counted_ptr<someclass> in the same way as real pointers to
19  a pointer of, say, counted_ptr<someclassesBase>
20  Jonas.
21 */
22 
23 #ifndef COUNTED_PTR_H
24 #define COUNTED_PTR_H
25 
26 /* For ANSI-challenged compilers, you may want to #define
27  * NO_MEMBER_TEMPLATES or explicit */
28 
29 //#define NO_MEMBER_TEMPLATES
30 
31 namespace MINT{
32 
33 class counted_ptr_counter{
34  public:
35  counted_ptr_counter(void* p = 0, unsigned c = 1) : ptr(p), count(c) {}
36  void* ptr;
37  unsigned count;
38 };
39 
40 
41 template <typename X> class counted_ptr
42 {
43  public:
44  // itsCounter, aquire, release SHOULD be private, but then things like
45  // counted_ptr<IDalitzEvent> = sth that is counted_ptr<DalitzEvent>
46  // don't work - I think it's a compiler limitation.
48 
49  void acquire(counted_ptr_counter* c) throw()
50  { // increment the count
51  itsCounter = c;
52  if (c) ++(c->count);
53  }
54 
55  void release()
56  { // decrement the count, delete if it is 0
57  if (itsCounter) {
58  if (--itsCounter->count == 0) {
59  delete (X*)(itsCounter->ptr);
60  delete itsCounter;
61  }
62  itsCounter = 0;
63  }
64  }
65 
66  public:
67  typedef X element_type;
68 
69  explicit counted_ptr(X* p = 0) // allocate a new counter
70  : itsCounter(0) {if (p) itsCounter = new counted_ptr_counter(p);}
72  {release();}
73  counted_ptr(const counted_ptr& r) throw()
74  {acquire(r.itsCounter);}
76  {
77  if (this != &r) {
78  release();
80  }
81  return *this;
82  }
83 
84 #ifndef NO_MEMBER_TEMPLATES
85  //template <class Y> friend class counted_ptr<Y>; // jr
86  template <class Y> counted_ptr(const counted_ptr<Y>& r) throw()
87  {
88  // with this trick we check (I hope) if the conversion
89  // is legal (jr):
90  static_cast<X*>(r.get());
91  acquire(r.itsCounter);
92  }
93  template <class Y> counted_ptr& operator=(const counted_ptr<Y>& r)
94  {
95  if (this != &r) {
96  // with this trick we check (I hope) if the conversion
97  // is legal (jr):
98  static_cast<X*>(r.get());
99  release();
100  acquire(r.itsCounter);
101  }
102  return *this;
103  }
104 #endif // NO_MEMBER_TEMPLATES
105 
106  X& operator*() const throw(){return *(static_cast<X*>(itsCounter->ptr));}
107  X* operator->() const throw(){return static_cast<X*>(itsCounter->ptr);}
108  X* get() const throw(){return ((itsCounter) ? static_cast<X*>(itsCounter->ptr) : 0);}
109  bool unique() const throw()
110  {return (itsCounter ? itsCounter->count == 1 : true);}
111 
112 
113  operator void*() const{ // jr
114  return (void*) get();
115  }
116 
117 
118 
119  // operator bool() const{ // jr
120  // return 0 != get();
121  // }
122 
123 
124  /* wise? not sure.
125  operator X*() const{ // jr
126  return (X*) get();
127  }
128  */
129 
130 
131 };
132 
133 template <typename X> class const_counted_ptr : public counted_ptr<X>{
134  public:
135  void acquire(counted_ptr_counter* c) throw(){
137  void release(){
139 
140  explicit const_counted_ptr(X* p = 0) // allocate a new counter
141  : counted_ptr<X>(p){};
143  {release();}
144  const_counted_ptr(const const_counted_ptr& other) throw()
145  :counted_ptr<X>(other)
146  {}
147  const_counted_ptr(const counted_ptr<X>& other) throw()
148  :counted_ptr<X>(other)
149  {}
151  {
152  if (this != &r) {
153  release();
154  acquire(r.itsCounter);
155  }
156  return *this;
157  }
159  {
160  if (this != &r) {
161  release();
162  acquire(r.itsCounter);
163  }
164  return *this;
165  }
166 
167 #ifndef NO_MEMBER_TEMPLATES
168  template <class Y> const_counted_ptr(const const_counted_ptr<Y>& r) throw()
169  {
170  // with this trick we check (I hope) if the conversion
171  // is legal (jr):
172  static_cast<X*>(r.get());
173  acquire(r.itsCounter);
174  }
175  template <class Y> const_counted_ptr(const counted_ptr<Y>& r) throw()
176  {
177  // with this trick we check (I hope) if the conversion
178  // is legal (jr):
179  static_cast<X*>(r.get());
180  acquire(r.itsCounter);
181  }
182  template <class Y> const_counted_ptr& operator=(const const_counted_ptr<Y>& r)
183  {
184  if (this != &r) {
185  // with this trick we check (I hope) if the conversion
186  // is legal (jr):
187  static_cast<X*>(r.get());
188  release();
189  acquire(r.itsCounter);
190  }
191  return *this;
192  }
193  template <class Y> const_counted_ptr& operator=(const counted_ptr<Y>& r)
194  {
195  if (this != &r) {
196  // with this trick we check (I hope) if the conversion
197  // is legal (jr):
198  static_cast<X*>(r.get());
199  release();
200  acquire(r.itsCounter);
201  }
202  return *this;
203  }
204 #endif // NO_MEMBER_TEMPLATES
205  const X& operator*() const throw(){return *(static_cast<X*>(this->itsCounter->ptr));}
206  const X* operator->() const throw(){return static_cast<X*>(this->itsCounter->ptr);}
207  const X* get() const throw(){return ((this->itsCounter) ? static_cast<X*>(this->itsCounter->ptr) : 0);}
208 
209 };
210 
211 }// namespace MINT
212 
213 #endif // COUNTED_PTR_H
const_counted_ptr(const const_counted_ptr< Y > &r)
const X * get() const
void acquire(const counted_ptr< X > &c)
Definition: counted_ptr.h:155
const X & operator *() const
Definition: counted_ptr.h:219
void acquire(counted_ptr_counter *c)
const X * operator->() const
counted_ptr & operator=(const counted_ptr< Y > &r)
counted_ptr_counter(void *p=0, unsigned c=1)
counted_ptr & operator=(const counted_ptr &r)
X & operator *() const
Definition: counted_ptr.h:124
const_counted_ptr & operator=(const const_counted_ptr< Y > &r)
const_counted_ptr(const counted_ptr< X > &other)
const_counted_ptr(const const_counted_ptr &other)
const_counted_ptr & operator=(const counted_ptr< Y > &r)
const_counted_ptr & operator=(const counted_ptr< X > &r)
void acquire(const counted_ptr< X > &other)
Definition: counted_ptr.h:50
const_counted_ptr(const counted_ptr< Y > &r)
counted_ptr(const counted_ptr &r)
counted_ptr(const counted_ptr< Y > &r)
X * operator->() const
void acquire(counted_ptr_counter *c)
const_counted_ptr & operator=(const const_counted_ptr &r)
bool unique() const
X * get() const
Definition: counted_ptr.h:123
counted_ptr_counter * itsCounter
Definition: counted_ptr.h:44