#define SG_NASAL_GHOST_HXX_
#include "NasalCallContext.hxx"
+#include "NasalObjectHolder.hxx"
#include <simgear/debug/logstream.hxx>
{
public:
virtual ~MethodHolder() {}
- virtual naRef get_naRef(naContext c) = 0;
+
+ naRef get_naRef(naContext c)
+ {
+ if( !_obj )
+ _obj = ObjectHolder::makeShared(createNasalObject(c));
+ return _obj->get_naRef();
+ }
+
+ protected:
+ ObjectHolderRef _obj;
+
+ virtual naRef createNasalObject(naContext c) = 0;
};
BOOST_MPL_HAS_XXX_TRAIT_DEF(element_type)
public internal::MethodHolder
{
public:
- MethodHolder():
- _naRef(naNil())
- {}
-
explicit MethodHolder(const method_t& method):
- _method(method),
- _naRef(naNil())
+ _method(method)
{}
- virtual naRef get_naRef(naContext c)
- {
- if( naIsNil(_naRef) )
- {
- _naRef = naNewFunc(c, naNewCCodeU(c, &MethodHolder::call, this));
- naSave(c, _naRef);
- }
- return _naRef;
- }
-
protected:
method_t _method;
- naRef _naRef;
+
+ virtual naRef createNasalObject(naContext c)
+ {
+ return naNewFunc(c, naNewCCodeU(c, &MethodHolder::call, this));
+ }
static naRef call( naContext c,
naRef me,
}
//----------------------------------------------------------------------------
- SGSharedPtr<ObjectHolder> ObjectHolder::makeShared(naRef obj)
+ ObjectHolderRef ObjectHolder::makeShared(naRef obj)
{
return new ObjectHolder(obj);
}
namespace nasal
{
+ class ObjectHolder;
+ typedef SGSharedPtr<ObjectHolder> ObjectHolderRef;
+
/**
* Prevent a Nasal object from being destroyed by the garbage collector during
* the lifetime of this object.
*
* @param obj Object to save
*/
- static SGSharedPtr<ObjectHolder> makeShared(naRef obj);
+ static ObjectHolderRef makeShared(naRef obj);
protected:
naRef _ref;