[Orxonox-commit 4014] r8688 - in code/branches/unity_build: cmake/tools src src/external/tolua/lua src/libraries/core src/modules/notifications src/modules/pickup src/modules/questsystem src/orxonox

rgrieder at orxonox.net rgrieder at orxonox.net
Mon May 30 18:51:00 CEST 2011


Author: rgrieder
Date: 2011-05-30 18:51:00 +0200 (Mon, 30 May 2011)
New Revision: 8688

Added:
   code/branches/unity_build/src/libraries/core/ToluaInterfaceHook.lua
Removed:
   code/branches/unity_build/src/libraries/core/ToluaInterface.h
Modified:
   code/branches/unity_build/cmake/tools/GenerateToluaBindings.cmake
   code/branches/unity_build/src/CMakeLists.txt
   code/branches/unity_build/src/external/tolua/lua/basic.lua
   code/branches/unity_build/src/external/tolua/lua/package.lua
   code/branches/unity_build/src/libraries/core/LuaState.cc
   code/branches/unity_build/src/libraries/core/LuaState.h
   code/branches/unity_build/src/modules/notifications/NotificationManager.cc
   code/branches/unity_build/src/modules/pickup/PickupManager.cc
   code/branches/unity_build/src/modules/questsystem/QuestManager.cc
   code/branches/unity_build/src/orxonox/Main.cc
Log:
Removed the need to declare the tolua interface explicitly (DeclareToluaInterface).
This is now automatically done in the ToluaBindLibrary.cc files.
That also removes the need for tolua bind header files.

Modified: code/branches/unity_build/cmake/tools/GenerateToluaBindings.cmake
===================================================================
--- code/branches/unity_build/cmake/tools/GenerateToluaBindings.cmake	2011-05-30 16:45:06 UTC (rev 8687)
+++ code/branches/unity_build/cmake/tools/GenerateToluaBindings.cmake	2011-05-30 16:51:00 UTC (rev 8688)
@@ -48,12 +48,11 @@
 
   SET(_tolua_pkgfile "${CMAKE_CURRENT_BINARY_DIR}/tolua.pkg")
   SET(_tolua_cxxfile "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/ToluaBind${_tolua_package}.cc")
-  SET(_tolua_hfile   "${CMAKE_BINARY_DIR}/src/toluabind/${CMAKE_CFG_INTDIR}/ToluaBind${_tolua_package}.h")
+  #SET(_tolua_hfile   "${CMAKE_BINARY_DIR}/src/toluabind/${CMAKE_CFG_INTDIR}/ToluaBind${_tolua_package}.h")
 
   SET(${_target_source_files}
     ${${_target_source_files}}
     ${_tolua_cxxfile}
-    ${_tolua_hfile}
     PARENT_SCOPE
   )
 
@@ -75,13 +74,18 @@
     LIST(APPEND _implicit_dependencies CXX ${_tolua_inputfile})
   ENDFOREACH(_tolua_inputfile)
 
+  IF(TOLUA_PARSER_HOOK_SCRIPT)
+    # Hook scripts may contain functions that act as Tolua hooks
+    SET(_hook_script -L "${TOLUA_PARSER_HOOK_SCRIPT}")
+  ENDIF()
+
   ADD_CUSTOM_COMMAND(
-    OUTPUT ${_tolua_cxxfile} ${_tolua_hfile}
+    OUTPUT ${_tolua_cxxfile}
     COMMAND toluaapp_orxonox -n ${_tolua_package}
                              -w ${CMAKE_CURRENT_SOURCE_DIR}
                              -o ${_tolua_cxxfile}
-                             -H ${_tolua_hfile}
                              -s ${TOLUA_PARSER_SOURCE}
+                                ${_hook_script}
                                 ${_tolua_pkgfile}
     DEPENDS           ${TOLUA_PARSER_DEPENDENCIES}
     IMPLICIT_DEPENDS  ${_implicit_dependencies}

Modified: code/branches/unity_build/src/CMakeLists.txt
===================================================================
--- code/branches/unity_build/src/CMakeLists.txt	2011-05-30 16:45:06 UTC (rev 8687)
+++ code/branches/unity_build/src/CMakeLists.txt	2011-05-30 16:51:00 UTC (rev 8688)
@@ -144,13 +144,10 @@
 
 ################### Tolua Bind ##################
 
-# Create directory because the tolua application doesn't work otherwise
-IF(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/toluabind/${CMAKE_CFG_INTDIR})
-  FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/toluabind/${CMAKE_CFG_INTDIR})
-ENDIF()
+# Add hook script to the lua code that generates the bindings
+SET(TOLUA_PARSER_HOOK_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/libraries/core/ToluaInterfaceHook.lua)
+SET(TOLUA_PARSER_DEPENDENCIES ${TOLUA_PARSER_DEPENDENCIES} ${TOLUA_PARSER_HOOK_SCRIPT})
 
-INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/toluabind/${CMAKE_CFG_INTDIR})
-
 ################ Sub Directories ################
 
 ADD_SUBDIRECTORY(external)

Modified: code/branches/unity_build/src/external/tolua/lua/basic.lua
===================================================================
--- code/branches/unity_build/src/external/tolua/lua/basic.lua	2011-05-30 16:45:06 UTC (rev 8687)
+++ code/branches/unity_build/src/external/tolua/lua/basic.lua	2011-05-30 16:51:00 UTC (rev 8688)
@@ -353,4 +353,6 @@
     return nil
 end
 
-
+-- called after all the required C++ includes have been written
+function post_include_hook(package_name)
+end

Modified: code/branches/unity_build/src/external/tolua/lua/package.lua
===================================================================
--- code/branches/unity_build/src/external/tolua/lua/package.lua	2011-05-30 16:45:06 UTC (rev 8687)
+++ code/branches/unity_build/src/external/tolua/lua/package.lua	2011-05-30 16:51:00 UTC (rev 8688)
@@ -135,6 +135,8 @@
         i = i+1
     end
 
+    post_include_hook(self.name)
+
     output('\n')
     output('#ifdef ORXONOX_RELEASE\n')
     output('#  define TOLUA_RELEASE\n')

Modified: code/branches/unity_build/src/libraries/core/LuaState.cc
===================================================================
--- code/branches/unity_build/src/libraries/core/LuaState.cc	2011-05-30 16:45:06 UTC (rev 8687)
+++ code/branches/unity_build/src/libraries/core/LuaState.cc	2011-05-30 16:51:00 UTC (rev 8688)
@@ -39,19 +39,12 @@
 #include "util/Debug.h"
 #include "util/Exception.h"
 #include "Resource.h"
-#include "ToluaBindCore.h"
 #include "command/IOConsole.h"
 
 namespace orxonox
 {
-    LuaState::ToluaInterfaceMap LuaState::toluaInterfaces_s;
-    std::vector<LuaState*> LuaState::instances_s;
-
     const std::string LuaState::ERROR_HANDLER_NAME = "errorHandler";
 
-    // Do this after declaring toluaInterfaces_s and instances_s to avoid larger problems
-    DeclareToluaInterface(Core);
-
     LuaState::LuaState()
         : bIsRunning_(false)
         , includeParseFunction_(NULL)
@@ -276,9 +269,21 @@
         return IOConsole::exists();
     }
 
+    /*static*/ LuaState::ToluaInterfaceMap& LuaState::getToluaInterfaces()
+    {
+        static ToluaInterfaceMap p;
+        return p;
+    }
+
+    /*static*/ std::vector<LuaState*>& LuaState::getInstances()
+    {
+        static std::vector<LuaState*> p;
+        return p;
+    }
+
     /*static*/ bool LuaState::addToluaInterface(int (*function)(lua_State*), const std::string& name)
     {
-        for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it)
+        for (ToluaInterfaceMap::const_iterator it = getToluaInterfaces().begin(); it != getToluaInterfaces().end(); ++it)
         {
             if (it->first == name || it->second == function)
             {
@@ -286,10 +291,10 @@
                 return true;
             }
         }
-        toluaInterfaces_s[name] = function;
+        getToluaInterfaces()[name] = function;
 
         // Open interface in all LuaStates
-        for (std::vector<LuaState*>::const_iterator it = instances_s.begin(); it != instances_s.end(); ++it)
+        for (std::vector<LuaState*>::const_iterator it = getInstances().begin(); it != getInstances().end(); ++it)
             (*function)((*it)->luaState_);
 
         // Return dummy bool
