doc tweaks
[luabind.git] / luabind / detail / object_funs.hpp
blob34bdc1284520b07ade6c6927113f81391d0e5cd3
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>
33 namespace luabind
36 namespace detail
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;
45 obj.pushvalue();
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));
56 #else
57 cast_failed_callback_fun e = get_cast_failed_callback();
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()");
61 std::terminate();
62 #endif
64 #endif
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;
75 obj.pushvalue();
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>();
84 #endif
86 return boost::optional<T>(converter.apply(L, LUABIND_DECORATE_TYPE(T), -1));
90 template<class T>
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); }
98 template<class T>
99 boost::optional<T> object_cast_nothrow(const object& obj)
100 { return detail::object_cast_nothrow_impl<T>(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<T>(obj, p); }
107 template<class T>
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); }
115 template<class T>
116 boost::optional<T> object_cast_nothrow(const detail::proxy_object& obj)
117 { return detail::object_cast_nothrow_impl<T>(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<T>(obj, p); }
124 template<class T>
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); }
132 template<class T>
133 boost::optional<T> object_cast_nothrow(const detail::proxy_raw_object& obj)
134 { return detail::object_cast_nothrow_impl<T>(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<T>(obj, p); }
141 template<class T>
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); }
149 template<class T>
150 boost::optional<T> object_cast_nothrow(const detail::proxy_array_object& obj)
151 { return detail::object_cast_nothrow_impl<T>(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<T>(obj, p); }
160 inline object get_globals(lua_State* L)
162 lua_pushvalue(L, LUA_GLOBALSINDEX);
163 detail::lua_reference ref;
164 ref.set(L);
165 return object(L, ref, true/*object::reference()*/);
168 inline object get_registry(lua_State* L)
170 lua_pushvalue(L, LUA_REGISTRYINDEX);
171 detail::lua_reference ref;
172 ref.set(L);
173 return object(L, ref, true/*object::reference()*/);
176 inline object newtable(lua_State* L)
178 lua_newtable(L);
179 detail::lua_reference ref;
180 ref.set(L);
181 return object(L, ref, true/*object::reference()*/);
187 struct A
191 object f = class_<A>();
193 A* ptr = object_cast<A*>(f(), adopt(_1));
195 delete ptr;
199 #endif // LUABIND_OBJECT_PROXY_HPP_INCLUDED