OSDN Git Service

build moflib1.0 on cmake-base system
[moflib/moflib.git] / extlib / luabind-0.8 / luabind / detail / convert_to_lua.hpp
1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
2
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:
9
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
12
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.
22
23
24 #ifndef LUABIND_CONVERT_TO_LUA_HPP_INCLUDED
25 #define LUABIND_CONVERT_TO_LUA_HPP_INCLUDED
26
27 #include <luabind/config.hpp>
28 #include <luabind/detail/policy.hpp>
29 #include <boost/ref.hpp>
30
31 #include <boost/mpl/apply_wrap.hpp>
32
33 namespace luabind { namespace detail
34 {
35         template<bool IsReferenceWrapper = false>
36         struct unwrap_ref
37         {
38                 template<class T>
39                 static const T& get(const T& r) { return r; }
40
41                 template<class T>
42                 struct apply
43                 {
44                         typedef T type;
45                 };
46         };
47
48         template<>
49         struct unwrap_ref<true>
50         {
51                 template<class T>
52                 static T& get(const boost::reference_wrapper<T>& r) { return r.get(); }
53
54                 template<class T>
55                 struct apply
56                 {
57                         typedef typename T::type& type;
58                 };
59         };
60
61         namespace mpl = boost::mpl;
62         
63         template<class T>
64         void convert_to_lua(lua_State* L, const T& v)
65         {
66                 typedef typename mpl::apply_wrap1<
67                         unwrap_ref<boost::is_reference_wrapper<T>::value>
68                   , T
69                 >::type value_type;
70
71                 typename mpl::apply_wrap2<default_policy,value_type,cpp_to_lua>::type converter;
72
73                 converter.apply(L, unwrap_ref<boost::is_reference_wrapper<T>::value>::get(v));
74         }
75
76         template<int Index, class T, class Policies>
77         void convert_to_lua_p(lua_State* L, const T& v, const Policies&)
78         {
79                 typedef typename mpl::apply_wrap1<
80                         unwrap_ref<boost::is_reference_wrapper<T>::value>
81                   , T
82                 >::type value_type;
83
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;
86
87                 converter.apply(L, unwrap_ref<boost::is_reference_wrapper<T>::value>::get(v));
88         }
89 }}
90
91 #endif
92