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
>
38 typedef adopt_pointer type
;
41 T
* apply(lua_State
* L
, by_pointer
<T
>, int index
)
44 // lua_isuserdata(L, index);
45 // getmetatable().__lua_class is true
46 // object_rep->flags() & object_rep::constant == 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
);
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
));
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;
72 return implicit_cast(obj
->crep(), LUABIND_TYPEID(T
), d
);
76 void converter_postcall(lua_State
*, T
, int) {}
80 struct adopt_pointer
<cpp_to_lua
>
82 typedef adopt_pointer type
;
85 void apply(lua_State
* L
, T
* ptr
)
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
);
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);*/
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);
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
>
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
;
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