Create instance table reference in the main thread.
[luabind.git] / luabind / adopt_policy.hpp
blob0d41efdcd05cb1253f9ca61628c7091eaf89be6e
1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
24 #ifndef LUABIND_ADOPT_POLICY_HPP_INCLUDED
25 #define LUABIND_ADOPT_POLICY_HPP_INCLUDED
27 #include <luabind/config.hpp>
28 #include <luabind/detail/policy.hpp>
29 #include <luabind/detail/implicit_cast.hpp>
30 #include <boost/mpl/bool.hpp>
31 #include <luabind/back_reference_fwd.hpp>
33 namespace luabind { namespace detail
35 template<class Direction = lua_to_cpp>
36 struct adopt_pointer
38 typedef adopt_pointer type;
40 template<class T>
41 T* apply(lua_State* L, by_pointer<T>, int index)
43 // preconditions:
44 // lua_isuserdata(L, index);
45 // getmetatable().__lua_class is true
46 // object_rep->flags() & object_rep::constant == 0
48 int offset = 0;
49 object_rep* obj = static_cast<object_rep*>(lua_touserdata(L, index));
50 assert((obj != 0) && "internal error, please report");
51 const class_rep* crep = obj->crep();
53 int steps = implicit_cast(crep, LUABIND_TYPEID(T), offset);
54 (void)steps;
56 assert((steps >= 0) && "adopt_pointer used with type that cannot be converted");
57 obj->remove_ownership();
58 T* ptr = reinterpret_cast<T*>(obj->ptr(offset));
60 return ptr;
63 template<class T>
64 static int match(lua_State* L, by_pointer<T>, int index)
66 object_rep* obj = is_class_object(L, index);
67 if (obj == 0) return -1;
68 // cannot cast a constant object to nonconst
69 if (obj->flags() & object_rep::constant) return -1;
70 if (!(obj->flags() & object_rep::owner)) return -1;
71 int d;
72 return implicit_cast(obj->crep(), LUABIND_TYPEID(T), d);
75 template<class T>
76 void converter_postcall(lua_State*, T, int) {}
79 template<>
80 struct adopt_pointer<cpp_to_lua>
82 typedef adopt_pointer type;
84 template<class T>
85 void apply(lua_State* L, T* ptr)
87 if (ptr == 0)
89 lua_pushnil(L);
90 return;
93 // if there is a back_reference, then the
94 // ownership will be removed from the
95 // back reference and put on the lua stack.
96 if (luabind::move_back_reference(L, ptr))
98 object_rep* obj = static_cast<object_rep*>(lua_touserdata(L, -1));
99 obj->set_flags(obj->flags() | object_rep::owner);
100 return;
103 class_registry* registry = class_registry::get_registry(L);
104 class_rep* crep = registry->find_class(LUABIND_TYPEID(T));
106 /* // create the struct to hold the object
107 void* obj = lua_newuserdata(L, sizeof(object_rep));
108 // we send 0 as destructor since we know it will never be called
109 new(obj) object_rep(ptr, crep, object_rep::owner, delete_s<T>::apply);*/
111 void* obj;
112 void* held;
114 boost::tie(obj,held) = crep->allocate(L);
116 new(obj) object_rep(ptr, crep, object_rep::owner, delete_s<T>::apply);
118 // set the meta table
119 detail::getref(L, crep->metatable_ref());
120 lua_setmetatable(L, -2);
124 template<int N>
125 // struct adopt_policy : converter_policy_tag
126 struct adopt_policy : conversion_policy<N>
128 // BOOST_STATIC_CONSTANT(int, index = N);
130 static void precall(lua_State*, const index_map&) {}
131 static void postcall(lua_State*, const index_map&) {}
133 struct only_accepts_nonconst_pointers {};
135 template<class T, class Direction>
136 struct apply
138 typedef luabind::detail::is_nonconst_pointer<T> is_nonconst_p;
139 typedef typename boost::mpl::if_<is_nonconst_p, adopt_pointer<Direction>, only_accepts_nonconst_pointers>::type type;
145 namespace luabind
147 template<int N>
148 detail::policy_cons<detail::adopt_policy<N>, detail::null_type>
149 adopt(LUABIND_PLACEHOLDER_ARG(N))
151 return detail::policy_cons<detail::adopt_policy<N>, detail::null_type>();
155 #endif // LUABIND_ADOPT_POLICY_HPP_INCLUDE