Fix bug when passing type with holder by value to Lua.
[luabind.git] / luabind / detail / convert_to_lua.hpp
blobf5aa89aea7263a8a121ade3cce378a4ade19bb81
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_CONVERT_TO_LUA_HPP_INCLUDED
25 #define LUABIND_CONVERT_TO_LUA_HPP_INCLUDED
27 #include <luabind/config.hpp>
28 #include <luabind/detail/policy.hpp>
29 #include <boost/ref.hpp>
31 #include <boost/mpl/apply_wrap.hpp>
33 namespace luabind { namespace detail
35 template<bool IsReferenceWrapper = false>
36 struct unwrap_ref
38 template<class T>
39 static const T& get(const T& r) { return r; }
41 template<class T>
42 struct apply
44 typedef T type;
48 template<>
49 struct unwrap_ref<true>
51 template<class T>
52 static T& get(const boost::reference_wrapper<T>& r) { return r.get(); }
54 template<class T>
55 struct apply
57 typedef typename T::type& type;
61 namespace mpl = boost::mpl;
63 template<class T>
64 void convert_to_lua(lua_State* L, const T& v)
66 typedef typename mpl::apply_wrap1<
67 unwrap_ref<boost::is_reference_wrapper<T>::value>
68 , T
69 >::type value_type;
71 typename mpl::apply_wrap2<default_policy,value_type,cpp_to_lua>::type converter;
73 converter.apply(L, unwrap_ref<boost::is_reference_wrapper<T>::value>::get(v));
76 template<int Index, class T, class Policies>
77 void convert_to_lua_p(lua_State* L, const T& v, const Policies&)
79 typedef typename mpl::apply_wrap1<
80 unwrap_ref<boost::is_reference_wrapper<T>::value>
81 , T
82 >::type value_type;
84 typedef typename find_conversion_policy<Index, Policies>::type converter_policy;
85 typename mpl::apply_wrap2<converter_policy,value_type,cpp_to_lua>::type converter;
87 converter.apply(L, unwrap_ref<boost::is_reference_wrapper<T>::value>::get(v));
91 #endif