Preliminary support for wrapping methods in the lua script plugin.

This commit is contained in:
Robert Osfield 2013-12-25 17:36:32 +00:00
parent 865a47bd6b
commit afab78ed40
12 changed files with 466 additions and 39 deletions

View File

@ -451,8 +451,8 @@ int main(int argc, char** argv)
osg::ref_ptr<osg::Script> script = osgDB::readFile<osg::Script>(str);
if (script.valid())
{
osg::ScriptEngine::Parameters inputParameters;
osg::ScriptEngine::Parameters outputParameters;
osg::Parameters inputParameters;
osg::Parameters outputParameters;
inputParameters.push_back(new osg::StringValueObject("string","my very first string input"));
inputParameters.push_back(new osg::DoubleValueObject("double",1.234));

View File

@ -21,6 +21,9 @@
namespace osg
{
typedef std::vector< osg::ref_ptr<osg::Object> > Parameters;
// forward declare
class ScriptEngine;
@ -93,8 +96,6 @@ class ScriptEngine : public osg::Object
/** get the scripting language supported by the ScriptEngine.*/
inline const std::string& getLanguage() const { return _language; }
typedef std::vector< osg::ref_ptr<osg::Object> > Parameters;
/** run a Script.*/
bool run(osg::Script* script)
{

View File

@ -95,7 +95,8 @@ class ValueObject : public Object
virtual bool get(GetValueVisitor& /*gvv*/) const { return false; }
virtual bool set(SetValueVisitor& /*gvv*/) { return false; }
protected:
protected:
virtual ~ValueObject() {}
};
@ -207,6 +208,7 @@ void osg::Object::setUserValue(const std::string& name, const T& value)
else udc->addUserObject(new UserValueObject(name,value));
}
}
#endif

View File

@ -45,12 +45,12 @@ void ScriptCallback::operator()(Node* node, NodeVisitor* nv)
ref_ptr<NodeVisitor> ref_nv(nv);
{
ScriptEngine::Parameters inputParameters;
Parameters inputParameters;
inputParameters.push_back(node);
inputParameters.push_back(nv);
// empty outputParameters
ScriptEngine::Parameters outputParameters;
Parameters outputParameters;
engine->run(_script.get(), _entryPoint, inputParameters, outputParameters);
}

View File

@ -1,7 +1,11 @@
SET(TARGET_H
MethodObject.h
LuaScriptEngine.h
)
SET(TARGET_SRC
MethodObject.cpp
GeodeMethods.cpp
GroupMethods.cpp
LuaScriptEngine.cpp
ReaderWriterLua.cpp
)

View File

@ -0,0 +1,82 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osg/Geode>
#include <osg/ValueObject>
#include "MethodObject.h"
struct GeodeGetNumDrawables : public osgDB::MethodObject
{
virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
{
osg::Geode* geode = reinterpret_cast<osg::Geode*>(objectPtr);
outputParameters.push_back(new osg::UIntValueObject("return", geode->getNumDrawables()));
return true;
}
};
struct GeodeGetDrawable : public osgDB::MethodObject
{
virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
{
if (inputParameters.empty()) return false;
osg::Object* indexObject = inputParameters[0].get();
osg::UIntValueObject* uivo = dynamic_cast<osg::UIntValueObject*>(indexObject);
if (!uivo) return false;
osg::Geode* geode = reinterpret_cast<osg::Geode*>(objectPtr);
outputParameters.push_back(geode->getDrawable(uivo->getValue()));
return true;
}
};
struct GeodeAddDrawable : public osgDB::MethodObject
{
virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
{
if (inputParameters.empty()) return false;
osg::Drawable* child = dynamic_cast<osg::Drawable*>(inputParameters[0].get());
if (!child) return false;
osg::Geode* geode = reinterpret_cast<osg::Geode*>(objectPtr);
geode->addDrawable(child);
return true;
}
};
struct GeodeRemoveDrawable : public osgDB::MethodObject
{
virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
{
if (inputParameters.empty()) return false;
osg::Drawable* child = dynamic_cast<osg::Drawable*>(inputParameters[0].get());
if (!child) return false;
osg::Geode* geode = reinterpret_cast<osg::Geode*>(objectPtr);
geode->removeDrawable(child);
return true;
}
};
static osgDB::RegisterMethodObjectProxy s_getNumDrawables("osg::Geode","getNumDrawables",new GeodeGetNumDrawables());
static osgDB::RegisterMethodObjectProxy s_getNumDrawable("osg::Geode","getDrawable",new GeodeGetDrawable());
static osgDB::RegisterMethodObjectProxy s_getAddDrawable("osg::Geode","addDrawable",new GeodeAddDrawable());
static osgDB::RegisterMethodObjectProxy s_getRemoveDrawable("osg::Geode","removeDrawable",new GeodeRemoveDrawable());

