Skip to content

Commit

Permalink
Added Map serializer size() method and support for it in the lua plug…
Browse files Browse the repository at this point in the history
…in. Renamed functions in lua plugin to be more consistent.
  • Loading branch information
robertosfield committed Feb 26, 2014
1 parent 8e9d0c5 commit d0b1ca7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
8 changes: 8 additions & 0 deletions include/osgDB/Serializer
Expand Up @@ -1145,6 +1145,7 @@ public:
virtual void setElement(osg::Object& obj, void* ptrKey, void* ptrValue) const {}
virtual void* getElement(osg::Object& obj, void* ptrKey) const { return 0; }
virtual const void* getElement(const osg::Object& obj, void* ptrKey) const { return 0; }
virtual unsigned int size(const osg::Object& obj) const { return 0; }

protected:
Type _keyType;
Expand Down Expand Up @@ -1204,6 +1205,13 @@ public:
else return &(itr->second);
}

virtual unsigned int size(const osg::Object& obj) const
{
const C& object = OBJECT_CAST<const C&>(obj);
const P& map = (object.*_constgetter)();
return map.size();
}

virtual bool read( InputStream& is, osg::Object& obj )
{
C& object = OBJECT_CAST<C&>(obj);
Expand Down
53 changes: 37 additions & 16 deletions src/osgPlugins/lua/LuaScriptEngine.cpp
Expand Up @@ -223,7 +223,7 @@ static int getContainerSize(lua_State* _lua)
return 0;
}

static int getContainerClear(lua_State* _lua)
static int callVectorClear(lua_State* _lua)
{
const LuaScriptEngine* lse = reinterpret_cast<const LuaScriptEngine*>(lua_topointer(_lua, lua_upvalueindex(1)));
int n = lua_gettop(_lua); /* number of arguments */
Expand All @@ -245,7 +245,7 @@ static int getContainerClear(lua_State* _lua)
return 0;
}

static int getContainerResize(lua_State* _lua)
static int callVectorResize(lua_State* _lua)
{
const LuaScriptEngine* lse = reinterpret_cast<const LuaScriptEngine*>(lua_topointer(_lua, lua_upvalueindex(1)));
int n = lua_gettop(_lua); /* number of arguments */
Expand All @@ -267,7 +267,7 @@ static int getContainerResize(lua_State* _lua)
return 0;
}

static int getContainerReserve(lua_State* _lua)
static int callVectorReserve(lua_State* _lua)
{
const LuaScriptEngine* lse = reinterpret_cast<const LuaScriptEngine*>(lua_topointer(_lua, lua_upvalueindex(1)));
int n = lua_gettop(_lua); /* number of arguments */
Expand All @@ -290,7 +290,7 @@ static int getContainerReserve(lua_State* _lua)
}


static int getContainerAdd(lua_State* _lua)
static int callVectorAdd(lua_State* _lua)
{
const LuaScriptEngine* lse = reinterpret_cast<const LuaScriptEngine*>(lua_topointer(_lua, lua_upvalueindex(1)));
int n = lua_gettop(_lua); /* number of arguments */
Expand Down Expand Up @@ -425,7 +425,7 @@ static int setMapProperty(lua_State* _lua)
return 0;
}

static int getMapClear(lua_State* _lua)
static int callMapClear(lua_State* _lua)
{
const LuaScriptEngine* lse = reinterpret_cast<const LuaScriptEngine*>(lua_topointer(_lua, lua_upvalueindex(1)));
int n = lua_gettop(_lua); /* number of arguments */
Expand All @@ -440,15 +440,35 @@ static int getMapClear(lua_State* _lua)
osgDB::MapBaseSerializer* ms = dynamic_cast<osgDB::MapBaseSerializer*>(bs);
if (ms)
{
OSG_NOTICE<<"Doing map clear"<<std::endl;

ms->clear(*object);
return 0;
}

return 0;
}

static int getMapSize(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) return 0;

osg::Object* object = lse->getObjectFromTable<osg::Object>(1);
std::string containerPropertyName = lse->getStringFromTable(1,"containerPropertyName");

// check to see if Object "is a" vector
osgDB::BaseSerializer::Type type;
osgDB::BaseSerializer* bs = lse->getPropertyInterface().getSerializer(object, containerPropertyName, type);
osgDB::MapBaseSerializer* ms = dynamic_cast<osgDB::MapBaseSerializer*>(bs);
if (ms)
{
lua_pushinteger(lse->getLuaState(), ms->size(*object));
return 1;
}

return 0;
}

//////////////////////////////////////////////////////////////////////////////////////
//
// Method calling support
Expand Down Expand Up @@ -2875,18 +2895,19 @@ void LuaScriptEngine::pushContainer(osg::Object* object, const std::string& prop
if (vs)
{
assignClosure("size", getContainerSize);
assignClosure("clear", getContainerClear);
assignClosure("resize", getContainerResize);
assignClosure("reserve", getContainerReserve);
assignClosure("add", getContainerAdd);
assignClosure("clear", callVectorClear);
assignClosure("resize", callVectorResize);
assignClosure("reserve", callVectorReserve);
assignClosure("add", callVectorAdd);

luaL_getmetatable(_lua, "LuaScriptEngine.Container");
lua_setmetatable(_lua, -2);
}
else if (ms)
{
OSG_NOTICE<<"Need to set up map object"<<std::endl;
assignClosure("clear", getMapClear);
assignClosure("clear", callMapClear);
assignClosure("size", getMapSize);

luaL_getmetatable(_lua, "LuaScriptEngine.Map");
lua_setmetatable(_lua, -2);
Expand Down Expand Up @@ -2949,10 +2970,10 @@ void LuaScriptEngine::pushObject(osg::Object* object) const
lua_pushstring(_lua, "containerPropertyName"); lua_pushstring(_lua, "vector"); lua_settable(_lua, -3);

assignClosure("size", getContainerSize);
assignClosure("clear", getContainerClear);
assignClosure("resize", getContainerResize);
assignClosure("reserve", getContainerReserve);
assignClosure("add", getContainerAdd);
assignClosure("clear", callVectorClear);
assignClosure("resize", callVectorResize);
assignClosure("reserve", callVectorReserve);
assignClosure("add", callVectorAdd);

luaL_getmetatable(_lua, "LuaScriptEngine.Container");
lua_setmetatable(_lua, -2);
Expand Down

0 comments on commit d0b1ca7

Please sign in to comment.