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.
23 #ifndef LUABIND_OBJECT_PROXY_HPP_INCLUDED
24 #define LUABIND_OBJECT_PROXY_HPP_INCLUDED
26 #include <boost/optional.hpp>
28 #include <luabind/config.hpp>
29 #include <luabind/detail/policy.hpp>
30 #include <luabind/error.hpp>
31 #include <luabind/detail/convert_to_lua.hpp>
32 #include <luabind/detail/debug.hpp>
40 template<class T
, class Obj
, class Policies
>
41 inline T
object_cast_impl(const Obj
& obj
, const Policies
&)
43 if (obj
.lua_state() == 0) throw cast_failed(0, LUABIND_TYPEID(T
));
44 LUABIND_CHECK_STACK(obj
.lua_state());
46 typedef typename
detail::find_conversion_policy
<0, Policies
>::type converter_policy
;
47 typename
converter_policy::template generate_converter
<T
, lua_to_cpp
>::type converter
;
51 lua_State
* L
= obj
.lua_state();
52 detail::stack_pop
p(L
, 1);
54 #ifndef LUABIND_NO_ERROR_CHECKING
56 if (converter
.match(L
, LUABIND_DECORATE_TYPE(T
), -1) < 0)
58 #ifndef LUABIND_NO_EXCEPTIONS
59 throw cast_failed(L
, LUABIND_TYPEID(T
));
61 cast_failed_callback_fun e
= get_cast_failed_callback();
62 if (e
) e(L
, LUABIND_TYPEID(T
));
64 assert(0 && "object_cast failed. If you want to handle this error use luabind::set_error_callback()");
70 return converter
.apply(L
, LUABIND_DECORATE_TYPE(T
), -1);
73 template<class T
, class Obj
, class Policies
>
74 boost::optional
<T
> object_cast_nothrow_impl(const Obj
& obj
, const Policies
&)
76 typedef typename
detail::find_conversion_policy
<0, Policies
>::type converter_policy
;
77 typename
converter_policy::template generate_converter
<T
, lua_to_cpp
>::type converter
;
79 if (obj
.lua_state() == 0) return boost::optional
<T
>();
80 LUABIND_CHECK_STACK(obj
.lua_state());
84 lua_State
* L
= obj
.lua_state();
85 detail::stack_pop
p(L
, 1);
87 #ifndef LUABIND_NO_ERROR_CHECKING
89 if (converter
.match(L
, LUABIND_DECORATE_TYPE(T
), -1) < 0)
90 return boost::optional
<T
>();
93 return boost::optional
<T
>(converter
.apply(L
, LUABIND_DECORATE_TYPE(T
), -1));
98 T
object_cast(const object
& obj
)
99 { return detail::object_cast_impl
<T
>(obj
, detail::null_type()); }
101 template<class T
, class Policies
>
102 T
object_cast(const object
& obj
, const Policies
& p
)
103 { return detail::object_cast_impl
<T
>(obj
, p
); }
106 boost::optional
<T
> object_cast_nothrow(const object
& obj
)
107 { return detail::object_cast_nothrow_impl
<T
>(obj
, detail::null_type()); }
109 template<class T
, class Policies
>
110 boost::optional
<T
> object_cast_nothrow(const object
& obj
, const Policies
& p
)
111 { return detail::object_cast_nothrow_impl
<T
>(obj
, p
); }
115 T
object_cast(const detail::proxy_object
& obj
)
116 { return detail::object_cast_impl
<T
>(obj
, detail::null_type()); }
118 template<class T
, class Policies
>
119 T
object_cast(const detail::proxy_object
& obj
, const Policies
& p
)
120 { return detail::object_cast_impl
<T
>(obj
, p
); }
123 boost::optional
<T
> object_cast_nothrow(const detail::proxy_object
& obj
)
124 { return detail::object_cast_nothrow_impl
<T
>(obj
, detail::null_type()); }
126 template<class T
, class Policies
>
127 boost::optional
<T
> object_cast_nothrow(const detail::proxy_object
& obj
, const Policies
& p
)
128 { return detail::object_cast_nothrow_impl
<T
>(obj
, p
); }
132 T
object_cast(const detail::proxy_raw_object
& obj
)
133 { return detail::object_cast_impl
<T
>(obj
, detail::null_type()); }
135 template<class T
, class Policies
>
136 T
object_cast(const detail::proxy_raw_object
& obj
, const Policies
& p
)
137 { return detail::object_cast_impl
<T
>(obj
, p
); }
140 boost::optional
<T
> object_cast_nothrow(const detail::proxy_raw_object
& obj
)
141 { return detail::object_cast_nothrow_impl
<T
>(obj
, detail::null_type()); }
143 template<class T
, class Policies
>
144 boost::optional
<T
> object_cast_nothrow(const detail::proxy_raw_object
& obj
, const Policies
& p
)
145 { return detail::object_cast_nothrow_impl
<T
>(obj
, p
); }
149 T
object_cast(const detail::proxy_array_object
& obj
)
150 { return detail::object_cast_impl
<T
>(obj
, detail::null_type()); }
152 template<class T
, class Policies
>
153 T
object_cast(const detail::proxy_array_object
& obj
, const Policies
& p
)
154 { return detail::object_cast_impl
<T
>(obj
, p
); }
157 boost::optional
<T
> object_cast_nothrow(const detail::proxy_array_object
& obj
)
158 { return detail::object_cast_nothrow_impl
<T
>(obj
, detail::null_type()); }
160 template<class T
, class Policies
>
161 boost::optional
<T
> object_cast_nothrow(const detail::proxy_array_object
& obj
, const Policies
& p
)
162 { return detail::object_cast_nothrow_impl
<T
>(obj
, p
); }
167 inline object
get_globals(lua_State
* L
)
169 lua_pushvalue(L
, LUA_GLOBALSINDEX
);
170 detail::lua_reference ref
;
172 return object(L
, ref
, true/*object::reference()*/);
175 inline object
get_registry(lua_State
* L
)
177 lua_pushvalue(L
, LUA_REGISTRYINDEX
);
178 detail::lua_reference ref
;
180 return object(L
, ref
, true/*object::reference()*/);
183 inline object
newtable(lua_State
* L
)
186 detail::lua_reference ref
;
188 return object(L
, ref
, true/*object::reference()*/);
198 object f = class_<A>();
200 A* ptr = object_cast<A*>(f(), adopt(_1));
206 #endif // LUABIND_OBJECT_PROXY_HPP_INCLUDED