View File

@ -0,0 +1,91 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osg/Group>
#include <osg/ValueObject>
#include "MethodObject.h"
using namespace osgDB;
struct GroupGetNumChildren : public osgDB::MethodObject
{
virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
{
osg::Group* group = reinterpret_cast<osg::Group*>(objectPtr);
outputParameters.push_back(new osg::UIntValueObject("return", group->getNumChildren()));
return true;
}
};
struct GroupGetChild : public osgDB::MethodObject
{
virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
{
if (inputParameters.empty()) return false;
osg::Object* indexObject = inputParameters[0].get();
OSG_NOTICE<<"GroupGetChild "<<indexObject->className()<<std::endl;
unsigned int index = 0;
osg::DoubleValueObject* dvo = dynamic_cast<osg::DoubleValueObject*>(indexObject);
if (dvo) index = static_cast<unsigned int>(dvo->getValue());
else
{
osg::UIntValueObject* uivo = dynamic_cast<osg::UIntValueObject*>(indexObject);
if (uivo) index = uivo->getValue();
}
osg::Group* group = reinterpret_cast<osg::Group*>(objectPtr);
outputParameters.push_back(group->getChild(index));
return true;
}
};
struct GroupAddChild : public osgDB::MethodObject
{
virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
{
if (inputParameters.empty()) return false;
osg::Node* child = dynamic_cast<osg::Node*>(inputParameters[0].get());
if (!child) return false;
osg::Group* group = reinterpret_cast<osg::Group*>(objectPtr);
group->addChild(child);
return true;
}
};
struct GroupRemoveChild : public osgDB::MethodObject
{
virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
{
if (inputParameters.empty()) return false;
osg::Node* child = dynamic_cast<osg::Node*>(inputParameters[0].get());
if (!child) return false;
osg::Group* group = reinterpret_cast<osg::Group*>(objectPtr);
group->removeChild(child);
return true;
}
};
static osgDB::RegisterMethodObjectProxy s_getNumChildren("osg::Group","getNumChildren",new GroupGetNumChildren());
static osgDB::RegisterMethodObjectProxy s_getNumChild("osg::Group","getChild",new GroupGetChild());
static osgDB::RegisterMethodObjectProxy s_getAddChild("osg::Group","addChild",new GroupAddChild());
static osgDB::RegisterMethodObjectProxy s_getRemoveChild("osg::Group","removeChild",new GroupRemoveChild());

View File

