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/type_traits/is_same.hpp>
29 #include <luabind/detail/other.hpp>
30 #include <luabind/raw_policy.hpp>
32 #if defined(__GNUC__) && __GNUC__ < 3
33 # define LUABIND_NO_STRINGSTREAM
35 # if defined(BOOST_NO_STRINGSTREAM)
36 # define LUABIND_NO_STRINGSTREAM
40 #ifdef LUABIND_NO_STRINGSTREAM
46 namespace luabind
{ namespace detail
{
48 template<class W
, class T
> struct unwrap_parameter_type
;
49 template<class Derived
> struct operator_
{};
51 struct operator_void_return
{};
53 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
55 inline T
const& operator,(T
const& x
, operator_void_return
)
61 }} // namespace luabind
63 namespace luabind
{ namespace operators
{
65 #define BOOST_PP_ITERATION_PARAMS_1 (3, \
66 (0, LUABIND_MAX_ARITY, <luabind/detail/call_operator_iterate.hpp>))
67 #include BOOST_PP_ITERATE()
69 }} // namespace luabind::operators
71 #include <boost/preprocessor/iteration/local.hpp>
75 template<class Derived
>
78 operators::call_operator0
<Derived
> operator()() const
83 #define BOOST_PP_LOCAL_MACRO(n) \
84 template<BOOST_PP_ENUM_PARAMS(n, class A)> \
85 BOOST_PP_CAT(operators::call_operator, n)< \
87 BOOST_PP_ENUM_TRAILING_PARAMS(n, A) \
90 BOOST_PP_ENUM_BINARY_PARAMS(n, A, const& BOOST_PP_INTERCEPT) \
96 #define BOOST_PP_LOCAL_LIMITS (1, LUABIND_MAX_ARITY)
97 #include BOOST_PP_LOCAL_ITERATE()
101 struct self_type
: self_base
<self_type
>
105 struct const_self_type
: self_base
<const_self_type
>
111 template<class W
, class T
>
112 struct unwrap_parameter_type
114 typedef typename
boost::mpl::eval_if
<
115 boost::is_same
<T
, self_type
>
116 , boost::mpl::identity
<W
&>
117 , boost::mpl::eval_if
<
118 boost::is_same
<T
, const_self_type
>
119 , boost::mpl::identity
<W
const&>
125 template<class Derived
, class A
, class B
>
126 struct binary_operator
127 : operator_
<binary_operator
<Derived
, A
, B
> >
129 binary_operator(int) {}
131 template<class T
, class Policies
>
134 typedef typename unwrap_parameter_type
<T
, A
>::type arg0
;
135 typedef typename unwrap_parameter_type
<T
, B
>::type arg1
;
137 static void execute(lua_State
* L
, arg0 _0
, arg1 _1
)
139 Derived::template apply
<arg0
, arg1
, Policies
>::execute(
144 static char const* name()
146 return Derived::name();
150 template<class Derived
, class A
>
151 struct unary_operator
152 : operator_
<unary_operator
<Derived
, A
> >
154 unary_operator(int) {}
156 template<class T
, class Policies
>
159 typedef typename unwrap_parameter_type
<T
, A
>::type arg0
;
161 static void execute(lua_State
* L
, arg0 _0
)
163 Derived::template apply
<arg0
, Policies
>::execute(L
, _0
);
167 static char const* name()
169 return Derived::name();
173 template<class Policies
>
174 inline void operator_result(lua_State
* L
, operator_void_return
, Policies
*)
178 template<class T
, class Policies
>
179 inline void operator_result(lua_State
* L
, T
const& x
, Policies
*)
181 typedef typename find_conversion_policy
<
186 typename
cv_policy::template generate_converter
<
194 }} // namespace detail::luabind
198 #define LUABIND_BINARY_OPERATOR(name_, op) \
199 namespace operators { \
203 template<class T0, class T1, class Policies> \
206 static void execute(lua_State* L, T0 _0, T1 _1) \
208 detail::operator_result(L, _0 op _1, (Policies*)0); \
212 static char const* name() \
214 return "__" # name_; \
220 template<class T, class U> \
221 detail::binary_operator< \
226 inline operator op(self_base<U>, T const&) \
231 template<class T, class U> \
232 detail::binary_operator< \
237 inline operator op(T const&, self_base<U>) \
242 detail::binary_operator< \
247 inline operator op(self_type, self_type) \
252 detail::binary_operator< \
257 inline operator op(self_type, const_self_type) \
262 detail::binary_operator< \
267 inline operator op(const_self_type, self_type) \
272 detail::binary_operator< \
277 inline operator op(const_self_type, const_self_type) \
282 LUABIND_BINARY_OPERATOR(add
, +)
283 LUABIND_BINARY_OPERATOR(sub
, -)
284 LUABIND_BINARY_OPERATOR(mul
, *)
285 LUABIND_BINARY_OPERATOR(div
, /)
286 LUABIND_BINARY_OPERATOR(pow
, ^)
287 LUABIND_BINARY_OPERATOR(lt
, <)
288 LUABIND_BINARY_OPERATOR(le
, <=)
289 LUABIND_BINARY_OPERATOR(eq
, ==)
291 #undef LUABIND_UNARY_OPERATOR
293 #define LUABIND_UNARY_OPERATOR(name_, op, fn) \
294 namespace operators { \
298 template<class T, class Policies> \
301 static void execute(lua_State* L, T x) \
303 detail::operator_result(L, op(x), (Policies*)0); \
307 static char const* name() \
309 return "__" # name_; \
316 detail::unary_operator< \
320 inline fn(self_base<T>) \
326 std::string
tostring_operator(T
const& x
)
328 #ifdef LUABIND_NO_STRINGSTREAM
338 LUABIND_UNARY_OPERATOR(tostring
, tostring_operator
, tostring
)
339 LUABIND_UNARY_OPERATOR(unm
, -, operator-)
341 #undef LUABIND_BINARY_OPERATOR
345 LUABIND_ANONYMOUS_FIX self_type self
;
346 LUABIND_ANONYMOUS_FIX const_self_type const_self
;
348 } // namespace unnamed
350 } // namespace luabind
352 #endif // OPERATOR_040729_HPP