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.
23 #ifndef LUABIND_NO_ERROR_CHECKING
25 #if !BOOST_PP_IS_ITERATING
27 #ifndef LUABIND_GET_SIGNATURE_HPP_INCLUDED
28 #define LUABIND_GET_SIGNATURE_HPP_INCLUDED
30 #include <boost/config.hpp>
31 #include <boost/preprocessor/repeat.hpp>
32 #include <boost/preprocessor/iteration/iterate.hpp>
33 #include <boost/preprocessor/repetition/enum.hpp>
34 #include <boost/preprocessor/repetition/enum_params.hpp>
35 #include <boost/preprocessor/punctuation/comma_if.hpp>
36 #include <boost/preprocessor/cat.hpp>
38 #include <luabind/config.hpp>
39 #include <luabind/detail/signature_match.hpp>
42 namespace luabind
{ namespace detail
48 static std::string
name(lua_State
* L
)
51 class_registry
* r
= class_registry::get_registry(L
);
52 std::map
<LUABIND_TYPE_INFO
, class_rep
*>::iterator i
= r
->classes
.find(LUABIND_TYPEID(T
));
53 if (i
== r
->classes
.end())
59 ret
+= i
->second
->name();
66 std::string
name_of_type(by_value
<T
>, lua_State
* L
) { return get_class_name
<T
>::name(L
); };
68 std::string
name_of_type(by_reference
<T
>, lua_State
* L
) { return get_class_name
<T
>::name(L
); };
70 std::string
name_of_type(by_pointer
<T
>, lua_State
* L
) { return get_class_name
<T
>::name(L
); };
72 std::string
name_of_type(by_const_reference
<T
>, lua_State
* L
) { return "const " + get_class_name
<T
>::name(L
); };
74 std::string
name_of_type(by_const_pointer
<T
>, lua_State
* L
) { return "const " + get_class_name
<T
>::name(L
); };
76 inline std::string
name_of_type(by_value
<luabind::object
>, lua_State
*) { return "object"; };
77 inline std::string
name_of_type(by_const_reference
<luabind::object
>, lua_State
*) { return "object"; };
78 inline std::string
name_of_type(by_value
<char>, lua_State
*) { return "number"; }
79 inline std::string
name_of_type(by_value
<short>, lua_State
*) { return "number"; }
80 inline std::string
name_of_type(by_value
<int>, lua_State
*) { return "number"; }
81 inline std::string
name_of_type(by_value
<long>, lua_State
*) { return "number"; }
82 inline std::string
name_of_type(by_value
<unsigned char>, lua_State
*) { return "number"; }
83 inline std::string
name_of_type(by_value
<unsigned short>, lua_State
*) { return "number"; }
84 inline std::string
name_of_type(by_value
<unsigned int>, lua_State
*) { return "number"; }
85 inline std::string
name_of_type(by_value
<unsigned long>, lua_State
*) { return "number"; }
87 inline std::string
name_of_type(by_value
<const char>, lua_State
*) { return "number"; }
88 inline std::string
name_of_type(by_value
<const short>, lua_State
*) { return "number"; }
89 inline std::string
name_of_type(by_value
<const int>, lua_State
*) { return "number"; }
90 inline std::string
name_of_type(by_value
<const long>, lua_State
*) { return "number"; }
91 inline std::string
name_of_type(by_value
<const unsigned char>, lua_State
*) { return "number"; }
92 inline std::string
name_of_type(by_value
<const unsigned short>, lua_State
*) { return "number"; }
93 inline std::string
name_of_type(by_value
<const unsigned int>, lua_State
*) { return "number"; }
94 inline std::string
name_of_type(by_value
<const unsigned long>, lua_State
*) { return "number"; }
96 inline std::string
name_of_type(by_value
<std::string
>, lua_State
*) { return "string"; }
97 inline std::string
name_of_type(by_const_pointer
<char>, lua_State
*) { return "string"; }
100 struct type_name_unless_void
102 inline static void apply(std::string
& s
, lua_State
* L
, bool first
)
104 if (!first
) s
+= ", ";
105 s
+= name_of_type(LUABIND_DECORATE_TYPE(T
), L
);
110 struct type_name_unless_void
<null_type
>
112 inline static void apply(std::string
&, lua_State
*, bool) {}
115 #define LUABIND_ADD_LUA_TYPE_NAME(z, n, _) type_name_unless_void<BOOST_PP_CAT(A, BOOST_PP_INC(n))>::apply(s, L, false);
117 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/detail/get_signature.hpp>, 1))
118 #include BOOST_PP_ITERATE()
121 struct get_member_signature
123 static inline void apply(lua_State
* L
, std::string
& s
)
125 get_member_signature_impl(static_cast<F
>(0), L
, s
);
130 struct get_free_function_signature
132 static inline void apply(lua_State
* L
, std::string
& s
)
134 get_free_function_signature_impl(static_cast<F
>(0), L
, s
);
142 static inline void apply(lua_State
* L
, std::string
& s
)
144 get_signature_impl(static_cast<const Sig
*>(0), L
, s
);
148 template<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
, class A
)>
149 inline void get_signature_impl(const constructor
<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
, A
)>*, lua_State
* L
, std::string
& s
)
152 type_name_unless_void
<A0
>::apply(s
, L
, true);
153 BOOST_PP_REPEAT(BOOST_PP_DEC(LUABIND_MAX_ARITY
), LUABIND_ADD_LUA_TYPE_NAME
, _
)
157 #undef LUABIND_ADD_LUA_TYPE_NAME
161 #endif // LUABIND_GET_SIGNATURE_HPP_INCLUDED
163 #elif BOOST_PP_ITERATION_FLAGS() == 1
166 template<class T
, class C
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
167 inline void get_member_signature_impl(T(C::*)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A
)), lua_State
* L
, std::string
& s
)
170 BOOST_PP_REPEAT(BOOST_PP_DEC(BOOST_PP_ITERATION()), LUABIND_ADD_LUA_TYPE_NAME
, _
)
171 #if BOOST_PP_ITERATION() > 0
172 s
+= name_of_type(LUABIND_DECORATE_TYPE(BOOST_PP_CAT(A
, BOOST_PP_DEC(BOOST_PP_ITERATION()))), L
);
174 // BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_ADD_LUA_TYPE_NAME, _)
178 template<class T
, class C
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
179 inline void get_member_signature_impl(T(C::*)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A
)) const, lua_State
* L
, std::string
& s
)
182 BOOST_PP_REPEAT(BOOST_PP_DEC(BOOST_PP_ITERATION()), LUABIND_ADD_LUA_TYPE_NAME
, _
)
183 #if BOOST_PP_ITERATION() > 0
184 s
+= name_of_type(LUABIND_DECORATE_TYPE(BOOST_PP_CAT(A
, BOOST_PP_DEC(BOOST_PP_ITERATION()))), L
);
186 // BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_ADD_LUA_TYPE_NAME, _)
190 template<class T
, class C
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
191 inline void get_member_signature_impl(T(*)(C
* obj
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A
)), lua_State
* L
, std::string
& s
)
194 BOOST_PP_REPEAT(BOOST_PP_DEC(BOOST_PP_ITERATION()), LUABIND_ADD_LUA_TYPE_NAME
, _
)
195 #if BOOST_PP_ITERATION() > 0
196 s
+= name_of_type(LUABIND_DECORATE_TYPE(BOOST_PP_CAT(A
, BOOST_PP_DEC(BOOST_PP_ITERATION()))), L
);
198 // BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_ADD_LUA_TYPE_NAME, _)
202 template<class T
, class C
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
203 inline void get_member_signature_impl(T(*f
)(const C
* obj
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A
)), lua_State
* L
, std::string
& s
)
206 BOOST_PP_REPEAT(BOOST_PP_DEC(BOOST_PP_ITERATION()), LUABIND_ADD_LUA_TYPE_NAME
, _
)
207 #if BOOST_PP_ITERATION() > 0
208 s
+= name_of_type(LUABIND_DECORATE_TYPE(BOOST_PP_CAT(A
, BOOST_PP_DEC(BOOST_PP_ITERATION()))), L
);
210 // BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_ADD_LUA_TYPE_NAME, _)
214 template<class T
, class C
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
215 inline void get_member_signature_impl(T(*f
)(C
& obj
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A
)), lua_State
* L
, std::string
& s
)
218 BOOST_PP_REPEAT(BOOST_PP_DEC(BOOST_PP_ITERATION()), LUABIND_ADD_LUA_TYPE_NAME
, _
)
219 #if BOOST_PP_ITERATION() > 0
220 s
+= name_of_type(LUABIND_DECORATE_TYPE(BOOST_PP_CAT(A
, BOOST_PP_DEC(BOOST_PP_ITERATION()))), L
);
222 // BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_ADD_LUA_TYPE_NAME, _)
226 template<class T
, class C
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
227 inline void get_member_signature_impl(T(*f
)(const C
& obj
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A
)), lua_State
* L
, std::string
& s
)
230 BOOST_PP_REPEAT(BOOST_PP_DEC(BOOST_PP_ITERATION()), LUABIND_ADD_LUA_TYPE_NAME
, _
)
231 #if BOOST_PP_ITERATION() > 0
232 s
+= name_of_type(LUABIND_DECORATE_TYPE(BOOST_PP_CAT(A
, BOOST_PP_DEC(BOOST_PP_ITERATION()))), L
);
234 // BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_ADD_LUA_TYPE_NAME, _)
239 template<class T
BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A
)>
240 inline void get_free_function_signature_impl(T(*f
)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A
)), lua_State
* L
, std::string
& s
)
243 BOOST_PP_REPEAT(BOOST_PP_DEC(BOOST_PP_ITERATION()), LUABIND_ADD_LUA_TYPE_NAME
, _
)
244 #if BOOST_PP_ITERATION() > 0
245 s
+= name_of_type(LUABIND_DECORATE_TYPE(BOOST_PP_CAT(A
, BOOST_PP_DEC(BOOST_PP_ITERATION()))), L
);
247 // BOOST_PP_REPEAT(BOOST_PP_ITERATION(), LUABIND_ADD_LUA_TYPE_NAME, _)
253 #endif // LUABIND_NO_ERROR_CHECKING