Add define that strips extern "C" {} around Lua includes.
[luabind.git] / luabind / detail / get_signature.hpp
blob25be866f10c16038af0e2c503c8bbda301a8399a
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>
40 #include <luabind/typeid.hpp>
43 namespace luabind { namespace detail
46 std::string LUABIND_API get_class_name(lua_State* L, type_id const& i);
48 template<class T>
49 std::string name_of_type(by_value<T>, lua_State* L, int) { return luabind::detail::get_class_name(L, typeid(T)); };
50 template<class T>
51 std::string name_of_type(by_reference<T>, lua_State* L, int) { return name_of_type(LUABIND_DECORATE_TYPE(T), L, 0L) + "&"; };
52 template<class T>
53 std::string name_of_type(by_pointer<T>, lua_State* L, int) { return name_of_type(LUABIND_DECORATE_TYPE(T), L, 0L) + "*"; };
54 template<class T>
55 std::string name_of_type(by_const_reference<T>, lua_State* L, int) { return "const " + name_of_type(LUABIND_DECORATE_TYPE(T), L, 0L) + "&"; };
56 template<class T>
57 std::string name_of_type(by_const_pointer<T>, lua_State* L, int) { return "const " + name_of_type(LUABIND_DECORATE_TYPE(T), L, 0L) + "*"; };
59 inline std::string name_of_type(by_value<luabind::object>, lua_State*, int) { return "object"; };
60 inline std::string name_of_type(by_const_reference<luabind::object>, lua_State*, int) { return "object"; };
61 inline std::string name_of_type(by_value<bool>, lua_State*, int) { return "boolean"; }
62 inline std::string name_of_type(by_value<char>, lua_State*, int) { return "number"; }
63 inline std::string name_of_type(by_value<short>, lua_State*, int) { return "number"; }
64 inline std::string name_of_type(by_value<int>, lua_State*, int) { return "number"; }
65 inline std::string name_of_type(by_value<long>, lua_State*, int) { return "number"; }
66 inline std::string name_of_type(by_value<unsigned char>, lua_State*, int) { return "number"; }
67 inline std::string name_of_type(by_value<unsigned short>, lua_State*, int) { return "number"; }
68 inline std::string name_of_type(by_value<unsigned int>, lua_State*, int) { return "number"; }
69 inline std::string name_of_type(by_value<unsigned long>, lua_State*, int) { return "number"; }
71 inline std::string name_of_type(by_value<const bool>, lua_State*, int) { return "boolean"; }
72 inline std::string name_of_type(by_value<const char>, lua_State*, int) { return "number"; }
73 inline std::string name_of_type(by_value<const short>, lua_State*, int) { return "number"; }
74 inline std::string name_of_type(by_value<const int>, lua_State*, int) { return "number"; }
75 inline std::string name_of_type(by_value<const long>, lua_State*, int) { return "number"; }
76 inline std::string name_of_type(by_value<const unsigned char>, lua_State*, int) { return "number"; }
77 inline std::string name_of_type(by_value<const unsigned short>, lua_State*, int) { return "number"; }
78 inline std::string name_of_type(by_value<const unsigned int>, lua_State*, int) { return "number"; }
79 inline std::string name_of_type(by_value<const unsigned long>, lua_State*, int) { return "number"; }
81 // template<class T>
82 // inline std::string name_of_type(by_value<luabind::functor<T> >, lua_State* L, long) { return "function<" + name_of_type(LUABIND_DECORATE_TYPE(T), L, 0L) + ">"; }
84 inline std::string name_of_type(by_value<std::string>, lua_State*, int) { return "string"; }
85 inline std::string name_of_type(by_const_pointer<char>, lua_State*, int) { return "string"; }
86 inline std::string name_of_type(by_pointer<lua_State>, lua_State*, int) { return "lua_State*"; }
88 template<class T>
89 struct type_name_unless_void
91 inline static void apply(std::string& s, lua_State* L, bool first)
93 if (!first) s += ", ";
94 s += name_of_type(LUABIND_DECORATE_TYPE(T), L, 0L);
98 template<>
99 struct type_name_unless_void<null_type>
101 inline static void apply(std::string&, lua_State*, bool) {}
104 #define LUABIND_ADD_LUA_TYPE_NAME(z, n, _) type_name_unless_void<BOOST_PP_CAT(A, BOOST_PP_INC(n))>::apply(s, L, false);
106 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/detail/get_signature.hpp>, 1))
107 #include BOOST_PP_ITERATE()
109 template<class F>
110 struct get_member_signature
112 static inline void apply(lua_State* L, std::string& s)
114 get_member_signature_impl(static_cast<F>(0), L, s);
118 template<class F>
119 struct get_free_function_signature
121 static inline void apply(lua_State* L, std::string& s)
123 get_free_function_signature_impl(static_cast<F>(0), L, s);
128 template<class Sig>
129 struct get_signature
131 static inline void apply(lua_State* L, std::string& s)
133 get_signature_impl(static_cast<const Sig*>(0), L, s);
137 template<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY, class A)>
138 inline void get_signature_impl(const constructor<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY, A)>*, lua_State* L, std::string& s)
140 s += "(";
141 type_name_unless_void<A0>::apply(s, L, true);
142 BOOST_PP_REPEAT(BOOST_PP_DEC(LUABIND_MAX_ARITY), LUABIND_ADD_LUA_TYPE_NAME, _)
143 s += ")";
146 #undef LUABIND_ADD_LUA_TYPE_NAME
148 template<class T>
149 struct get_setter_signature
151 static void apply(lua_State* L, std::string& s)
153 s += "(";
154 s += name_of_type(LUABIND_DECORATE_TYPE(T), L, 0L);
155 s += ")";
161 #endif // LUABIND_GET_SIGNATURE_HPP_INCLUDED
163 #elif BOOST_PP_ITERATION_FLAGS() == 1
165 // member functions
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)
169 s += "(";
170 #if BOOST_PP_ITERATION() > 0
171 s += name_of_type(LUABIND_DECORATE_TYPE(A0), L, 0L);
172 BOOST_PP_REPEAT(BOOST_PP_DEC(BOOST_PP_ITERATION()), LUABIND_ADD_LUA_TYPE_NAME, _)
173 #endif
174 s += ")";
177 template<class T, class C BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
178 inline void get_member_signature_impl(T(C::*)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)) const, lua_State* L, std::string& s)
180 (void)L;
181 s += "(";
182 #if BOOST_PP_ITERATION() > 0
183 s += name_of_type(LUABIND_DECORATE_TYPE(A0), L, 0L);
184 BOOST_PP_REPEAT(BOOST_PP_DEC(BOOST_PP_ITERATION()), LUABIND_ADD_LUA_TYPE_NAME, _)
185 #endif
186 s += ") const";
189 // const C& obj
190 template<class T BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
191 inline void get_member_signature_impl(T(*f)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), lua_State* L, std::string& s)
193 s += "(";
194 #if BOOST_PP_ITERATION() > 0
195 s += name_of_type(LUABIND_DECORATE_TYPE(A0), L, 0L);
196 BOOST_PP_REPEAT(BOOST_PP_DEC(BOOST_PP_ITERATION()), LUABIND_ADD_LUA_TYPE_NAME, _)
197 #endif
198 s += ")";
201 // free functions
202 template<class T BOOST_PP_COMMA_IF(BOOST_PP_ITERATION()) BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
203 inline void get_free_function_signature_impl(T(*f)(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), A)), lua_State* L, std::string& s)
205 (void)f;
206 s += "(";
207 #if BOOST_PP_ITERATION() > 0
208 s += name_of_type(LUABIND_DECORATE_TYPE(A0), L, 0L);
209 BOOST_PP_REPEAT(BOOST_PP_DEC(BOOST_PP_ITERATION()), LUABIND_ADD_LUA_TYPE_NAME, _)
210 #endif
211 s += ")";
214 #endif
216 #endif // LUABIND_NO_ERROR_CHECKING