@@ -298,22 +303,22 @@
 
     /*static*/ bool LuaState::removeToluaInterface(const std::string& name)
     {
-        ToluaInterfaceMap::iterator it = toluaInterfaces_s.find(name);
-        if (it == toluaInterfaces_s.end())
+        ToluaInterfaceMap::iterator it = getToluaInterfaces().find(name);
+        if (it == getToluaInterfaces().end())
         {
             COUT(2) << "Warning: Cannot remove Tolua interface '" << name << "': Not found" << std::endl;
             return true;
         }
 
         // Close interface in all LuaStates
-        for (std::vector<LuaState*>::const_iterator itState = instances_s.begin(); itState != instances_s.end(); ++itState)
+        for (std::vector<LuaState*>::const_iterator itState = getInstances().begin(); itState != getInstances().end(); ++itState)
         {
             lua_pushnil((*itState)->luaState_);
             lua_setglobal((*itState)->luaState_, it->first.c_str());
         }
 
         // Remove entry
-        toluaInterfaces_s.erase(it);
+        getToluaInterfaces().erase(it);
 
         // Return dummy bool
         return true;
@@ -321,13 +326,13 @@
 
     /*static*/ void LuaState::openToluaInterfaces(lua_State* state)
     {
-        for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it)
+        for (ToluaInterfaceMap::const_iterator it = getToluaInterfaces().begin(); it != getToluaInterfaces().end(); ++it)
             (*it->second)(state);
     }
 
     /*static*/ void LuaState::closeToluaInterfaces(lua_State* state)
     {
-        for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it)
+        for (ToluaInterfaceMap::const_iterator it = getToluaInterfaces().begin(); it != getToluaInterfaces().end(); ++it)
         {
             lua_pushnil(state);
             lua_setglobal(state, it->first.c_str());

Modified: code/branches/unity_build/src/libraries/core/LuaState.h
===================================================================
--- code/branches/unity_build/src/libraries/core/LuaState.h	2011-05-30 16:45:06 UTC (rev 8687)
+++ code/branches/unity_build/src/libraries/core/LuaState.h	2011-05-30 16:51:00 UTC (rev 8688)
@@ -48,8 +48,6 @@
 #include <vector>
 #include <boost/shared_ptr.hpp>
 
-#include "ToluaInterface.h"
-
 namespace orxonox // tolua_export
 { // tolua_export
     class LuaFunctor; // tolua_export
@@ -119,9 +117,27 @@
         std::string (*includeParseFunction_)(const std::string&);
 
         typedef std::map<std::string, int (*)(lua_State *L)> ToluaInterfaceMap;
-        static ToluaInterfaceMap toluaInterfaces_s;
-        static std::vector<LuaState*> instances_s;
+        static ToluaInterfaceMap& getToluaInterfaces();
+        static std::vector<LuaState*>& getInstances();
     }; // tolua_export
+
+
+    //! Helper class that registers/unregisters tolua bindings
+    class ToluaBindingsHelper
+    {
+    public:
+        ToluaBindingsHelper(int (*function)(lua_State*), const std::string& libraryName)
+            : libraryName_(libraryName)
+        {
+            LuaState::addToluaInterface(function, libraryName_);
+        }
+        ~ToluaBindingsHelper()
+        {
+            LuaState::removeToluaInterface(libraryName_);
+        }
+    private:
+        std::string libraryName_;
+    };
 } // tolua_export
 
 #endif /* _LuaState_H__ */

