- Cal3D 0.11 API Reference -

refptr.h
1 #ifndef CAL_REF_PTR_H
2 #define CAL_REF_PTR_H
3 
4 
5 namespace cal3d
6 {
7 
9  template<typename T>
10  class RefPtr
11  {
12  public:
13  // For compatibility with Boost.Python.
14  typedef T element_type;
15 
16  RefPtr(T* ptr = 0)
17  {
18  m_ptr = 0;
19  *this = ptr;
20  }
21 
22  RefPtr(const RefPtr<T>& ptr)
23  {
24  m_ptr = 0;
25  *this = ptr;
26  }
27 
28  ~RefPtr()
29  {
30  if (m_ptr)
31  {
32  explicitDecRef(m_ptr);
33  m_ptr = 0;
34  }
35  }
36 
37  template<typename U>
38  RefPtr<T>& operator=(U* ptr)
39  {
40  if (ptr != m_ptr)
41  {
42  if (m_ptr)
43  {
44  explicitDecRef(m_ptr);
45  }
46  m_ptr = ptr;
47  if (m_ptr)
48  {
49  explicitIncRef(m_ptr);
50  }
51  }
52  return *this;
53  }
54 
55  template<typename U>
56  RefPtr<T>& operator=(const RefPtr<U>& ptr)
57  {
58  *this = ptr.get();
59  return *this;
60  }
61 
64  {
65  *this = ptr.get();
66  return *this;
67  }
68 
70  bool operator!() const
71  {
72  return !get();
73  }
74 
75  T* operator->() const
76  {
77  assert(get() && "Accessing member of null pointer!");
78  return get();
79  }
80 
81  T& operator*() const
82  {
83  assert(get() && "Dereferencing null pointer!");
84  return *get();
85  }
86 
87  typedef RefPtr<T> this_type;
88 
91 
93  operator unspecified_bool_type() const
94  {
95  return (get() ? &this_type::m_ptr : 0);
96  }
97 
98  T* get() const
99  {
100  assert(!m_ptr || m_ptr->getRefCount() > 0 &&
101  "Dereferencing pointer with refCount <= 0");
102  return m_ptr;
103  }
104 
105  private:
106  T* m_ptr;
107  };
108 
109 
110  // For compatibility with Boost.Python.
111  template<class T>
112  T* get_pointer(const RefPtr<T>& p)
113  {
114  return p.get();
115  }
116 
117 
118  template<typename T, typename U>
119  bool operator==(const RefPtr<T>& a, const RefPtr<U>& b)
120  {
121  return (a.get() == b.get());
122  }
123 
124  template<typename T, typename U>
125  bool operator==(const RefPtr<T>& a, const U* b)
126  {
127  return (a.get() == b);
128  }
129 
130  template<typename T, typename U>
131  bool operator==(const T* a, const RefPtr<U>& b)
132  {
133  return (a == b.get());
134  }
135 
136 
137  template<typename T, typename U>
138  bool operator!=(const RefPtr<T>& a, const RefPtr<U>& b)
139  {
140  return (a.get() != b.get());
141  }
142 
143  template<typename T, typename U>
144  bool operator!=(const RefPtr<T>& a, const U* b)
145  {
146  return (a.get() != b);
147  }
148 
149  template<typename T, typename U>
150  bool operator!=(const T* a, const RefPtr<U>& b)
151  {
152  return (a != b.get());
153  }
154 
155 
156  template<typename T, typename U>
157  bool operator<(const RefPtr<T>& a, const RefPtr<U>& b)
158  {
159  return (a.get() < b.get());
160  }
161 
162  template<typename T, typename U>
163  bool operator<(const RefPtr<T>& a, const U* b)
164  {
165  return (a.get() < b);
166  }
167 
168  template<typename T, typename U>
169  bool operator<(const T* a, const RefPtr<U>& b)
170  {
171  return (a < b.get());
172  }
173 
174 
175 }
176 
177 
178 #endif
RefPtr< T > & operator=(const RefPtr< T > &ptr)
Need this to override the built-in operator=.
Definition: refptr.h:63
A container-safe smart pointer used for refcounted classes.
Definition: refcounted.h:11
bool operator!() const
Need this to override the built-in operator!
Definition: refptr.h:70
T *this_type::* unspecified_bool_type
Inspired by boost&#39;s smart_ptr facilities.
Definition: refptr.h:90
Definition: global.h:37

Generated by The Cal3D Team with Doxygen 1.8.14