Differences

This shows you the differences between two versions of the page.

Link to this comparison view

lua_wrapping [2014/01/14 11:46] (current)
hadrien created
Line 1: Line 1:
 +HBot uses [[https://​github.com/​Tomasu/​LuaGlue|LuaGlue]],​ a C++11 template-based wrapper. The developer(s) is adding features and fixing bugs very fast, but I've done some modifications (LuaGlue manages shared pointers very well, but you cannot construct directly a shared pointer) which I might contribute back at some point. Code is based on commit 00e0ab497cdeaf.
  
 +Note to self, here is some code on how to use it.
 +
 +<​code>​
 +#include <​iostream>​
 +#include "​LuaGlue/​LuaGlue.h"​
 +
 +template<​typename T>
 +struct stack<​std::​vector<​T>>​ {
 +    static std::​vector<​T>​ get(LuaGlueBase *g, lua_State *s, int idx)
 +    {
 +        std::​vector<​T>​ vect;
 +        lua_pushnil(g->​state()); ​   // first key: push 0 on the stack 
 +        while (lua_next(g->​state(),​ idx) != 0) {
 +            vect.push_back(stack<​T>::​get(g,​ s, -1));
 +            lua_pop(g->​state(),​1);​
 +        }
 +        return vect;
 +    }
 +
 +    static void put(LuaGlueBase *g, lua_State *s, std::​vector<​T>​ v)
 +    {
 +        put(g,​s,​v[0]);​
 +    }
 +};
 +
 +class RobotTracker {
 +public:
 +    RobotTracker(std::​string s) { std::cout << "In RobotTracker constructor:​ " << s << std::endl; }
 +    ~RobotTracker() { std::cout << "In RobotTracker destructor"​ << std::endl; }
 +    void helloWorld(std::​vector<​int>​ x) {
 +        for (auto i: x) {
 +            std::cout << "Hello world! " << i << std::endl;
 +        }
 +     }
 +};
 +
 +class MvtPlan {
 +public:
 +    MvtPlan(std::​shared_ptr<​RobotTracker>​ tracker): _tracker(tracker),​ _count(1000)
 +    {
 +    }
 +    void bonjour(int x) {
 +        _tracker->​helloWorld({_count++});​
 +    }
 +private:
 +    std::​shared_ptr<​RobotTracker>​ _tracker;
 +    int _count;
 +};
 +
 +
 +
 +int main(int, char**)
 +{
 +    {
 +        LuaGlue state;
 +
 +        auto & robotClass = state.Class<​RobotTracker>​("​Tracker"​).
 +            ctor<​std::​string>​("​new"​).
 +            method("​hello",​ &​RobotTracker::​helloWorld);​
 +
 +        state.Class<​MvtPlan>​("​MvtPlan"​).
 +            ctor<​std::​shared_ptr<​RobotTracker>>​("​new"​).
 +            method("​bonjour",​ &​MvtPlan::​bonjour);​
 +
 +        state.open().glue();​
 +
 +        std::​shared_ptr<​RobotTracker>​ plop(new RobotTracker("​CppMao"​));​
 +        lua_rawgeti(state.state(),​ LUA_REGISTRYINDEX,​ LUA_RIDX_GLOBALS);​
 +        int globId = lua_gettop(state.state());​
 +        robotClass.pushInstance(state.state(),​ plop);
 +        lua_setfield(state.state(),​ globId, "​cppX"​);​
 +
 +        printf("​running lua script!\n"​);​
 +        if(!state.doFile("​plop.lua"​))
 +        {
 +            printf("​failed to dofile: shared_ptr.lua\n"​);​
 +            printf("​err:​ %s\n", state.lastError().c_str());​
 +        }
 +
 +        printf("​done!\n"​);​
 +
 +        lua_gc(state.state(),​ LUA_GCCOLLECT,​ 0);
 +
 +        printf("​GC done!\n"​);​
 +    }
 +    printf("​State destroyed\n"​);​
 +    return 0;
 +}
 +</​code>​
 
lua_wrapping.txt ยท Last modified: 2014/01/14 11:46 by hadrien
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS La rache Driven by DokuWiki