]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/NasalObjectHolder.hxx
cppbind.Ghost: clean up a bit
[simgear.git] / simgear / nasal / cppbind / NasalObjectHolder.hxx
1 ///@file Wrapper class for keeping Nasal objects save from the garbage collector
2 //
3 // Copyright (C) 2013  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
18
19 #ifndef SG_NASAL_OBJECT_HOLDER_HXX_
20 #define SG_NASAL_OBJECT_HOLDER_HXX_
21
22 #include <simgear/nasal/nasal.h>
23 #include <simgear/structure/SGSharedPtr.hxx>
24
25 namespace nasal
26 {
27
28   /**
29    * Usable for example as empty base class if a base class is required.(Eg. as
30    * parameter for a mixin class).
31    */
32   struct empty_class {};
33
34   /**
35    * Prevent a Nasal object from being destroyed by the garbage collector during
36    * the lifetime of this object.
37    */
38   template<class Base = empty_class>
39   class ObjectHolder:
40     public Base
41   {
42     public:
43
44       /**
45        * @param obj Object to save
46        */
47       explicit ObjectHolder(naRef obj);
48
49       /**
50        *
51        */
52       ObjectHolder();
53
54       /**
55        *
56        */
57       ~ObjectHolder();
58
59       /**
60        * Get captured Nasal object
61        */
62       naRef get_naRef() const;
63
64       /**
65        * Release the managed object
66        */
67       void reset();
68
69       /**
70        * Replaces the managed object (the old object is released)
71        *
72        * @param obj New object to save
73        */
74       void reset(naRef obj);
75
76       /**
77        * Check if there is a managed object
78        */
79       bool valid() const;
80
81       /**
82        * Save the given object as long as the returned holder exists.
83        *
84        * @param obj Object to save
85        */
86       static SGSharedPtr<ObjectHolder<Base> > makeShared(naRef obj);
87
88     protected:
89       naRef _ref;
90       int _gc_key;
91   };
92
93   //----------------------------------------------------------------------------
94   template<class Base>
95   ObjectHolder<Base>::~ObjectHolder()
96   {
97     if( !naIsNil(_ref) )
98       naGCRelease(_gc_key);
99   }
100
101   //----------------------------------------------------------------------------
102   template<class Base>
103   naRef ObjectHolder<Base>::get_naRef() const
104   {
105     return _ref;
106   }
107
108   //----------------------------------------------------------------------------
109   template<class Base>
110   void ObjectHolder<Base>::reset()
111   {
112     if( !naIsNil(_ref) )
113       naGCRelease(_gc_key);
114
115     _ref = naNil();
116     _gc_key = 0;
117   }
118
119   //----------------------------------------------------------------------------
120   template<class Base>
121   void ObjectHolder<Base>::reset(naRef obj)
122   {
123     if( !naIsNil(_ref) )
124       naGCRelease(_gc_key);
125
126     _ref = obj;
127     _gc_key = naGCSave(obj);
128   }
129
130   //----------------------------------------------------------------------------
131   template<class Base>
132   bool ObjectHolder<Base>::valid() const
133   {
134     return !naIsNil(_ref);
135   }
136
137   //----------------------------------------------------------------------------
138   template<class Base>
139   ObjectHolder<Base>::ObjectHolder(naRef obj):
140     _ref(obj),
141     _gc_key(0)
142   {
143     if( !naIsNil(obj) )
144       naGCSave(obj);
145   }
146
147   //----------------------------------------------------------------------------
148   template<class Base>
149   ObjectHolder<Base>::ObjectHolder():
150     _ref(naNil()),
151     _gc_key(0)
152   {
153
154   }
155
156   //----------------------------------------------------------------------------
157   template<class Base>
158   SGSharedPtr<ObjectHolder<Base> >
159   ObjectHolder<Base>::makeShared(naRef obj)
160   {
161     return SGSharedPtr<ObjectHolder<Base> >( new ObjectHolder<SGReferenced>(obj) );
162   }
163
164 } // namespace nasal
165
166 #endif /* SG_NASAL_OBJECT_HOLDER_HXX_ */