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