]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGSharedPtr.hxx
Allow nasal::Ghost to only wrap classes by their shared pointers
[simgear.git] / simgear / structure / SGSharedPtr.hxx
1 /* -*-c++-*-
2  *
3  * Copyright (C) 2005-2012 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   typedef T element_type;
52
53   SGSharedPtr(void) : _ptr(0)
54   {}
55   SGSharedPtr(T* ptr) : _ptr(ptr)
56   { get(_ptr); }
57   SGSharedPtr(const SGSharedPtr& p) : _ptr(p.get())
58   { get(_ptr); }
59   template<typename U>
60   SGSharedPtr(const SGSharedPtr<U>& p) : _ptr(p.get())
61   { get(_ptr); }
62   ~SGSharedPtr(void)
63   { put(); }
64   
65   SGSharedPtr& operator=(const SGSharedPtr& p)
66   { assign(p.get()); return *this; }
67   template<typename U>
68   SGSharedPtr& operator=(const SGSharedPtr<U>& p)
69   { assign(p.get()); return *this; }
70   template<typename U>
71   SGSharedPtr& operator=(U* p)
72   { assign(p); return *this; }
73
74   T* operator->(void) const
75   { return _ptr; }
76   T& operator*(void) const
77   { return *_ptr; }
78   operator T*(void) const
79   { return _ptr; }
80   T* ptr(void) const
81   { return _ptr; }
82   T* get(void) const
83   { return _ptr; }
84   T* release()
85   { T* tmp = _ptr; _ptr = 0; T::put(tmp); return tmp; }
86
87   bool isShared(void) const
88   { return T::shared(_ptr); }
89   unsigned getNumRefs(void) const
90   { return T::count(_ptr); }
91
92   bool valid(void) const
93   { return _ptr != (T*)0; }
94
95   void clear()
96   { put(); }
97   void swap(SGSharedPtr& sharedPtr)
98   { T* tmp = _ptr; _ptr = sharedPtr._ptr; sharedPtr._ptr = tmp; }
99
100 private:
101   void assign(T* p)
102   { get(p); put(); _ptr = p; }
103   void assignNonRef(T* p)
104   { put(); _ptr = p; }
105
106   void get(const T* p) const
107   { T::get(p); }
108   void put(void)
109   { if (!T::put(_ptr)) delete _ptr; _ptr = 0; }
110   
111   // The reference itself.
112   T* _ptr;
113
114   template<typename U>
115   friend class SGWeakPtr;
116 };
117
118 /**
119  * Support for boost::mem_fn
120  */
121 template<typename T>
122 T* get_pointer(SGSharedPtr<T> const & p)
123 {
124   return p.ptr();
125 }
126 #endif