Initial C++0x support.
[luabind.git] / luabind / detail / format_signature.hpp
blobc159dd40a6765245d1ee9de4478b8d6249a1d276
1 // Copyright Daniel Wallin 2008. Use, modification and distribution is
2 // subject to the Boost Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #ifndef LUABIND_FORMAT_SIGNATURE_081014_HPP
6 # define LUABIND_FORMAT_SIGNATURE_081014_HPP
8 # include <luabind/config.hpp>
9 # include <luabind/lua_include.hpp>
10 # include <luabind/typeid.hpp>
12 # include <boost/mpl/begin_end.hpp>
13 # include <boost/mpl/next.hpp>
14 # include <boost/mpl/size.hpp>
16 namespace luabind { namespace adl
19 class object;
20 class argument;
21 template <class Base>
22 struct table;
24 } // namespace adl
26 using adl::object;
27 using adl::argument;
28 using adl::table;
30 } // namespace luabind
32 namespace luabind { namespace detail {
34 LUABIND_API std::string get_class_name(lua_State* L, type_id const& i);
36 template <class T>
37 struct type_to_string
39 static void get(lua_State* L)
41 lua_pushstring(L, get_class_name(L, typeid(T)).c_str());
45 template <class T>
46 struct type_to_string<T*>
48 static void get(lua_State* L)
50 type_to_string<T>::get(L);
51 lua_pushstring(L, "*");
52 lua_concat(L, 2);
56 template <class T>
57 struct type_to_string<T&>
59 static void get(lua_State* L)
61 type_to_string<T>::get(L);
62 lua_pushstring(L, "&");
63 lua_concat(L, 2);
67 template <class T>
68 struct type_to_string<T const>
70 static void get(lua_State* L)
72 type_to_string<T>::get(L);
73 lua_pushstring(L, " const");
74 lua_concat(L, 2);
78 # define LUABIND_TYPE_TO_STRING(x) \
79 template <> \
80 struct type_to_string<x> \
81 { \
82 static void get(lua_State* L) \
83 { \
84 lua_pushstring(L, #x); \
85 } \
88 # define LUABIND_INTEGRAL_TYPE_TO_STRING(x) \
89 LUABIND_TYPE_TO_STRING(x) \
90 LUABIND_TYPE_TO_STRING(unsigned x)
92 LUABIND_INTEGRAL_TYPE_TO_STRING(char)
93 LUABIND_INTEGRAL_TYPE_TO_STRING(short)
94 LUABIND_INTEGRAL_TYPE_TO_STRING(int)
95 LUABIND_INTEGRAL_TYPE_TO_STRING(long)
97 LUABIND_TYPE_TO_STRING(void)
98 LUABIND_TYPE_TO_STRING(bool)
99 LUABIND_TYPE_TO_STRING(std::string)
100 LUABIND_TYPE_TO_STRING(lua_State)
102 LUABIND_TYPE_TO_STRING(luabind::object)
103 LUABIND_TYPE_TO_STRING(luabind::argument)
105 # undef LUABIND_INTEGRAL_TYPE_TO_STRING
106 # undef LUABIND_TYPE_TO_STRING
108 template <class Base>
109 struct type_to_string<table<Base> >
111 static void get(lua_State* L)
113 lua_pushstring(L, "table");
117 # ifndef LUABIND_CPP0x
119 template <class End>
120 void format_signature_aux(lua_State*, bool, End, End)
123 template <class Iter, class End>
124 void format_signature_aux(lua_State* L, bool first, Iter, End end)
126 if (!first)
127 lua_pushstring(L, ",");
128 type_to_string<typename Iter::type>::get(L);
129 format_signature_aux(L, false, typename mpl::next<Iter>::type(), end);
132 template <class Signature>
133 void format_signature(lua_State* L, char const* function, Signature)
135 typedef typename mpl::begin<Signature>::type first;
137 type_to_string<typename first::type>::get(L);
139 lua_pushstring(L, " ");
140 lua_pushstring(L, function);
142 lua_pushstring(L, "(");
143 format_signature_aux(
145 , true
146 , typename mpl::next<first>::type()
147 , typename mpl::end<Signature>::type()
149 lua_pushstring(L, ")");
151 lua_concat(L, static_cast<int>(mpl::size<Signature>()) * 2 + 2);
154 # else // LUABIND_CPP0x
156 inline void format_signature_aux(lua_State*, vector<>, bool)
159 template <class T, class... Args>
160 void format_signature_aux(lua_State* L, vector<T, Args...>, bool first = true)
162 if (!first)
163 lua_pushstring(L, ",");
164 type_to_string<T>::get(L);
165 format_signature_aux(L, vector<Args...>(), false);
168 template <class R, class... Args>
169 void format_signature(lua_State* L, char const* function, vector<R, Args...>)
171 type_to_string<R>::get(L);
173 lua_pushstring(L, " ");
174 lua_pushstring(L, function);
176 lua_pushstring(L, "(");
177 format_signature_aux(L, vector<R, Args...>());
178 lua_pushstring(L, ")");
180 lua_concat(L, (sizeof...(Args) + 1) * 2 + 2);
183 # endif // LUABIND_CPP0x
185 }} // namespace luabind::detail
187 #endif // LUABIND_FORMAT_SIGNATURE_081014_HPP