From bc7bd7da9d2ce532d565f56f81ed892ccfe8f692 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 4 Jul 2016 10:11:34 +0100 Subject: [PATCH] Replaced pushVec(Vec2f etc.) implementations with template implementations. --- src/osgPlugins/lua/LuaScriptEngine.cpp | 74 -------------------------- src/osgPlugins/lua/LuaScriptEngine.h | 47 +++++++++++++--- 2 files changed, 39 insertions(+), 82 deletions(-) diff --git a/src/osgPlugins/lua/LuaScriptEngine.cpp b/src/osgPlugins/lua/LuaScriptEngine.cpp index 1ad972678..8fe3ca9c4 100644 --- a/src/osgPlugins/lua/LuaScriptEngine.cpp +++ b/src/osgPlugins/lua/LuaScriptEngine.cpp @@ -3773,33 +3773,6 @@ bool LuaScriptEngine::getValue(int pos, osg::BoundingSphered& value) const return true; } -void LuaScriptEngine::pushValue(const osg::Vec2f& value) const -{ - lua_newtable(_lua); - lua_newtable(_lua); luaL_getmetatable(_lua, "LuaScriptEngine.Table"); lua_setmetatable(_lua, -2); - lua_pushstring(_lua, "x"); lua_pushnumber(_lua, value.x()); lua_settable(_lua, -3); - lua_pushstring(_lua, "y"); lua_pushnumber(_lua, value.y()); lua_settable(_lua, -3); -} - -void LuaScriptEngine::pushValue(const osg::Vec3f& value) const -{ - lua_newtable(_lua); - lua_newtable(_lua); luaL_getmetatable(_lua, "LuaScriptEngine.Table"); lua_setmetatable(_lua, -2); - lua_pushstring(_lua, "x"); lua_pushnumber(_lua, value.x()); lua_settable(_lua, -3); - lua_pushstring(_lua, "y"); lua_pushnumber(_lua, value.y()); lua_settable(_lua, -3); - lua_pushstring(_lua, "z"); lua_pushnumber(_lua, value.z()); lua_settable(_lua, -3); -} - -void LuaScriptEngine::pushValue(const osg::Vec4f& value) const -{ - lua_newtable(_lua); - lua_newtable(_lua); luaL_getmetatable(_lua, "LuaScriptEngine.Table"); lua_setmetatable(_lua, -2); - lua_pushstring(_lua, "x"); lua_pushnumber(_lua, value.x()); lua_settable(_lua, -3); - lua_pushstring(_lua, "y"); lua_pushnumber(_lua, value.y()); lua_settable(_lua, -3); - lua_pushstring(_lua, "z"); lua_pushnumber(_lua, value.z()); lua_settable(_lua, -3); - lua_pushstring(_lua, "w"); lua_pushnumber(_lua, value.w()); lua_settable(_lua, -3); -} - void LuaScriptEngine::pushValue(const osg::Matrixf& value) const { lua_newtable(_lua); @@ -3814,53 +3787,6 @@ void LuaScriptEngine::pushValue(const osg::Matrixf& value) const } } -void LuaScriptEngine::pushValue(const osg::Vec2d& value) const -{ - lua_newtable(_lua); - lua_newtable(_lua); luaL_getmetatable(_lua, "LuaScriptEngine.Table"); lua_setmetatable(_lua, -2); - lua_pushstring(_lua, "x"); lua_pushnumber(_lua, value.x()); lua_settable(_lua, -3); - lua_pushstring(_lua, "y"); lua_pushnumber(_lua, value.y()); lua_settable(_lua, -3); -} - -void LuaScriptEngine::pushValue(const osg::Vec3d& value) const -{ - lua_newtable(_lua); - lua_newtable(_lua); luaL_getmetatable(_lua, "LuaScriptEngine.Table"); lua_setmetatable(_lua, -2); - lua_pushstring(_lua, "x"); lua_pushnumber(_lua, value.x()); lua_settable(_lua, -3); - lua_pushstring(_lua, "y"); lua_pushnumber(_lua, value.y()); lua_settable(_lua, -3); - lua_pushstring(_lua, "z"); lua_pushnumber(_lua, value.z()); lua_settable(_lua, -3); -} - -void LuaScriptEngine::pushValue(const osg::Vec4d& value) const -{ - lua_newtable(_lua); - lua_newtable(_lua); luaL_getmetatable(_lua, "LuaScriptEngine.Table"); lua_setmetatable(_lua, -2); - lua_pushstring(_lua, "x"); lua_pushnumber(_lua, value.x()); lua_settable(_lua, -3); - lua_pushstring(_lua, "y"); lua_pushnumber(_lua, value.y()); lua_settable(_lua, -3); - lua_pushstring(_lua, "z"); lua_pushnumber(_lua, value.z()); lua_settable(_lua, -3); - lua_pushstring(_lua, "w"); lua_pushnumber(_lua, value.w()); lua_settable(_lua, -3); -} - -void LuaScriptEngine::pushValue(const osg::Quat& value) const -{ - lua_newtable(_lua); - lua_newtable(_lua); luaL_getmetatable(_lua, "LuaScriptEngine.Table"); lua_setmetatable(_lua, -2); - lua_pushstring(_lua, "x"); lua_pushnumber(_lua, value.x()); lua_settable(_lua, -3); - lua_pushstring(_lua, "y"); lua_pushnumber(_lua, value.y()); lua_settable(_lua, -3); - lua_pushstring(_lua, "z"); lua_pushnumber(_lua, value.z()); lua_settable(_lua, -3); - lua_pushstring(_lua, "w"); lua_pushnumber(_lua, value.w()); lua_settable(_lua, -3); -} - -void LuaScriptEngine::pushValue(const osg::Plane& value) const -{ - lua_newtable(_lua); - lua_newtable(_lua); luaL_getmetatable(_lua, "LuaScriptEngine.Table"); lua_setmetatable(_lua, -2); - lua_pushstring(_lua, "x"); lua_pushnumber(_lua, value.asVec4().x()); lua_settable(_lua, -3); - lua_pushstring(_lua, "y"); lua_pushnumber(_lua, value.asVec4().y()); lua_settable(_lua, -3); - lua_pushstring(_lua, "z"); lua_pushnumber(_lua, value.asVec4().z()); lua_settable(_lua, -3); - lua_pushstring(_lua, "w"); lua_pushnumber(_lua, value.asVec4().w()); lua_settable(_lua, -3); -} - void LuaScriptEngine::pushValue(const osg::Matrixd& value) const { lua_newtable(_lua); diff --git a/src/osgPlugins/lua/LuaScriptEngine.h b/src/osgPlugins/lua/LuaScriptEngine.h index 37f2f7858..12754480b 100644 --- a/src/osgPlugins/lua/LuaScriptEngine.h +++ b/src/osgPlugins/lua/LuaScriptEngine.h @@ -147,15 +147,46 @@ class LuaScriptEngine : public osg::ScriptEngine void pushValue(osgDB::BaseSerializer::Type type, const void* ptr) const; - void pushValue(const osg::Vec2f& value) const; - void pushValue(const osg::Vec3f& value) const; - void pushValue(const osg::Vec4f& value) const; + template + void pushVec2(const T& value) const + { + lua_newtable(_lua); + lua_newtable(_lua); luaL_getmetatable(_lua, "LuaScriptEngine.Table"); lua_setmetatable(_lua, -2); + lua_pushstring(_lua, "x"); lua_pushnumber(_lua, value.x()); lua_settable(_lua, -3); + lua_pushstring(_lua, "y"); lua_pushnumber(_lua, value.y()); lua_settable(_lua, -3); + } - void pushValue(const osg::Vec2d& value) const; - void pushValue(const osg::Vec3d& value) const; - void pushValue(const osg::Vec4d& value) const; - void pushValue(const osg::Quat& value) const; - void pushValue(const osg::Plane& value) const; + template + void pushVec3(const T& value) const + { + lua_newtable(_lua); + lua_newtable(_lua); luaL_getmetatable(_lua, "LuaScriptEngine.Table"); lua_setmetatable(_lua, -2); + lua_pushstring(_lua, "x"); lua_pushnumber(_lua, value.x()); lua_settable(_lua, -3); + lua_pushstring(_lua, "y"); lua_pushnumber(_lua, value.y()); lua_settable(_lua, -3); + lua_pushstring(_lua, "z"); lua_pushnumber(_lua, value.z()); lua_settable(_lua, -3); + } + + template + void pushVec4(const T& value) const + { + lua_newtable(_lua); + lua_newtable(_lua); luaL_getmetatable(_lua, "LuaScriptEngine.Table"); lua_setmetatable(_lua, -2); + lua_pushstring(_lua, "x"); lua_pushnumber(_lua, value.x()); lua_settable(_lua, -3); + lua_pushstring(_lua, "y"); lua_pushnumber(_lua, value.y()); lua_settable(_lua, -3); + lua_pushstring(_lua, "z"); lua_pushnumber(_lua, value.z()); lua_settable(_lua, -3); + lua_pushstring(_lua, "w"); lua_pushnumber(_lua, value.w()); lua_settable(_lua, -3); + } + + void pushValue(const osg::Vec2f& value) const { pushVec2(value); } + void pushValue(const osg::Vec3f& value) const { pushVec3(value); } + void pushValue(const osg::Vec4f& value) const { pushVec4(value); } + + void pushValue(const osg::Vec2d& value) const { pushVec2(value); } + void pushValue(const osg::Vec3d& value) const { pushVec3(value); } + void pushValue(const osg::Vec4d& value) const { pushVec4(value); } + + void pushValue(const osg::Quat& value) const { pushVec4(value); } + void pushValue(const osg::Plane& value) const { pushVec4(value.asVec4()); } void pushValue(const osg::Matrixf& value) const; void pushValue(const osg::Matrixd& value) const;