]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGSharedPtr.hxx
Working 'noshadow' animation
[simgear.git] / simgear / structure / SGSharedPtr.hxx
1 /* -*-c++-*-
2  *
3  * Copyright (C) 2005-2009 Mathias Froehlich 
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  *
19  */
20
21 #ifndef SGSharedPtr_HXX
22 #define SGSharedPtr_HXX
23
24 #include "SGReferenced.hxx"
25
26 /// This class is a pointer proxy doing reference counting on the object
27 /// it is pointing to.
28 /// The SGSharedPtr class handles reference counting and possible
29 /// destruction if no nore references are in use automatically.
30 /// Classes derived from @SGReferenced can be handled with SGSharedPtr.
31 /// Once you have a SGSharedPtr available you can use it just like
32 /// a usual pointer with the exception that you don't need to delete it.
33 /// Such a reference is initialized by zero if not initialized with a
34 /// class pointer.
35 /// One thing you need to avoid are cyclic loops with such pointers.
36 /// As long as such a cyclic loop exists the reference count never drops
37 /// to zero and consequently the objects will never be destroyed.
38 /// Always try to use directed graphs where the references away from the
39 /// top node are made with SGSharedPtr's and the back references are done with
40 /// ordinary pointers or SGWeakPtr's.
41 /// There is a very good description of OpenSceneGraphs ref_ptr which is
42 /// pretty much the same than this one at
43 /// http://dburns.dhs.org/OSG/Articles/RefPointers/RefPointers.html
44
45 template<typename T>
46 class SGWeakPtr;
47
48 template<typename T>
49 class SGSharedPtr {
50 public:
51   SGSharedPtr(void) : _ptr(0)
52   {}
53   SGSharedPtr(T* ptr) : _ptr(ptr)
54   { get(_ptr); }
55   SGSharedPtr(const SGSharedPtr& p) : _ptr(p.get())
56   { get(_ptr); }
57   template<typename U>
58   SGSharedPtr(const SGSharedPtr<U>& p) : _ptr(p.get())
59   { get(_ptr); }
60   ~SGSharedPtr(void)
61   { put(); }
62   
63   SGSharedPtr& operator=(const SGSharedPtr& p)
64   { assign(p.get()); return *this; }
65   template<typename U>
66   SGSharedPtr& operator=(const SGSharedPtr<U>& p)
67   { assign(p.get()); return *this; }
68   template<typename U>
69   SGSharedPtr& operator=(U* p)
70   { assign(p); return *this; }
71
72   T* operator->(void) const
73   { return _ptr; }
74   T& operator*(void) const
75   { return *_ptr; }
76   operator T*(void) const
77   { return _ptr; }
78   T* ptr(void) const
79   { return _ptr; }
80   T* get(void) const
81   { return _ptr; }
82   T* release()
83   { T* tmp = _ptr; _ptr = 0; T::put(tmp); return tmp; }
84
85   bool isShared(void) const
86   { return T::shared(_ptr); }
87   unsigned getNumRefs(void) const
88   { return T::count(_ptr); }
89
90   bool valid(void) const
91   { return _ptr != (T*)0; }
92
93   void clear()
94   { put(); }
95   void swap(SGSharedPtr& sharedPtr)
96   { T* tmp = _ptr; _ptr = sharedPtr._ptr; sharedPtr._ptr = tmp; }
97
98 private:
99   void assign(T* p)
100   { get(p); put(); _ptr = p; }
101   void assignNonRef(T* p)
102   { put(); _ptr = p; }
103
104   void get(const T* p) const
105   { T::get(p); }
106   void put(void)
107   { if (!T::put(_ptr)) { delete _ptr; _ptr = 0; } }
108   
109   // The reference itself.
110   T* _ptr;
111
112   template<typename U>
113   friend class SGWeakPtr;
114 };
115
116 /**
117  * Support for boost::mem_fn
118  */
119 template<typename T>
120 T* get_pointer(SGSharedPtr<T> const & p)
121 {
122   return p.ptr();
123 }
124 #endif