Deleted: code/branches/unity_build/src/libraries/core/ToluaInterface.h
===================================================================
--- code/branches/unity_build/src/libraries/core/ToluaInterface.h	2011-05-30 16:45:06 UTC (rev 8687)
+++ code/branches/unity_build/src/libraries/core/ToluaInterface.h	2011-05-30 16:51:00 UTC (rev 8688)
@@ -1,48 +0,0 @@
-/*
- *   ORXONOX - the hottest 3D action shooter ever to exist
- *                    > www.orxonox.net <
- *
- *
- *   License notice:
- *
- *   This program is free software; you can redistribute it and/or
- *   modify it under the terms of the GNU General Public License
- *   as published by the Free Software Foundation; either version 2
- *   of the License, or (at your option) any later version.
- *
- *   This program 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
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- *   Author:
- *      Reto Grieder
- *   Co-authors:
- *      ...
- *
- */
-
-/**
- at file
- at ingroup Lua
- at brief
-    This is required because tolua would parse this macro in LuaState.h and fail
-*/
-
-#ifndef _ToluaInterface_H__
-#define _ToluaInterface_H__
-
-#include "CorePrereqs.h"
-#include <boost/preprocessor/cat.hpp>
-#include <loki/ScopeGuard.h>
-
-// Macro for declaring a tolua interface of a library/module
-#define DeclareToluaInterface(libraryName) \
-    static bool BOOST_PP_CAT(bDummy##libraryName, __UNIQUE_NUMBER__) = orxonox::LuaState::addToluaInterface(&tolua_##libraryName##_open, #libraryName); \
-    static Loki::ScopeGuardImpl1<bool (*)(const std::string&), std::string> BOOST_PP_CAT(dummy##libraryName, __UNIQUE_NUMBER__)(&orxonox::LuaState::removeToluaInterface, #libraryName)
-
-#endif /* _ToluaInterface_H__ */

Added: code/branches/unity_build/src/libraries/core/ToluaInterfaceHook.lua
===================================================================
--- code/branches/unity_build/src/libraries/core/ToluaInterfaceHook.lua	                        (rev 0)
+++ code/branches/unity_build/src/libraries/core/ToluaInterfaceHook.lua	2011-05-30 16:51:00 UTC (rev 8688)
@@ -0,0 +1,30 @@
+--
+--             ORXONOX - the hottest 3D action shooter ever to exist
+--                             > www.orxonox.net <
+--
+--        This program is free software; you can redistribute it and/or
+--         modify it under the terms of the GNU General Public License
+--        as published by the Free Software Foundation; either version 2
+--            of the License, or (at your option) any later version.
+--
+--       This program 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
+--                 GNU General Public License for more details.
+--
+--   You should have received a copy of the GNU General Public License along
+--      with this program; if not, write to the Free Software Foundation,
+--     Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+--
+--
+--  Author:
+--    Reto Grieder
+--
+
+-- Writes the static initialiser that registers the package open() function
+function post_include_hook(package_name)
+    output('\n')
+    output('#include "core/LuaState.h"\n')
+    output('int tolua_'..package_name..'_open(lua_State* tolua_S);\n')
+    output('static orxonox::ToluaBindingsHelper initialiser(&tolua_'..package_name..'_open, "'..package_name..'");')
+end


Property changes on: code/branches/unity_build/src/libraries/core/ToluaInterfaceHook.lua
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: code/branches/unity_build/src/modules/notifications/NotificationManager.cc
===================================================================
--- code/branches/unity_build/src/modules/notifications/NotificationManager.cc	2011-05-30 16:45:06 UTC (rev 8687)
+++ code/branches/unity_build/src/modules/notifications/NotificationManager.cc	2011-05-30 16:51:00 UTC (rev 8688)
@@ -46,17 +46,12 @@
 #include "Notification.h"
 #include "NotificationQueue.h"
 
-#include "ToluaBindNotifications.h"
-
 namespace orxonox
 {
 
     const std::string NotificationManager::ALL("all");
     const std::string NotificationManager::NONE("none");
 
-    // Register tolua_open function when loading the library.
-    DeclareToluaInterface(Notifications);
-
     ManageScopedSingleton(NotificationManager, ScopeID::Root, false);
 
     // Setting console command to enter the edit mode.

Modified: code/branches/unity_build/src/modules/pickup/PickupManager.cc
===================================================================
--- code/branches/unity_build/src/modules/pickup/PickupManager.cc	2011-05-30 16:45:06 UTC (rev 8687)
+++ code/branches/unity_build/src/modules/pickup/PickupManager.cc	2011-05-30 16:51:00 UTC (rev 8688)
@@ -48,13 +48,8 @@
 #include "CollectiblePickup.h"
 #include "PickupRepresentation.h"
 
-#include "ToluaBindPickup.h"
-
 namespace orxonox
 {
-    // Register tolua_open function when loading the library
-    DeclareToluaInterface(Pickup);
-
     ManageScopedSingleton(PickupManager, ScopeID::Root, false);
 
     // Initialization of the name of the PickupInventory GUI.

Modified: code/branches/unity_build/src/modules/questsystem/QuestManager.cc
===================================================================
--- code/branches/unity_build/src/modules/questsystem/QuestManager.cc	2011-05-30 16:45:06 UTC (rev 8687)
+++ code/branches/unity_build/src/modules/questsystem/QuestManager.cc	2011-05-30 16:51:00 UTC (rev 8688)
@@ -46,13 +46,8 @@
 #include "QuestHint.h"
 #include "QuestItem.h"
 
-#include "ToluaBindQuestsystem.h"
-
 namespace orxonox
 {
-    // Register tolua_open function when loading the library
-    DeclareToluaInterface(Questsystem);
-
     ManageScopedSingleton(QuestManager, ScopeID::Root, false);
 
     /**

Modified: code/branches/unity_build/src/orxonox/Main.cc
===================================================================
--- code/branches/unity_build/src/orxonox/Main.cc	2011-05-30 16:45:06 UTC (rev 8687)
+++ code/branches/unity_build/src/orxonox/Main.cc	2011-05-30 16:51:00 UTC (rev 8688)
@@ -38,12 +38,7 @@
 #include "core/CommandLineParser.h"
 #include "core/Game.h"
 #include "core/LuaState.h"
-#include "ToluaBindOrxonox.h"
-#include "ToluaBindNetwork.h"
 
-DeclareToluaInterface(Orxonox);
-DeclareToluaInterface(Network);
-
 namespace orxonox
 {
     SetCommandLineSwitch(console).information("Start in console mode (text IO only)");




More information about the Orxonox-commit mailing list