1 // Boost Lambda Library - operators.hpp --------------------------------------
3 // Copyright (C) 1999, 2000 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // For more information, see www.boost.org
11 // ---------------------------------------------------------------
13 #ifndef BOOST_LAMBDA_OPERATORS_HPP
14 #define BOOST_LAMBDA_OPERATORS_HPP
16 #include "boost/lambda/detail/is_instance_of.hpp"
21 #if defined BOOST_LAMBDA_BE1
22 #error "Multiple defines of BOOST_LAMBDA_BE1"
25 // For all BOOSTA_LAMBDA_BE* macros:
27 // CONSTA must be either 'A' or 'const A'
28 // CONSTB must be either 'B' or 'const B'
30 // It is stupid to have the names A and B as macro arguments, but it avoids
31 // the need to pass in emtpy macro arguments, which gives warnings on some
34 #define BOOST_LAMBDA_BE1(OPER_NAME, ACTION, CONSTA, CONSTB, CONVERSION) \
35 template<class Arg, class B> \
38 lambda_functor_base< \
40 tuple<lambda_functor<Arg>, typename CONVERSION <CONSTB>::type> \
43 OPER_NAME (const lambda_functor<Arg>& a, CONSTB& b) { \
45 lambda_functor_base< \
47 tuple<lambda_functor<Arg>, typename CONVERSION <CONSTB>::type> \
49 (tuple<lambda_functor<Arg>, typename CONVERSION <CONSTB>::type>(a, b)); \
53 #if defined BOOST_LAMBDA_BE2
54 #error "Multiple defines of BOOST_LAMBDA_BE2"
57 #define BOOST_LAMBDA_BE2(OPER_NAME, ACTION, CONSTA, CONSTB, CONVERSION) \
58 template<class A, class Arg> \
61 lambda_functor_base< \
63 tuple<typename CONVERSION <CONSTA>::type, lambda_functor<Arg> > \
66 OPER_NAME (CONSTA& a, const lambda_functor<Arg>& b) { \
68 lambda_functor_base< \
70 tuple<typename CONVERSION <CONSTA>::type, lambda_functor<Arg> > \
72 (tuple<typename CONVERSION <CONSTA>::type, lambda_functor<Arg> >(a, b)); \
76 #if defined BOOST_LAMBDA_BE3
77 #error "Multiple defines of BOOST_LAMBDA_BE3"
80 #define BOOST_LAMBDA_BE3(OPER_NAME, ACTION, CONSTA, CONSTB, CONVERSION) \
81 template<class ArgA, class ArgB> \
84 lambda_functor_base< \
86 tuple<lambda_functor<ArgA>, lambda_functor<ArgB> > \
89 OPER_NAME (const lambda_functor<ArgA>& a, const lambda_functor<ArgB>& b) { \
91 lambda_functor_base< \
93 tuple<lambda_functor<ArgA>, lambda_functor<ArgB> > \
95 (tuple<lambda_functor<ArgA>, lambda_functor<ArgB> >(a, b)); \
98 #if defined BOOST_LAMBDA_BE
99 #error "Multiple defines of BOOST_LAMBDA_BE"
102 #define BOOST_LAMBDA_BE(OPER_NAME, ACTION, CONSTA, CONSTB, CONST_CONVERSION) \
103 BOOST_LAMBDA_BE1(OPER_NAME, ACTION, CONSTA, CONSTB, CONST_CONVERSION) \
104 BOOST_LAMBDA_BE2(OPER_NAME, ACTION, CONSTA, CONSTB, CONST_CONVERSION) \
105 BOOST_LAMBDA_BE3(OPER_NAME, ACTION, CONSTA, CONSTB, CONST_CONVERSION)
107 #define BOOST_LAMBDA_EMPTY()
109 BOOST_LAMBDA_BE(operator+, arithmetic_action<plus_action>, const A, const B, const_copy_argument)
110 BOOST_LAMBDA_BE(operator-, arithmetic_action<minus_action>, const A, const B, const_copy_argument)
111 BOOST_LAMBDA_BE(operator*, arithmetic_action<multiply_action>, const A, const B, const_copy_argument)
112 BOOST_LAMBDA_BE(operator/, arithmetic_action<divide_action>, const A, const B, const_copy_argument)
113 BOOST_LAMBDA_BE(operator%, arithmetic_action<remainder_action>, const A, const B, const_copy_argument)
114 BOOST_LAMBDA_BE(operator<<, bitwise_action<leftshift_action>, const A, const B, const_copy_argument)
115 BOOST_LAMBDA_BE(operator>>, bitwise_action<rightshift_action>, const A, const B, const_copy_argument)
116 BOOST_LAMBDA_BE(operator&, bitwise_action<and_action>, const A, const B, const_copy_argument)
117 BOOST_LAMBDA_BE(operator|, bitwise_action<or_action>, const A, const B, const_copy_argument)
118 BOOST_LAMBDA_BE(operator^, bitwise_action<xor_action>, const A, const B, const_copy_argument)
119 BOOST_LAMBDA_BE(operator&&, logical_action<and_action>, const A, const B, const_copy_argument)
120 BOOST_LAMBDA_BE(operator||, logical_action<or_action>, const A, const B, const_copy_argument)
121 BOOST_LAMBDA_BE(operator<, relational_action<less_action>, const A, const B, const_copy_argument)
122 BOOST_LAMBDA_BE(operator>, relational_action<greater_action>, const A, const B, const_copy_argument)
123 BOOST_LAMBDA_BE(operator<=, relational_action<lessorequal_action>, const A, const B, const_copy_argument)
124 BOOST_LAMBDA_BE(operator>=, relational_action<greaterorequal_action>, const A, const B, const_copy_argument)
125 BOOST_LAMBDA_BE(operator==, relational_action<equal_action>, const A, const B, const_copy_argument)
126 BOOST_LAMBDA_BE(operator!=, relational_action<notequal_action>, const A, const B, const_copy_argument)
128 BOOST_LAMBDA_BE(operator+=, arithmetic_assignment_action<plus_action>, A, const B, reference_argument)
129 BOOST_LAMBDA_BE(operator-=, arithmetic_assignment_action<minus_action>, A, const B, reference_argument)
130 BOOST_LAMBDA_BE(operator*=, arithmetic_assignment_action<multiply_action>, A, const B, reference_argument)
131 BOOST_LAMBDA_BE(operator/=, arithmetic_assignment_action<divide_action>, A, const B, reference_argument)
132 BOOST_LAMBDA_BE(operator%=, arithmetic_assignment_action<remainder_action>, A, const B, reference_argument)
133 BOOST_LAMBDA_BE(operator<<=, bitwise_assignment_action<leftshift_action>, A, const B, reference_argument)
134 BOOST_LAMBDA_BE(operator>>=, bitwise_assignment_action<rightshift_action>, A, const B, reference_argument)
135 BOOST_LAMBDA_BE(operator&=, bitwise_assignment_action<and_action>, A, const B, reference_argument)
136 BOOST_LAMBDA_BE(operator|=, bitwise_assignment_action<or_action>, A, const B, reference_argument)
137 BOOST_LAMBDA_BE(operator^=, bitwise_assignment_action<xor_action>, A, const B, reference_argument)
140 // A special trick for comma operator for correct preprocessing
141 #if defined BOOST_LAMBDA_COMMA_OPERATOR_NAME
142 #error "Multiple defines of BOOST_LAMBDA_COMMA_OPERATOR_NAME"
145 #define BOOST_LAMBDA_COMMA_OPERATOR_NAME operator,
147 BOOST_LAMBDA_BE1(BOOST_LAMBDA_COMMA_OPERATOR_NAME, other_action<comma_action>, const A, const B, const_copy_argument)
148 BOOST_LAMBDA_BE2(BOOST_LAMBDA_COMMA_OPERATOR_NAME, other_action<comma_action>, const A, const B, const_copy_argument)
149 BOOST_LAMBDA_BE3(BOOST_LAMBDA_COMMA_OPERATOR_NAME, other_action<comma_action>, const A, const B, const_copy_argument)
155 // special cases for ostream& << Any and istream& >> Any ---------------
156 // the actual stream classes may vary and thus a specialisation for,
157 // say ostream& does not match (the general case above is chosen).
158 // Therefore we specialise for non-const reference:
159 // if the left argument is a stream, we store the stream as reference
160 // if it is something else, we store a const plain by default
162 // Note that the overloading is const vs. non-const first argument
164 #ifdef BOOST_NO_TEMPLATED_STREAMS
165 template<class T> struct convert_ostream_to_ref_others_to_c_plain_by_default {
166 typedef typename detail::IF<
167 boost::is_convertible<T*, std::ostream*>::value,
169 typename const_copy_argument <T>::type
173 template<class T> struct convert_istream_to_ref_others_to_c_plain_by_default {
174 typedef typename detail::IF<
175 boost::is_convertible<T*, std::istream*>::value,
177 typename const_copy_argument <T>::type
182 template<class T> struct convert_ostream_to_ref_others_to_c_plain_by_default {
183 typedef typename detail::IF<
185 T, std::basic_ostream
188 typename const_copy_argument <T>::type
192 template<class T> struct convert_istream_to_ref_others_to_c_plain_by_default {
193 typedef typename detail::IF<
195 T, std::basic_istream
198 typename const_copy_argument <T>::type
205 BOOST_LAMBDA_BE2(operator<<, bitwise_action< leftshift_action>, A, const B, detail::convert_ostream_to_ref_others_to_c_plain_by_default)
206 BOOST_LAMBDA_BE2(operator>>, bitwise_action< rightshift_action>, A, const B, detail::convert_istream_to_ref_others_to_c_plain_by_default)
209 // special case for io_manipulators.
210 // function references cannot be given as arguments to lambda operator
211 // expressions in general. With << and >> the use of manipulators is
212 // so common, that specializations are provided to make them work.
214 template<class Arg, class Ret, class ManipArg>
218 bitwise_action<leftshift_action>,
219 tuple<lambda_functor<Arg>, Ret(&)(ManipArg)>
222 operator<<(const lambda_functor<Arg>& a, Ret(&b)(ManipArg))
226 bitwise_action<leftshift_action>,
227 tuple<lambda_functor<Arg>, Ret(&)(ManipArg)>
229 ( tuple<lambda_functor<Arg>, Ret(&)(ManipArg)>(a, b) );
232 template<class Arg, class Ret, class ManipArg>
236 bitwise_action<rightshift_action>,
237 tuple<lambda_functor<Arg>, Ret(&)(ManipArg)>
240 operator>>(const lambda_functor<Arg>& a, Ret(&b)(ManipArg))
244 bitwise_action<rightshift_action>,
245 tuple<lambda_functor<Arg>, Ret(&)(ManipArg)>
247 ( tuple<lambda_functor<Arg>, Ret(&)(ManipArg)>(a, b) );
251 // (+ and -) take their arguments as const references.
252 // This has consquences with pointer artihmetic
253 // E.g int a[]; ... *a = 1 works but not *(a+1) = 1.
254 // the result of a+1 would be const
255 // To make the latter work too,
256 // non-const arrays are taken as non-const and stored as non-const as well.
257 #if defined BOOST_LAMBDA_PTR_ARITHMETIC_E1
258 #error "Multiple defines of BOOST_LAMBDA_PTR_ARITHMETIC_E1"
261 #define BOOST_LAMBDA_PTR_ARITHMETIC_E1(OPER_NAME, ACTION, CONSTB) \
262 template<class Arg, int N, class B> \
265 lambda_functor_base<ACTION, tuple<lambda_functor<Arg>, CONSTB(&)[N]> > \
267 OPER_NAME (const lambda_functor<Arg>& a, CONSTB(&b)[N]) \
269 return lambda_functor< \
270 lambda_functor_base<ACTION, tuple<lambda_functor<Arg>, CONSTB(&)[N]> > \
271 >(tuple<lambda_functor<Arg>, CONSTB(&)[N]>(a, b)); \
275 #if defined BOOST_LAMBDA_PTR_ARITHMETIC_E2
276 #error "Multiple defines of BOOST_LAMBDA_PTR_ARITHMETIC_E2"
279 #define BOOST_LAMBDA_PTR_ARITHMETIC_E2(OPER_NAME, ACTION, CONSTA) \
280 template<int N, class A, class Arg> \
283 lambda_functor_base<ACTION, tuple<CONSTA(&)[N], lambda_functor<Arg> > > \
285 OPER_NAME (CONSTA(&a)[N], const lambda_functor<Arg>& b) \
288 lambda_functor_base<ACTION, tuple<CONSTA(&)[N], lambda_functor<Arg> > > \
289 (tuple<CONSTA(&)[N], lambda_functor<Arg> >(a, b)); \
293 BOOST_LAMBDA_PTR_ARITHMETIC_E1(operator+, arithmetic_action<plus_action>, B)
294 BOOST_LAMBDA_PTR_ARITHMETIC_E2(operator+, arithmetic_action<plus_action>, A)
295 BOOST_LAMBDA_PTR_ARITHMETIC_E1(operator+, arithmetic_action<plus_action>,const B)
296 BOOST_LAMBDA_PTR_ARITHMETIC_E2(operator+, arithmetic_action<plus_action>,const A)
299 //BOOST_LAMBDA_PTR_ARITHMETIC_E1(operator-, arithmetic_action<minus_action>)
300 // This is not needed, since the result of ptr-ptr is an rvalue anyway
302 BOOST_LAMBDA_PTR_ARITHMETIC_E2(operator-, arithmetic_action<minus_action>, A)
303 BOOST_LAMBDA_PTR_ARITHMETIC_E2(operator-, arithmetic_action<minus_action>, const A)
306 #undef BOOST_LAMBDA_BE1
307 #undef BOOST_LAMBDA_BE2
308 #undef BOOST_LAMBDA_BE3
309 #undef BOOST_LAMBDA_BE
310 #undef BOOST_LAMBDA_COMMA_OPERATOR_NAME
312 #undef BOOST_LAMBDA_PTR_ARITHMETIC_E1
313 #undef BOOST_LAMBDA_PTR_ARITHMETIC_E2
316 // ---------------------------------------------------------------------
317 // unary operators -----------------------------------------------------
318 // ---------------------------------------------------------------------
320 #if defined BOOST_LAMBDA_UE
321 #error "Multiple defines of BOOST_LAMBDA_UE"
324 #define BOOST_LAMBDA_UE(OPER_NAME, ACTION) \
325 template<class Arg> \
327 lambda_functor<lambda_functor_base<ACTION, tuple<lambda_functor<Arg> > > > \
328 OPER_NAME (const lambda_functor<Arg>& a) \
331 lambda_functor_base<ACTION, tuple<lambda_functor<Arg> > > \
332 ( tuple<lambda_functor<Arg> >(a) ); \
336 BOOST_LAMBDA_UE(operator+, unary_arithmetic_action<plus_action>)
337 BOOST_LAMBDA_UE(operator-, unary_arithmetic_action<minus_action>)
338 BOOST_LAMBDA_UE(operator~, bitwise_action<not_action>)
339 BOOST_LAMBDA_UE(operator!, logical_action<not_action>)
340 BOOST_LAMBDA_UE(operator++, pre_increment_decrement_action<increment_action>)
341 BOOST_LAMBDA_UE(operator--, pre_increment_decrement_action<decrement_action>)
342 BOOST_LAMBDA_UE(operator*, other_action<contentsof_action>)
343 BOOST_LAMBDA_UE(operator&, other_action<addressof_action>)
345 #if defined BOOST_LAMBDA_POSTFIX_UE
346 #error "Multiple defines of BOOST_LAMBDA_POSTFIX_UE"
349 #define BOOST_LAMBDA_POSTFIX_UE(OPER_NAME, ACTION) \
350 template<class Arg> \
352 lambda_functor<lambda_functor_base<ACTION, tuple<lambda_functor<Arg> > > > \
353 OPER_NAME (const lambda_functor<Arg>& a, int) \
356 lambda_functor_base<ACTION, tuple<lambda_functor<Arg> > > \
357 ( tuple<lambda_functor<Arg> >(a) ); \
361 BOOST_LAMBDA_POSTFIX_UE(operator++, post_increment_decrement_action<increment_action>)
362 BOOST_LAMBDA_POSTFIX_UE(operator--, post_increment_decrement_action<decrement_action>)
364 #undef BOOST_LAMBDA_UE
365 #undef BOOST_LAMBDA_POSTFIX_UE
367 } // namespace lambda