fixed LUABIND_NO_EXCEPTION problem in class_rep.cpp
[luabind.git] / luabind / adopt_policy.hpp
blob219c0c8c8740adf4a4ad60b99ac35012dbde042e
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>
31 namespace luabind { namespace detail
33 template<class Direction = lua_to_cpp>
34 struct adopt_pointer
36 template<class T>
37 T* apply(lua_State* L, by_pointer<T>, int index)
39 // preconditions:
40 // lua_isuserdata(L, index);
41 // getmetatable().__lua_class is true
42 // object_rep->flags() & object_rep::constant == 0
44 int offset = 0;
45 object_rep* obj = static_cast<object_rep*>(lua_touserdata(L, index));
46 assert((obj != 0) && "internal error, please report");
47 const class_rep* crep = obj->crep();
49 int steps = implicit_cast(crep, LUABIND_TYPEID(T), offset);
50 (void)steps;
52 assert((steps >= 0) && "adopt_pointer used with type that cannot be converted");
53 obj->remove_ownership();
54 T* ptr = reinterpret_cast<T*>(obj->ptr(offset));
56 return ptr;
59 template<class T>
60 static int match(lua_State* L, by_pointer<T>, int index)
62 object_rep* obj = is_class_object(L, index);
63 if (obj == 0) return -1;
64 // cannot cast a constant object to nonconst
65 if (obj->flags() & object_rep::constant) return -1;
66 if (!(obj->flags() & object_rep::owner)) return -1;
67 int d;
68 return implicit_cast(obj->crep(), LUABIND_TYPEID(T), d);
71 template<class T>
72 void converter_postcall(lua_State*, T, int) {}
75 template<>
76 struct adopt_pointer<cpp_to_lua>
78 template<class T>
79 void apply(lua_State* L, T* ptr)
81 if (ptr == 0)
83 lua_pushnil(L);
84 return;
87 // if there is a back_reference, then the
88 // ownership will be removed from the
89 // back reference and put on the lua stack.
90 if (back_reference<T>::move(L, ptr))
92 object_rep* obj = static_cast<object_rep*>(lua_touserdata(L, -1));
93 obj->set_flags(obj->flags() | object_rep::owner);
94 return;
97 class_registry* registry = class_registry::get_registry(L);
98 class_rep* crep = registry->find_class(LUABIND_TYPEID(T));
100 /* // create the struct to hold the object
101 void* obj = lua_newuserdata(L, sizeof(object_rep));
102 // we send 0 as destructor since we know it will never be called
103 new(obj) object_rep(ptr, crep, object_rep::owner, delete_s<T>::apply);*/
105 void* obj;
106 void* held;
108 boost::tie(obj,held) = crep->allocate(L);
110 new(obj) object_rep(ptr, crep, object_rep::owner, delete_s<T>::apply);
112 // set the meta table
113 detail::getref(L, crep->metatable_ref());
114 lua_setmetatable(L, -2);
118 template<int N>
119 // struct adopt_policy : converter_policy_tag
120 struct adopt_policy : conversion_policy<N>
122 // BOOST_STATIC_CONSTANT(int, index = N);
124 static void precall(lua_State*, const index_map&) {}
125 static void postcall(lua_State*, const index_map&) {}
127 struct only_accepts_nonconst_pointers {};
129 template<class T, class Direction>
130 struct generate_converter
132 typedef luabind::detail::is_nonconst_pointer<T> is_nonconst_p;
133 typedef typename boost::mpl::if_<is_nonconst_p, adopt_pointer<Direction>, only_accepts_nonconst_pointers>::type type;
139 namespace luabind
141 template<int N>
142 detail::policy_cons<detail::adopt_policy<N>, detail::null_type>
143 adopt(boost::arg<N>) { return detail::policy_cons<detail::adopt_policy<N>, detail::null_type>(); }
146 #endif // LUABIND_ADOPT_POLICY_HPP_INCLUDE