operators.hpp 38 KB

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