From 3501c7b97580b87aa8ff399a93531fc37673fd09 Mon Sep 17 00:00:00 2001 From: Daniel Wallin Date: Thu, 15 May 2003 16:43:22 +0000 Subject: [PATCH] more holder type features --- luabind/detail/policy.hpp | 2 +- src/class_rep.cpp | 31 +++++++++++++++++++++---- test/test_held_type.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 86 insertions(+), 6 deletions(-) diff --git a/luabind/detail/policy.hpp b/luabind/detail/policy.hpp index 3028206..4a034a4 100644 --- a/luabind/detail/policy.hpp +++ b/luabind/detail/policy.hpp @@ -551,7 +551,7 @@ namespace luabind { namespace detail struct value_converter { template - typename make_const_reference::type apply(lua_State* L, by_value, int index) + /*typename make_const_reference::type*/T apply(lua_State* L, by_value, int index) { // preconditions: // lua_isuserdata(L, index); diff --git a/src/class_rep.cpp b/src/class_rep.cpp index 3269796..267232e 100755 --- a/src/class_rep.cpp +++ b/src/class_rep.cpp @@ -144,9 +144,22 @@ int luabind::detail::class_rep::gettable(lua_State* L) return 1; } + object_rep* obj = static_cast(lua_touserdata(L, 1)); + // we have to ignore the first argument since this may point to // a method that is not present in this class (but in a subclass) const char* key = lua_tostring(L, 2); + + if (key && !strcmp(key, "__ok")) + { + class_rep* crep = obj->crep(); + + void* p = crep->extract_ptr()(obj->ptr()); + + lua_pushboolean(L, p != 0); + return 1; + } + std::map::iterator i = m_methods.find(key); if (i != m_methods.end()) @@ -1183,6 +1196,20 @@ void luabind::detail::class_rep::add_static_constant(const char* name, int val) #endif + // we have to ignore the first argument since this may point to + // a method that is not present in this class (but in a subclass) + const char* key = lua_tostring(L, 2); + + if (key && !strcmp(key, "__ok")) + { + class_rep* crep = obj->crep(); + + void* p = crep->extract_ptr()(obj->ptr()); + + lua_pushboolean(L, p != 0); + return 1; + } + detail::getref(L, obj->lua_table_ref()); lua_pushvalue(L, 2); lua_gettable(L, -2); @@ -1205,10 +1232,6 @@ void luabind::detail::class_rep::add_static_constant(const char* name, int val) return 1; } - // we have to ignore the first argument since this may point to - // a method that is not present in this class (but in a subclass) - const char* key = lua_tostring(L, 2); - std::map::iterator i = crep->m_methods.find(key); if (i != crep->m_methods.end()) { diff --git a/test/test_held_type.cpp b/test/test_held_type.cpp index 9b18571..c088cfe 100644 --- a/test/test_held_type.cpp +++ b/test/test_held_type.cpp @@ -1,5 +1,6 @@ #include - +#include +#include namespace luabind { @@ -8,6 +9,12 @@ namespace luabind template T* get_pointer(boost::shared_ptr& p) { return p.get(); } } + + namespace detail + { + template + T* get_pointer(const std::auto_ptr& p) { return p.get(); } + } } #include "test.h" @@ -84,6 +91,14 @@ namespace if ((*t)->n == 4) feedback = 14; } + void tester8(boost::weak_ptr t) + { + if (!t.expired()) feedback = 19; + } + + struct ownership {}; + + void test_auto_ptr(std::auto_ptr) {} } // anonymous namespace @@ -97,6 +112,31 @@ namespace luabind } } +#define LUABIND_IMPLICIT_CONVERSION(from, to, to_stripped) \ + namespace luabind { namespace converters { \ + yes_t is_user_defined(to); \ + int match_lua_to_cpp(lua_State* L, to, int index) \ + { \ + luabind::detail::default_policy \ + ::generate_converter::type c; \ + return c.match(L, LUABIND_DECORATE_TYPE(from), index); \ + } \ + \ + to_stripped convert_lua_to_cpp(lua_State* L, to, int index) \ + { \ + luabind::detail::default_policy \ + ::generate_converter::type c; \ + return static_cast(c.apply(L, \ + LUABIND_DECORATE_TYPE(from), index)); \ + } \ + }} + +typedef boost::shared_ptr t1; +typedef luabind::converters::by_value > t2; +typedef boost::weak_ptr t3; + +LUABIND_IMPLICIT_CONVERSION(t1,t2,t3) + bool test_held_type() { // This feature is not finished yet @@ -107,11 +147,17 @@ bool test_held_type() { lua_State* L = lua_open(); + lua_baselibopen(L); lua_closer c(L); int top = lua_gettop(L); open(L); + class_ >(L, "ownership") + .def(constructor<>()); + + function(L, "test_auto_ptr", &test_auto_ptr); + luabind::function(L, "tester", &tester); luabind::function(L, "tester", &tester_); luabind::function(L, "tester2", &tester2); @@ -120,6 +166,7 @@ bool test_held_type() luabind::function(L, "tester5", &tester5); luabind::function(L, "tester6", &tester6); luabind::function(L, "tester7", &tester7); + luabind::function(L, "tester8", &tester8); class_ >("base") .def(constructor<>()) @@ -135,6 +182,14 @@ bool test_held_type() object g = get_globals(L); g["test"] = ptr; + + if (dostring(L, "a = ownership()")) return false; + if (dostring(L, "if a.__ok then print('1: ok!') end")) return false; + if (dostring(L, "test_auto_ptr(a)")) return false; + if (dostring(L, "if a.__ok then print('2: ok!') end")) return false; + + if (dostring(L, "if test.__ok then print('ok!') end")) return false; + if (dostring(L, "tester(test)")) return false; if (feedback != 2) return false; if (dostring(L, "a = base()")) return false; @@ -157,6 +212,8 @@ bool test_held_type() if (feedback != 13) return false; if (dostring(L, "tester7(b)")) return false; if (feedback != 14) return false; + if (dostring(L, "tester8(b)")) return false; + if (feedback != 19) return false; if (top != lua_gettop(L)) return false; -- 2.11.4.GIT