1// Boost Lambda Library - operators.hpp --------------------------------------
2
3// Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
4//
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)
8//
9// For more information, see www.boost.org
10
11// ---------------------------------------------------------------
12
13#ifndef BOOST_LAMBDA_OPERATORS_HPP
14#define BOOST_LAMBDA_OPERATORS_HPP
15
16#include "boost/lambda/detail/is_instance_of.hpp"
17
18namespace boost {
19namespace lambda {
20
21#if defined BOOST_LAMBDA_BE1
22#error "Multiple defines of BOOST_LAMBDA_BE1"
23#endif
24
25 // For all BOOSTA_LAMBDA_BE* macros:
26
27 // CONSTA must be either 'A' or 'const A'
28 // CONSTB must be either 'B' or 'const B'
29
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
32 // compilers
33
34#define BOOST_LAMBDA_BE1(OPER_NAME, ACTION, CONSTA, CONSTB, CONVERSION) \
35template<class Arg, class B> \
36inline const \
37lambda_functor< \
38 lambda_functor_base< \
39 ACTION, \
40 tuple<lambda_functor<Arg>, typename const_copy_argument <CONSTB>::type> \
41 > \
42> \
43OPER_NAME (const lambda_functor<Arg>& a, CONSTB& b) { \
44 return \
45 lambda_functor_base< \
46 ACTION, \
47 tuple<lambda_functor<Arg>, typename const_copy_argument <CONSTB>::type>\
48 > \
49 (tuple<lambda_functor<Arg>, typename const_copy_argument <CONSTB>::type>(a, b)); \
50}
51
52
53#if defined BOOST_LAMBDA_BE2
54#error "Multiple defines of BOOST_LAMBDA_BE2"
55#endif
56
57#define BOOST_LAMBDA_BE2(OPER_NAME, ACTION, CONSTA, CONSTB, CONVERSION) \
58template<class A, class Arg> \
59inline const \
60lambda_functor< \
61 lambda_functor_base< \
62 ACTION, \
63 tuple<typename CONVERSION <CONSTA>::type, lambda_functor<Arg> > \
64 > \
65> \
66OPER_NAME (CONSTA& a, const lambda_functor<Arg>& b) { \
67 return \
68 lambda_functor_base< \
69 ACTION, \
70 tuple<typename CONVERSION <CONSTA>::type, lambda_functor<Arg> > \
71 > \
72 (tuple<typename CONVERSION <CONSTA>::type, lambda_functor<Arg> >(a, b)); \
73}
74
75
76#if defined BOOST_LAMBDA_BE3
77#error "Multiple defines of BOOST_LAMBDA_BE3"
78#endif
79
80#define BOOST_LAMBDA_BE3(OPER_NAME, ACTION, CONSTA, CONSTB, CONVERSION) \
81template<class ArgA, class ArgB> \
82inline const \
83lambda_functor< \
84 lambda_functor_base< \
85 ACTION, \
86 tuple<lambda_functor<ArgA>, lambda_functor<ArgB> > \
87 > \
88> \
89OPER_NAME (const lambda_functor<ArgA>& a, const lambda_functor<ArgB>& b) { \
90 return \
91 lambda_functor_base< \
92 ACTION, \
93 tuple<lambda_functor<ArgA>, lambda_functor<ArgB> > \
94 > \
95 (tuple<lambda_functor<ArgA>, lambda_functor<ArgB> >(a, b)); \
96}
97
98#if defined BOOST_LAMBDA_BE
99#error "Multiple defines of BOOST_LAMBDA_BE"
100#endif
101
102#define BOOST_LAMBDA_BE(OPER_NAME, ACTION, CONSTA, CONSTB, CONST_CONVERSION) \
103BOOST_LAMBDA_BE1(OPER_NAME, ACTION, CONSTA, CONSTB, CONST_CONVERSION) \
104BOOST_LAMBDA_BE2(OPER_NAME, ACTION, CONSTA, CONSTB, CONST_CONVERSION) \
105BOOST_LAMBDA_BE3(OPER_NAME, ACTION, CONSTA, CONSTB, CONST_CONVERSION)
106
107#define BOOST_LAMBDA_EMPTY()
108
109BOOST_LAMBDA_BE(operator+, arithmetic_action<plus_action>, const A, const B, const_copy_argument)
110BOOST_LAMBDA_BE(operator-, arithmetic_action<minus_action>, const A, const B, const_copy_argument)
111BOOST_LAMBDA_BE(operator*, arithmetic_action<multiply_action>, const A, const B, const_copy_argument)
112BOOST_LAMBDA_BE(operator/, arithmetic_action<divide_action>, const A, const B, const_copy_argument)
113BOOST_LAMBDA_BE(operator%, arithmetic_action<remainder_action>, const A, const B, const_copy_argument)
114BOOST_LAMBDA_BE(operator<<, bitwise_action<leftshift_action>, const A, const B, const_copy_argument)
115BOOST_LAMBDA_BE(operator>>, bitwise_action<rightshift_action>, const A, const B, const_copy_argument)
116BOOST_LAMBDA_BE(operator&, bitwise_action<and_action>, const A, const B, const_copy_argument)
117BOOST_LAMBDA_BE(operator|, bitwise_action<or_action>, const A, const B, const_copy_argument)
118BOOST_LAMBDA_BE(operator^, bitwise_action<xor_action>, const A, const B, const_copy_argument)
119BOOST_LAMBDA_BE(operator&&, logical_action<and_action>, const A, const B, const_copy_argument)
120BOOST_LAMBDA_BE(operator||, logical_action<or_action>, const A, const B, const_copy_argument)
121BOOST_LAMBDA_BE(operator<, relational_action<less_action>, const A, const B, const_copy_argument)
122BOOST_LAMBDA_BE(operator>, relational_action<greater_action>, const A, const B, const_copy_argument)
123BOOST_LAMBDA_BE(operator<=, relational_action<lessorequal_action>, const A, const B, const_copy_argument)
124BOOST_LAMBDA_BE(operator>=, relational_action<greaterorequal_action>, const A, const B, const_copy_argument)
125BOOST_LAMBDA_BE(operator==, relational_action<equal_action>, const A, const B, const_copy_argument)
126BOOST_LAMBDA_BE(operator!=, relational_action<notequal_action>, const A, const B, const_copy_argument)
127
128BOOST_LAMBDA_BE(operator+=, arithmetic_assignment_action<plus_action>, A, const B, reference_argument)
129BOOST_LAMBDA_BE(operator-=, arithmetic_assignment_action<minus_action>, A, const B, reference_argument)
130BOOST_LAMBDA_BE(operator*=, arithmetic_assignment_action<multiply_action>, A, const B, reference_argument)
131BOOST_LAMBDA_BE(operator/=, arithmetic_assignment_action<divide_action>, A, const B, reference_argument)
132BOOST_LAMBDA_BE(operator%=, arithmetic_assignment_action<remainder_action>, A, const B, reference_argument)
133BOOST_LAMBDA_BE(operator<<=, bitwise_assignment_action<leftshift_action>, A, const B, reference_argument)
134BOOST_LAMBDA_BE(operator>>=, bitwise_assignment_action<rightshift_action>, A, const B, reference_argument)
135BOOST_LAMBDA_BE(operator&=, bitwise_assignment_action<and_action>, A, const B, reference_argument)
136BOOST_LAMBDA_BE(operator|=, bitwise_assignment_action<or_action>, A, const B, reference_argument)
137BOOST_LAMBDA_BE(operator^=, bitwise_assignment_action<xor_action>, A, const B, reference_argument)
138
139
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"
143#endif
144
145#define BOOST_LAMBDA_COMMA_OPERATOR_NAME operator,
146
147BOOST_LAMBDA_BE1(BOOST_LAMBDA_COMMA_OPERATOR_NAME, other_action<comma_action>, const A, const B, const_copy_argument)
148BOOST_LAMBDA_BE2(BOOST_LAMBDA_COMMA_OPERATOR_NAME, other_action<comma_action>, const A, const B, const_copy_argument)
149BOOST_LAMBDA_BE3(BOOST_LAMBDA_COMMA_OPERATOR_NAME, other_action<comma_action>, const A, const B, const_copy_argument)
150
151
152
153namespace detail {
154
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
161
162// Note that the overloading is const vs. non-const first argument
163
164
165template<class T> struct convert_ostream_to_ref_others_to_c_plain_by_default {
166 typedef typename detail::IF<
167 is_instance_of_2<
168 T, std::basic_ostream
169 >::value,
170 T&,
171 typename const_copy_argument <T>::type
172 >::RET type;
173};
174
175template<class T> struct convert_istream_to_ref_others_to_c_plain_by_default {
176 typedef typename detail::IF<
177 is_instance_of_2<
178 T, std::basic_istream
179 >::value,
180 T&,
181 typename const_copy_argument <T>::type
182 >::RET type;
183};
184
185} // detail
186
187BOOST_LAMBDA_BE2(operator<<, bitwise_action< leftshift_action>, A, const B, detail::convert_ostream_to_ref_others_to_c_plain_by_default)
188BOOST_LAMBDA_BE2(operator>>, bitwise_action< rightshift_action>, A, const B, detail::convert_istream_to_ref_others_to_c_plain_by_default)
189
190
191// special case for io_manipulators.
192// function references cannot be given as arguments to lambda operator
193// expressions in general. With << and >> the use of manipulators is
194// so common, that specializations are provided to make them work.
195
196template<class Arg, class Ret, class ManipArg>
197inline const
198lambda_functor<
199 lambda_functor_base<
200 bitwise_action<leftshift_action>,
201 tuple<lambda_functor<Arg>, Ret(&)(ManipArg)>
202 >
203>
204operator<<(const lambda_functor<Arg>& a, Ret(&b)(ManipArg))
205{
206 return
207 lambda_functor_base<
208 bitwise_action<leftshift_action>,
209 tuple<lambda_functor<Arg>, Ret(&)(ManipArg)>
210 >
211 ( tuple<lambda_functor<Arg>, Ret(&)(ManipArg)>(a, b) );
212}
213
214template<class Arg, class Ret, class ManipArg>
215inline const
216lambda_functor<
217 lambda_functor_base<
218 bitwise_action<rightshift_action>,
219 tuple<lambda_functor<Arg>, Ret(&)(ManipArg)>
220 >
221>
222operator>>(const lambda_functor<Arg>& a, Ret(&b)(ManipArg))
223{
224 return
225 lambda_functor_base<
226 bitwise_action<rightshift_action>,
227 tuple<lambda_functor<Arg>, Ret(&)(ManipArg)>
228 >
229 ( tuple<lambda_functor<Arg>, Ret(&)(ManipArg)>(a, b) );
230}
231
232
233// (+ and -) take their arguments as const references.
234// This has consquences with pointer artihmetic
235// E.g int a[]; ... *a = 1 works but not *(a+1) = 1.
236// the result of a+1 would be const
237// To make the latter work too,
238// non-const arrays are taken as non-const and stored as non-const as well.
239#if defined BOOST_LAMBDA_PTR_ARITHMETIC_E1
240#error "Multiple defines of BOOST_LAMBDA_PTR_ARITHMETIC_E1"
241#endif
242
243#define BOOST_LAMBDA_PTR_ARITHMETIC_E1(OPER_NAME, ACTION, CONSTB) \
244template<class Arg, int N, class B> \
245inline const \
246lambda_functor< \
247 lambda_functor_base<ACTION, tuple<lambda_functor<Arg>, CONSTB(&)[N]> > \
248> \
249OPER_NAME (const lambda_functor<Arg>& a, CONSTB(&b)[N]) \
250{ \
251 return \
252 lambda_functor_base<ACTION, tuple<lambda_functor<Arg>, CONSTB(&)[N]> > \
253 (tuple<lambda_functor<Arg>, CONSTB(&)[N]>(a, b)); \
254}
255
256
257#if defined BOOST_LAMBDA_PTR_ARITHMETIC_E2
258#error "Multiple defines of BOOST_LAMBDA_PTR_ARITHMETIC_E2"
259#endif
260
261#define BOOST_LAMBDA_PTR_ARITHMETIC_E2(OPER_NAME, ACTION, CONSTA) \
262template<int N, class A, class Arg> \
263inline const \
264lambda_functor< \
265 lambda_functor_base<ACTION, tuple<CONSTA(&)[N], lambda_functor<Arg> > > \
266> \
267OPER_NAME (CONSTA(&a)[N], const lambda_functor<Arg>& b) \
268{ \
269 return \
270 lambda_functor_base<ACTION, tuple<CONSTA(&)[N], lambda_functor<Arg> > > \
271 (tuple<CONSTA(&)[N], lambda_functor<Arg> >(a, b)); \
272}
273
274
275BOOST_LAMBDA_PTR_ARITHMETIC_E1(operator+, arithmetic_action<plus_action>, B)
276BOOST_LAMBDA_PTR_ARITHMETIC_E2(operator+, arithmetic_action<plus_action>, A)
277BOOST_LAMBDA_PTR_ARITHMETIC_E1(operator+, arithmetic_action<plus_action>,const B)
278BOOST_LAMBDA_PTR_ARITHMETIC_E2(operator+, arithmetic_action<plus_action>,const A)
279
280
281//BOOST_LAMBDA_PTR_ARITHMETIC_E1(operator-, arithmetic_action<minus_action>)
282// This is not needed, since the result of ptr-ptr is an rvalue anyway
283
284BOOST_LAMBDA_PTR_ARITHMETIC_E2(operator-, arithmetic_action<minus_action>, A)
285BOOST_LAMBDA_PTR_ARITHMETIC_E2(operator-, arithmetic_action<minus_action>, const A)
286
287
288#undef BOOST_LAMBDA_BE1
289#undef BOOST_LAMBDA_BE2
290#undef BOOST_LAMBDA_BE3
291#undef BOOST_LAMBDA_BE
292#undef BOOST_LAMBDA_COMMA_OPERATOR_NAME
293
294#undef BOOST_LAMBDA_PTR_ARITHMETIC_E1
295#undef BOOST_LAMBDA_PTR_ARITHMETIC_E2
296
297
298// ---------------------------------------------------------------------
299// unary operators -----------------------------------------------------
300// ---------------------------------------------------------------------
301
302#if defined BOOST_LAMBDA_UE
303#error "Multiple defines of BOOST_LAMBDA_UE"
304#endif
305
306#define BOOST_LAMBDA_UE(OPER_NAME, ACTION) \
307template<class Arg> \
308inline const \
309lambda_functor<lambda_functor_base<ACTION, tuple<lambda_functor<Arg> > > > \
310OPER_NAME (const lambda_functor<Arg>& a) \
311{ \
312 return \
313 lambda_functor_base<ACTION, tuple<lambda_functor<Arg> > > \
314 ( tuple<lambda_functor<Arg> >(a) ); \
315}
316
317
318BOOST_LAMBDA_UE(operator+, unary_arithmetic_action<plus_action>)
319BOOST_LAMBDA_UE(operator-, unary_arithmetic_action<minus_action>)
320BOOST_LAMBDA_UE(operator~, bitwise_action<not_action>)
321BOOST_LAMBDA_UE(operator!, logical_action<not_action>)
322BOOST_LAMBDA_UE(operator++, pre_increment_decrement_action<increment_action>)
323BOOST_LAMBDA_UE(operator--, pre_increment_decrement_action<decrement_action>)
324BOOST_LAMBDA_UE(operator*, other_action<contentsof_action>)
325BOOST_LAMBDA_UE(operator&, other_action<addressof_action>)
326
327#if defined BOOST_LAMBDA_POSTFIX_UE
328#error "Multiple defines of BOOST_LAMBDA_POSTFIX_UE"
329#endif
330
331#define BOOST_LAMBDA_POSTFIX_UE(OPER_NAME, ACTION) \
332template<class Arg> \
333inline const \
334lambda_functor<lambda_functor_base<ACTION, tuple<lambda_functor<Arg> > > > \
335OPER_NAME (const lambda_functor<Arg>& a, int) \
336{ \
337 return \
338 lambda_functor_base<ACTION, tuple<lambda_functor<Arg> > > \
339 ( tuple<lambda_functor<Arg> >(a) ); \
340}
341
342
343BOOST_LAMBDA_POSTFIX_UE(operator++, post_increment_decrement_action<increment_action>)
344BOOST_LAMBDA_POSTFIX_UE(operator--, post_increment_decrement_action<decrement_action>)
345
346#undef BOOST_LAMBDA_UE
347#undef BOOST_LAMBDA_POSTFIX_UE
348
349} // namespace lambda
350} // namespace boost
351
352#endif
353