1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
19#ifdef OSG_USE_REF_PTR_SAFE_DEREFERENCE
27template<typename T> class observer_ptr;
29/** Smart pointer for handling referenced counted objects.*/
34 typedef T element_type;
36 ref_ptr() : _ptr(0) {}
37 ref_ptr(T* ptr) : _ptr(ptr) { if (_ptr) _ptr->ref(); }
38 ref_ptr(const ref_ptr& rp) : _ptr(rp._ptr) { if (_ptr) _ptr->ref(); }
39 template<class Other> ref_ptr(const ref_ptr<Other>& rp) : _ptr(rp._ptr) { if (_ptr) _ptr->ref(); }
40 ref_ptr(observer_ptr<T>& optr) : _ptr(0) { optr.lock(*this); }
41 ~ref_ptr() { if (_ptr) _ptr->unref(); _ptr = 0; }
43 ref_ptr& operator = (const ref_ptr& rp)
49 template<class Other> ref_ptr& operator = (const ref_ptr<Other>& rp)
55 inline ref_ptr& operator = (T* ptr)
57 if (_ptr==ptr) return *this;
60 if (_ptr) _ptr->ref();
61 // unref second to prevent any deletion of any object which might
62 // be referenced by the other object. i.e rp is child of the
64 if (tmp_ptr) tmp_ptr->unref();
68#ifdef OSG_USE_REF_PTR_IMPLICIT_OUTPUT_CONVERSION
69 // implicit output conversion
70 operator T*() const { return _ptr; }
72 // comparison operators for ref_ptr.
73 bool operator == (const ref_ptr& rp) const { return (_ptr==rp._ptr); }
74 bool operator == (const T* ptr) const { return (_ptr==ptr); }
75 friend bool operator == (const T* ptr, const ref_ptr& rp) { return (ptr==rp._ptr); }
77 bool operator != (const ref_ptr& rp) const { return (_ptr!=rp._ptr); }
78 bool operator != (const T* ptr) const { return (_ptr!=ptr); }
79 friend bool operator != (const T* ptr, const ref_ptr& rp) { return (ptr!=rp._ptr); }
81 bool operator < (const ref_ptr& rp) const { return (_ptr<rp._ptr); }
84 // follows is an implementation of the "safe bool idiom", details can be found at:
85 // http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool
86 // http://lists.boost.org/Archives/boost/2003/09/52856.php
89 typedef T* ref_ptr::*unspecified_bool_type;
92 // safe bool conversion
93 operator unspecified_bool_type() const { return valid()? &ref_ptr::_ptr : 0; }
98#ifdef OSG_USE_REF_PTR_SAFE_DEREFERENCE
100 // pointer is invalid, so throw an exception
101 throw std::runtime_error(std::string("could not dereference invalid osg pointer ") + std::string(typeid(T).name()));
106 T* operator->() const
108#ifdef OSG_USE_REF_PTR_SAFE_DEREFERENCE
110 // pointer is invalid, so throw an exception.
111 throw std::runtime_error(std::string("could not call invalid osg pointer ") + std::string(typeid(T).name()));
117 T* get() const { return _ptr; }
119 bool operator!() const { return _ptr==0; } // not required
120 bool valid() const { return _ptr!=0; }
122 /** release the pointer from ownership by this ref_ptr<>, decrementing the objects refencedCount() via unref_nodelete() to prevent the Object
123 * object from being deleted even if the reference count goes to zero. Use when using a local ref_ptr<> to an Object that you want to return
124 * from a function/method via a C pointer, whilst preventing the normal ref_ptr<> destructor from cleaning up the object. When using release()
125 * you are implicitly expecting other code to take over management of the object, otherwise a memory leak will result. */
126 T* release() { T* tmp=_ptr; if (_ptr) _ptr->unref_nodelete(); _ptr=0; return tmp; }
128 void swap(ref_ptr& rp) { T* tmp=_ptr; _ptr=rp._ptr; rp._ptr=tmp; }
132 template<class Other> void assign(const ref_ptr<Other>& rp)
134 if (_ptr==rp._ptr) return;
137 if (_ptr) _ptr->ref();
138 // unref second to prevent any deletion of any object which might
139 // be referenced by the other object. i.e rp is child of the
141 if (tmp_ptr) tmp_ptr->unref();
144 template<class Other> friend class ref_ptr;
150template<class T> inline
151void swap(ref_ptr<T>& rp1, ref_ptr<T>& rp2) { rp1.swap(rp2); }
153template<class T> inline
154T* get_pointer(const ref_ptr<T>& rp) { return rp.get(); }
156template<class T, class Y> inline
157ref_ptr<T> static_pointer_cast(const ref_ptr<Y>& rp) { return static_cast<T*>(rp.get()); }
159template<class T, class Y> inline
160ref_ptr<T> dynamic_pointer_cast(const ref_ptr<Y>& rp) { return dynamic_cast<T*>(rp.get()); }
162template<class T, class Y> inline
163ref_ptr<T> const_pointer_cast(const ref_ptr<Y>& rp) { return const_cast<T*>(rp.get()); }