1 // Copyright (c) 2004 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 OPERATOR_040729_HPP
24 #define OPERATOR_040729_HPP
26 #include <boost/mpl/eval_if.hpp>
27 #include <boost/mpl/identity.hpp>
28 #include <boost/mpl/apply_wrap.hpp>
29 #include <boost/preprocessor/repetition/enum_trailing.hpp>
30 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
31 #include <boost/type_traits/is_same.hpp>
32 #include <luabind/detail/other.hpp>
33 #include <luabind/raw_policy.hpp>
35 #if defined(__GNUC__) && __GNUC__ < 3
36 # define LUABIND_NO_STRINGSTREAM
38 # if defined(BOOST_NO_STRINGSTREAM)
39 # define LUABIND_NO_STRINGSTREAM
43 #ifdef LUABIND_NO_STRINGSTREAM
49 namespace luabind
{ namespace detail
{
51 template<class W
, class T
> struct unwrap_parameter_type
;
52 template<class Derived
> struct operator_
{};
54 struct operator_void_return
{};
56 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
58 inline T
const& operator,(T
const& x
, operator_void_return
)
64 }} // namespace luabind
66 namespace luabind
{ namespace operators
{
68 #define BOOST_PP_ITERATION_PARAMS_1 (3, \
69 (0, LUABIND_MAX_ARITY, <luabind/detail/call_operator_iterate.hpp>))
70 #include BOOST_PP_ITERATE()
72 }} // namespace luabind::operators
74 #include <boost/preprocessor/iteration/local.hpp>
78 template<class Derived
>
81 operators::call_operator0
<Derived
> operator()() const
86 #define BOOST_PP_LOCAL_MACRO(n) \
87 template<BOOST_PP_ENUM_PARAMS(n, class A)> \
88 BOOST_PP_CAT(operators::call_operator, n)< \
90 BOOST_PP_ENUM_TRAILING_PARAMS(n, A) \
93 BOOST_PP_ENUM_BINARY_PARAMS(n, A, const& BOOST_PP_INTERCEPT) \
99 #define BOOST_PP_LOCAL_LIMITS (1, LUABIND_MAX_ARITY)
100 #include BOOST_PP_LOCAL_ITERATE()
104 struct self_type
: self_base
<self_type
>
108 struct const_self_type
: self_base
<const_self_type
>
114 template<class W
, class T
>
115 struct unwrap_parameter_type
117 typedef typename
boost::mpl::eval_if
<
118 boost::is_same
<T
, self_type
>
119 , boost::mpl::identity
<W
&>
120 , boost::mpl::eval_if
<
121 boost::is_same
<T
, const_self_type
>
122 , boost::mpl::identity
<W
const&>
128 template<class Derived
, class A
, class B
>
129 struct binary_operator
130 : operator_
<binary_operator
<Derived
, A
, B
> >
132 binary_operator(int) {}
134 template<class T
, class Policies
>
137 typedef typename unwrap_parameter_type
<T
, A
>::type arg0
;
138 typedef typename unwrap_parameter_type
<T
, B
>::type arg1
;
140 static void execute(lua_State
* L
, arg0 _0
, arg1 _1
)
142 Derived::template apply
<arg0
, arg1
, Policies
>::execute(
147 static char const* name()
149 return Derived::name();
153 template<class Derived
, class A
>
154 struct unary_operator
155 : operator_
<unary_operator
<Derived
, A
> >
157 unary_operator(int) {}
159 template<class T
, class Policies
>
162 typedef typename unwrap_parameter_type
<T
, A
>::type arg0
;
164 static void execute(lua_State
* L
, arg0 _0
)
166 Derived::template apply
<arg0
, Policies
>::execute(L
, _0
);
170 static char const* name()
172 return Derived::name();
176 template<class Policies
>
177 inline void operator_result(lua_State
* L
, operator_void_return
, Policies
*)
181 namespace mpl
= boost::mpl
;
183 template<class T
, class Policies
>
184 inline void operator_result(lua_State
* L
, T
const& x
, Policies
*)
186 typedef typename find_conversion_policy
<
191 typename
mpl::apply_wrap2
<cv_policy
,T
,cpp_to_lua
>::type cv
;
196 }} // namespace detail::luabind
200 #define LUABIND_BINARY_OPERATOR(name_, op) \
201 namespace operators { \
205 template<class T0, class T1, class Policies> \
208 static void execute(lua_State* L, T0 _0, T1 _1) \
210 detail::operator_result(L, _0 op _1, (Policies*)0); \
214 static char const* name() \
216 return "__" # name_; \
222 template<class T, class U> \
223 detail::binary_operator< \
228 inline operator op(self_base<U>, T const&) \
233 template<class T, class U> \
234 detail::binary_operator< \
239 inline operator op(T const&, self_base<U>) \
244 detail::binary_operator< \
249 inline operator op(self_type, self_type) \
254 detail::binary_operator< \
259 inline operator op(self_type, const_self_type) \
264 detail::binary_operator< \
269 inline operator op(const_self_type, self_type) \
274 detail::binary_operator< \
279 inline operator op(const_self_type, const_self_type) \
284 LUABIND_BINARY_OPERATOR(add
, +)
285 LUABIND_BINARY_OPERATOR(sub
, -)
286 LUABIND_BINARY_OPERATOR(mul
, *)
287 LUABIND_BINARY_OPERATOR(div
, /)
288 LUABIND_BINARY_OPERATOR(pow
, ^)
289 LUABIND_BINARY_OPERATOR(lt
, <)
290 LUABIND_BINARY_OPERATOR(le
, <=)
291 LUABIND_BINARY_OPERATOR(eq
, ==)
293 #undef LUABIND_UNARY_OPERATOR
295 #define LUABIND_UNARY_OPERATOR(name_, op, fn) \
296 namespace operators { \
300 template<class T, class Policies> \
303 static void execute(lua_State* L, T x) \
305 detail::operator_result(L, op(x), (Policies*)0); \
309 static char const* name() \
311 return "__" # name_; \
318 detail::unary_operator< \
322 inline fn(self_base<T>) \
328 std::string
tostring_operator(T
const& x
)
330 #ifdef LUABIND_NO_STRINGSTREAM
340 LUABIND_UNARY_OPERATOR(tostring
, tostring_operator
, tostring
)
341 LUABIND_UNARY_OPERATOR(unm
, -, operator-)
343 #undef LUABIND_BINARY_OPERATOR
347 LUABIND_ANONYMOUS_FIX self_type self
;
348 LUABIND_ANONYMOUS_FIX const_self_type const_self
;
350 } // namespace unnamed
352 } // namespace luabind
354 #endif // OPERATOR_040729_HPP