45 #ifndef __PBIO__SMART_PTR__OPENCV_CORE_PTR_INL_HPP__dbae1c6458e54ffbb5f63f308a482027
46 #define __PBIO__SMART_PTR__OPENCV_CORE_PTR_INL_HPP__dbae1c6458e54ffbb5f63f308a482027
51 #include "atomic_exchange_add.h"
57 namespace cv_smart_ptr {
66 struct RefOrVoid {
typedef T& type; };
69 struct RefOrVoid<void>{
typedef void type; };
72 struct RefOrVoid<const void>{
typedef const void type; };
75 struct RefOrVoid<volatile void>{
typedef volatile void type; };
78 struct RefOrVoid<const volatile void>{
typedef const volatile void type; };
88 void operator () (Y* p)
const;
94 typedef T element_type;
102 template<
typename Y,
typename D>
108 Ptr(
const Ptr<Y>& o);
111 Ptr(
const Ptr<Y>& o, T* p);
115 Ptr& operator = (
const Ptr& o);
118 Ptr& operator = (
const Ptr<Y>& o);
125 template<
typename Y,
typename D>
126 void reset(Y* p, D d);
132 typename detail::RefOrVoid<T>::type operator * ()
const;
134 T* operator -> ()
const;
136 operator T* ()
const;
141 Ptr<Y> staticCast()
const;
144 Ptr<Y> constCast()
const;
147 Ptr<Y> dynamicCast()
const;
150 detail::PtrOwner* owner;
158 void swap(Ptr<T>& ptr1, Ptr<T>& ptr2);
161 bool operator == (
const Ptr<T>& ptr1,
const Ptr<T>& ptr2);
163 bool operator != (
const Ptr<T>& ptr1,
const Ptr<T>& ptr2);
171 void DefaultDeleter<Y>::operator () (Y* p)
const
181 PtrOwner() : refCount(1)
186 atomic_exchange_add(refCount, 1);
191 if (atomic_exchange_add(refCount, -1) == 1) deleteSelf();
198 virtual void deleteSelf() = 0;
204 PtrOwner(
const PtrOwner&);
205 PtrOwner& operator = (
const PtrOwner&);
208 template<
typename Y,
typename D>
209 struct PtrOwnerImpl : PtrOwner
211 PtrOwnerImpl(Y* p, D d) : owned(p), deleter(d)
229 Ptr<T>::Ptr() : owner(NULL), stored(NULL)
236 ? new detail::PtrOwnerImpl<Y, DefaultDeleter<Y> >(p, DefaultDeleter<Y>())
242 template<
typename Y,
typename D>
243 Ptr<T>::Ptr(Y* p, D d)
245 ? new detail::PtrOwnerImpl<Y, D>(p, d)
251 Ptr<T>::Ptr(
const Ptr& o) : owner(o.owner), stored(o.stored)
253 if (owner) owner->incRef();
258 Ptr<T>::Ptr(
const Ptr<Y>& o) : owner(o.owner), stored(o.stored)
260 if (owner) owner->incRef();
265 Ptr<T>::Ptr(
const Ptr<Y>& o, T* p) : owner(o.owner), stored(p)
267 if (owner) owner->incRef();
277 Ptr<T>& Ptr<T>::operator = (
const Ptr<T>& o)
285 Ptr<T>& Ptr<T>::operator = (
const Ptr<Y>& o)
292 void Ptr<T>::release()
294 if (owner) owner->decRef();
301 void Ptr<T>::reset(Y* p)
307 template<
typename Y,
typename D>
308 void Ptr<T>::reset(Y* p, D d)
310 Ptr(p, d).swap(*
this);
314 void Ptr<T>::swap(Ptr<T>& o)
316 std::swap(owner, o.owner);
317 std::swap(stored, o.stored);
321 T* Ptr<T>::get()
const
327 typename detail::RefOrVoid<T>::type Ptr<T>::operator * ()
const
331 throw pbio::Error(0x1afccd1c,
"Error: Ptr::operator* called with NULL pointer stored, error code: 0x1afccd1c.");
337 T* Ptr<T>::operator -> ()
const
341 throw pbio::Error(0x42a96bad,
"Error: Ptr::operator-> called with NULL pointer stored, error code: 0x42a96bad.");
347 Ptr<T>::operator T* ()
const
354 bool Ptr<T>::empty()
const
361 Ptr<Y> Ptr<T>::staticCast()
const
363 return Ptr<Y>(*
this,
static_cast<Y*
>(stored));
368 Ptr<Y> Ptr<T>::constCast()
const
370 return Ptr<Y>(*
this,
const_cast<Y*
>(stored));
375 Ptr<Y> Ptr<T>::dynamicCast()
const
377 return Ptr<Y>(*
this,
dynamic_cast<Y*
>(stored));
381 void swap(Ptr<T>& ptr1, Ptr<T>& ptr2){
386 bool operator == (
const Ptr<T>& ptr1,
const Ptr<T>& ptr2)
388 return ptr1.get() == ptr2.get();
392 bool operator != (
const Ptr<T>& ptr1,
const Ptr<T>& ptr2)
394 return ptr1.get() != ptr2.get();
404 #endif // __PBIO__SMART_PTR__OPENCV_CORE_PTR_INL_HPP__dbae1c6458e54ffbb5f63f308a482027
The class of exceptions thrown when errors occur.
Definition: Error.h:26