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/wrapper_base.hpp>
29 #include <luabind/detail/policy.hpp>
30 #include <luabind/detail/implicit_cast.hpp>
31 #include <luabind/back_reference_fwd.hpp>
32 #include <luabind/wrapper_base.hpp>
33 #include <boost/type_traits/is_polymorphic.hpp>
35 namespace luabind
{ namespace detail
38 void adjust_backref_ownership(T
* ptr
, mpl::true_
)
40 if (wrap_base
* p
= dynamic_cast<wrap_base
*>(ptr
))
42 wrapped_self_t
& wrapper
= wrap_access::ref(*p
);
43 wrapper
.get(wrapper
.state());
44 wrapper
.m_strong_ref
.set(wrapper
.state());
48 inline void adjust_backref_ownership(void*, mpl::false_
)
51 template<class Direction
= lua_to_cpp
>
52 struct adopt_pointer
: pointer_converter
54 typedef adopt_pointer type
;
56 int const consumed_args(...)
62 T
* apply(lua_State
* L
, by_pointer
<T
>, int index
)
64 T
* ptr
= pointer_converter::apply(
65 L
, LUABIND_DECORATE_TYPE(T
*), index
);
67 object_rep
* obj
= static_cast<object_rep
*>(
68 lua_touserdata(L
, index
));
71 adjust_backref_ownership(ptr
, boost::is_polymorphic
<T
>());
77 int match(lua_State
* L
, by_pointer
<T
>, int index
)
79 return pointer_converter::match(
80 L
, LUABIND_DECORATE_TYPE(T
*), index
);
84 void converter_postcall(lua_State
*, T
, int) {}
88 struct adopt_pointer
<cpp_to_lua
>
90 typedef adopt_pointer type
;
93 void apply(lua_State
* L
, T
* ptr
)
101 // if there is a back_reference, then the
102 // ownership will be removed from the
103 // back reference and put on the lua stack.
104 if (luabind::move_back_reference(L
, ptr
))
107 make_instance(L
, std::auto_ptr
<T
>(ptr
));
112 // struct adopt_policy : converter_policy_tag
113 struct adopt_policy
: conversion_policy
<N
>
115 // BOOST_STATIC_CONSTANT(int, index = N);
117 static void precall(lua_State
*, const index_map
&) {}
118 static void postcall(lua_State
*, const index_map
&) {}
120 struct only_accepts_nonconst_pointers
{};
122 template<class T
, class Direction
>
125 typedef luabind::detail::is_nonconst_pointer
<T
> is_nonconst_p
;
126 typedef typename
boost::mpl::if_
<is_nonconst_p
, adopt_pointer
<Direction
>, only_accepts_nonconst_pointers
>::type type
;
135 detail::policy_cons
<detail::adopt_policy
<N
>, detail::null_type
>
136 adopt(LUABIND_PLACEHOLDER_ARG(N
))
138 return detail::policy_cons
<detail::adopt_policy
<N
>, detail::null_type
>();
142 #endif // LUABIND_ADOPT_POLICY_HPP_INCLUDE