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/detail/error.hpp>
31 #include <luabind/detail/convert_to_lua.hpp>
39 template<class T
, class Obj
, class Policies
>
40 inline T
object_cast_impl(const Obj
& obj
, const Policies
&)
42 typedef typename
detail::find_conversion_policy
<0, Policies
>::type converter_policy
;
43 typename
converter_policy::template generate_converter
<T
, lua_to_cpp
>::type converter
;
47 lua_State
* L
= obj
.lua_state();
48 detail::stack_pop
p(L
, 1);
50 #ifndef LUABIND_NO_ERROR_CHECKING
52 if (converter
.match(L
, LUABIND_DECORATE_TYPE(T
), -1) < 0)
54 #ifndef LUABIND_NO_EXCEPTIONS
55 throw cast_failed(L
, LUABIND_TYPEID(T
));
57 cast_failed_callback_fun e
= detail::error_callback::get().cast
;
58 if (e
) e(L
, LUABIND_TYPEID(T
));
60 assert(0 && "object_cast failed. If you want to handle this error use luabind::set_error_callback()");
66 return converter
.apply(L
, LUABIND_DECORATE_TYPE(T
), -1);
69 template<class T
, class Obj
, class Policies
>
70 boost::optional
<T
> object_cast_nothrow_impl(const Obj
& obj
, const Policies
&)
72 typedef typename
detail::find_conversion_policy
<0, Policies
>::type converter_policy
;
73 typename
converter_policy::template generate_converter
<T
, lua_to_cpp
>::type converter
;
77 lua_State
* L
= obj
.lua_state();
78 detail::stack_pop
p(L
, 1);
80 #ifndef LUABIND_NO_ERROR_CHECKING
82 if (converter
.match(L
, LUABIND_DECORATE_TYPE(T
), -1) < 0)
83 return boost::optional
<T
>();
86 return boost::optional
<T
>(converter
.apply(L
, LUABIND_DECORATE_TYPE(T
), -1));
91 T
object_cast(const object
& obj
)
92 { return detail::object_cast_impl
<T
>(obj
, detail::null_type()); }
94 template<class T
, class Policies
>
95 T
object_cast(const object
& obj
, const Policies
& p
)
96 { return detail::object_cast_impl
<T
>(obj
, p
); }
99 boost::optional
<T
> object_cast_nothrow(const object
& obj
)
100 { return detail::object_cast_nothrow_impl(obj
, detail::null_type()); }
102 template<class T
, class Policies
>
103 boost::optional
<T
> object_cast_nothrow(const object
& obj
, const Policies
& p
)
104 { return detail::object_cast_nothrow_impl(obj
, p
); }
108 T
object_cast(const detail::proxy_object
& obj
)
109 { return detail::object_cast_impl
<T
>(obj
, detail::null_type()); }
111 template<class T
, class Policies
>
112 T
object_cast(const detail::proxy_object
& obj
, const Policies
& p
)
113 { return detail::object_cast_impl
<T
>(obj
, p
); }
116 boost::optional
<T
> object_cast_nothrow(const detail::proxy_object
& obj
)
117 { return detail::object_cast_nothrow_impl(obj
, detail::null_type()); }
119 template<class T
, class Policies
>
120 boost::optional
<T
> object_cast_nothrow(const detail::proxy_object
& obj
, const Policies
& p
)
121 { return detail::object_cast_nothrow_impl(obj
, p
); }
125 T
object_cast(const detail::proxy_raw_object
& obj
)
126 { return detail::object_cast_impl
<T
>(obj
, detail::null_type()); }
128 template<class T
, class Policies
>
129 T
object_cast(const detail::proxy_raw_object
& obj
, const Policies
& p
)
130 { return detail::object_cast_impl
<T
>(obj
, p
); }
133 boost::optional
<T
> object_cast_nothrow(const detail::proxy_raw_object
& obj
)
134 { return detail::object_cast_nothrow_impl(obj
, detail::null_type()); }
136 template<class T
, class Policies
>
137 boost::optional
<T
> object_cast_nothrow(const detail::proxy_raw_object
& obj
, const Policies
& p
)
138 { return detail::object_cast_nothrow_impl(obj
, p
); }
142 T
object_cast(const detail::proxy_array_object
& obj
)
143 { return detail::object_cast_impl
<T
>(obj
, detail::null_type()); }
145 template<class T
, class Policies
>
146 T
object_cast(const detail::proxy_array_object
& obj
, const Policies
& p
)
147 { return detail::object_cast_impl
<T
>(obj
, p
); }
150 boost::optional
<T
> object_cast_nothrow(const detail::proxy_array_object
& obj
)
151 { return detail::object_cast_nothrow_impl(obj
, detail::null_type()); }
153 template<class T
, class Policies
>
154 boost::optional
<T
> object_cast_nothrow(const detail::proxy_array_object
& obj
, const Policies
& p
)
155 { return detail::object_cast_nothrow_impl(obj
, p
); }
160 inline object
get_globals(lua_State
* L
)
162 lua_pushvalue(L
, LUA_GLOBALSINDEX
);
163 int ref
= detail::ref(L
);
164 return object(L
, ref
, true/*object::reference()*/);
167 inline object
get_registry(lua_State
* L
)
169 lua_pushvalue(L
, LUA_REGISTRYINDEX
);
170 int ref
= detail::ref(L
);
171 return object(L
, ref
, true/*object::reference()*/);
174 inline object
newtable(lua_State
* L
)
177 int ref
= detail::ref(L
);
178 return object(L
, ref
, true/*object::reference()*/);
188 object f = class_<A>();
190 A* ptr = object_cast<A*>(f(), adopt(_1));
196 #endif // LUABIND_OBJECT_PROXY_HPP_INCLUDED