My Project
format_signature.hpp
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)
4 
5 #ifndef LUABIND_FORMAT_SIGNATURE_081014_HPP
6 # define LUABIND_FORMAT_SIGNATURE_081014_HPP
7 
8 # include <luabind/config.hpp>
9 # include <luabind/lua_include.hpp>
10 # include <luabind/typeid.hpp>
11 
12 # include <boost/mpl/begin_end.hpp>
13 # include <boost/mpl/next.hpp>
14 # include <boost/mpl/size.hpp>
15 
16 namespace luabind { namespace adl
17 {
18 
19  class object;
20  class argument;
21  template <class Base>
22  struct table;
23 
24 } // namespace adl
25 
26 using adl::object;
27 using adl::argument;
28 using adl::table;
29 
30 } // namespace luabind
31 
32 namespace luabind { namespace detail {
33 
34 LUABIND_API std::string get_class_name(lua_State* L, type_id const& i);
35 
36 template <class T>
38 {
39  static void get(lua_State* L)
40  {
41  lua_pushstring(L, get_class_name(L, typeid(T)).c_str());
42  }
43 };
44 
45 template <class T>
46 struct type_to_string<T*>
47 {
48  static void get(lua_State* L)
49  {
51  lua_pushstring(L, "*");
52  lua_concat(L, 2);
53  }
54 };
55 
56 template <class T>
57 struct type_to_string<T&>
58 {
59  static void get(lua_State* L)
60  {
62  lua_pushstring(L, "&");
63  lua_concat(L, 2);
64  }
65 };
66 
67 template <class T>
68 struct type_to_string<T const>
69 {
70  static void get(lua_State* L)
71  {
73  lua_pushstring(L, " const");
74  lua_concat(L, 2);
75  }
76 };
77 
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  } \
86  };
87 
88 # define LUABIND_INTEGRAL_TYPE_TO_STRING(x) \
89  LUABIND_TYPE_TO_STRING(x) \
90  LUABIND_TYPE_TO_STRING(unsigned x)
91 
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)
96 
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)
101 
102 LUABIND_TYPE_TO_STRING(luabind::object)
103 LUABIND_TYPE_TO_STRING(luabind::argument)
104 
105 # undef LUABIND_INTEGRAL_TYPE_TO_STRING
106 # undef LUABIND_TYPE_TO_STRING
107 
108 template <class Base>
109 struct type_to_string<table<Base> >
110 {
111  static void get(lua_State* L)
112  {
113  lua_pushstring(L, "table");
114  }
115 };
116 
117 # ifndef LUABIND_CPP0x
118 
119 template <class End>
120 void format_signature_aux(lua_State*, bool, End, End)
121 {}
122 
123 template <class Iter, class End>
124 void format_signature_aux(lua_State* L, bool first, Iter, End end)
125 {
126  if (!first)
127  lua_pushstring(L, ",");
129  format_signature_aux(L, false, typename mpl::next<Iter>::type(), end);
130 }
131 
132 template <class Signature>
133 void format_signature(lua_State* L, char const* function, Signature)
134 {
135  typedef typename mpl::begin<Signature>::type first;
136 
138 
139  lua_pushstring(L, " ");
140  lua_pushstring(L, function);
141 
142  lua_pushstring(L, "(");
143  format_signature_aux(
144  L
145  , true
146  , typename mpl::next<first>::type()
147  , typename mpl::end<Signature>::type()
148  );
149  lua_pushstring(L, ")");
150 
151  lua_concat(L, static_cast<int>(mpl::size<Signature>()) * 2 + 2);
152 }
153 
154 # else // LUABIND_CPP0x
155 
156 inline void format_signature_aux(lua_State*, vector<>, bool)
157 {}
158 
159 template <class T, class... Args>
160 void format_signature_aux(lua_State* L, vector<T, Args...>, bool first = true)
161 {
162  if (!first)
163  lua_pushstring(L, ",");
165  format_signature_aux(L, vector<Args...>(), false);
166 }
167 
168 template <class R, class... Args>
169 void format_signature(lua_State* L, char const* function, vector<R, Args...>)
170 {
172 
173  lua_pushstring(L, " ");
174  lua_pushstring(L, function);
175 
176  lua_pushstring(L, "(");
177  format_signature_aux(L, vector<R, Args...>());
178  lua_pushstring(L, ")");
179 
180  lua_concat(L, (sizeof...(Args) + 1) * 2 + 2);
181 }
182 
183 # endif // LUABIND_CPP0x
184 
185 }} // namespace luabind::detail
186 
187 #endif // LUABIND_FORMAT_SIGNATURE_081014_HPP
188 
Definition: typeid.hpp:22
Definition: format_signature.hpp:22
Definition: object.hpp:749
Definition: minilua.c:461
Definition: object.hpp:816
Definition: PEtypes.h:507
Definition: format_signature.hpp:37