diff --git a/include/osgDB/Serializer b/include/osgDB/Serializer index 663b92ecb..835e651ad 100644 --- a/include/osgDB/Serializer +++ b/include/osgDB/Serializer @@ -1186,6 +1186,7 @@ public: virtual unsigned int size(const osg::Object& obj) const { return 0; } virtual MapIteratorObject* createIterator(osg::Object& obj) const { return 0; } + virtual MapIteratorObject* createReverseIterator(osg::Object& obj) const { return 0; } protected: Type _keyType; @@ -1204,6 +1205,7 @@ public: typedef typename P::key_type KeyType; typedef typename P::mapped_type ElementType; typedef typename P::iterator Iterator; + typedef typename P::reverse_iterator ReverseIterator; typedef typename P::const_iterator ConstIterator; typedef P& (C::*Getter)(); typedef const P& (C::*ConstGetter)() const; @@ -1270,6 +1272,23 @@ public: virtual void setElement(void* ptr) const { if (valid()) _itr->second = *reinterpret_cast(ptr); } }; + struct ReverseMapIterator : public MapIteratorObject + { + ReverseMapIterator(BaseSerializer::Type keyType, unsigned int keySize, BaseSerializer::Type elementType, unsigned int elementSize, + ReverseIterator itr, ReverseIterator endItr): + MapIteratorObject(keyType, keySize, elementType, elementSize), + _itr(itr),_endItr(endItr) {} + + ReverseIterator _itr; + ReverseIterator _endItr; + + virtual bool advance() { if (valid()) ++_itr; return valid(); } + virtual bool valid() const { return _itr!=_endItr; } + virtual const void* getKey() const { return valid() ? &(_itr->first) : 0; } + virtual void* getElement() const { return valid() ? &(_itr->second) : 0; } + virtual void setElement(void* ptr) const { if (valid()) _itr->second = *reinterpret_cast(ptr); } + }; + virtual MapIteratorObject* createIterator(osg::Object& obj) const { C& object = OBJECT_CAST(obj); @@ -1277,6 +1296,13 @@ public: return new MapIterator(_keyType, _keySize, _elementType, _elementSize, map.begin(), map.end()); } + virtual MapIteratorObject* createReverseIterator(osg::Object& obj) const + { + C& object = OBJECT_CAST(obj); + P& map = (object.*_getter)(); + return new ReverseMapIterator(_keyType, _keySize, _elementType, _elementSize, map.rbegin(), map.rend()); + } + virtual bool read( InputStream& is, osg::Object& obj ) { C& object = OBJECT_CAST(obj); diff --git a/src/osgPlugins/lua/LuaScriptEngine.cpp b/src/osgPlugins/lua/LuaScriptEngine.cpp index b5a788a85..5bd2c9207 100644 --- a/src/osgPlugins/lua/LuaScriptEngine.cpp +++ b/src/osgPlugins/lua/LuaScriptEngine.cpp @@ -492,6 +492,28 @@ static int createMapIterator(lua_State* _lua) return 0; } +static int createMapReverseIterator(lua_State* _lua) +{ + const LuaScriptEngine* lse = reinterpret_cast(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(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(bs); + if (ms) + { + lse->pushObject(ms->createReverseIterator(*object)); + return 1; + } + + return 0; +} + ////////////////////////////////////////////////////////////////////////////////////// // // MapIteratorObject support @@ -3037,6 +3059,7 @@ void LuaScriptEngine::pushContainer(osg::Object* object, const std::string& prop assignClosure("clear", callMapClear); assignClosure("size", getMapSize); assignClosure("createIterator", createMapIterator); + assignClosure("createReverseIterator", createMapReverseIterator); luaL_getmetatable(_lua, "LuaScriptEngine.Map"); lua_setmetatable(_lua, -2);