Advertisement
LeSnip3R

luabind shared_ptr hierarchies

Jun 29th, 2012
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <memory>
  3. #include <sstream>
  4. #include <string>
  5. #include <luabind/luabind.hpp>
  6.  
  7. namespace luabind
  8. {
  9.     template <typename T>
  10.     T* get_pointer(std::shared_ptr<T> const& p)
  11.     {
  12.         return p.get();
  13.     }
  14. }
  15.  
  16.     class BaseWidget
  17.     {
  18.     public:
  19.         static void Bind2Lua(lua_State* l)
  20.         {
  21.             luabind::module(l)
  22.             [
  23.                 luabind::class_<BaseWidget, std::shared_ptr<BaseWidget>> ("BaseWidget")
  24.                 .def(luabind::constructor<>())
  25.             ];
  26.         }
  27.  
  28.         virtual ~BaseWidget()
  29.         {
  30.  
  31.         }
  32.     };
  33.  
  34.     class Button : public BaseWidget
  35.     {
  36.     public:
  37.         static void Bind2Lua(lua_State* l)
  38.         {
  39.             luabind::module(l)
  40.             [
  41. #if 0
  42.                 luabind::class_<Button, BaseWidget, std::shared_ptr<BaseWidget>>("Button")
  43. #else
  44.                 luabind::class_<Button, BaseWidget, std::shared_ptr<Button>> ("Button")
  45. #endif
  46.                 .def(luabind::constructor<>())
  47.                 .def("Click", &Button::Click)
  48.             ];
  49.         }
  50.  
  51.         void Click()
  52.         {
  53.             std::cout << "Button::Click" << std::endl;
  54.         }
  55.     };
  56.  
  57.     class Action
  58.     {
  59.     public:
  60.         void DoClick(const std::shared_ptr<Button>& b)
  61.         {
  62.             // perform click action
  63.             b->Click();
  64.         }
  65.  
  66.         static void Bind2Lua(lua_State* l)
  67.         {
  68.             luabind::module(l)
  69.             [
  70.                 luabind::class_<Action> ("Action")
  71.                 .def(luabind::constructor<>())
  72.                 .def("DoClick", &Action::DoClick)
  73.             ];
  74.         }
  75.     };
  76.  
  77. int GetCallStackSize(lua_State* l)
  78. {
  79.     int level = 0;
  80.     lua_Debug ar;
  81.  
  82.     while (true)
  83.     {
  84.         if (lua_getstack(l, level, &ar) == 0)
  85.             return level;
  86.         level += 1;
  87.     }
  88. }
  89.  
  90. std::string GetCallStack(lua_State* l)
  91. {
  92.     std::stringstream sstr;
  93.     lua_Debug ar;
  94.     int level = 0;
  95.     std::string tab;
  96.     int size = GetCallStackSize(l);
  97.     int i;
  98.  
  99.     //Display call stack.  I'm going to make the main function '0' and reverse the level numbers
  100.     //because that makes more sense to me, but remember 0 = the current level.
  101.     for (level = 0; level < size; ++level)
  102.     {
  103.         lua_getstack(l, size - 1 - level, &ar);
  104.         sstr << tab << "[Level " << level << "]" << std::endl <<
  105.             tab << "{" << std::endl <<
  106.             lua_getinfo(l, "nSluf", &ar) << std::endl <<
  107.  
  108.             tab << "        source: " << ar.source << std::endl <<
  109.             tab << "        short_src: " << ar.short_src << std::endl <<    
  110.             tab << "        linedefined: " << ar.linedefined << std::endl <<
  111.             tab << "        lastlinedefined: " << ar.lastlinedefined << std::endl <<
  112.             tab << "        what: " << ar.what << std::endl <<
  113.             tab << "        currentline: " << ar.currentline << std::endl;
  114.         if (ar.name != NULL)
  115.             sstr << tab << "  name: " << ar.name << std::endl;;
  116.         if (ar.namewhat != NULL)
  117.             sstr << tab << "      namewhat: " << ar.namewhat << std::endl;;
  118.  
  119.         sstr << tab << "     nups: " << ar.nups << std::endl;;
  120.  
  121.         tab += "        ";
  122.     }
  123.  
  124.     //Add closing brackets
  125.     for (level=0; level<size; level++)
  126.     {
  127.         tab = "";
  128.         for (i = 0; i < size - level - 1; ++i)
  129.             tab += "   ";
  130.         sstr << tab << "}" << std::endl;
  131.     }
  132.     sstr << std::endl;
  133.  
  134.     lua_pop(l,1);
  135.     return sstr.str();
  136. }
  137.  
  138. int ErrorHandler(lua_State* L)
  139. {
  140.     const char* str = lua_tostring(L,-1);
  141.     std::stringstream ss;
  142.  
  143.     ss << "lua: " << str << std::endl;
  144.     ss << GetCallStack(L).c_str();
  145.     std::cerr << ss.str() << std::endl;
  146.  
  147.     return 0;
  148. }
  149.  
  150. template <class T>
  151. void PrintClassID(const char *name)
  152. {
  153.     std::cout << name << "::" << luabind::detail::static_class_id<T>(nullptr) << std::endl;
  154. }
  155.  
  156. #define STR(s) DOSTR(s)
  157. #define DOSTR(s) #s
  158. #define PRINT_CLASS_ID(x) PrintClassID<x>(STR(x))
  159.  
  160. void Foo()
  161. {
  162.     lua_State* l = lua_open();
  163.  
  164.     luabind::open(l);
  165.  
  166.     BaseWidget::Bind2Lua(l);
  167.     Button::Bind2Lua(l);
  168.     Action::Bind2Lua(l);
  169.  
  170.     PRINT_CLASS_ID(BaseWidget);
  171.     PRINT_CLASS_ID(std::shared_ptr<BaseWidget>);
  172.     PRINT_CLASS_ID(Button);
  173.     PRINT_CLASS_ID(std::shared_ptr<Button>);
  174.     PRINT_CLASS_ID(Action);
  175.  
  176.     int ret = 0;
  177.     if (ret = luaL_dostring(l ,"b = Button() \
  178.                                a = Action() \
  179.                                a:DoClick(b)"))
  180.         ErrorHandler(l);
  181.  
  182.     std::shared_ptr<Button> b = std::make_shared<Button>();
  183.     {
  184.         Action a;
  185.         a.DoClick(b);
  186.     }
  187.  
  188.     lua_close(l);
  189. }
  190.  
  191. int main(int argc, char **argv)
  192. {
  193.     Foo();
  194.     return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement