SCENE C++ API  2.1.0
ref_ptr.h
1 #pragma once
2 
3 #include <core_api/lsglobaldefinitions.h>
4 
5 namespace SCENE_API {
6 
13 template<class T>
14 class ref_ptr
15 {
16 public:
17 
18  ref_ptr() throw() : m_obj(0)
19  {
20  }
21 
22  ref_ptr(T* obj) : m_obj(obj)
23  {
24  if (m_obj != 0)
25  m_obj->ref();
26  }
27 
28  ref_ptr(const ref_ptr& rhs) : m_obj(rhs.get())
29  {
30  if (m_obj != 0)
31  m_obj->ref();
32  }
33 
34  template<class U>
35  ref_ptr(ref_ptr<U> const & rhs ) : m_obj( rhs.get())
36  {
37  if (m_obj != 0)
38  m_obj->ref();
39  }
40 
41  ~ref_ptr()
42  {
43  if (m_obj != 0)
44  m_obj->unref();
45  }
46 
47  ref_ptr& operator=(ref_ptr const & rhs)
48  {
49  if (m_obj)
50  m_obj->unref();
51 
52  m_obj = rhs.get();
53 
54  if (m_obj)
55  m_obj->ref();
56 
57  return *this;
58  }
59 
60  ref_ptr& operator=(T* rhs)
61  {
62  if (m_obj)
63  m_obj->unref();
64 
65  m_obj = rhs;
66 
67  if (m_obj)
68  m_obj->ref();
69 
70  return *this;
71  }
72 
73  template<class U>
74  ref_ptr & operator=(ref_ptr<U> const & rhs)
75  {
76  if (m_obj)
77  m_obj->unref();
78 
79  m_obj = rhs.get();
80 
81  if (m_obj)
82  m_obj->ref();
83 
84  return *this;
85  }
86 
87  void reset() throw()
88  {
89  if (m_obj)
90  m_obj->unref();
91 
92  m_obj = 0;
93  }
94 
95  void reset(T* rhs)
96  {
97  if (m_obj)
98  m_obj->unref();
99 
100  m_obj = rhs;
101 
102  if (m_obj)
103  m_obj->ref();
104  }
105 
106  T* get() const throw()
107  {
108  return m_obj;
109  }
110 
111  T& operator*() const
112  {
113  return *m_obj;
114  }
115 
116  T* operator->() const
117  {
118  return m_obj;
119  }
120 
121  // Safe bool idiom for bool comparison
122  typedef T* ref_ptr<T>::*unspecified_bool_type;
123  operator unspecified_bool_type() const
124  {
125  return m_obj == 0 ? 0: &ref_ptr<T>::m_obj;
126  }
127 
129  void swap(ref_ptr<T>& other)
130  {
131  std::swap(m_obj, other.m_obj);
132  }
133 
134 private:
135  T* m_obj;
136 };
137 
138 template<class T, class U>
139 inline bool operator==(ref_ptr<T> const & p1, ref_ptr<U> const & p2)
140 {
141  return p1.get() == p2.get();
142 }
143 
144 template<class T, class U>
145 inline bool operator!=(ref_ptr<T> const & p1, ref_ptr<U> const & p2)
146 {
147  return p1.get() != p2.get();
148 }
149 
150 template<class T, class U>
151 inline bool operator==(ref_ptr<T> const & p1, U* p2)
152 {
153  return p1.get() == p2;
154 }
155 
156 template<class T, class U>
157 inline bool operator!=(ref_ptr<T> const & p1, U* p2)
158 {
159  return p1.get() != p2;
160 }
161 
162 template<class T, class U>
163 inline bool operator==(T* p1, ref_ptr<U> const & p2)
164 {
165  return p1 == p2.get();
166 }
167 
168 template<class T, class U>
169 inline bool operator!=(T* p1, ref_ptr<U> const & p2)
170 {
171  return p1 != p2.get();
172 }
173 
174 template<class T>
175 inline bool operator<(ref_ptr<T> const & p1, ref_ptr<T> const & p2)
176 {
177  return std::less<T *>()(p1.get(), p2.get());
178 }
179 
180 template<class T> void swap(ref_ptr<T> & lhs, ref_ptr<T> & rhs)
181 {
182  lhs.swap(rhs);
183 }
184 
185 template<class T, class U>
186 ref_ptr<T> static_pointer_cast(ref_ptr<U> const & p)
187 {
188  return static_cast<T *>(p.get());
189 }
190 
191 template<class T, class U>
192 ref_ptr<T> const_pointer_cast(ref_ptr<U> const & p)
193 {
194  return const_cast<T *>(p.get());
195 }
196 
197 template<class T, class U>
198 ref_ptr<T> dynamic_pointer_cast(ref_ptr<U> const & p)
199 {
200  return dynamic_cast<T *>(p.get());
201 }
202 
203 }