Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <memory>
- #include <sstream>
- #include <string>
- #include <luabind/luabind.hpp>
- namespace luabind
- {
- template <typename T>
- T* get_pointer(std::shared_ptr<T> const& p)
- {
- return p.get();
- }
- }
- class BaseWidget
- {
- public:
- static void Bind2Lua(lua_State* l)
- {
- luabind::module(l)
- [
- luabind::class_<BaseWidget, std::shared_ptr<BaseWidget>> ("BaseWidget")
- .def(luabind::constructor<>())
- ];
- }
- virtual ~BaseWidget()
- {
- }
- };
- class Button : public BaseWidget
- {
- public:
- static void Bind2Lua(lua_State* l)
- {
- luabind::module(l)
- [
- #if 0
- luabind::class_<Button, BaseWidget, std::shared_ptr<BaseWidget>>("Button")
- #else
- luabind::class_<Button, BaseWidget, std::shared_ptr<Button>> ("Button")
- #endif
- .def(luabind::constructor<>())
- .def("Click", &Button::Click)
- ];
- }
- void Click()
- {
- std::cout << "Button::Click" << std::endl;
- }
- };
- class Action
- {
- public:
- void DoClick(const std::shared_ptr<Button>& b)
- {
- // perform click action
- b->Click();
- }
- static void Bind2Lua(lua_State* l)
- {
- luabind::module(l)
- [
- luabind::class_<Action> ("Action")
- .def(luabind::constructor<>())
- .def("DoClick", &Action::DoClick)
- ];
- }
- };
- int GetCallStackSize(lua_State* l)
- {
- int level = 0;
- lua_Debug ar;
- while (true)
- {
- if (lua_getstack(l, level, &ar) == 0)
- return level;
- level += 1;
- }
- }
- std::string GetCallStack(lua_State* l)
- {
- std::stringstream sstr;
- lua_Debug ar;
- int level = 0;
- std::string tab;
- int size = GetCallStackSize(l);
- int i;
- //Display call stack. I'm going to make the main function '0' and reverse the level numbers
- //because that makes more sense to me, but remember 0 = the current level.
- for (level = 0; level < size; ++level)
- {
- lua_getstack(l, size - 1 - level, &ar);
- sstr << tab << "[Level " << level << "]" << std::endl <<
- tab << "{" << std::endl <<
- lua_getinfo(l, "nSluf", &ar) << std::endl <<
- tab << " source: " << ar.source << std::endl <<
- tab << " short_src: " << ar.short_src << std::endl <<
- tab << " linedefined: " << ar.linedefined << std::endl <<
- tab << " lastlinedefined: " << ar.lastlinedefined << std::endl <<
- tab << " what: " << ar.what << std::endl <<
- tab << " currentline: " << ar.currentline << std::endl;
- if (ar.name != NULL)
- sstr << tab << " name: " << ar.name << std::endl;;
- if (ar.namewhat != NULL)
- sstr << tab << " namewhat: " << ar.namewhat << std::endl;;
- sstr << tab << " nups: " << ar.nups << std::endl;;
- tab += " ";
- }
- //Add closing brackets
- for (level=0; level<size; level++)
- {
- tab = "";
- for (i = 0; i < size - level - 1; ++i)
- tab += " ";
- sstr << tab << "}" << std::endl;
- }
- sstr << std::endl;
- lua_pop(l,1);
- return sstr.str();
- }
- int ErrorHandler(lua_State* L)
- {
- const char* str = lua_tostring(L,-1);
- std::stringstream ss;
- ss << "lua: " << str << std::endl;
- ss << GetCallStack(L).c_str();
- std::cerr << ss.str() << std::endl;
- return 0;
- }
- template <class T>
- void PrintClassID(const char *name)
- {
- std::cout << name << "::" << luabind::detail::static_class_id<T>(nullptr) << std::endl;
- }
- #define STR(s) DOSTR(s)
- #define DOSTR(s) #s
- #define PRINT_CLASS_ID(x) PrintClassID<x>(STR(x))
- void Foo()
- {
- lua_State* l = lua_open();
- luabind::open(l);
- BaseWidget::Bind2Lua(l);
- Button::Bind2Lua(l);
- Action::Bind2Lua(l);
- PRINT_CLASS_ID(BaseWidget);
- PRINT_CLASS_ID(std::shared_ptr<BaseWidget>);
- PRINT_CLASS_ID(Button);
- PRINT_CLASS_ID(std::shared_ptr<Button>);
- PRINT_CLASS_ID(Action);
- int ret = 0;
- if (ret = luaL_dostring(l ,"b = Button() \
- a = Action() \
- a:DoClick(b)"))
- ErrorHandler(l);
- std::shared_ptr<Button> b = std::make_shared<Button>();
- {
- Action a;
- a.DoClick(b);
- }
- lua_close(l);
- }
- int main(int argc, char **argv)
- {
- Foo();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement