1 ///@file Wrapper class for keeping Nasal objects save from the garbage collector
3 // Copyright (C) 2013 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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
19 #ifndef SG_NASAL_OBJECT_HOLDER_HXX_
20 #define SG_NASAL_OBJECT_HOLDER_HXX_
22 #include <simgear/nasal/nasal.h>
23 #include <simgear/structure/SGSharedPtr.hxx>
29 * Usable for example as empty base class if a base class is required.(Eg. as
30 * parameter for a mixin class).
32 struct empty_class {};
35 * Prevent a Nasal object from being destroyed by the garbage collector during
36 * the lifetime of this object.
38 template<class Base = empty_class>
45 * @param obj Object to save
47 explicit ObjectHolder(naRef obj);
60 * Get captured Nasal object
62 naRef get_naRef() const;
65 * Release the managed object
70 * Replaces the managed object (the old object is released)
72 * @param obj New object to save
74 void reset(naRef obj);
77 * Check if there is a managed object
82 * Save the given object as long as the returned holder exists.
84 * @param obj Object to save
86 static SGSharedPtr<ObjectHolder<Base> > makeShared(naRef obj);
93 //----------------------------------------------------------------------------
95 ObjectHolder<Base>::~ObjectHolder()
101 //----------------------------------------------------------------------------
103 naRef ObjectHolder<Base>::get_naRef() const
108 //----------------------------------------------------------------------------
110 void ObjectHolder<Base>::reset()
113 naGCRelease(_gc_key);
119 //----------------------------------------------------------------------------
121 void ObjectHolder<Base>::reset(naRef obj)
124 naGCRelease(_gc_key);
127 _gc_key = naGCSave(obj);
130 //----------------------------------------------------------------------------
132 bool ObjectHolder<Base>::valid() const
134 return !naIsNil(_ref);
137 //----------------------------------------------------------------------------
139 ObjectHolder<Base>::ObjectHolder(naRef obj):
147 //----------------------------------------------------------------------------
149 ObjectHolder<Base>::ObjectHolder():
156 //----------------------------------------------------------------------------
158 SGSharedPtr<ObjectHolder<Base> >
159 ObjectHolder<Base>::makeShared(naRef obj)
161 return SGSharedPtr<ObjectHolder<Base> >( new ObjectHolder<SGReferenced>(obj) );
166 #endif /* SG_NASAL_OBJECT_HOLDER_HXX_ */