smart_ptr.h
Go to the documentation of this file.
1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ZORBA_SMARTPTR_API_H
18 #define ZORBA_SMARTPTR_API_H
19 
20 #include <zorba/config.h>
21 
22 namespace zorba {
23 
24 class ZORBA_DLL_PUBLIC SmartObject
25 {
26 protected:
27  mutable unsigned int theRefCount;
28 
29 public:
30  SmartObject() : theRefCount(0) { }
31 
32  SmartObject(const SmartObject&) : theRefCount(0) { }
33 
34  virtual ~SmartObject();
35 
36  virtual void free();
37 
38  long getRefCount() const { return theRefCount; }
39 
40  void addReference() const { ++theRefCount; }
41 
42  void removeReference () {
43  if (--theRefCount == 0)
44  free();
45  }
46 
47  SmartObject& operator=(const SmartObject&) { return *this; }
48 }; /* class SmartObject */
49 
50 template<class T>
51 class SmartPtr
52 {
53 protected:
54  T *p;
55 
56  void init() {
57  if (p != 0) p->addReference();
58  }
59 
60  template <class otherT> SmartPtr&
61  assign (const SmartPtr<otherT>& rhs) {
62  if (p != rhs.get())
63  {
64  if (p) p->removeReference();
65  p = static_cast<T*>(rhs.get());
66  init();
67  }
68  return *this;
69  }
70 
71 public:
72  SmartPtr(T* realPtr = 0) : p(realPtr) {
73  init();
74  }
75 
76  SmartPtr(SmartPtr const& rhs) : p(rhs.get()) {
77  init();
78  }
79 
81  if (p)
82  p->removeReference();
83  p = 0;
84  }
85 
86  bool isNull () const { return p == 0; }
87 
88  T* get() const { return p; }
89 
90  operator T* () { return get(); }
91  operator const T * () const { return get(); }
92 
93  T* operator->() const { return p; }
94  T& operator*() const { return *p; }
95 
96  bool operator==(SmartPtr const& h) const { return p == h.p; }
97  bool operator!=(SmartPtr const& h) const { return p != h.p; }
98  bool operator==(T const* pp) const { return p == pp; }
99  bool operator!=(T const* pp) const { return p != pp; }
100  bool operator<(const SmartPtr& h) const { return p < h.p; }
101 
102  SmartPtr& operator=(SmartPtr const& rhs) {
103  return assign (rhs);
104  }
105 
106  template <class otherT> SmartPtr& operator=(SmartPtr<otherT> const& rhs) {
107  return assign (rhs);
108  }
109 
110 }; // SmartPtr
111 
112 } // namespace zorba
113 #endif /* ZORBA_SMARTPTR_API_H */
114 /* vim:set et sw=2 ts=2: */
bool operator<(const SmartPtr &h) const
Definition: smart_ptr.h:100
T * operator->() const
Definition: smart_ptr.h:93
unsigned int theRefCount
Definition: smart_ptr.h:27
T * get() const
Definition: smart_ptr.h:88
bool operator==(SmartPtr const &h) const
Definition: smart_ptr.h:96
bool isNull() const
Definition: smart_ptr.h:86
SmartPtr(T *realPtr=0)
Definition: smart_ptr.h:72
void removeReference()
Definition: smart_ptr.h:42
SmartPtr & assign(const SmartPtr< otherT > &rhs)
Definition: smart_ptr.h:61
SmartObject(const SmartObject &)
Definition: smart_ptr.h:32
long getRefCount() const
Definition: smart_ptr.h:38
T & operator*() const
Definition: smart_ptr.h:94
void addReference() const
Definition: smart_ptr.h:40
SmartPtr(SmartPtr const &rhs)
Definition: smart_ptr.h:76
bool operator==(T const *pp) const
Definition: smart_ptr.h:98
SmartPtr & operator=(SmartPtr const &rhs)
Definition: smart_ptr.h:102
SmartObject & operator=(const SmartObject &)
Definition: smart_ptr.h:47
bool operator!=(SmartPtr const &h) const
Definition: smart_ptr.h:97
bool operator!=(T const *pp) const
Definition: smart_ptr.h:99
SmartPtr & operator=(SmartPtr< otherT > const &rhs)
Definition: smart_ptr.h:106