openscenegraph
ref_ptr
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
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.
7 *
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.
12*/
13
14#ifndef OSG_REF_PTR
15#define OSG_REF_PTR 1
16
17#include <osg/Config>
18
19#ifdef OSG_USE_REF_PTR_SAFE_DEREFERENCE
20#include <typeinfo>
21#include <stdexcept>
22#include <string>
23#endif
24
25namespace osg {
26
27template<typename T> class observer_ptr;
28
29/** Smart pointer for handling referenced counted objects.*/
30template<class T>
31class ref_ptr
32{
33 public:
34 typedef T element_type;
35
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; }
42
43 ref_ptr& operator = (const ref_ptr& rp)
44 {
45 assign(rp);
46 return *this;
47 }
48
49 template<class Other> ref_ptr& operator = (const ref_ptr<Other>& rp)
50 {
51 assign(rp);
52 return *this;
53 }
54
55 inline ref_ptr& operator = (T* ptr)
56 {
57 if (_ptr==ptr) return *this;
58 T* tmp_ptr = _ptr;
59 _ptr = ptr;
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
63 // original _ptr.
64 if (tmp_ptr) tmp_ptr->unref();
65 return *this;
66 }
67
68#ifdef OSG_USE_REF_PTR_IMPLICIT_OUTPUT_CONVERSION
69 // implicit output conversion
70 operator T*() const { return _ptr; }
71#else
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); }
76
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); }
80
81 bool operator < (const ref_ptr& rp) const { return (_ptr<rp._ptr); }
82
83
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
87
88 private:
89 typedef T* ref_ptr::*unspecified_bool_type;
90
91 public:
92 // safe bool conversion
93 operator unspecified_bool_type() const { return valid()? &ref_ptr::_ptr : 0; }
94#endif
95
96 T& operator*() const
97 {
98#ifdef OSG_USE_REF_PTR_SAFE_DEREFERENCE
99 if( !_ptr ) {
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()));
102 }
103#endif
104 return *_ptr;
105 }
106 T* operator->() const
107 {
108#ifdef OSG_USE_REF_PTR_SAFE_DEREFERENCE
109 if( !_ptr ) {
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()));
112 }
113#endif
114 return _ptr;
115 }
116
117 T* get() const { return _ptr; }
118
119 bool operator!() const { return _ptr==0; } // not required
120 bool valid() const { return _ptr!=0; }
121
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; }
127
128 void swap(ref_ptr& rp) { T* tmp=_ptr; _ptr=rp._ptr; rp._ptr=tmp; }
129
130 private:
131
132 template<class Other> void assign(const ref_ptr<Other>& rp)
133 {
134 if (_ptr==rp._ptr) return;
135 T* tmp_ptr = _ptr;
136 _ptr = rp._ptr;
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
140 // original _ptr.
141 if (tmp_ptr) tmp_ptr->unref();
142 }
143
144 template<class Other> friend class ref_ptr;
145
146 T* _ptr;
147};
148
149
150template<class T> inline
151void swap(ref_ptr<T>& rp1, ref_ptr<T>& rp2) { rp1.swap(rp2); }
152
153template<class T> inline
154T* get_pointer(const ref_ptr<T>& rp) { return rp.get(); }
155
156template<class T, class Y> inline
157ref_ptr<T> static_pointer_cast(const ref_ptr<Y>& rp) { return static_cast<T*>(rp.get()); }
158
159template<class T, class Y> inline
160ref_ptr<T> dynamic_pointer_cast(const ref_ptr<Y>& rp) { return dynamic_cast<T*>(rp.get()); }
161
162template<class T, class Y> inline
163ref_ptr<T> const_pointer_cast(const ref_ptr<Y>& rp) { return const_cast<T*>(rp.get()); }
164
165}
166
167#endif