From 8ac875337d87c4de9cd35059ba5cc3add3b4b619 Mon Sep 17 00:00:00 2001 From: Daniel Wallin Date: Fri, 29 Aug 2003 10:55:40 +0000 Subject: [PATCH] *** empty log message *** --- config | 2 + luabind/class.hpp | 48 ++++++++++++++++++++ luabind/config.hpp | 2 + luabind/dependency_policy.hpp | 23 ++++++++++ luabind/detail/object_rep.hpp | 16 ++++++- luabind/detail/policy.hpp | 2 +- src/makefile | 5 ++- test/main.cpp | 3 ++ test/makefile | 4 +- test/test_implicit_cast.cpp | 15 ++++++- test/test_lua_classes.cpp | 6 ++- test/test_object.cpp | 101 +++++++++++++++++++++++++++++++++++++++++- 12 files changed, 218 insertions(+), 9 deletions(-) diff --git a/config b/config index c6ca2a2..f708e41 100644 --- a/config +++ b/config @@ -18,6 +18,8 @@ #lua lib path is used for the test program #LUA_LIB_PATH = -L/scratch/lua-5.0/lib +BOOST_PATH = -I/home/Dew/boost + LUA_LIB_PATH = -L/usr/local/lib CXX = g++ diff --git a/luabind/class.hpp b/luabind/class.hpp index 18c9bd4..1f9d9dc 100644 --- a/luabind/class.hpp +++ b/luabind/class.hpp @@ -126,6 +126,8 @@ #include #include +//#include + namespace luabind { namespace detail @@ -898,6 +900,9 @@ namespace luabind std::swap(crep->m_static_constants, m_static_constants); + // here! +// crep->m_methods = m_methods; + for (std::vector::iterator i = m_bases.begin(); i != m_bases.end(); ++i) @@ -913,6 +918,28 @@ namespace luabind crep->add_base_class(base); + typedef std::map methods_t; + + for (methods_t::const_iterator i + = bcrep->m_methods.begin() + ; i != bcrep->m_methods.end() + ; ++i) + { + detail::method_rep& m = m_methods[i->first]; + + typedef std::vector overloads_t; + + for (overloads_t::const_iterator j + = i->second.overloads().begin() + ; j != i->second.overloads().end() + ; ++j) + { + detail::overload_rep o = *j; + o.add_offset(base.pointer_offset); + m.add_overload(o); + } + } + // copy base class table detail::getref(L, crep->table_ref()); detail::getref(L, bcrep->table_ref()); @@ -940,6 +967,12 @@ namespace luabind for (std::map::iterator i = m_methods.begin(); i != m_methods.end(); ++i) { + detail::getref(L, crep->table_ref()); + lua_pushstring(L, i->first); + lua_pushnil(L); + lua_settable(L, -3); + lua_pop(L, 1); + crep->add_method(L, i->first, crep->m_methods[i->first]); i->second.crep = crep; } @@ -1051,13 +1084,26 @@ namespace luabind > , detail::null_type >::type HeldType; +/* + template + void register_downcast(boost::mpl::true_, detail::type* = 0) + { boost::langbinding::register_conversion(true); } + template + void register_downcast(boost::mpl::false_, detail::type* = 0) + {} +*/ // this function generates conversion information // in the given class_rep structure. It will be able // to implicitly cast to the given template type template void gen_base_info(detail::type) { +// boost::langbinding::register_dynamic_id(); +// boost::langbinding::register_conversion(false); + +// register_downcast(boost::mpl::bool_::value>()); + // fist, make sure the given base class is registered. // if it's not registered we can't push it's lua table onto // the stack because it doesn't have a table @@ -1373,6 +1419,8 @@ namespace luabind HeldType* crap = 0; +// boost::langbinding::register_dynamic_id(); + class_base::init(LUABIND_TYPEID(T) , detail::internal_holder_type::apply() , detail::internal_holder_extractor::apply(detail::type()) diff --git a/luabind/config.hpp b/luabind/config.hpp index f6bed6f..f2fbcab 100644 --- a/luabind/config.hpp +++ b/luabind/config.hpp @@ -118,6 +118,8 @@ namespace std #define LUABIND_INVALID_TYPE_INFO &typeid(detail::null_type) #endif +#define BOOST_LANGBINDING_TYPE_INFO LUABIND_TYPE_INFO + // LUABIND_NO_EXCEPTIONS // this define will disable all usage of try, catch and throw in // luabind. This will in many cases disable runtime-errors, such diff --git a/luabind/dependency_policy.hpp b/luabind/dependency_policy.hpp index 25ba523..00aba1e 100644 --- a/luabind/dependency_policy.hpp +++ b/luabind/dependency_policy.hpp @@ -48,6 +48,8 @@ namespace luabind { namespace detail }} +#if defined (BOOST_MSVC) && (BOOST_MSVC <= 1200) + namespace luabind { // most absurd workaround of all time? @@ -79,5 +81,26 @@ namespace luabind { return detail::policy_cons::value>, detail::null_type>(); } } +#else + +namespace luabind +{ + template + detail::policy_cons, detail::null_type> + dependency(boost::arg, boost::arg) + { + return detail::policy_cons, detail::null_type>(); + } + + template + detail::policy_cons, detail::null_type> + return_internal_reference(boost::arg) + { + return detail::policy_cons, detail::null_type>(); + } +} + +#endif + #endif // LUABIND_DEPENDENCY_POLICY_HPP_INCLUDED diff --git a/luabind/detail/object_rep.hpp b/luabind/detail/object_rep.hpp index fa3c56b..88ddecb 100644 --- a/luabind/detail/object_rep.hpp +++ b/luabind/detail/object_rep.hpp @@ -26,6 +26,7 @@ #include #include +#include namespace luabind { namespace detail { @@ -49,7 +50,8 @@ namespace luabind { namespace detail m_lua_table_ref(LUA_NOREF), m_destructor(dest), m_dependency_cnt(1), - m_dependency_ref(LUA_NOREF) + m_dependency_ref(LUA_NOREF), + m_instance(0) { // if the object is owned by lua, a valid destructor must be given assert((((m_flags & owner) && dest) || !(m_flags & owner)) && "internal error, please report"); @@ -67,11 +69,14 @@ namespace luabind { namespace detail , m_destructor(0) , m_dependency_cnt(1) , m_dependency_ref(LUA_NOREF) + , m_instance(0) { } ~object_rep() { + delete m_instance; + if (m_flags & owner && m_destructor) m_destructor(m_object); } @@ -123,6 +128,12 @@ namespace luabind { namespace detail if (m_lua_table_ref != LUA_NOREF) detail::unref(L, m_lua_table_ref); // correct? } + instance_holder* instance() const + { return m_instance; } + + void set_instance(instance_holder* x) + { m_instance = x; } + static int garbage_collector(lua_State* L) { object_rep* obj = static_cast(lua_touserdata(L, -1)); @@ -144,6 +155,9 @@ namespace luabind { namespace detail void(*m_destructor)(void*); // this could be in class_rep? it can't: see intrusive_ptr int m_dependency_cnt; // counts dependencies int m_dependency_ref; // reference to lua table holding dependency references + + // ======== the new way, separate object_rep from the holder + instance_holder* m_instance; }; template diff --git a/luabind/detail/policy.hpp b/luabind/detail/policy.hpp index 7dbd9a1..d1269a6 100644 --- a/luabind/detail/policy.hpp +++ b/luabind/detail/policy.hpp @@ -45,7 +45,7 @@ #include #include #include -#include +//#include namespace luabind { diff --git a/src/makefile b/src/makefile index 4e0167a..d1c1eeb 100755 --- a/src/makefile +++ b/src/makefile @@ -9,7 +9,8 @@ SOURCES = \ create_class.cpp \ stack_content_by_name.cpp \ object.cpp \ - class_info.cpp + class_info.cpp \ + find_instance.cpp OBJECTS = $(SOURCES:.cpp=.o) @@ -26,7 +27,7 @@ $(TARGET): $(OBJECTS) ranlib $@ %.o:%.cpp - $(CXX) -O3 -Wall -c $? -o $@ $(LUA_PATH) -I- -I.. $(BOOST_PATH) $(CONFIG) + $(CXX) -g -Wall -c $? -o $@ $(LUA_PATH) -I- -I.. $(BOOST_PATH) $(CONFIG) clean: rm -f $(OBJECTS) $(TARGET) diff --git a/test/main.cpp b/test/main.cpp index 520aeaf..5823579 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -153,6 +153,8 @@ namespace } #endif + +/* int main() { bool passed = true; @@ -184,3 +186,4 @@ int main() return 0; } +*/ diff --git a/test/makefile b/test/makefile index 03ca7e8..cfd95bb 100644 --- a/test/makefile +++ b/test/makefile @@ -29,10 +29,10 @@ OBJECTS = $(SOURCES:.cpp=.o) all: $(TARGET) $(TARGET): $(OBJECTS) - $(CXX) -O3 $(OBJECTS) $(LUA_LIB_PATH) -L../lib -lluabind -llua -llualib -o $@ + $(CXX) -g $(OBJECTS) $(LUA_LIB_PATH) -L../lib -lluabind -llangbinding-static -llangbinding -llua -llualib -o $@ %.o:%.cpp - $(CXX) -Wall -ftemplate-depth-50 -O3 $(CONFIG) $(LUA_PATH) -I. -I- -I../ $(BOOST_PATH) -c $? -o $@ + $(CXX) -Wall -ftemplate-depth-50 -g $(CONFIG) $(LUA_PATH) -I. -I- -I../ $(BOOST_PATH) -c $? -o $@ relink: rm -f $(TARGET) diff --git a/test/test_implicit_cast.cpp b/test/test_implicit_cast.cpp index 42e474a..b3085dc 100644 --- a/test/test_implicit_cast.cpp +++ b/test/test_implicit_cast.cpp @@ -4,7 +4,7 @@ namespace { LUABIND_ANONYMOUS_FIX int feedback = 0; - struct A {}; + struct A { virtual ~A() {} }; struct B: public A {}; struct test_implicit @@ -14,7 +14,18 @@ namespace }; } // anonymous namespace +/* +#include +void test_instance_holder(lua_State* L) +{ + using namespace luabind; + +// pointer_holder holder(new B); + +// void* p = holder.holds(LUABIND_TYPEID(B)); +} +*/ bool test_implicit_cast() { using namespace luabind; @@ -42,6 +53,8 @@ bool test_implicit_cast() .def("f", (f2) &test_implicit::f) ]; +// test_instance_holder(L); + if (dostring(L, "a = A()")) return false; if (dostring(L, "b = B()")) return false; if (dostring(L, "t = test()")) return false; diff --git a/test/test_lua_classes.cpp b/test/test_lua_classes.cpp index cedb487..8fd709d 100644 --- a/test/test_lua_classes.cpp +++ b/test/test_lua_classes.cpp @@ -1,6 +1,7 @@ #include #include "test.h" +#include #include namespace @@ -65,9 +66,10 @@ namespace void take_abstract(abstract_base*) {} + void take_void_ptr(void*) {} + } // anonymous namespace - bool test_lua_classes() { using namespace luabind; @@ -87,6 +89,7 @@ bool test_lua_classes() class_("abstract_base") .def("f", &abstract_base::f), def("take_abstract", &take_abstract), + def("take_void_ptr", &take_void_ptr, void_ptr(_1)), class_("base") .def(constructor<>()) @@ -132,6 +135,7 @@ bool test_lua_classes() if (top != lua_gettop(L)) return false; dostring(L, "a = derived()"); + dostring(L, "take_void_ptr(a)"); dostring(L, "info = class_info(a)"); dostring(L, "print(info.name)"); diff --git a/test/test_object.cpp b/test/test_object.cpp index 237b288..d50a7bc 100644 --- a/test/test_object.cpp +++ b/test/test_object.cpp @@ -1,5 +1,11 @@ #include "test.h" #include +#include + +extern "C" +{ + #include "lauxlib.h" +} namespace { @@ -95,13 +101,86 @@ namespace feedback1 = 27; } +/*******************************************/ +// Evan's diabolical test case + class Message {}; + + class IMessageClient + { + public: + virtual bool OnMessage( Message* pMessage ) = 0; + }; + + class IState + { + public: + virtual bool OnMessage( Message* pMessage ) = 0; + virtual const char * getName() = 0; + }; + class StateImpl : public IState + { + public: + virtual bool OnMessage( Message* pMessage ) { return false; } + virtual const char * getName() { return NULL; } + }; + class FSM + : public IMessageClient, public StateImpl + { + protected: + IState* m_pCurrentState; + IState* m_pNewState; + IState* m_pGlobalState; + IState* m_pPreviousState; + bool m_bForceStateTransition; + public: + FSM() {} + virtual ~FSM() {} + IState * getCurrentState() { return m_pCurrentState; } + IState * getNewState() { return m_pNewState; } + IState * getPreviousState() { return m_pPreviousState; } + IState * getGlobalState() { return m_pGlobalState; } + + virtual const char * getName() { return NULL; } + virtual void Update( float fDeltaTime ) {} + + virtual bool OnMessage( Message* pMessage ) { return true; } + + virtual void addState( IState * state ) {} + virtual void addState( IState * state, const std::string & stateName ) {} + }; + + + class ActorBrain : public IMessageClient + { + public: + ActorBrain() {} + virtual ~ActorBrain() {} + virtual void setActorParent(void* pParent) { m_p= pParent; } + void* getParentActor() { return m_p; } + virtual void Update(float fDeltaTime) = 0; + protected: + void* m_p; + }; + class AgentBrain + : public ActorBrain, public FSM + { + public: + AgentBrain() {} + virtual bool OnMessage(Message * pMessage ) { return true; } + virtual void Update( float fDeltaTime ) {} + }; + } // anonymous namespace + + bool test_object() { using namespace luabind; lua_State* L = lua_open(); + luaopen_base( L ); + int top = lua_gettop(L); { open(L); @@ -118,9 +197,29 @@ bool test_object() .def("foo",&test_param::foo ), class_("test_param2") .def(constructor<>()) - .def("foo",(void(test_param2::*)(bool))&test_param2::foo ) + .def("foo",(void(test_param2::*)(bool))&test_param2::foo ), + + // Evan's diabolical test + class_( "IMessageClient" ) + .def( "onMessage", &IMessageClient::OnMessage ) + , + class_( "State" ) + , + + class_( "FSM" ) + .def( constructor<>() ) + .def( "addState", (void(FSM::*)(IState*))&FSM::addState )//, adopt(_1) ) + .def( "addState", (void(FSM::*)(IState*,const std::string &))&FSM::addState )//, adopt(_1) ) + , + class_( "ActorBrain" ) + , + class_ >( "AgentBrain" ) + .def( constructor<>() ) ]; + // Evan's diabolical test + int nResult = lua_dofile( L, "ew_test.lua" ); + dostring(L, "t = test_param();"); dostring(L, "t:foo();"); if (feedback1 != 1) return false; -- 2.11.4.GIT