]> git.lyx.org Git - lyx.git/blob - boost/boost/operators.hpp
fix bulding on cygwin
[lyx.git] / boost / boost / operators.hpp
1 //  Boost operators.hpp header file  ----------------------------------------//
2
3 //  (C) Copyright David Abrahams, Jeremy Siek, and Daryle Walker 1999-2001.
4 //  Permission to copy, use, modify, sell and distribute this software is
5 //  granted provided this copyright notice appears in all copies.  This
6 //  software is provided "as is" without express or implied warranty, and
7 //  with no claim as to its suitability for any purpose.
8
9 //  See http://www.boost.org/libs/utility/operators.htm for documentation.
10
11 //  Revision History
12 //  21 Oct 02 Modified implementation of operators to allow compilers with a
13 //            correct named return value optimization (NRVO) to produce optimal
14 //            code.  (Daniel Frey)
15 //  02 Dec 01 Bug fixed in random_access_iteratable.  (Helmut Zeisel)
16 //  28 Sep 01 Factored out iterator operator groups.  (Daryle Walker)
17 //  27 Aug 01 'left' form for non commutative operators added;
18 //            additional classes for groups of related operators added;
19 //            workaround for empty base class optimization
20 //            bug of GCC 3.0 (Helmut Zeisel)
21 //  25 Jun 01 output_iterator_helper changes: removed default template 
22 //            parameters, added support for self-proxying, additional 
23 //            documentation and tests (Aleksey Gurtovoy)
24 //  29 May 01 Added operator classes for << and >>.  Added input and output
25 //            iterator helper classes.  Added classes to connect equality and
26 //            relational operators.  Added classes for groups of related
27 //            operators.  Reimplemented example operator and iterator helper
28 //            classes in terms of the new groups.  (Daryle Walker, with help
29 //            from Alexy Gurtovoy)
30 //  11 Feb 01 Fixed bugs in the iterator helpers which prevented explicitly
31 //            supplied arguments from actually being used (Dave Abrahams)
32 //  04 Jul 00 Fixed NO_OPERATORS_IN_NAMESPACE bugs, major cleanup and
33 //            refactoring of compiler workarounds, additional documentation
34 //            (Alexy Gurtovoy and Mark Rodgers with some help and prompting from
35 //            Dave Abrahams) 
36 //  28 Jun 00 General cleanup and integration of bugfixes from Mark Rodgers and
37 //            Jeremy Siek (Dave Abrahams)
38 //  20 Jun 00 Changes to accommodate Borland C++Builder 4 and Borland C++ 5.5
39 //            (Mark Rodgers)
40 //  20 Jun 00 Minor fixes to the prior revision (Aleksey Gurtovoy)
41 //  10 Jun 00 Support for the base class chaining technique was added
42 //            (Aleksey Gurtovoy). See documentation and the comments below 
43 //            for the details. 
44 //  12 Dec 99 Initial version with iterator operators (Jeremy Siek)
45 //  18 Nov 99 Change name "divideable" to "dividable", remove unnecessary
46 //            specializations of dividable, subtractable, modable (Ed Brey) 
47 //  17 Nov 99 Add comments (Beman Dawes)
48 //            Remove unnecessary specialization of operators<> (Ed Brey)
49 //  15 Nov 99 Fix less_than_comparable<T,U> second operand type for first two
50 //            operators.(Beman Dawes)
51 //  12 Nov 99 Add operators templates (Ed Brey)
52 //  11 Nov 99 Add single template parameter version for compilers without
53 //            partial specialization (Beman Dawes)
54 //  10 Nov 99 Initial version
55
56 // 10 Jun 00:
57 // An additional optional template parameter was added to most of 
58 // operator templates to support the base class chaining technique (see 
59 // documentation for the details). Unfortunately, a straightforward
60 // implementation of this change would have broken compatibility with the
61 // previous version of the library by making it impossible to use the same
62 // template name (e.g. 'addable') for both the 1- and 2-argument versions of
63 // an operator template. This implementation solves the backward-compatibility
64 // issue at the cost of some simplicity.
65 //
66 // One of the complications is an existence of special auxiliary class template
67 // 'is_chained_base<>' (see 'detail' namespace below), which is used
68 // to determine whether its template parameter is a library's operator template
69 // or not. You have to specialize 'is_chained_base<>' for each new 
70 // operator template you add to the library.
71 //
72 // However, most of the non-trivial implementation details are hidden behind 
73 // several local macros defined below, and as soon as you understand them,
74 // you understand the whole library implementation. 
75
76 #ifndef BOOST_OPERATORS_HPP
77 #define BOOST_OPERATORS_HPP
78
79 #include <boost/config.hpp>
80 #include <boost/iterator.hpp>
81
82 #if defined(__sgi) && !defined(__GNUC__)
83 #pragma set woff 1234
84 #endif
85
86 #if defined(BOOST_MSVC)
87 #   pragma warning( disable : 4284 ) // complaint about return type of 
88 #endif                               // operator-> not begin a UDT
89
90 namespace boost {
91 namespace detail {
92
93 // Helmut Zeisel, empty base class optimization bug with GCC 3.0.0
94 #if defined(__GNUC__) && __GNUC__==3 && __GNUC_MINOR__==0 && __GNU_PATCHLEVEL__==0
95 class empty_base {
96   bool dummy; 
97 };
98 #else
99 class empty_base {};
100 #endif
101
102 } // namespace detail
103 } // namespace boost
104
105 // In this section we supply the xxxx1 and xxxx2 forms of the operator
106 // templates, which are explicitly targeted at the 1-type-argument and
107 // 2-type-argument operator forms, respectively. Some compilers get confused
108 // when inline friend functions are overloaded in namespaces other than the
109 // global namespace. When BOOST_NO_OPERATORS_IN_NAMESPACE is defined, all of
110 // these templates must go in the global namespace.
111
112 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
113 namespace boost
114 {
115 #endif
116
117 //  Basic operator classes (contributed by Dave Abrahams) ------------------//
118
119 //  Note that friend functions defined in a class are implicitly inline.
120 //  See the C++ std, 11.4 [class.friend] paragraph 5
121
122 template <class T, class U, class B = ::boost::detail::empty_base>
123 struct less_than_comparable2 : B
124 {
125      friend bool operator<=(const T& x, const U& y) { return !(x > y); }
126      friend bool operator>=(const T& x, const U& y) { return !(x < y); }
127      friend bool operator>(const U& x, const T& y)  { return y < x; }
128      friend bool operator<(const U& x, const T& y)  { return y > x; }
129      friend bool operator<=(const U& x, const T& y) { return !(y < x); }
130      friend bool operator>=(const U& x, const T& y) { return !(y > x); }
131 };
132
133 template <class T, class B = ::boost::detail::empty_base>
134 struct less_than_comparable1 : B
135 {
136      friend bool operator>(const T& x, const T& y)  { return y < x; }
137      friend bool operator<=(const T& x, const T& y) { return !(y < x); }
138      friend bool operator>=(const T& x, const T& y) { return !(x < y); }
139 };
140
141 template <class T, class U, class B = ::boost::detail::empty_base>
142 struct equality_comparable2 : B
143 {
144      friend bool operator==(const U& y, const T& x) { return x == y; }
145      friend bool operator!=(const U& y, const T& x) { return !(x == y); }
146      friend bool operator!=(const T& y, const U& x) { return !(y == x); }
147 };
148
149 template <class T, class B = ::boost::detail::empty_base>
150 struct equality_comparable1 : B
151 {
152      friend bool operator!=(const T& x, const T& y) { return !(x == y); }
153 };
154
155 //  NRVO-friendly implementation (contributed by Daniel Frey) ---------------//
156
157 #if defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
158
159 // This is the optimal implementation for ISO/ANSI C++,
160 // but it requires the compiler to implement the NRVO.
161 // If the compiler has no NRVO, this is the best symmetric
162 // implementation available.
163
164 #define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP )                         \
165 template <class T, class U, class B = ::boost::detail::empty_base>            \
166 struct NAME##2 : B                                                            \
167 {                                                                             \
168   friend T operator OP( const T& lhs, const U& rhs )                          \
169     { T nrv( lhs ); nrv OP##= rhs; return nrv; }                              \
170   friend T operator OP( const U& lhs, const T& rhs )                          \
171     { T nrv( rhs ); nrv OP##= lhs; return nrv; }                              \
172 };                                                                            \
173                                                                               \
174 template <class T, class B = ::boost::detail::empty_base>                     \
175 struct NAME##1 : B                                                            \
176 {                                                                             \
177   friend T operator OP( const T& lhs, const T& rhs )                          \
178     { T nrv( lhs ); nrv OP##= rhs; return nrv; }                              \
179 };
180
181 #define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP )                     \
182 template <class T, class U, class B = ::boost::detail::empty_base>            \
183 struct NAME##2 : B                                                            \
184 {                                                                             \
185   friend T operator OP( const T& lhs, const U& rhs )                          \
186     { T nrv( lhs ); nrv OP##= rhs; return nrv; }                              \
187 };                                                                            \
188                                                                               \
189 template <class T, class U, class B = ::boost::detail::empty_base>            \
190 struct NAME##2_left : B                                                       \
191 {                                                                             \
192   friend T operator OP( const U& lhs, const T& rhs )                          \
193     { T nrv( lhs ); nrv OP##= rhs; return nrv; }                              \
194 };                                                                            \
195                                                                               \
196 template <class T, class B = ::boost::detail::empty_base>                     \
197 struct NAME##1 : B                                                            \
198 {                                                                             \
199   friend T operator OP( const T& lhs, const T& rhs )                          \
200     { T nrv( lhs ); nrv OP##= rhs; return nrv; }                              \
201 };
202
203 #else // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
204
205 // For compilers without NRVO the following code is optimal, but not symmetric!
206 // Note that the implementation of NAME##2_left only looks cool, but doesn't
207 // provide optimization opportunities to the compiler :)
208
209 #define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP )                         \
210 template <class T, class U, class B = ::boost::detail::empty_base>            \
211 struct NAME##2 : B                                                            \
212 {                                                                             \
213   friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; }       \
214   friend T operator OP( const U& lhs, T rhs ) { return rhs OP##= lhs; }       \
215 };                                                                            \
216                                                                               \
217 template <class T, class B = ::boost::detail::empty_base>                     \
218 struct NAME##1 : B                                                            \
219 {                                                                             \
220   friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; }       \
221 };
222
223 #define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP )                     \
224 template <class T, class U, class B = ::boost::detail::empty_base>            \
225 struct NAME##2 : B                                                            \
226 {                                                                             \
227   friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; }       \
228 };                                                                            \
229                                                                               \
230 template <class T, class U, class B = ::boost::detail::empty_base>            \
231 struct NAME##2_left : B                                                       \
232 {                                                                             \
233   friend T operator OP( const U& lhs, const T& rhs )                          \
234     { return T( lhs ) OP##= rhs; }                                            \
235 };                                                                            \
236                                                                               \
237 template <class T, class B = ::boost::detail::empty_base>                     \
238 struct NAME##1 : B                                                            \
239 {                                                                             \
240   friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; }       \
241 };
242
243 #endif // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
244
245 BOOST_BINARY_OPERATOR_COMMUTATIVE( multipliable, * )
246 BOOST_BINARY_OPERATOR_COMMUTATIVE( addable, + )
247 BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( subtractable, - )
248 BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( dividable, / )
249 BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( modable, % )
250 BOOST_BINARY_OPERATOR_COMMUTATIVE( xorable, ^ )
251 BOOST_BINARY_OPERATOR_COMMUTATIVE( andable, & )
252 BOOST_BINARY_OPERATOR_COMMUTATIVE( orable, | )
253
254 #undef BOOST_BINARY_OPERATOR_COMMUTATIVE
255 #undef BOOST_BINARY_OPERATOR_NON_COMMUTATIVE
256
257 //  incrementable and decrementable contributed by Jeremy Siek
258
259 template <class T, class B = ::boost::detail::empty_base>
260 struct incrementable : B
261 {
262   friend T operator++(T& x, int)
263   {
264     incrementable_type nrv(x);
265     ++x;
266     return nrv;
267   }
268 private: // The use of this typedef works around a Borland bug
269   typedef T incrementable_type;
270 };
271
272 template <class T, class B = ::boost::detail::empty_base>
273 struct decrementable : B
274 {
275   friend T operator--(T& x, int)
276   {
277     decrementable_type nrv(x);
278     --x;
279     return nrv;
280   }
281 private: // The use of this typedef works around a Borland bug
282   typedef T decrementable_type;
283 };
284
285 //  Iterator operator classes (contributed by Jeremy Siek) ------------------//
286
287 template <class T, class P, class B = ::boost::detail::empty_base>
288 struct dereferenceable : B
289 {
290   P operator->() const
291   { 
292     return &*static_cast<const T&>(*this); 
293   }
294 };
295
296 template <class T, class I, class R, class B = ::boost::detail::empty_base>
297 struct indexable : B
298 {
299   R operator[](I n) const
300   {
301     return *(static_cast<const T&>(*this) + n);
302   }
303 };
304
305 //  More operator classes (contributed by Daryle Walker) --------------------//
306 //  (NRVO-friendly implementation contributed by Daniel Frey) ---------------//
307
308 #if defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
309
310 #define BOOST_BINARY_OPERATOR( NAME, OP )                                     \
311 template <class T, class U, class B = ::boost::detail::empty_base>            \
312 struct NAME##2 : B                                                            \
313 {                                                                             \
314   friend T operator OP( const T& lhs, const U& rhs )                          \
315     { T nrv( lhs ); nrv OP##= rhs; return nrv; }                              \
316 };                                                                            \
317                                                                               \
318 template <class T, class B = ::boost::detail::empty_base>                     \
319 struct NAME##1 : B                                                            \
320 {                                                                             \
321   friend T operator OP( const T& lhs, const T& rhs )                          \
322     { T nrv( lhs ); nrv OP##= rhs; return nrv; }                              \
323 };
324
325 #else // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
326
327 #define BOOST_BINARY_OPERATOR( NAME, OP )                                     \
328 template <class T, class U, class B = ::boost::detail::empty_base>            \
329 struct NAME##2 : B                                                            \
330 {                                                                             \
331   friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; }       \
332 };                                                                            \
333                                                                               \
334 template <class T, class B = ::boost::detail::empty_base>                     \
335 struct NAME##1 : B                                                            \
336 {                                                                             \
337   friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; }       \
338 };
339
340 #endif // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
341
342 BOOST_BINARY_OPERATOR( left_shiftable, << )
343 BOOST_BINARY_OPERATOR( right_shiftable, >> )
344
345 #undef BOOST_BINARY_OPERATOR
346
347 template <class T, class U, class B = ::boost::detail::empty_base>
348 struct equivalent2 : B
349 {
350   friend bool operator==(const T& x, const U& y)
351   {
352     return !(x < y) && !(x > y);
353   }
354 };
355
356 template <class T, class B = ::boost::detail::empty_base>
357 struct equivalent1 : B
358 {
359   friend bool operator==(const T&x, const T&y)
360   {
361     return !(x < y) && !(y < x);
362   }
363 };
364
365 template <class T, class U, class B = ::boost::detail::empty_base>
366 struct partially_ordered2 : B
367 {
368   friend bool operator<=(const T& x, const U& y)
369     { return (x < y) || (x == y); }
370   friend bool operator>=(const T& x, const U& y)
371     { return (x > y) || (x == y); }
372   friend bool operator>(const U& x, const T& y)
373     { return y < x; }
374   friend bool operator<(const U& x, const T& y)
375     { return y > x; }
376   friend bool operator<=(const U& x, const T& y)
377     { return (y > x) || (y == x); }
378   friend bool operator>=(const U& x, const T& y)
379     { return (y < x) || (y == x); }
380 };
381
382 template <class T, class B = ::boost::detail::empty_base>
383 struct partially_ordered1 : B
384 {
385   friend bool operator>(const T& x, const T& y)
386     { return y < x; }
387   friend bool operator<=(const T& x, const T& y)
388     { return (x < y) || (x == y); }
389   friend bool operator>=(const T& x, const T& y)
390     { return (y < x) || (x == y); }
391 };
392
393 //  Combined operator classes (contributed by Daryle Walker) ----------------//
394
395 template <class T, class U, class B = ::boost::detail::empty_base>
396 struct totally_ordered2
397     : less_than_comparable2<T, U
398     , equality_comparable2<T, U, B
399       > > {};
400
401 template <class T, class B = ::boost::detail::empty_base>
402 struct totally_ordered1
403     : less_than_comparable1<T
404     , equality_comparable1<T, B
405       > > {};
406
407 template <class T, class U, class B = ::boost::detail::empty_base>
408 struct additive2
409     : addable2<T, U
410     , subtractable2<T, U, B
411       > > {};
412
413 template <class T, class B = ::boost::detail::empty_base>
414 struct additive1
415     : addable1<T
416     , subtractable1<T, B
417       > > {};
418
419 template <class T, class U, class B = ::boost::detail::empty_base>
420 struct multiplicative2
421     : multipliable2<T, U
422     , dividable2<T, U, B
423       > > {};
424
425 template <class T, class B = ::boost::detail::empty_base>
426 struct multiplicative1
427     : multipliable1<T
428     , dividable1<T, B
429       > > {};
430
431 template <class T, class U, class B = ::boost::detail::empty_base>
432 struct integer_multiplicative2
433     : multiplicative2<T, U
434     , modable2<T, U, B
435       > > {};
436
437 template <class T, class B = ::boost::detail::empty_base>
438 struct integer_multiplicative1
439     : multiplicative1<T
440     , modable1<T, B
441       > > {};
442
443 template <class T, class U, class B = ::boost::detail::empty_base>
444 struct arithmetic2
445     : additive2<T, U
446     , multiplicative2<T, U, B
447       > > {};
448
449 template <class T, class B = ::boost::detail::empty_base>
450 struct arithmetic1
451     : additive1<T
452     , multiplicative1<T, B
453       > > {};
454
455 template <class T, class U, class B = ::boost::detail::empty_base>
456 struct integer_arithmetic2
457     : additive2<T, U
458     , integer_multiplicative2<T, U, B
459       > > {};
460
461 template <class T, class B = ::boost::detail::empty_base>
462 struct integer_arithmetic1
463     : additive1<T
464     , integer_multiplicative1<T, B
465       > > {};
466
467 template <class T, class U, class B = ::boost::detail::empty_base>
468 struct bitwise2
469     : xorable2<T, U
470     , andable2<T, U
471     , orable2<T, U, B
472       > > > {};
473
474 template <class T, class B = ::boost::detail::empty_base>
475 struct bitwise1
476     : xorable1<T
477     , andable1<T
478     , orable1<T, B
479       > > > {};
480
481 template <class T, class B = ::boost::detail::empty_base>
482 struct unit_steppable
483     : incrementable<T
484     , decrementable<T, B
485       > > {};
486
487 template <class T, class U, class B = ::boost::detail::empty_base>
488 struct shiftable2
489     : left_shiftable2<T, U
490     , right_shiftable2<T, U, B
491       > > {};
492
493 template <class T, class B = ::boost::detail::empty_base>
494 struct shiftable1
495     : left_shiftable1<T
496     , right_shiftable1<T, B
497       > > {};
498
499 template <class T, class U, class B = ::boost::detail::empty_base>
500 struct ring_operators2
501     : additive2<T, U
502     , subtractable2_left<T, U
503     , multipliable2<T, U, B
504       > > > {};
505
506 template <class T, class B = ::boost::detail::empty_base>
507 struct ring_operators1
508     : additive1<T
509     , multipliable1<T, B
510       > > {};
511
512 template <class T, class U, class B = ::boost::detail::empty_base>
513 struct ordered_ring_operators2
514     : ring_operators2<T, U
515     , totally_ordered2<T, U, B
516       > > {};
517
518 template <class T, class B = ::boost::detail::empty_base>
519 struct ordered_ring_operators1
520     : ring_operators1<T
521     , totally_ordered1<T, B
522       > > {};
523
524 template <class T, class U, class B = ::boost::detail::empty_base>
525 struct field_operators2
526     : ring_operators2<T, U
527     , dividable2<T, U
528     , dividable2_left<T, U, B
529       > > > {};
530
531 template <class T, class B = ::boost::detail::empty_base>
532 struct field_operators1
533     : ring_operators1<T
534     , dividable1<T, B
535       > > {};
536
537 template <class T, class U, class B = ::boost::detail::empty_base>
538 struct ordered_field_operators2
539     : field_operators2<T, U
540     , totally_ordered2<T, U, B
541       > > {};
542
543 template <class T, class B = ::boost::detail::empty_base>
544 struct ordered_field_operators1
545     : field_operators1<T
546     , totally_ordered1<T, B
547       > > {};
548
549 template <class T, class U, class B = ::boost::detail::empty_base>
550 struct euclidian_ring_operators2
551     : ring_operators2<T, U
552     , dividable2<T, U
553     , dividable2_left<T, U
554     , modable2<T, U
555     , modable2_left<T, U, B
556       > > > > > {};
557
558 template <class T, class B = ::boost::detail::empty_base>
559 struct euclidian_ring_operators1
560     : ring_operators1<T
561     , dividable1<T
562     , modable1<T, B
563       > > > {};
564
565 template <class T, class U, class B = ::boost::detail::empty_base>
566 struct ordered_euclidian_ring_operators2
567     : totally_ordered2<T, U
568     , euclidian_ring_operators2<T, U, B
569       > > {};
570
571 template <class T, class B = ::boost::detail::empty_base>
572 struct ordered_euclidian_ring_operators1
573     : totally_ordered1<T
574     , euclidian_ring_operators1<T, B
575       > > {};
576       
577 template <class T, class P, class B = ::boost::detail::empty_base>
578 struct input_iteratable
579     : equality_comparable1<T
580     , incrementable<T
581     , dereferenceable<T, P, B
582       > > > {};
583
584 template <class T, class B = ::boost::detail::empty_base>
585 struct output_iteratable
586     : incrementable<T, B
587       > {};
588
589 template <class T, class P, class B = ::boost::detail::empty_base>
590 struct forward_iteratable
591     : input_iteratable<T, P, B
592       > {};
593
594 template <class T, class P, class B = ::boost::detail::empty_base>
595 struct bidirectional_iteratable
596     : forward_iteratable<T, P
597     , decrementable<T, B
598       > > {};
599
600 //  To avoid repeated derivation from equality_comparable,
601 //  which is an indirect base class of bidirectional_iterable,
602 //  random_access_iteratable must not be derived from totally_ordered1
603 //  but from less_than_comparable1 only. (Helmut Zeisel, 02-Dec-2001)
604 template <class T, class P, class D, class R, class B = ::boost::detail::empty_base>
605 struct random_access_iteratable
606     : bidirectional_iteratable<T, P
607     , less_than_comparable1<T
608     , additive2<T, D
609     , indexable<T, D, R, B
610       > > > > {};
611
612 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
613 } // namespace boost
614 #endif // BOOST_NO_OPERATORS_IN_NAMESPACE
615
616
617 // BOOST_IMPORT_TEMPLATE1 .. BOOST_IMPORT_TEMPLATE4 -
618 //
619 // When BOOST_NO_OPERATORS_IN_NAMESPACE is defined we need a way to import an
620 // operator template into the boost namespace. BOOST_IMPORT_TEMPLATE1 is used
621 // for one-argument forms of operator templates; BOOST_IMPORT_TEMPLATE2 for
622 // two-argument forms. Note that these macros expect to be invoked from within
623 // boost.
624
625 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
626
627   // The template is already in boost so we have nothing to do.
628 # define BOOST_IMPORT_TEMPLATE4(template_name)
629 # define BOOST_IMPORT_TEMPLATE3(template_name)
630 # define BOOST_IMPORT_TEMPLATE2(template_name)
631 # define BOOST_IMPORT_TEMPLATE1(template_name)
632
633 #else // BOOST_NO_OPERATORS_IN_NAMESPACE
634
635 #  ifndef BOOST_NO_USING_TEMPLATE
636
637      // Bring the names in with a using-declaration
638      // to avoid stressing the compiler.
639 #    define BOOST_IMPORT_TEMPLATE4(template_name) using ::template_name;
640 #    define BOOST_IMPORT_TEMPLATE3(template_name) using ::template_name;
641 #    define BOOST_IMPORT_TEMPLATE2(template_name) using ::template_name;
642 #    define BOOST_IMPORT_TEMPLATE1(template_name) using ::template_name;
643
644 #  else
645
646      // Otherwise, because a Borland C++ 5.5 bug prevents a using declaration
647      // from working, we are forced to use inheritance for that compiler.
648 #    define BOOST_IMPORT_TEMPLATE4(template_name)                                          \
649      template <class T, class U, class V, class W, class B = ::boost::detail::empty_base>  \
650      struct template_name : ::template_name<T, U, V, W, B> {};
651
652 #    define BOOST_IMPORT_TEMPLATE3(template_name)                                 \
653      template <class T, class U, class V, class B = ::boost::detail::empty_base>  \
654      struct template_name : ::template_name<T, U, V, B> {};
655
656 #    define BOOST_IMPORT_TEMPLATE2(template_name)                              \
657      template <class T, class U, class B = ::boost::detail::empty_base>        \
658      struct template_name : ::template_name<T, U, B> {};
659
660 #    define BOOST_IMPORT_TEMPLATE1(template_name)                              \
661      template <class T, class B = ::boost::detail::empty_base>                 \
662      struct template_name : ::template_name<T, B> {};
663
664 #  endif // BOOST_NO_USING_TEMPLATE
665
666 #endif // BOOST_NO_OPERATORS_IN_NAMESPACE
667
668 //
669 // Here's where we put it all together, defining the xxxx forms of the templates
670 // in namespace boost. We also define specializations of is_chained_base<> for
671 // the xxxx, xxxx1, and xxxx2 templates, importing them into boost:: as
672 // neccessary.
673 //
674 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
675
676 // is_chained_base<> - a traits class used to distinguish whether an operator
677 // template argument is being used for base class chaining, or is specifying a
678 // 2nd argument type.
679
680 namespace boost {
681 // A type parameter is used instead of a plain bool because Borland's compiler
682 // didn't cope well with the more obvious non-type template parameter.
683 namespace detail {
684   struct true_t {};
685   struct false_t {};
686 } // namespace detail
687
688 // Unspecialized version assumes that most types are not being used for base
689 // class chaining. We specialize for the operator templates defined in this
690 // library.
691 template<class T> struct is_chained_base {
692   typedef ::boost::detail::false_t value;
693 };
694
695 } // namespace boost
696
697 // Import a 4-type-argument operator template into boost (if neccessary) and
698 // provide a specialization of 'is_chained_base<>' for it.
699 # define BOOST_OPERATOR_TEMPLATE4(template_name4)                     \
700   BOOST_IMPORT_TEMPLATE4(template_name4)                              \
701   template<class T, class U, class V, class W, class B>               \
702   struct is_chained_base< ::boost::template_name4<T, U, V, W, B> > {  \
703     typedef ::boost::detail::true_t value;                            \
704   };
705
706 // Import a 3-type-argument operator template into boost (if neccessary) and
707 // provide a specialization of 'is_chained_base<>' for it.
708 # define BOOST_OPERATOR_TEMPLATE3(template_name3)                     \
709   BOOST_IMPORT_TEMPLATE3(template_name3)                              \
710   template<class T, class U, class V, class B>                        \
711   struct is_chained_base< ::boost::template_name3<T, U, V, B> > {     \
712     typedef ::boost::detail::true_t value;                            \
713   };
714
715 // Import a 2-type-argument operator template into boost (if neccessary) and
716 // provide a specialization of 'is_chained_base<>' for it.
717 # define BOOST_OPERATOR_TEMPLATE2(template_name2)                  \
718   BOOST_IMPORT_TEMPLATE2(template_name2)                           \
719   template<class T, class U, class B>                              \
720   struct is_chained_base< ::boost::template_name2<T, U, B> > {     \
721     typedef ::boost::detail::true_t value;                         \
722   };
723
724 // Import a 1-type-argument operator template into boost (if neccessary) and
725 // provide a specialization of 'is_chained_base<>' for it.
726 # define BOOST_OPERATOR_TEMPLATE1(template_name1)                  \
727   BOOST_IMPORT_TEMPLATE1(template_name1)                           \
728   template<class T, class B>                                       \
729   struct is_chained_base< ::boost::template_name1<T, B> > {        \
730     typedef ::boost::detail::true_t value;                         \
731   };
732
733 // BOOST_OPERATOR_TEMPLATE(template_name) defines template_name<> such that it
734 // can be used for specifying both 1-argument and 2-argument forms. Requires the
735 // existence of two previously defined class templates named '<template_name>1'
736 // and '<template_name>2' which must implement the corresponding 1- and 2-
737 // argument forms.
738 //
739 // The template type parameter O == is_chained_base<U>::value is used to
740 // distinguish whether the 2nd argument to <template_name> is being used for
741 // base class chaining from another boost operator template or is describing a
742 // 2nd operand type. O == true_t only when U is actually an another operator
743 // template from the library. Partial specialization is used to select an
744 // implementation in terms of either '<template_name>1' or '<template_name>2'.
745 //
746
747 # define BOOST_OPERATOR_TEMPLATE(template_name)                    \
748 template <class T                                                  \
749          ,class U = T                                              \
750          ,class B = ::boost::detail::empty_base                    \
751          ,class O = typename is_chained_base<U>::value             \
752          >                                                         \
753 struct template_name : template_name##2<T, U, B> {};               \
754                                                                    \
755 template<class T, class U, class B>                                \
756 struct template_name<T, U, B, ::boost::detail::true_t>             \
757   : template_name##1<T, U> {};                                     \
758                                                                    \
759 template <class T, class B>                                        \
760 struct template_name<T, T, B, ::boost::detail::false_t>            \
761   : template_name##1<T, B> {};                                     \
762                                                                    \
763 template<class T, class U, class B, class O>                       \
764 struct is_chained_base< ::boost::template_name<T, U, B, O> > {     \
765   typedef ::boost::detail::true_t value;                           \
766 };                                                                 \
767                                                                    \
768 BOOST_OPERATOR_TEMPLATE2(template_name##2)                         \
769 BOOST_OPERATOR_TEMPLATE1(template_name##1)
770
771
772 #else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
773
774 #  define BOOST_OPERATOR_TEMPLATE4(template_name4) \
775         BOOST_IMPORT_TEMPLATE4(template_name4)
776 #  define BOOST_OPERATOR_TEMPLATE3(template_name3) \
777         BOOST_IMPORT_TEMPLATE3(template_name3)
778 #  define BOOST_OPERATOR_TEMPLATE2(template_name2) \
779         BOOST_IMPORT_TEMPLATE2(template_name2)
780 #  define BOOST_OPERATOR_TEMPLATE1(template_name1) \
781         BOOST_IMPORT_TEMPLATE1(template_name1)
782
783    // In this case we can only assume that template_name<> is equivalent to the
784    // more commonly needed template_name1<> form.
785 #  define BOOST_OPERATOR_TEMPLATE(template_name)                   \
786    template <class T, class B = ::boost::detail::empty_base>       \
787    struct template_name : template_name##1<T, B> {};
788
789 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
790
791 namespace boost {
792     
793 BOOST_OPERATOR_TEMPLATE(less_than_comparable)
794 BOOST_OPERATOR_TEMPLATE(equality_comparable)
795 BOOST_OPERATOR_TEMPLATE(multipliable)
796 BOOST_OPERATOR_TEMPLATE(addable)
797 BOOST_OPERATOR_TEMPLATE(subtractable)
798 BOOST_OPERATOR_TEMPLATE2(subtractable2_left)
799 BOOST_OPERATOR_TEMPLATE(dividable)
800 BOOST_OPERATOR_TEMPLATE2(dividable2_left)
801 BOOST_OPERATOR_TEMPLATE(modable)
802 BOOST_OPERATOR_TEMPLATE2(modable2_left)
803 BOOST_OPERATOR_TEMPLATE(xorable)
804 BOOST_OPERATOR_TEMPLATE(andable)
805 BOOST_OPERATOR_TEMPLATE(orable)
806
807 BOOST_OPERATOR_TEMPLATE1(incrementable)
808 BOOST_OPERATOR_TEMPLATE1(decrementable)
809
810 BOOST_OPERATOR_TEMPLATE2(dereferenceable)
811 BOOST_OPERATOR_TEMPLATE3(indexable)
812
813 BOOST_OPERATOR_TEMPLATE(left_shiftable)
814 BOOST_OPERATOR_TEMPLATE(right_shiftable)
815 BOOST_OPERATOR_TEMPLATE(equivalent)
816 BOOST_OPERATOR_TEMPLATE(partially_ordered)
817
818 BOOST_OPERATOR_TEMPLATE(totally_ordered)
819 BOOST_OPERATOR_TEMPLATE(additive)
820 BOOST_OPERATOR_TEMPLATE(multiplicative)
821 BOOST_OPERATOR_TEMPLATE(integer_multiplicative)
822 BOOST_OPERATOR_TEMPLATE(arithmetic)
823 BOOST_OPERATOR_TEMPLATE(integer_arithmetic)
824 BOOST_OPERATOR_TEMPLATE(bitwise)
825 BOOST_OPERATOR_TEMPLATE1(unit_steppable)
826 BOOST_OPERATOR_TEMPLATE(shiftable)
827 BOOST_OPERATOR_TEMPLATE(ring_operators)
828 BOOST_OPERATOR_TEMPLATE(ordered_ring_operators)
829 BOOST_OPERATOR_TEMPLATE(field_operators)
830 BOOST_OPERATOR_TEMPLATE(ordered_field_operators)
831 BOOST_OPERATOR_TEMPLATE(euclidian_ring_operators)
832 BOOST_OPERATOR_TEMPLATE(ordered_euclidian_ring_operators)
833 BOOST_OPERATOR_TEMPLATE2(input_iteratable)
834 BOOST_OPERATOR_TEMPLATE1(output_iteratable)
835 BOOST_OPERATOR_TEMPLATE2(forward_iteratable)
836 BOOST_OPERATOR_TEMPLATE2(bidirectional_iteratable)
837 BOOST_OPERATOR_TEMPLATE4(random_access_iteratable)
838
839 #undef BOOST_OPERATOR_TEMPLATE
840 #undef BOOST_OPERATOR_TEMPLATE4
841 #undef BOOST_OPERATOR_TEMPLATE3
842 #undef BOOST_OPERATOR_TEMPLATE2
843 #undef BOOST_OPERATOR_TEMPLATE1
844 #undef BOOST_IMPORT_TEMPLATE1
845 #undef BOOST_IMPORT_TEMPLATE2
846 #undef BOOST_IMPORT_TEMPLATE3
847 #undef BOOST_IMPORT_TEMPLATE4
848
849 // The following 'operators' classes can only be used portably if the derived class
850 // declares ALL of the required member operators.
851 template <class T, class U>
852 struct operators2
853     : totally_ordered2<T,U
854     , integer_arithmetic2<T,U
855     , bitwise2<T,U
856       > > > {};
857
858 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
859 template <class T, class U = T>
860 struct operators : operators2<T, U> {};
861
862 template <class T> struct operators<T, T>
863 #else
864 template <class T> struct operators
865 #endif
866     : totally_ordered<T
867     , integer_arithmetic<T
868     , bitwise<T
869     , unit_steppable<T
870       > > > > {};
871
872 //  Iterator helper classes (contributed by Jeremy Siek) -------------------//
873 //  (Input and output iterator helpers contributed by Daryle Walker) -------//
874 //  (Changed to use combined operator classes by Daryle Walker) ------------//
875 template <class T,
876           class V,
877           class D = std::ptrdiff_t,
878           class P = V const *,
879           class R = V const &>
880 struct input_iterator_helper
881   : input_iteratable<T, P
882   , boost::iterator<std::input_iterator_tag, V, D, P, R
883     > > {};
884
885 template<class T>
886 struct output_iterator_helper
887   : output_iteratable<T
888   , boost::iterator<std::output_iterator_tag, void, void, void, void
889   > >
890 {
891   T& operator*()  { return static_cast<T&>(*this); }
892   T& operator++() { return static_cast<T&>(*this); }
893 };
894
895 template <class T,
896           class V,
897           class D = std::ptrdiff_t,
898           class P = V*,
899           class R = V&>
900 struct forward_iterator_helper
901   : forward_iteratable<T, P
902   , boost::iterator<std::forward_iterator_tag, V, D, P, R
903     > > {};
904
905 template <class T,
906           class V,
907           class D = std::ptrdiff_t,
908           class P = V*,
909           class R = V&>
910 struct bidirectional_iterator_helper
911   : bidirectional_iteratable<T, P
912   , boost::iterator<std::bidirectional_iterator_tag, V, D, P, R
913     > > {};
914
915 template <class T,
916           class V, 
917           class D = std::ptrdiff_t,
918           class P = V*,
919           class R = V&>
920 struct random_access_iterator_helper
921   : random_access_iteratable<T, P, D, R
922   , boost::iterator<std::random_access_iterator_tag, V, D, P, R
923     > >
924 {
925   friend D requires_difference_operator(const T& x, const T& y) {
926     return x - y;
927   }
928 }; // random_access_iterator_helper
929
930 } // namespace boost
931
932 #if defined(__sgi) && !defined(__GNUC__)
933 #pragma reset woff 1234
934 #endif
935
936 #endif // BOOST_OPERATORS_HPP