From 319ec9e89f68fc218f25a0e4d55aa6657260f5e8 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 1 Apr 2004 16:28:31 +0000 Subject: [PATCH] *** empty log message *** --- luabind/detail/enum_maker.hpp | 3 -- luabind/detail/get_signature.hpp | 1 - luabind/detail/object_rep.hpp | 93 +++++------------------------------- luabind/detail/policy.hpp | 31 ++---------- luabind/detail/ref.hpp | 1 + luabind/object.hpp | 17 +++++-- src/Jamfile | 1 + src/makefile | 1 + src/object_rep.cpp | 100 +++++++++++++++++++++++++++++++++++++++ 9 files changed, 133 insertions(+), 115 deletions(-) create mode 100755 src/object_rep.cpp diff --git a/luabind/detail/enum_maker.hpp b/luabind/detail/enum_maker.hpp index 22a882a..9d5e261 100644 --- a/luabind/detail/enum_maker.hpp +++ b/luabind/detail/enum_maker.hpp @@ -52,9 +52,6 @@ namespace luabind , val_(v) {} - // TODO: this doesn't really need to be a std::string, right? It could maintain the pointer - // until it's registered as a static data member -// std::string name_; const char* name_; int val_; diff --git a/luabind/detail/get_signature.hpp b/luabind/detail/get_signature.hpp index 3a8e97a..9064ced 100644 --- a/luabind/detail/get_signature.hpp +++ b/luabind/detail/get_signature.hpp @@ -42,7 +42,6 @@ namespace luabind { namespace detail { - // TODO: move to its own transation unit std::string LUABIND_API get_class_name(lua_State* L, LUABIND_TYPE_INFO i); template diff --git a/luabind/detail/object_rep.hpp b/luabind/detail/object_rep.hpp index 9f94c19..4c46102 100644 --- a/luabind/detail/object_rep.hpp +++ b/luabind/detail/object_rep.hpp @@ -33,8 +33,6 @@ namespace luabind { namespace detail void finalize(lua_State* L, class_rep* crep); - // TODO: move code into separate compilation unit - // this class is allocated inside lua for each pointer. // it contains the actual c++ object-pointer. // it also tells if it is const or not. @@ -44,98 +42,33 @@ namespace luabind { namespace detail enum { constant = 1, owner = 2, smart_pointer = 4, lua_class = 8, call_super = 16 }; // dest is a function that is called to delete the c++ object this struct holds - object_rep(void* obj, class_rep* crep, int flags, void(*dest)(void*)) - : m_object(obj) - , m_classrep(crep) - , m_flags(flags) - , m_destructor(dest) - , m_dependency_cnt(1) - { - // 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"); - } + object_rep(void* obj, class_rep* crep, int flags, void(*dest)(void*)); + object_rep(class_rep* crep, int flags, detail::lua_reference const& table_ref); + ~object_rep(); - object_rep(class_rep* crep, int flags, detail::lua_reference const& table_ref) - : m_object(0) - , m_classrep(crep) - , m_flags(flags) - , m_lua_table_ref(table_ref) - , m_destructor(0) - , m_dependency_cnt(1) - { - } + void* ptr() const { return m_object; } - ~object_rep() - { -// delete m_instance; - - if (m_flags & owner && m_destructor) m_destructor(m_object); - } - - void* ptr() const throw() { return m_object; } - - void* ptr(int pointer_offset) const throw() + void* ptr(int pointer_offset) const { return reinterpret_cast(m_object) + pointer_offset; } - const class_rep* crep() const throw() { return m_classrep; } - class_rep* crep() throw() { return m_classrep; } - int flags() const throw() { return m_flags; } + const class_rep* crep() const { return m_classrep; } + class_rep* crep() { return m_classrep; } + int flags() const { return m_flags; } void set_flags(int flags) { m_flags = flags; } void get_lua_table(lua_State* L) const { m_lua_table_ref.get(L); } - void remove_ownership() - { - assert((m_flags & owner) && "cannot remove ownership of object that's not owned"); - m_flags &= ~owner; - } - - void set_destructor(void(*ptr)(void*)) - { - m_destructor = ptr; - } + void remove_ownership(); + void set_destructor(void(*ptr)(void*)); void set_object(void* p) { m_object = p; } - void add_dependency(lua_State* L, int index) - { - if (!m_dependency_ref.is_valid()) - { - lua_newtable(L); - m_dependency_ref.set(L); - } - - m_dependency_ref.get(L); - lua_pushvalue(L, index); - lua_rawseti(L, -2, m_dependency_cnt); - lua_pop(L, 1); - ++m_dependency_cnt; - } - - void release_refs(lua_State* L) - { - m_dependency_ref.reset(); - m_lua_table_ref.reset(); - } + void add_dependency(lua_State* L, int index); + void release_refs(lua_State* L); -// 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)); - - finalize(L, obj->crep()); - - obj->release_refs(L); - obj->~object_rep(); - return 0; - } + static int garbage_collector(lua_State* L); private: diff --git a/luabind/detail/policy.hpp b/luabind/detail/policy.hpp index 447a8ce..c52af7b 100644 --- a/luabind/detail/policy.hpp +++ b/luabind/detail/policy.hpp @@ -649,9 +649,6 @@ namespace luabind { namespace detail class_rep* crep = get_class_rep(L); -// class_registry* registry = class_registry::get_registry(L); -// class_rep* crep = registry->find_class(LUABIND_TYPEID(T)); - // if you get caught in this assert you are // trying to use an unregistered type assert(crep && "you are trying to use an unregistered type"); @@ -740,8 +737,6 @@ namespace luabind { namespace detail template void apply(lua_State* L, const T& ref) { - //class_registry* registry = class_registry::get_registry(L); - //class_rep* crep = registry->find_class(LUABIND_TYPEID(T)); class_rep* crep = get_class_rep(L); // if you get caught in this assert you are @@ -813,10 +808,7 @@ namespace luabind { namespace detail // special case if we get nil in, try to convert the holder type if (lua_isnil(L, index)) { - detail::class_registry* reg = detail::class_registry::get_registry(L); - assert(reg); - - crep = reg->find_class(LUABIND_TYPEID(T)); + crep = get_class_rep(L);; assert(crep); } else @@ -843,10 +835,7 @@ namespace luabind { namespace detail // special case if we get nil in, try to match the holder type if (lua_isnil(L, index)) { - detail::class_registry* reg = detail::class_registry::get_registry(L); - assert(reg); - - class_rep* crep = reg->find_class(LUABIND_TYPEID(T)); + class_rep* crep = get_class_rep(L); if (crep == 0) return -1; if ((LUABIND_TYPE_INFO_EQUAL(crep->holder_type(), LUABIND_TYPEID(T)))) return 0; @@ -887,8 +876,6 @@ namespace luabind { namespace detail return; } - //class_registry* registry = class_registry::get_registry(L); - //class_rep* crep = registry->find_class(LUABIND_TYPEID(T)); class_rep* crep = get_class_rep(L); // if you get caught in this assert you are @@ -953,8 +940,6 @@ namespace luabind { namespace detail template void apply(lua_State* L, T& ref) { - //class_registry* registry = class_registry::get_registry(L); - //class_rep* crep = registry->find_class(LUABIND_TYPEID(T)); class_rep* crep = get_class_rep(L); // if you get caught in this assert you are @@ -1005,8 +990,6 @@ namespace luabind { namespace detail template void apply(lua_State* L, const T& ref) { - //class_registry* registry = class_registry::get_registry(L); - //class_rep* crep = registry->find_class(LUABIND_TYPEID(T)); class_rep* crep = get_class_rep(L); // if you get caught in this assert you are @@ -1064,10 +1047,7 @@ namespace luabind { namespace detail // special case if we get nil in, try to convert the holder type if (lua_isnil(L, index)) { - detail::class_registry* reg = detail::class_registry::get_registry(L); - assert(reg); - - crep = reg->find_class(LUABIND_TYPEID(T)); + crep = get_class_rep(L); assert(crep); } else @@ -1093,10 +1073,7 @@ namespace luabind { namespace detail // special case if we get nil in, try to match the holder type if (lua_isnil(L, index)) { - detail::class_registry* reg = detail::class_registry::get_registry(L); - assert(reg); - - class_rep* crep = reg->find_class(LUABIND_TYPEID(T)); + class_rep* crep = get_class_rep(L);; if (crep == 0) return -1; if ((LUABIND_TYPE_INFO_EQUAL(crep->holder_type(), LUABIND_TYPEID(T)))) return 0; diff --git a/luabind/detail/ref.hpp b/luabind/detail/ref.hpp index 190fee1..6dcf138 100644 --- a/luabind/detail/ref.hpp +++ b/luabind/detail/ref.hpp @@ -25,6 +25,7 @@ #define LUABIND_REF_HPP_INCLUDED #include +#include #include #include diff --git a/luabind/object.hpp b/luabind/object.hpp index 7377fb8..e47b803 100644 --- a/luabind/object.hpp +++ b/luabind/object.hpp @@ -207,6 +207,9 @@ namespace luabind return *this; } + template + detail::proxy_object operator[](const T& key) const; + proxy_object& operator=(const object& p); proxy_object& operator=(const proxy_object& p); proxy_object& operator=(const proxy_raw_object& p); @@ -332,6 +335,9 @@ namespace luabind return *this; } + template + detail::proxy_object operator[](const T& key) const; + proxy_raw_object& operator=(const object& p); proxy_raw_object& operator=(const proxy_object& p); proxy_raw_object& operator=(const proxy_raw_object& p); @@ -432,6 +438,9 @@ namespace luabind return *this; } + template + detail::proxy_object operator[](const T& key) const; + proxy_array_object& operator=(const object& p); proxy_array_object& operator=(const proxy_object& p); proxy_array_object& operator=(const proxy_raw_object& p); @@ -489,16 +498,16 @@ namespace luabind template inline object raw_at(const T& key); #endif - +/* template inline detail::proxy_object operator[](const T& key) const { detail::convert_to_lua(m_state, key); - lua_reference ref;\ - ref.set(m_state);\ + lua_reference ref; + ref.set(m_state); return detail::proxy_object(const_cast(this), ref); } - +*/ inline bool is_valid() const { return true; } lua_State* lua_state() const; void pushvalue() const; diff --git a/src/Jamfile b/src/Jamfile index d26f0de..784ac4c 100755 --- a/src/Jamfile +++ b/src/Jamfile @@ -13,6 +13,7 @@ lib luabind create_class.cpp stack_content_by_name.cpp object.cpp + object_rep.cpp class_info.cpp ref.cpp class_registry.cpp diff --git a/src/makefile b/src/makefile index a6b9ed9..84c670c 100755 --- a/src/makefile +++ b/src/makefile @@ -14,6 +14,7 @@ SOURCES = \ create_class.cpp \ stack_content_by_name.cpp \ object.cpp \ + object_rep.cpp \ class_info.cpp \ class_registry.cpp \ link_compatibility.cpp diff --git a/src/object_rep.cpp b/src/object_rep.cpp new file mode 100755 index 0000000..745a3b8 --- /dev/null +++ b/src/object_rep.cpp @@ -0,0 +1,100 @@ +// Copyright (c) 2003 Daniel Wallin and Arvid Norberg + +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR +// ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE +// OR OTHER DEALINGS IN THE SOFTWARE. + +#include + + +namespace luabind { namespace detail +{ + + // dest is a function that is called to delete the c++ object this struct holds + object_rep::object_rep(void* obj, class_rep* crep, int flags, void(*dest)(void*)) + : m_object(obj) + , m_classrep(crep) + , m_flags(flags) + , m_destructor(dest) + , m_dependency_cnt(1) + { + // 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"); + } + + object_rep::object_rep(class_rep* crep, int flags, detail::lua_reference const& table_ref) + : m_object(0) + , m_classrep(crep) + , m_flags(flags) + , m_lua_table_ref(table_ref) + , m_destructor(0) + , m_dependency_cnt(1) + { + } + + object_rep::~object_rep() + { + if (m_flags & owner && m_destructor) m_destructor(m_object); + } + + void object_rep::remove_ownership() + { + assert((m_flags & owner) && "cannot remove ownership of object that's not owned"); + m_flags &= ~owner; + } + + void object_rep::set_destructor(void(*ptr)(void*)) + { + m_destructor = ptr; + } + + void object_rep::add_dependency(lua_State* L, int index) + { + if (!m_dependency_ref.is_valid()) + { + lua_newtable(L); + m_dependency_ref.set(L); + } + + m_dependency_ref.get(L); + lua_pushvalue(L, index); + lua_rawseti(L, -2, m_dependency_cnt); + lua_pop(L, 1); + ++m_dependency_cnt; + } + + void object_rep::release_refs(lua_State* L) + { + m_dependency_ref.reset(); + m_lua_table_ref.reset(); + } + + int object_rep::garbage_collector(lua_State* L) + { + object_rep* obj = static_cast(lua_touserdata(L, -1)); + + finalize(L, obj->crep()); + + obj->release_refs(L); + obj->~object_rep(); + return 0; + } + +}} + -- 2.11.4.GIT