@ -12,6 +12,7 @@
*/
#include "LuaScriptEngine.h"
#include "MethodObject.h"
#include <osg/io_utils>
#include <osgDB/ReadFile>
@ -77,10 +78,45 @@ static int setProperty(lua_State* _lua)
}
}
OSG_NOTICE<<"Warning: Lua getProperty() not matched"<<std::endl;
OSG_NOTICE<<"Warning: Lua setProperty() not matched"<<std::endl;
return 0;
}
static int callClassMethod(lua_State* _lua)
{
const LuaScriptEngine* lse = reinterpret_cast<const LuaScriptEngine*>(lua_topointer(_lua, lua_upvalueindex(1)));
std::string methodName = lua_tostring(_lua, lua_upvalueindex(2));
int n = lua_gettop(_lua); /* number of arguments */
OSG_NOTICE<<"callClassMethod(), n = "<<n<<", "<<lua_type(_lua, 1)<<std::endl;
if (n>=1 && lua_type(_lua, 1)==LUA_TTABLE)
{
osg::Object* object = lse->getObjectFromTable<osg::Object>(1);
OSG_NOTICE<<"callClassMethod() on "<<object->className()<<" method name "<<methodName<<std::endl;
// need to put within a c function
osg::Parameters inputParameters, outputParameters;
for(int i=2; i<=n; ++i)
{
OSG_NOTICE<<" need to push parameter "<<lua_typename(_lua, lua_type(_lua, n))<<std::endl;
inputParameters.push_back(lse->popParameterObject());
}
if (osgDB::MethodsObjectManager::instance()->run(object, object->getCompoundClassName(), methodName, inputParameters, outputParameters))
{
for(osg::Parameters::iterator itr = outputParameters.begin();
itr != outputParameters.end();
++itr)
{
OSG_NOTICE<<" pushing return "<<(*itr)->className()<<std::endl;
lse->pushParameter(itr->get());
}
return outputParameters.size();
}
}
return 0;
}
#if 0
static int getChild(lua_State * _lua)
{
const LuaScriptEngine* lse = reinterpret_cast<const LuaScriptEngine*>(lua_topointer(_lua, lua_upvalueindex(1)));
@ -112,16 +148,11 @@ static int getChild(lua_State * _lua)
return 0;
}
static int setChild(lua_State* _lua)
{
const LuaScriptEngine* lse = reinterpret_cast<const LuaScriptEngine*>(lua_topointer(_lua, lua_upvalueindex(1)));
int n = lua_gettop(_lua); /* number of arguments */
if (n==3 &&
lua_type(_lua, 1)==LUA_TTABLE &&
lua_type(_lua, 2)==LUA_TNUMBER &&
lua_type(_lua, 3)==LUA_TTABLE)
if (lse->matchLuaParameters(LUA_TTABLE, LUA_TNUMBER, LUA_TTABLE))
{
osg::Group* group = lse->getObjectFromTable<osg::Group>(1);
@ -150,15 +181,13 @@ static int addChild(lua_State* _lua)
{
const LuaScriptEngine* lse = reinterpret_cast<const LuaScriptEngine*>(lua_topointer(_lua, lua_upvalueindex(1)));
int n = lua_gettop(_lua); /* number of arguments */
if (n==2 &&
lua_type(_lua, 1)==LUA_TTABLE &&
lua_type(_lua, 2)==LUA_TTABLE)
if (lse->matchLuaParameters(LUA_TTABLE, LUA_TTABLE))
{
osg::Group* group = lse->getObjectFromTable<osg::Group>(1);
osg::Node* child = lse->getObjectFromTable<osg::Node>(2);
if (group && child)
{
OSG_NOTICE<<"addChild("<<child->className()<<") ptr = "<<child<<std::endl;
group->addChild(child);
return 0;
}
@ -172,8 +201,7 @@ static int getNumChildren(lua_State * _lua)
{
const LuaScriptEngine* lse = reinterpret_cast<const LuaScriptEngine*>(lua_topointer(_lua, lua_upvalueindex(1)));
int n = lua_gettop(_lua); /* number of arguments */
if (n==1 && (lua_type(_lua, 1)==LUA_TTABLE))
if (lse->matchLuaParameters(LUA_TTABLE))
{
osg::Group* group = lse->getObjectFromTable<osg::Group>(1);
if (group)
@ -186,6 +214,7 @@ static int getNumChildren(lua_State * _lua)
OSG_NOTICE<<"Warning: Lua Group::getNumChildren() not matched"<<std::endl;
return 0;
}
#endif
static int garabageCollectObject(lua_State* _lua)
{
@ -391,7 +420,7 @@ bool LuaScriptEngine::loadScript(osg::Script* script)
}
bool LuaScriptEngine::run(osg::Script* script, const std::string& entryPoint, Parameters& inputParameters, Parameters& outputParameters)
bool LuaScriptEngine::run(osg::Script* script, const std::string& entryPoint, osg::Parameters& inputParameters, osg::Parameters& outputParameters)
{
if (!script || !_lua) return false;
@ -424,7 +453,7 @@ bool LuaScriptEngine::run(osg::Script* script, const std::string& entryPoint, Pa
lua_getglobal(_lua, entryPoint.c_str()); /* function to be called */
}
for(osg::ScriptEngine::Parameters::const_iterator itr = inputParameters.begin();
for(osg::Parameters::const_iterator itr = inputParameters.begin();
itr != inputParameters.end();
++itr)
{
@ -457,10 +486,10 @@ class PushStackValueVisitor : public osg::ValueObject::GetValueVisitor
{
public:
LuaScriptEngine* _lsg;
const LuaScriptEngine* _lsg;
lua_State* _lua;
PushStackValueVisitor(LuaScriptEngine* lsg) : _lsg(lsg) { _lua = lsg->getLuaState(); }
PushStackValueVisitor(const LuaScriptEngine* lsg) : _lsg(lsg) { _lua = const_cast<LuaScriptEngine*>(lsg)->getLuaState(); }
virtual void apply(bool value) { lua_pushboolean(_lua, value ? 0 : 1); }
virtual void apply(char value) { lua_pushnumber(_lua, value); }
@ -492,12 +521,12 @@ class GetStackValueVisitor : public osg::ValueObject::SetValueVisitor
{
public:
LuaScriptEngine* _lsg;
const LuaScriptEngine* _lsg;
lua_State* _lua;
int _index;
int _numberToPop;
GetStackValueVisitor(LuaScriptEngine* lsg, int index) : _lsg(lsg), _lua(0), _index(index), _numberToPop(0) { _lua = lsg->getLuaState(); }
GetStackValueVisitor(const LuaScriptEngine* lsg, int index) : _lsg(lsg), _lua(0), _index(index), _numberToPop(0) { _lua = const_cast<LuaScriptEngine*>(lsg)->getLuaState(); }
virtual void apply(bool& value) { if (lua_isboolean(_lua, _index)) { value = (lua_toboolean(_lua, _index)!=0); _numberToPop = 1; } }
@ -527,6 +556,16 @@ int LuaScriptEngine::pushPropertyToStack(osg::Object* object, const std::string&
osgDB::BaseSerializer::Type type;
if (!_pi.getPropertyType(object, propertyName, type))
{
if (osgDB::MethodsObjectManager::instance()->hasMethod(object->getCompoundClassName(), propertyName))
{
OSG_NOTICE<<"LuaScriptEngine::pushPropertyToStack("<<object<<", "<<propertyName<<") has method need to call it."<<std::endl;
lua_pushlightuserdata(_lua, const_cast<LuaScriptEngine*>(this));
lua_pushstring(_lua, propertyName.c_str());
lua_pushcclosure(_lua, callClassMethod, 2);
return 1;
}
OSG_NOTICE<<"LuaScriptEngine::pushPropertyToStack("<<object<<", "<<propertyName<<") no property found."<<std::endl;
return 0;
}
@ -670,6 +709,11 @@ int LuaScriptEngine::pushPropertyToStack(osg::Object* object, const std::string&
}
break;
}
case(osgDB::BaseSerializer::RW_LIST):
{
OSG_NOTICE<<"Need to implement RW_LIST support"<<std::endl;
break;
}
case(osgDB::BaseSerializer::RW_IMAGE):
case(osgDB::BaseSerializer::RW_OBJECT):
{
@ -862,6 +906,11 @@ int LuaScriptEngine::setPropertyFromStack(osg::Object* object, const std::string
}
break;
}
case(osgDB::BaseSerializer::RW_LIST):
{
OSG_NOTICE<<"Need to implement RW_LIST support"<<std::endl;
break;
}
case(osgDB::BaseSerializer::RW_IMAGE):
case(osgDB::BaseSerializer::RW_OBJECT):
{
@ -1019,7 +1068,7 @@ osgDB::BaseSerializer::Type LuaScriptEngine::getType() const
return osgDB::BaseSerializer::RW_MATRIXD;
}
// not supported
OSG_NOTICE<<"Warning: LuaScriptEngine::getType()Lua table configuration not supported."<<std::endl;
OSG_NOTICE<<"Warning: LuaScriptEngine::getType() Lua table configuration not supported."<<std::endl;
break;
}
default:
@ -1275,7 +1324,7 @@ void LuaScriptEngine::pushValue(const osg::Matrixd& value) const
}
}
bool LuaScriptEngine::pushParameter(osg::Object* object)
bool LuaScriptEngine::pushParameter(osg::Object* object) const
{
osg::ValueObject* vo = dynamic_cast<osg::ValueObject*>(object);
if (vo)
@ -1291,7 +1340,7 @@ bool LuaScriptEngine::pushParameter(osg::Object* object)
return false;
}
bool LuaScriptEngine::popParameter(osg::Object* object)
bool LuaScriptEngine::popParameter(osg::Object* object) const
{
osg::ValueObject* vo = dynamic_cast<osg::ValueObject*>(object);
if (vo)
@ -1308,7 +1357,7 @@ bool LuaScriptEngine::popParameter(osg::Object* object)
return false;
}
osg::Object* LuaScriptEngine::popParameterObject()
osg::Object* LuaScriptEngine::popParameterObject() const
{
osg::ref_ptr<osg::Object> object = 0;
@ -1412,6 +1461,11 @@ osg::Object* LuaScriptEngine::popParameterObject()
if (getValue(value)) object = new osg::MatrixdValueObject("", value);
break;
}
case(osgDB::BaseSerializer::RW_LIST):
{
OSG_NOTICE<<"Need to implement RW_LIST support"<<std::endl;
break;
}
case(osgDB::BaseSerializer::RW_IMAGE):
case(osgDB::BaseSerializer::RW_OBJECT):
{
@ -1484,8 +1538,16 @@ void LuaScriptEngine::pushObject(osg::Object* object) const
lua_pushstring(_lua, "className"); lua_pushstring(_lua, object->className()); lua_settable(_lua, -3);
osg::Group* group = dynamic_cast<osg::Group*>(object);
osg::Geode* geode = dynamic_cast<osg::Geode*>(object);
if (group)
{
#if 0
assignClosure("getChild", getChild);
assignClosure("setChild", setChild);
assignClosure("addChild", addChild);
assignClosure("getNumChildren", getNumChildren);
#endif
#if 0
lua_pushstring(_lua, "getChild");
lua_pushlightuserdata(_lua, const_cast<LuaScriptEngine*>(this));
lua_pushcclosure(_lua, getChild, 1);
@ -1505,7 +1567,18 @@ void LuaScriptEngine::pushObject(osg::Object* object) const
lua_pushlightuserdata(_lua, const_cast<LuaScriptEngine*>(this));
lua_pushcclosure(_lua, getNumChildren, 1);
lua_settable(_lua, -3);
#endif
luaL_getmetatable(_lua, "LuaScriptEngine.Object");
lua_setmetatable(_lua, -2);
}
else if (geode)
{
#if 0
assignClosure("getDrawable",getDrawable,1);
assignClosure("setDrawable",setDrawable,1);
assignClosure("addDrawable",addDrawable,1);
assignClosure("getNumDrawables",getNumDrawables,1);
#endif
luaL_getmetatable(_lua, "LuaScriptEngine.Object");
lua_setmetatable(_lua, -2);
}
@ -1521,3 +1594,10 @@ void LuaScriptEngine::pushObject(osg::Object* object) const
}
}
void LuaScriptEngine::assignClosure(const char* name, lua_CFunction fn) const
{
lua_pushstring(_lua, name);
lua_pushlightuserdata(_lua, const_cast<LuaScriptEngine*>(this));
lua_pushcclosure(_lua, fn, 1);
lua_settable(_lua, -3);
}

View File

@ -26,6 +26,9 @@ extern "C" {
namespace lua
{
// forward declare
class LuaScriptEngine;
class LuaScriptEngine : public osg::ScriptEngine
{
public:
@ -37,10 +40,10 @@ class LuaScriptEngine : public osg::ScriptEngine
virtual const std::string& getLanguage() const { return _language; }
/** run a Script.*/
virtual bool run(osg::Script* script, const std::string& entryPoint, Parameters& inputParameters, Parameters& outputParameters);
virtual bool run(osg::Script* script, const std::string& entryPoint, osg::Parameters& inputParameters, osg::Parameters& outputParameters);
/** get the lua_State object.*/
lua_State* getLuaState() { return _lua; }
lua_State* getLuaState() const { return _lua; }
int pushPropertyToStack(osg::Object* object, const std::string& propertyName) const;
int setPropertyFromStack(osg::Object* object, const std::string& propertyName) const;
@ -83,9 +86,9 @@ class LuaScriptEngine : public osg::ScriptEngine
void pushValue(const osg::Plane& value) const;
void pushValue(const osg::Matrixd& value) const;
bool pushParameter(osg::Object* object);
bool popParameter(osg::Object* object);
osg::Object* popParameterObject();
bool pushParameter(osg::Object* object) const;
bool popParameter(osg::Object* object) const;
osg::Object* popParameterObject() const;
void createAndPushObject(const std::string& compoundName) const;
@ -110,6 +113,13 @@ class LuaScriptEngine : public osg::ScriptEngine
else return 0;
}
void assignClosure(const char* name, lua_CFunction fn) const;
bool matchLuaParameters(int luaType1) const { return ((lua_gettop(_lua)==1) && (lua_type(_lua, 1)==luaType1)); }
bool matchLuaParameters(int luaType1, int luaType2) const { return ((lua_gettop(_lua)==2) && (lua_type(_lua, 1)==luaType1) && (lua_type(_lua, 2)==luaType2)); }
bool matchLuaParameters(int luaType1, int luaType2, int luaType3) const { return ((lua_gettop(_lua)==3) && (lua_type(_lua, 1)==luaType1) && (lua_type(_lua, 2)==luaType2) && (lua_type(_lua, 3)==luaType3)); }
bool matchLuaParameters(int luaType1, int luaType2, int luaType3, int luaType4) const { return ((lua_gettop(_lua)==4) && (lua_type(_lua, 1)==luaType1) && (lua_type(_lua, 2)==luaType2) && (lua_type(_lua, 3)==luaType3) && (lua_type(_lua, 4)==luaType4)); }
protected:
void initialize();

View File

@ -0,0 +1,85 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include "MethodObject.h"
#include <osg/Notify>
using namespace osgDB;
void MethodsObject::add(const std::string& methodName, MethodObject* mo)
{
methodMap.insert(MethodMap::value_type(methodName, mo));
}
bool MethodsObject::run(void* objectPtr, const std::string& methodName, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
{
MethodMap::const_iterator itr = methodMap.find(methodName);
while (itr != methodMap.end())
{
if (itr->first==methodName)
{
OSG_NOTICE<<"Calling methodObject for "<<methodName<<std::endl;
if (itr->second->run(objectPtr, inputParameters, outputParameters)) return true;
}
++itr;
}
OSG_NOTICE<<"No matching methodObject found for "<<methodName<<std::endl;
return false;
}
bool MethodsObject::hasMethod(const std::string& methodName) const
{
return (methodMap.find(methodName)!=methodMap.end());
}
osg::ref_ptr<MethodsObjectManager>& MethodsObjectManager::instance()
{
static osg::ref_ptr<MethodsObjectManager> s_MethodsObjectManager = new MethodsObjectManager;
return s_MethodsObjectManager;
}
void MethodsObjectManager::add(const std::string& compoundClassName, const std::string& methodName, MethodObject* mo)
{
MethodsObjectMap::iterator itr = methodsObjects.find( compoundClassName );
if (itr==methodsObjects.end())
{
methodsObjects[compoundClassName] = new MethodsObject;
itr = methodsObjects.find( compoundClassName );
}
itr->second->add(methodName, mo);
}
bool MethodsObjectManager::run(void* objectPtr, const std::string& compoundClassName, const std::string& methodName, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
{
MethodsObjectMap::const_iterator itr = methodsObjects.find( compoundClassName );
if (itr!=methodsObjects.end())
{
return itr->second->run(objectPtr, methodName, inputParameters, outputParameters);
}
OSG_NOTICE<<"No matching methodObject found for class "<<compoundClassName<<", method "<<methodName<<std::endl;
return false;
}
bool MethodsObjectManager::hasMethod(const std::string& compoundClassName, const std::string& methodName) const
{
MethodsObjectMap::const_iterator itr = methodsObjects.find( compoundClassName );
if (itr!=methodsObjects.end())
{
return itr->second->hasMethod(methodName);
}
return false;
}

View File

@ -0,0 +1,72 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef METHODSOBJECT_H
#define METHODSOBJECT_H
#include <osg/ScriptEngine>
namespace osgDB
{
struct MethodObject : public osg::Referenced
{
typedef std::vector< osg::ref_ptr<osg::Object> > Parameters;
virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const = 0;
virtual ~MethodObject() {}
};
struct MethodsObject : public osg::Referenced
{
MethodsObject() {}
virtual ~MethodsObject() {}
void add(const std::string& methodName, MethodObject* mo);
bool run(void* objectPtr, const std::string& methodName, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const;
bool hasMethod(const std::string& methodName) const;
typedef std::multimap< std::string, osg::ref_ptr<MethodObject> > MethodMap;
MethodMap methodMap;
};
struct MethodsObjectManager : public osg::Referenced
{
MethodsObjectManager() {}
virtual ~MethodsObjectManager() {}
static osg::ref_ptr<MethodsObjectManager>& instance();
void add(const std::string& compoundClassName, const std::string& methodName, MethodObject* mo);
bool run(void* objectPtr, const std::string& compoundClassName, const std::string& methodName, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const;
bool hasMethod(const std::string& compoundClassName, const std::string& methodName) const;
typedef std::map< std::string, osg::ref_ptr<MethodsObject> > MethodsObjectMap;
MethodsObjectMap methodsObjects;
};
struct RegisterMethodObjectProxy
{
RegisterMethodObjectProxy(const std::string& compoundClassName, const std::string& methodName, MethodObject* mo)
{
MethodsObjectManager::instance()->add(compoundClassName, methodName, mo);
}
};
}
#endif

View File

@ -73,8 +73,8 @@ class ReaderWriterLua : public osgDB::ReaderWriter
if (!script) return ReadResult::ERROR_IN_READING_FILE;
std::string entryPoint = "";
osg::ScriptEngine::Parameters inputParameters;
osg::ScriptEngine::Parameters outputParameters;
osg::Parameters inputParameters;
osg::Parameters outputParameters;
osg::ref_ptr<lua::LuaScriptEngine> se = new lua::LuaScriptEngine();
if (!se->run(script.get(), entryPoint, inputParameters, outputParameters)) return 0;
@ -84,7 +84,7 @@ class ReaderWriterLua : public osgDB::ReaderWriter
typedef std::vector< osg::ref_ptr<osg::Object> > Objects;
Objects objects;
for(osg::ScriptEngine::Parameters::iterator itr = outputParameters.begin();
for(osg::Parameters::iterator itr = outputParameters.begin();
itr != outputParameters.end();
++itr)
{