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/back_reference_fwd.hpp>
31 #include <luabind/wrapper_base.hpp>
32 #include <boost/type_traits/is_polymorphic.hpp>
34 namespace luabind
{ namespace detail
37 void adjust_backref_ownership(T
* ptr
, mpl::true_
)
39 if (wrap_base
* p
= dynamic_cast<wrap_base
*>(ptr
))
41 wrapped_self_t
& wrapper
= wrap_access::ref(*p
);
42 wrapper
.get(wrapper
.state());
43 wrapper
.m_strong_ref
.set(wrapper
.state());
47 inline void adjust_backref_ownership(void*, mpl::false_
)
50 template <class Pointer
, class Direction
= lua_to_cpp
>
51 struct adopt_pointer
: pointer_converter
53 typedef adopt_pointer type
;
55 int const consumed_args(...)
61 T
* apply(lua_State
* L
, by_pointer
<T
>, int index
)
63 T
* ptr
= pointer_converter::apply(
64 L
, LUABIND_DECORATE_TYPE(T
*), index
);
66 object_rep
* obj
= static_cast<object_rep
*>(
67 lua_touserdata(L
, index
));
70 adjust_backref_ownership(ptr
, boost::is_polymorphic
<T
>());
76 int match(lua_State
* L
, by_pointer
<T
>, int index
)
78 return pointer_converter::match(
79 L
, LUABIND_DECORATE_TYPE(T
*), index
);
83 void converter_postcall(lua_State
*, T
, int) {}
86 template <class Pointer
, class T
>
87 struct pointer_or_default
93 struct pointer_or_default
<void, T
>
95 typedef std::auto_ptr
<T
> type
;
98 template <class Pointer
>
99 struct adopt_pointer
<Pointer
, cpp_to_lua
>
101 typedef adopt_pointer type
;
104 void apply(lua_State
* L
, T
* ptr
)
112 // if there is a back_reference, then the
113 // ownership will be removed from the
114 // back reference and put on the lua stack.
115 if (luabind::move_back_reference(L
, ptr
))
118 typedef typename pointer_or_default
<Pointer
, T
>::type
121 make_instance(L
, pointer_type(ptr
));
125 template <int N
, class Pointer
= void>
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_
<
141 , adopt_pointer
<Pointer
, Direction
>
142 , only_accepts_nonconst_pointers
152 detail::policy_cons
<detail::adopt_policy
<N
>, detail::null_type
>
153 adopt(LUABIND_PLACEHOLDER_ARG(N
))
155 return detail::policy_cons
<detail::adopt_policy
<N
>, detail::null_type
>();
158 template <class Pointer
, int N
>
159 detail::policy_cons
<detail::adopt_policy
<N
, Pointer
>, detail::null_type
>
160 adopt(LUABIND_PLACEHOLDER_ARG(N
))
162 return detail::policy_cons
<detail::adopt_policy
<N
, Pointer
>, detail::null_type
>();
166 #endif // LUABIND_ADOPT_POLICY_HPP_INCLUDE