pointer extraction for holder_types works for pointer_converter.
[luabind.git] / luabind / detail / policy.hpp
blob528beff1d38d2aa7da2b5614ef82c137ca758d3f
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_POLICY_HPP_INCLUDED
25 #define LUABIND_POLICY_HPP_INCLUDED
27 #include <luabind/config.hpp>
29 #include <typeinfo>
31 #include <boost/type_traits/is_enum.hpp>
32 #include <boost/mpl/bool.hpp>
33 #include <boost/mpl/integral_c.hpp>
34 #include <boost/mpl/equal_to.hpp>
35 #include <boost/type_traits/add_reference.hpp>
36 #include <boost/limits.hpp>
38 #include <luabind/detail/class_registry.hpp>
39 #include <luabind/detail/primitives.hpp>
40 #include <luabind/detail/object_rep.hpp>
41 #include <luabind/detail/typetraits.hpp>
43 #include <boost/type_traits/add_reference.hpp>
45 #include <luabind/detail/decorate_type.hpp>
46 #include <luabind/object.hpp>
48 namespace luabind
50 namespace detail
52 struct conversion_policy_base {};
55 template<int N, bool HasArg = true>
56 struct conversion_policy : detail::conversion_policy_base
58 BOOST_STATIC_CONSTANT(int, index = N);
59 BOOST_STATIC_CONSTANT(bool, has_arg = HasArg);
62 class index_map
64 public:
66 index_map(const int* m): m_map(m) {}
68 int operator[](int index) const
70 return m_map[index + 1];
73 private:
75 const int* m_map;
78 namespace converters
80 using luabind::detail::yes_t;
81 using luabind::detail::no_t;
82 using luabind::detail::by_value;
83 using luabind::detail::by_reference;
84 using luabind::detail::by_const_reference;
85 using luabind::detail::by_pointer;
86 using luabind::detail::by_const_pointer;
88 no_t is_user_defined(...);
91 namespace detail
93 template<class T>
94 struct is_user_defined
96 BOOST_STATIC_CONSTANT(bool, value =
97 sizeof(luabind::converters::is_user_defined(LUABIND_DECORATE_TYPE(T))) == sizeof(yes_t));
100 int implicit_cast(const class_rep* crep, LUABIND_TYPE_INFO const&, int& pointer_offset);
103 template<class T> class functor;
104 class object;
107 namespace luabind { namespace detail
109 template<class>
110 struct is_primitive;
112 template<class T>
113 yes_t is_lua_functor_test(const functor<T>&);
115 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
116 no_t is_lua_functor_test(...);
117 #else
118 template<class T>
119 no_t is_lua_functor_test(const T&);
120 #endif
122 template<class T>
123 struct is_lua_functor
125 static T t;
127 BOOST_STATIC_CONSTANT(bool, value = sizeof(is_lua_functor_test(t)) == sizeof(yes_t));
130 namespace
132 static char msvc_fix[64];
135 template<class T>
136 struct indirect_type
138 typedef typename
139 boost::mpl::if_<is_primitive<T>
140 , const type<T>&
141 , typename boost::mpl::apply_if<boost::mpl::or_<boost::is_reference<T>, boost::is_pointer<T> >
142 , identity<T>
143 , boost::add_reference<T>
144 >::type
145 >::type result_type;
147 static inline result_type get()
149 return reinterpret_cast<result_type>(msvc_fix);
153 template<class H, class T>
154 struct policy_cons
156 typedef H head;
157 typedef T tail;
159 template<class U>
160 policy_cons<U, policy_cons<H,T> > operator,(policy_cons<U,detail::null_type>)
162 return policy_cons<U, policy_cons<H,T> >();
165 template<class U>
166 policy_cons<U, policy_cons<H,T> > operator+(policy_cons<U,detail::null_type>)
168 return policy_cons<U, policy_cons<H,T> >();
171 template<class U>
172 policy_cons<U, policy_cons<H,T> > operator|(policy_cons<U,detail::null_type>)
174 return policy_cons<U, policy_cons<H,T> >();
178 struct indirection_layer
180 template<class T>
181 indirection_layer(const T&);
184 template<class H, class T>
185 yes_t is_policy_cons_test(const policy_cons<H,T>&);
186 no_t is_policy_cons_test(...);
188 template<class T>
189 struct is_policy_cons
191 static const T& t;
193 BOOST_STATIC_CONSTANT(bool, value =
194 sizeof(is_policy_cons_test(t)) == sizeof(yes_t));
197 no_t is_string_literal(indirection_layer);
198 yes_t is_string_literal(const char*);
200 template<class T>
201 struct is_primitive/*: boost::mpl::bool_c<false>*/
203 static T t;
205 BOOST_STATIC_CONSTANT(bool, value = sizeof(is_string_literal(t)) == sizeof(yes_t));
208 template<> struct is_primitive<luabind::object>: boost::mpl::bool_<true> {};
209 template<> struct is_primitive<const luabind::object>: boost::mpl::bool_<true> {};
210 template<> struct is_primitive<const luabind::object&>: boost::mpl::bool_<true> {};
212 template<> struct is_primitive<int>: boost::mpl::bool_<true> {};
213 template<> struct is_primitive<char>: boost::mpl::bool_<true> {};
214 template<> struct is_primitive<short>: boost::mpl::bool_<true> {};
215 template<> struct is_primitive<long>: boost::mpl::bool_<true> {};
216 template<> struct is_primitive<unsigned char>: boost::mpl::bool_<true> {};
217 template<> struct is_primitive<unsigned short>: boost::mpl::bool_<true> {};
218 template<> struct is_primitive<unsigned long>: boost::mpl::bool_<true> {};
219 template<> struct is_primitive<unsigned int>: boost::mpl::bool_<true> {};
220 template<> struct is_primitive<float>: boost::mpl::bool_<true> {};
221 template<> struct is_primitive<double>: boost::mpl::bool_<true> {};
222 template<> struct is_primitive<long double>: boost::mpl::bool_<true> {};
223 template<> struct is_primitive<char*>: boost::mpl::bool_<true> {};
224 template<> struct is_primitive<bool>: boost::mpl::bool_<true> {};
226 template<> struct is_primitive<const int>: boost::mpl::bool_<true> {};
227 template<> struct is_primitive<const char>: boost::mpl::bool_<true> {};
228 template<> struct is_primitive<const short>: boost::mpl::bool_<true> {};
229 template<> struct is_primitive<const long>: boost::mpl::bool_<true> {};
230 template<> struct is_primitive<const unsigned int>: boost::mpl::bool_<true> {};
231 template<> struct is_primitive<const unsigned char>: boost::mpl::bool_<true> {};
232 template<> struct is_primitive<const unsigned short>: boost::mpl::bool_<true> {};
233 template<> struct is_primitive<const unsigned long>: boost::mpl::bool_<true> {};
234 template<> struct is_primitive<const float>: boost::mpl::bool_<true> {};
235 template<> struct is_primitive<const double>: boost::mpl::bool_<true> {};
236 template<> struct is_primitive<const long double>: boost::mpl::bool_<true> {};
237 template<> struct is_primitive<const char*>: boost::mpl::bool_<true> {};
238 template<> struct is_primitive<const char* const>: boost::mpl::bool_<true> {};
239 template<> struct is_primitive<const bool>: boost::mpl::bool_<true> {};
241 // TODO: add more
242 template<> struct is_primitive<const int&>: boost::mpl::bool_<true> {};
243 template<> struct is_primitive<const char&>: boost::mpl::bool_<true> {};
244 template<> struct is_primitive<const short&>: boost::mpl::bool_<true> {};
245 template<> struct is_primitive<const long&>: boost::mpl::bool_<true> {};
246 template<> struct is_primitive<const unsigned int&>: boost::mpl::bool_<true> {};
247 template<> struct is_primitive<const unsigned char&>: boost::mpl::bool_<true> {};
248 template<> struct is_primitive<const unsigned short&>: boost::mpl::bool_<true> {};
249 template<> struct is_primitive<const unsigned long&>: boost::mpl::bool_<true> {};
250 template<> struct is_primitive<const float&>: boost::mpl::bool_<true> {};
251 template<> struct is_primitive<const double&>: boost::mpl::bool_<true> {};
252 template<> struct is_primitive<const long double&>: boost::mpl::bool_<true> {};
253 template<> struct is_primitive<const bool&>: boost::mpl::bool_<true> {};
255 template<> struct is_primitive<const std::string&>: boost::mpl::bool_<true> {};
256 template<> struct is_primitive<std::string>: boost::mpl::bool_<true> {};
257 template<> struct is_primitive<const std::string>: boost::mpl::bool_<true> {};
260 template<class Direction> struct primitive_converter;
262 template<>
263 struct primitive_converter<cpp_to_lua>
265 void apply(lua_State* L, const luabind::object& v)
267 // if you hit this assert you are trying to return a value from one state into another lua state
268 assert((v.lua_state() == L) && "you cannot return a value from one lua state into another");
269 v.pushvalue();
271 void apply(lua_State* L, int v) { lua_pushnumber(L, v); }
272 void apply(lua_State* L, short v) { lua_pushnumber(L, v); }
273 void apply(lua_State* L, char v) { lua_pushnumber(L, v); }
274 void apply(lua_State* L, long v) { lua_pushnumber(L, v); }
275 void apply(lua_State* L, unsigned int v) { lua_pushnumber(L, v); }
276 void apply(lua_State* L, unsigned short v) { lua_pushnumber(L, v); }
277 void apply(lua_State* L, unsigned char v) { lua_pushnumber(L, v); }
278 void apply(lua_State* L, unsigned long v) { lua_pushnumber(L, v); }
279 void apply(lua_State* L, float v) { lua_pushnumber(L, v); }
280 void apply(lua_State* L, double v) { lua_pushnumber(L, v); }
281 void apply(lua_State* L, long double v) { lua_pushnumber(L, v); }
282 void apply(lua_State* L, const char* v) { lua_pushstring(L, v); }
283 void apply(lua_State* L, const std::string& v) { lua_pushstring(L, v.c_str()); }
284 void apply(lua_State* L, bool b) { lua_pushboolean(L, b); }
287 template<>
288 struct primitive_converter<lua_to_cpp>
290 // TODO: add more
291 bool apply(lua_State* L, detail::by_value<bool>, int index) { return lua_toboolean(L, index) == 1; }
292 float apply(lua_State* L, detail::by_value<float>, int index) { return static_cast<float>(lua_tonumber(L, index)); }
293 double apply(lua_State* L, detail::by_value<double>, int index) { return static_cast<double>(lua_tonumber(L, index)); }
294 long double apply(lua_State* L, detail::by_value<long double>, int index) { return static_cast<long double>(lua_tonumber(L, index)); }
295 int apply(lua_State* L, detail::by_value<int>, int index) { return static_cast<int>(lua_tonumber(L, index)); }
296 short apply(lua_State* L, detail::by_value<short>, int index) { return static_cast<short>(lua_tonumber(L, index)); }
297 char apply(lua_State* L, detail::by_value<char>, int index) { return static_cast<char>(lua_tonumber(L, index)); }
298 long apply(lua_State* L, detail::by_value<long>, int index) { return static_cast<long>(lua_tonumber(L, index)); }
299 unsigned int apply(lua_State* L, detail::by_value<unsigned int>, int index) { return static_cast<unsigned int>(lua_tonumber(L, index)); }
300 unsigned short apply(lua_State* L, detail::by_value<unsigned short>, int index) { return static_cast<short>(lua_tonumber(L, index)); }
301 unsigned char apply(lua_State* L, detail::by_value<unsigned char>, int index) { return static_cast<char>(lua_tonumber(L, index)); }
302 unsigned long apply(lua_State* L, detail::by_value<unsigned long>, int index) { return static_cast<long>(lua_tonumber(L, index)); }
304 float apply(lua_State* L, detail::by_value<const float>, int index) { return static_cast<float>(lua_tonumber(L, index)); }
305 double apply(lua_State* L, detail::by_value<const double>, int index) { return static_cast<double>(lua_tonumber(L, index)); }
306 long double apply(lua_State* L, detail::by_value<const long double>, int index) {return static_cast<long double>(lua_tonumber(L, index)); }
307 int apply(lua_State* L, detail::by_value<const int>, int index) { return static_cast<int>(lua_tonumber(L, index)); }
308 short apply(lua_State* L, detail::by_value<const short>, int index) { return static_cast<short>(lua_tonumber(L, index)); }
309 char apply(lua_State* L, detail::by_value<const char>, int index) { return static_cast<char>(lua_tonumber(L, index)); }
310 long apply(lua_State* L, detail::by_value<const long>, int index) { return static_cast<long>(lua_tonumber(L, index)); }
312 unsigned int apply(lua_State* L, detail::by_value<const unsigned int>, int index) { return static_cast<int>(lua_tonumber(L, index)); }
313 unsigned short apply(lua_State* L, detail::by_value<const unsigned short>, int index) { return static_cast<short>(lua_tonumber(L, index)); }
314 unsigned char apply(lua_State* L, detail::by_value<const unsigned char>, int index) { return static_cast<char>(lua_tonumber(L, index)); }
315 unsigned long apply(lua_State* L, detail::by_value<const unsigned long>, int index) { return static_cast<long>(lua_tonumber(L, index)); }
317 std::string apply(lua_State* L, detail::by_value<std::string>, int index) { return static_cast<const char*>(lua_tostring(L, index)); }
318 const std::string apply(lua_State* L, detail::by_value<const std::string>, int index) { return static_cast<const char*>(lua_tostring(L, index)); }
320 luabind::object apply(lua_State* L, detail::by_value<luabind::object>, int index)
322 lua_pushvalue(L, index);
323 return luabind::object(L, detail::ref(L), true/*luabind::object::reference()*/);
326 const luabind::object apply(lua_State* L, detail::by_value<const luabind::object>, int index)
328 lua_pushvalue(L, index);
329 return luabind::object(L, detail::ref(L), true/*luabind::object::reference()*/);
332 // TODO: add more
334 template<class T>
335 T apply(lua_State* L, detail::by_const_reference<T>, int index) { return apply(L, detail::by_value<T>(), index); }
337 const char* apply(lua_State* L, detail::by_const_pointer<char>, int index) { return static_cast<const char*>(lua_tostring(L, index)); }
339 // matchers
340 static int match(lua_State* L, detail::by_value<bool>, int index) { if (lua_type(L, index) == LUA_TBOOLEAN) return 0; else return -1;}
341 static int match(lua_State* L, detail::by_value<float>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
342 static int match(lua_State* L, detail::by_value<double>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
343 static int match(lua_State* L, detail::by_value<long double>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
344 static int match(lua_State* L, detail::by_value<int>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
345 static int match(lua_State* L, detail::by_value<short>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
346 static int match(lua_State* L, detail::by_value<char>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
347 static int match(lua_State* L, detail::by_value<long>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
348 static int match(lua_State* L, detail::by_value<unsigned int>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
349 static int match(lua_State* L, detail::by_value<unsigned short>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
350 static int match(lua_State* L, detail::by_value<unsigned char>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
351 static int match(lua_State* L, detail::by_value<unsigned long>, int index) { if (lua_type(L, index) == LUA_TNUMBER) return 0; else return -1;}
352 static int match(lua_State* L, detail::by_value<std::string>, int index) { if (lua_type(L, index) == LUA_TSTRING) return 0; else return -1;}
353 static int match(lua_State* L, detail::by_value<const std::string>, int index) { if (lua_type(L, index) == LUA_TSTRING) return 0; else return -1;}
354 static int match(lua_State*, detail::by_value<luabind::object>, int) { return std::numeric_limits<int>::max() - 1; }
355 static int match(lua_State*, detail::by_value<const luabind::object>, int) { return std::numeric_limits<int>::max() - 1; }
357 static int match(lua_State* L, by_const_pointer<char>, int index) { if (lua_type(L, index) == LUA_TSTRING) return 0; else return -1;}
358 static int match(lua_State* L, by_const_pointer<const char>, int index) { if (lua_type(L, index) == LUA_TSTRING) return 0; else return -1;}
360 template<class T>
361 static int match(lua_State* L, detail::by_const_reference<T>, int index) { return match(L, detail::by_value<T>(), index); }
363 template<class T>
364 void converter_postcall(lua_State*, T, int) {}
369 // *********** default converters ***************
372 // ********** user defined converter ***********
374 template<class Direction> struct user_defined_converter;
376 template<>
377 struct user_defined_converter<lua_to_cpp>
379 template<class T>
380 T apply(lua_State* L, detail::by_value<T>, int index)
382 // std::cerr << "user_defined_converter\n";
383 return converters::convert_lua_to_cpp(L, detail::by_value<T>(), index);
386 template<class T>
387 T apply(lua_State* L, detail::by_reference<T>, int index)
389 // std::cerr << "user_defined_converter\n";
390 return converters::convert_lua_to_cpp(L, detail::by_reference<T>(), index);
393 template<class T>
394 T apply(lua_State* L, detail::by_const_reference<T>, int index)
396 // std::cerr << "user_defined_converter\n";
397 return converters::convert_lua_to_cpp(L, detail::by_const_reference<T>(), index);
400 template<class T>
401 T* apply(lua_State* L, detail::by_pointer<T>, int index)
403 // std::cerr << "user_defined_converter\n";
404 return converters::convert_lua_to_cpp(L, detail::by_pointer<T>(), index);
407 template<class T>
408 const T* apply(lua_State* L, detail::by_const_pointer<T>, int index)
410 // std::cerr << "user_defined_converter\n";
411 return converters::convert_lua_to_cpp(L, detail::by_pointer<T>(), index);
414 template<class T>
415 static int match(lua_State* L, T, int index)
417 return converters::match_lua_to_cpp(L, T(), index);
420 template<class T>
421 void converter_postcall(lua_State*, T, int) {}
424 template<>
425 struct user_defined_converter<cpp_to_lua>
427 template<class T>
428 void apply(lua_State* L, const T& v)
430 converters::convert_cpp_to_lua(L, v);
434 // ********** pointer converter ***********
437 template<class Direction> struct pointer_converter;
439 template<>
440 struct pointer_converter<cpp_to_lua>
442 template<class T>
443 void apply(lua_State* L, T* ptr)
445 if (ptr == 0)
447 lua_pushnil(L);
448 return;
451 class_registry* registry = class_registry::get_registry(L);
452 class_rep* crep = registry->find_class(LUABIND_TYPEID(T));
454 // if you get caught in this assert you are
455 // trying to use an unregistered type
456 assert(crep && "you are trying to use an unregistered type");
458 // create the struct to hold the object
459 void* obj = lua_newuserdata(L, sizeof(object_rep));
460 //new(obj) object_rep(ptr, crep, object_rep::owner, destructor_s<T>::apply);
461 new(obj) object_rep(ptr, crep, 0, 0);
463 // set the meta table
464 detail::getref(L, crep->metatable_ref());
465 lua_setmetatable(L, -2);
469 template<class T> struct make_pointer { typedef T* type; };
470 template<>
471 struct pointer_converter<lua_to_cpp>
473 template<class T>
474 typename make_pointer<T>::type apply(lua_State* L, by_pointer<T>, int index)
476 // preconditions:
477 // lua_isuserdata(L, index);
478 // getmetatable().__lua_class is true
479 // object_rep->flags() & object_rep::constant == 0
481 if (lua_isnil(L, index)) return 0;
483 int offset = 0;
484 object_rep* obj = static_cast<object_rep*>(lua_touserdata(L, index));
485 assert((obj != 0) && "internal error, please report"); // internal error
486 const class_rep* crep = obj->crep();
488 int steps = implicit_cast(crep, LUABIND_TYPEID(T), offset);
490 // should never be called with a type that can't be cast
491 assert((steps >= 0) && "internal error, please report");
493 if (LUABIND_TYPE_INFO_EQUAL(LUABIND_TYPEID(T), crep->holder_type()))
495 // if the type we are trying to convert to is the holder_type
496 // it means that his crep has a holder_type (since it would have
497 // been invalid otherwise, and T cannot be invalid). It also means
498 // that we need no conversion, since the holder_type is what the
499 // object points to.
500 return reinterpret_cast<T*>(obj->ptr());
503 void* raw_pointer;
505 if (crep->extract_ptr())
507 // this means that we have a holder type where the
508 // raw-pointer needs to be extracted
509 raw_pointer = crep->extract_ptr()(obj->ptr());
511 else
513 raw_pointer = obj->ptr();
516 T* ptr = reinterpret_cast<T*>(static_cast<char*>(raw_pointer) + offset);
517 // std::cerr << "pointer_converter<lua_to_cpp>: " << ptr << " " << offset << "\n";
519 return ptr;
522 template<class T>
523 static int match(lua_State* L, by_pointer<T>, int index)
525 if (lua_isnil(L, index)) return 0;
526 object_rep* obj = is_class_object(L, index);
527 if (obj == 0) return -1;
528 // cannot cast a constant object to nonconst
529 if (obj->flags() & object_rep::constant) return -1;
530 int d;
531 return implicit_cast(obj->crep(), LUABIND_TYPEID(T), d);
534 template<class T>
535 void converter_postcall(lua_State*, T, int) {}
538 // ******* value converter *******
540 template<class Direction> struct value_converter;
542 template<>
543 struct value_converter<cpp_to_lua>
545 template<class T>
546 void apply(lua_State* L, const T& ref)
548 class_registry* registry = class_registry::get_registry(L);
549 class_rep* crep = registry->find_class(LUABIND_TYPEID(T));
551 // if you get caught in this assert you are
552 // trying to use an unregistered type
553 assert(crep && "you are trying to use an unregistered type");
555 T* copied_obj = new T(ref);
557 // std::cerr << "value_converter<cpp_to_lua>: " << copied_obj << "\n";
559 // create the struct to hold the object
560 void* obj = lua_newuserdata(L, sizeof(object_rep));
561 // we send 0 as destructor since we know it will never be called
562 new(obj) object_rep(copied_obj, crep, object_rep::owner, delete_s<T>::apply);
564 // set the meta table
565 detail::getref(L, crep->metatable_ref());
566 lua_setmetatable(L, -2);
571 template<class T> struct make_const_reference { typedef const T& type; };
573 template<>
574 struct value_converter<lua_to_cpp>
576 template<class T>
577 typename make_const_reference<T>::type apply(lua_State* L, by_value<T>, int index)
579 // preconditions:
580 // lua_isuserdata(L, index);
581 // getmetatable().__lua_class is true
582 // object_rep->flags() & object_rep::constant == 0
584 assert((lua_isnil(L, index) == false) && "internal error, please report");
586 int offset = 0;
587 object_rep* obj = static_cast<object_rep*>(lua_touserdata(L, index));
588 assert((obj != 0) && "internal error, please report"); // internal error
589 const class_rep* crep = obj->crep();
591 int steps = implicit_cast(crep, LUABIND_TYPEID(T), offset);
593 // should never be called with a type that can't be cast
594 assert((steps >= 0) && "internal error, please report");
596 T* ptr = reinterpret_cast<T*>(obj->ptr(offset));
598 return *ptr;
601 template<class T>
602 static int match(lua_State* L, by_value<T>, int index)
604 if (lua_isnil(L, index)) return 0;
605 object_rep* obj = is_class_object(L, index);
606 if (obj == 0) return -1;
607 int d;
608 return implicit_cast(obj->crep(), LUABIND_TYPEID(T), d);
611 template<class T>
612 void converter_postcall(lua_State*, T, int) {}
615 // ******* const pointer converter *******
617 template<class Direction> struct const_pointer_converter;
619 template<>
620 struct const_pointer_converter<cpp_to_lua>
622 template<class T>
623 void apply(lua_State* L, const T* ptr)
625 if (ptr == 0)
627 lua_pushnil(L);
628 return;
631 class_registry* registry = class_registry::get_registry(L);
632 class_rep* crep = registry->find_class(LUABIND_TYPEID(T));
634 // if you get caught in this assert you are
635 // trying to use an unregistered type
636 assert(crep && "you are trying to use an unregistered type");
638 // create the struct to hold the object
639 void* obj = lua_newuserdata(L, sizeof(object_rep));
640 assert(obj && "internal error, please report");
641 // we send 0 as destructor since we know it will never be called
642 new(obj) object_rep(const_cast<T*>(ptr), crep, object_rep::constant, 0);
644 // set the meta table
645 detail::getref(L, crep->metatable_ref());
646 lua_setmetatable(L, -2);
651 template<class T> struct make_const_pointer { typedef const T* type; };
652 template<>
653 struct const_pointer_converter<lua_to_cpp>
655 template<class T>
656 typename make_const_pointer<T>::type apply(lua_State* L, by_const_pointer<T>, int index)
658 // std::cerr << "const_pointer_converter\n";
659 return pointer_converter<lua_to_cpp>().apply(L, by_pointer<T>(), index);
662 template<class T>
663 static int match(lua_State* L, by_const_pointer<T>, int index)
665 if (lua_isnil(L, index)) return 0;
666 object_rep* obj = is_class_object(L, index);
667 if (obj == 0) return -1; // if the type is not one of our own registered types, classify it as a non-match
668 int d;
669 return implicit_cast(obj->crep(), LUABIND_TYPEID(T), d);
672 template<class T>
673 void converter_postcall(lua_State*, T, int) {}
676 // ******* reference converter *******
678 template<class Direction> struct ref_converter;
680 template<>
681 struct ref_converter<cpp_to_lua>
683 template<class T>
684 void apply(lua_State* L, T& ref)
686 class_registry* registry = class_registry::get_registry(L);
687 class_rep* crep = registry->find_class(LUABIND_TYPEID(T));
689 // if you get caught in this assert you are
690 // trying to use an unregistered type
691 assert(crep && "you are trying to use an unregistered type");
693 T* ptr = &ref;
695 // create the struct to hold the object
696 void* obj = lua_newuserdata(L, sizeof(object_rep));
697 assert(obj && "internal error, please report");
698 new(obj) object_rep(ptr, crep, 0, 0);
700 // set the meta table
701 detail::getref(L, crep->metatable_ref());
702 lua_setmetatable(L, -2);
706 template<class T> struct make_reference { typedef T& type; };
707 template<>
708 struct ref_converter<lua_to_cpp>
710 template<class T>
711 typename make_reference<T>::type apply(lua_State* L, by_reference<T>, int index)
713 // std::cerr << "ref_converter<lua_to_cpp>\n";
714 return *pointer_converter<lua_to_cpp>().apply(L, by_pointer<T>(), index);
717 template<class T>
718 static int match(lua_State* L, by_reference<T>, int index)
720 return pointer_converter<lua_to_cpp>::match(L, by_pointer<T>(), index);
723 template<class T>
724 void converter_postcall(lua_State*, T, int) {}
727 // ******** const reference converter *********
729 template<class Direction> struct const_ref_converter;
731 template<>
732 struct const_ref_converter<cpp_to_lua>
734 template<class T>
735 void apply(lua_State* L, const T& ref)
737 class_registry* registry = class_registry::get_registry(L);
738 class_rep* crep = registry->find_class(LUABIND_TYPEID(T));
740 // if you get caught in this assert you are
741 // trying to use an unregistered type
742 assert(crep && "you are trying to use an unregistered type");
744 T* ptr = const_cast<T*>(&ref);
746 // std::cerr << "const_ref_converter<cpp_to_lua>: " << ptr << "\n";
748 // create the table to hold the object
749 object_rep* obj = static_cast<object_rep*>(lua_newuserdata(L, sizeof(object_rep)));
750 assert(obj && "internal error, please report");
751 new(obj) object_rep(ptr, crep, object_rep::constant, 0);
753 // set the meta table
754 detail::getref(L, crep->metatable_ref());
755 lua_setmetatable(L, -2);
759 template<>
760 struct const_ref_converter<lua_to_cpp>
762 template<class T>
763 typename make_const_reference<T>::type apply(lua_State* L, by_const_reference<T>, int index)
765 // std::cerr << "const_ref_converter<lua_to_cpp>\n";
766 return *const_pointer_converter<lua_to_cpp>().apply(L, by_const_pointer<T>(), index);
769 template<class T>
770 static int match(lua_State* L, by_const_reference<T>, int index)
772 return const_pointer_converter<lua_to_cpp>::match(L, by_const_pointer<T>(), index);
775 template<class T>
776 void converter_postcall(lua_State*, T, int) {}
779 // ****** enum converter ********
781 template<class Direction = cpp_to_lua>
782 struct enum_converter;
784 template<>
785 struct enum_converter<lua_to_cpp>
787 template<class T>
788 T apply(lua_State* L, by_value<T>, int index)
790 // std::cerr << "enum_converter\n";
791 return static_cast<T>(static_cast<int>(lua_tonumber(L, index)));
794 template<class T>
795 static int match(lua_State* L, by_value<T>, int index)
797 if (lua_isnumber(L, index)) return 0; else return -1;
800 template<class T>
801 void converter_postcall(lua_State*, T, int) {}
804 // ****** functor converter ********
806 template<class Direction> struct functor_converter;
808 template<>
809 struct functor_converter<lua_to_cpp>
811 template<class T>
812 functor<T> apply(lua_State* L, by_const_reference<functor<T> >, int index)
814 lua_pushvalue(L, index);
815 int ref = detail::ref(L);
816 return functor<T>(L, ref);
819 template<class T>
820 functor<T> apply(lua_State* L, by_value<functor<T> >, int index)
822 lua_pushvalue(L, index);
823 int ref = detail::ref(L);
824 return functor<T>(L, ref);
827 template<class T>
828 static int match(lua_State* L, by_const_reference<functor<T> >, int index)
830 if (lua_isfunction(L, index)) return 0; else return -1;
833 template<class T>
834 static int match(lua_State* L, by_value<functor<T> >, int index)
836 if (lua_isfunction(L, index)) return 0; else return -1;
839 template<class T>
840 void converter_postcall(lua_State*, T, int) {}
847 // *********** default_policy *****************
851 struct default_policy : converter_policy_tag
853 BOOST_STATIC_CONSTANT(bool, has_arg = true);
855 template<class T>
856 static void precall(lua_State*, T, int) {}
858 // template<class T>
859 // static void postcall(lua_State*, T, int) {}
861 template<class T, class Direction>
862 struct generate_converter
864 typedef typename boost::mpl::if_<is_user_defined<T>
865 , user_defined_converter<Direction>
866 , typename boost::mpl::if_<is_primitive<T>
867 , primitive_converter<Direction>
868 , typename boost::mpl::if_<is_lua_functor<T>
869 , functor_converter<Direction>
870 , typename boost::mpl::if_<boost::is_enum<T>
871 , enum_converter<Direction>
872 , typename boost::mpl::if_<is_nonconst_pointer<T>
873 , pointer_converter<Direction>
874 , typename boost::mpl::if_<is_const_pointer<T>
875 , const_pointer_converter<Direction>
876 , typename boost::mpl::if_<is_nonconst_reference<T>
877 , ref_converter<Direction>
878 , typename boost::mpl::if_<is_const_reference<T>
879 , const_ref_converter<Direction>
880 , value_converter<Direction>
881 >::type>::type>::type>::type>::type>::type>::type>::type type;
885 // ********** get policy **********
887 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
888 template<int N, class T>
889 struct get_policy_list_impl
891 template<class U>
892 struct inner
894 typedef typename U::head head;
895 typedef typename U::tail tail;
897 typedef typename boost::mpl::if_<boost::mpl::equal_to<boost::mpl::integral_c<int, head::index>, boost::mpl::integral_c<int, N> >
898 , policy_cons<head, typename get_policy_list_impl<N, tail>::type>
899 , typename get_policy_list_impl<N, tail>::type
900 >::type type;
903 template<>
904 struct inner<null_type>
906 typedef null_type type;
909 typedef typename inner<T>::type type;
911 #else
912 template<class List>
913 struct get_policy_list_impl
915 template<int N>
916 struct apply
918 typedef typename List::head head;
919 typedef typename List::tail tail;
921 typedef typename boost::mpl::if_<boost::mpl::equal_to<boost::mpl::integral_c<int, head::index>, boost::mpl::integral_c<int, N> >
922 , policy_cons<head, typename get_policy_list_impl<tail>::template apply<N>::type>
923 , typename get_policy_list_impl<tail>::template apply<N>::type
924 >::type type;
928 template<>
929 struct get_policy_list_impl<detail::null_type>
931 template<int N>
932 struct apply
934 typedef null_type type;
937 #endif
939 template<int N, class T>
940 struct get_policy_list
942 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
943 typedef typename get_policy_list_impl<N, T>::type type;
944 #else
945 typedef typename get_policy_list_impl<T>::template apply<N>::type type;
946 #endif
949 // ============== new policy system =================
951 template<int, class> struct find_conversion_policy;
953 template<bool IsConverter = false>
954 struct find_conversion_impl
956 template<int N, class Policies>
957 struct apply
959 typedef typename find_conversion_policy<N, typename Policies::tail>::type type;
963 template<>
964 struct find_conversion_impl<true>
966 template<int N, class Policies>
967 struct apply
969 typedef typename Policies::head head;
970 typedef typename Policies::tail tail;
972 BOOST_STATIC_CONSTANT(bool, found = (N == head::index));
974 typedef typename
975 boost::mpl::if_c<found
976 , head
977 , typename find_conversion_policy<N, tail>::type
978 >::type type;
982 template<class Policies>
983 struct find_conversion_impl2
985 template<int N>
986 struct apply
987 : find_conversion_impl<
988 boost::is_base_and_derived<conversion_policy_base, typename Policies::head>::value
989 >::template apply<N, Policies>
994 template<>
995 struct find_conversion_impl2<detail::null_type>
997 template<int N>
998 struct apply
1000 typedef default_policy type;
1004 template<int N, class Policies>
1005 struct find_conversion_policy : find_conversion_impl2<Policies>::template apply<N>
1009 template<class List>
1010 struct policy_list_postcall
1012 typedef typename List::head head;
1013 typedef typename List::tail tail;
1015 static void apply(lua_State* L, const index_map& i)
1017 head::postcall(L, i);
1018 policy_list_postcall<tail>::apply(L, i);
1022 template<>
1023 struct policy_list_postcall<detail::null_type>
1025 static void apply(lua_State*, const index_map&) {}
1028 /* template<int N>
1029 struct find_conversion_policy<N, detail::null_type>
1031 typedef default_policy type;
1032 };*/
1034 // ==================================================
1036 // ************** precall and postcall on policy_cons *********************
1039 template<class List>
1040 struct policy_precall
1042 typedef typename List::head head;
1043 typedef typename List::tail tail;
1045 static void apply(lua_State* L, int index)
1047 head::precall(L, index);
1048 policy_precall<tail>::apply(L, index);
1052 template<>
1053 struct policy_precall<detail::null_type>
1055 static void apply(lua_State*, int) {}
1058 template<class List>
1059 struct policy_postcall
1061 typedef typename List::head head;
1062 typedef typename List::tail tail;
1064 static void apply(lua_State* L, int index)
1066 head::postcall(L, index);
1067 policy_postcall<tail>::apply(L, index);
1071 template<>
1072 struct policy_postcall<detail::null_type>
1074 static void apply(lua_State*, int) {}
1078 struct pointer_only_converter
1080 template<class T>
1081 static const T* apply(lua_State* L, type<const T*>, int index)
1083 int a = index;
1087 struct only_one_converter_policy_can_be_used_per_index {};
1089 template<class List, class T> struct assert_converter_policy_impl;
1091 template<class List>
1092 struct assert_converter_policy
1094 template<class T>
1095 struct apply
1097 typedef typename boost::mpl::if_<boost::is_base_and_derived<converter_policy_tag, typename List::head>
1098 , only_one_converter_policy_can_be_used_per_index
1099 , typename assert_converter_policy_impl<typename List::tail, T>::type
1100 >::type type;
1104 template<>
1105 struct assert_converter_policy<detail::null_type>
1107 template<class T>
1108 struct apply
1110 typedef T type;
1114 template<class List, class T>
1115 struct assert_converter_policy_impl
1117 typedef typename assert_converter_policy<List>::template apply<T>::type type;
1120 template<class List>
1121 struct find_converter_policy_impl
1123 typedef typename List::head head;
1124 typedef typename List::tail tail;
1126 typedef typename boost::mpl::if_<boost::is_base_and_derived<converter_policy_tag, head>
1127 , typename assert_converter_policy_impl<tail, head>::type
1128 , typename find_converter_policy_impl<tail>::type
1129 >::type type;
1132 template<>
1133 struct find_converter_policy_impl<detail::null_type>
1135 typedef default_policy type;
1138 /* template<class List>
1139 struct find_converter_policy
1140 : find_converter_policy_impl<List>
1148 namespace luabind { namespace
1150 LUABIND_ANONYMOUS_FIX boost::arg<0> return_value;
1151 LUABIND_ANONYMOUS_FIX boost::arg<0> result;
1154 #include <luabind/detail/object_funs.hpp>
1156 #endif // LUABIND_POLICY_HPP_INCLUDED