- public:
- // TODO use variadic template when supporting C++11
- template<class A1>
- static naRef create( naContext c, const A1& a1 )
- {
- return makeGhost(c, new T(a1));
- }
-
- template<class A1, class A2>
- static naRef create( naContext c, const A1& a1,
- const A2& a2 )
- {
- return makeGhost(c, new T(a1, a2));
- }
-
- template<class A1, class A2, class A3>
- static naRef create( naContext c, const A1& a1,
- const A2& a2,
- const A3& a3 )
- {
- return makeGhost(c, new T(a1, a2, a3));
- }
-
- template<class A1, class A2, class A3, class A4>
- static naRef create( naContext c, const A1& a1,
- const A2& a2,
- const A3& a3,
- const A4& a4 )
- {
- return makeGhost(c, new T(a1, a2, a3, a4));
- }
-
- template<class A1, class A2, class A3, class A4, class A5>
- static naRef create( naContext c, const A1& a1,
- const A2& a2,
- const A3& a3,
- const A4& a4,
- const A5& a5 )
- {
- return makeGhost(c, new T(a1, a2, a3, a4, a5));
- }
-
- // TODO If you need more arguments just do some copy&paste :)
-
- static Derived& getInstance()
- {
- static Derived instance;
- return instance;
- }
-
- void setParent(const naRef& parent)
- {
- // TODO check if we need to take care of reference counting/gc
- _parents.resize(1);
- _parents[0] = parent;
- }
-
- protected:
-
- // TODO switch to boost::/std::function (with C++11 lambdas this can make
- // adding setters easier and shorter)
- typedef naRef (Derived::*getter_t)(naContext, const T&);
- typedef std::map<std::string, getter_t> MemberMap;
-
- const std::string _ghost_name;
- std::vector<naRef> _parents;
- MemberMap _members;
-
- NasalObject(const std::string& ghost_name):
- _ghost_name( ghost_name )
- {
- _ghost_type.destroy = &destroyGhost;
- _ghost_type.name = _ghost_name.c_str();
- _ghost_type.get_member = &Derived::getMember;
- _ghost_type.set_member = 0;
-
- _members["parents"] = &NasalObject::getParents;
- }
-
- naRef getParents(naContext c, const T&)
- {
- naRef parents = naNewVector(c);
- for(size_t i = 0; i < _parents.size(); ++i)
- naVec_append(parents, _parents[i]);
- return parents;
- }
-
- static naRef makeGhost(naContext c, void *ptr)
- {
- std::cout << "create " << ptr << std::endl;
- return naNewGhost2(c, &(getInstance()._ghost_type), ptr);
- }
-
- static void destroyGhost(void *ptr)
- {
- std::cout << "destroy " << ptr << std::endl;
- delete (T*)ptr;
- }
-
- static const char* getMember(naContext c, void* g, naRef field, naRef* out)
- {
- typename MemberMap::iterator getter =
- getInstance()._members.find(naStr_data(field));
-
- if( getter == getInstance()._members.end() )
- return 0;
-
- *out = (getInstance().*getter->second)(c, *static_cast<T*>(g));
- return "";
- }
-
- private:
-
- naGhostType _ghost_type;
-
-};
-
-typedef osg::ref_ptr<osgGA::GUIEventAdapter> GUIEventPtr;
-
-class NasalCanvasEvent:
- public NasalObject<GUIEventPtr, NasalCanvasEvent>