Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 30, 2011, 6:51:00 PM (14 years ago)
Author:
rgrieder
Message:

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.

Location:
code/branches/unity_build/src/libraries/core
Files:
1 added
1 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/unity_build/src/libraries/core/LuaState.cc

    r8351 r8688  
    4040#include "util/Exception.h"
    4141#include "Resource.h"
    42 #include "ToluaBindCore.h"
    4342#include "command/IOConsole.h"
    4443
    4544namespace orxonox
    4645{
    47     LuaState::ToluaInterfaceMap LuaState::toluaInterfaces_s;
    48     std::vector<LuaState*> LuaState::instances_s;
    49 
    5046    const std::string LuaState::ERROR_HANDLER_NAME = "errorHandler";
    51 
    52     // Do this after declaring toluaInterfaces_s and instances_s to avoid larger problems
    53     DeclareToluaInterface(Core);
    5447
    5548    LuaState::LuaState()
     
    277270    }
    278271
     272    /*static*/ LuaState::ToluaInterfaceMap& LuaState::getToluaInterfaces()
     273    {
     274        static ToluaInterfaceMap p;
     275        return p;
     276    }
     277
     278    /*static*/ std::vector<LuaState*>& LuaState::getInstances()
     279    {
     280        static std::vector<LuaState*> p;
     281        return p;
     282    }
     283
    279284    /*static*/ bool LuaState::addToluaInterface(int (*function)(lua_State*), const std::string& name)
    280285    {
    281         for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it)
     286        for (ToluaInterfaceMap::const_iterator it = getToluaInterfaces().begin(); it != getToluaInterfaces().end(); ++it)
    282287        {
    283288            if (it->first == name || it->second == function)
     
    287292            }
    288293        }
    289         toluaInterfaces_s[name] = function;
     294        getToluaInterfaces()[name] = function;
    290295
    291296        // Open interface in all LuaStates
    292         for (std::vector<LuaState*>::const_iterator it = instances_s.begin(); it != instances_s.end(); ++it)
     297        for (std::vector<LuaState*>::const_iterator it = getInstances().begin(); it != getInstances().end(); ++it)
    293298            (*function)((*it)->luaState_);
    294299
     
    299304    /*static*/ bool LuaState::removeToluaInterface(const std::string& name)
    300305    {
    301         ToluaInterfaceMap::iterator it = toluaInterfaces_s.find(name);
    302         if (it == toluaInterfaces_s.end())
     306        ToluaInterfaceMap::iterator it = getToluaInterfaces().find(name);
     307        if (it == getToluaInterfaces().end())
    303308        {
    304309            COUT(2) << "Warning: Cannot remove Tolua interface '" << name << "': Not found" << std::endl;
     
    307312
    308313        // Close interface in all LuaStates
    309         for (std::vector<LuaState*>::const_iterator itState = instances_s.begin(); itState != instances_s.end(); ++itState)
     314        for (std::vector<LuaState*>::const_iterator itState = getInstances().begin(); itState != getInstances().end(); ++itState)
    310315        {
    311316            lua_pushnil((*itState)->luaState_);
     
    314319
    315320        // Remove entry
    316         toluaInterfaces_s.erase(it);
     321        getToluaInterfaces().erase(it);
    317322
    318323        // Return dummy bool
     
    322327    /*static*/ void LuaState::openToluaInterfaces(lua_State* state)
    323328    {
    324         for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it)
     329        for (ToluaInterfaceMap::const_iterator it = getToluaInterfaces().begin(); it != getToluaInterfaces().end(); ++it)
    325330            (*it->second)(state);
    326331    }
     
    328333    /*static*/ void LuaState::closeToluaInterfaces(lua_State* state)
    329334    {
    330         for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it)
     335        for (ToluaInterfaceMap::const_iterator it = getToluaInterfaces().begin(); it != getToluaInterfaces().end(); ++it)
    331336        {
    332337            lua_pushnil(state);
  • code/branches/unity_build/src/libraries/core/LuaState.h

    r8672 r8688  
    4848#include <vector>
    4949#include <boost/shared_ptr.hpp>
    50 
    51 #include "ToluaInterface.h"
    5250
    5351namespace orxonox // tolua_export
     
    120118
    121119        typedef std::map<std::string, int (*)(lua_State *L)> ToluaInterfaceMap;
    122         static ToluaInterfaceMap toluaInterfaces_s;
    123         static std::vector<LuaState*> instances_s;
     120        static ToluaInterfaceMap& getToluaInterfaces();
     121        static std::vector<LuaState*>& getInstances();
    124122    }; // tolua_export
     123
     124
     125    //! Helper class that registers/unregisters tolua bindings
     126    class ToluaBindingsHelper
     127    {
     128    public:
     129        ToluaBindingsHelper(int (*function)(lua_State*), const std::string& libraryName)
     130            : libraryName_(libraryName)
     131        {
     132            LuaState::addToluaInterface(function, libraryName_);
     133        }
     134        ~ToluaBindingsHelper()
     135        {
     136            LuaState::removeToluaInterface(libraryName_);
     137        }
     138    private:
     139        std::string libraryName_;
     140    };
    125141} // tolua_export
    126142
Note: See TracChangeset for help on using the changeset viewer.