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
>
37 T
* apply(lua_State
* L
, by_pointer
<T
>, int index
)
40 // lua_isuserdata(L, index);
41 // getmetatable().__lua_class is true
42 // object_rep->flags() & object_rep::constant == 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
);
51 assert((steps
>= 0) && "adopt_pointer used with type that cannot be converted");
52 obj
->remove_ownership();
53 T
* ptr
= reinterpret_cast<T
*>(obj
->ptr(offset
));
59 static int match(lua_State
* L
, by_pointer
<T
>, int index
)
61 object_rep
* obj
= is_class_object(L
, index
);
62 if (obj
== 0) return -1;
63 // cannot cast a constant object to nonconst
64 if (obj
->flags() & object_rep::constant
) return -1;
65 if (!(obj
->flags() & object_rep::owner
)) return -1;
67 return implicit_cast(obj
->crep(), LUABIND_TYPEID(T
), d
);
71 void converter_postcall(lua_State
*, T
, int) {}
75 struct adopt_pointer
<cpp_to_lua
>
78 void apply(lua_State
* L
, T
* ptr
)
86 class_registry
* registry
= class_registry::get_registry(L
);
87 class_rep
* crep
= registry
->find_class(LUABIND_TYPEID(T
));
89 /* // create the struct to hold the object
90 void* obj = lua_newuserdata(L, sizeof(object_rep));
91 // we send 0 as destructor since we know it will never be called
92 new(obj) object_rep(ptr, crep, object_rep::owner, delete_s<T>::apply);*/
97 boost::tie(obj
,held
) = crep
->allocate(L
);
99 new(obj
) object_rep(ptr
, crep
, object_rep::owner
, delete_s
<T
>::apply
);
101 // set the meta table
102 detail::getref(L
, crep
->metatable_ref());
103 lua_setmetatable(L
, -2);
108 // struct adopt_policy : converter_policy_tag
109 struct adopt_policy
: conversion_policy
<N
>
111 // BOOST_STATIC_CONSTANT(int, index = N);
113 static void precall(lua_State
*, const index_map
&) {}
114 static void postcall(lua_State
*, const index_map
&) {}
116 struct only_accepts_nonconst_pointers
{};
118 template<class T
, class Direction
>
119 struct generate_converter
121 typedef luabind::detail::is_nonconst_pointer
<T
> is_nonconst_p
;
122 typedef typename
boost::mpl::if_
<is_nonconst_p
, adopt_pointer
<Direction
>, only_accepts_nonconst_pointers
>::type type
;
131 detail::policy_cons
<detail::adopt_policy
<N
>, detail::null_type
>
132 adopt(boost::arg
<N
>) { return detail::policy_cons
<detail::adopt_policy
<N
>, detail::null_type
>(); }
135 #endif // LUABIND_ADOPT_POLICY_HPP_INCLUDE