1 // Boost.TypeErasure library
3 // Copyright 2011 Steven Watanabe
5 // Distributed under the Boost Software License Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_TYPE_ERASURE_OPERATORS_HPP_INCLUDED
12 #define BOOST_TYPE_ERASURE_OPERATORS_HPP_INCLUDED
15 #include <boost/utility/enable_if.hpp>
16 #include <boost/type_erasure/detail/const.hpp>
17 #include <boost/type_erasure/call.hpp>
18 #include <boost/type_erasure/concept_interface.hpp>
19 #include <boost/type_erasure/placeholder.hpp>
20 #include <boost/type_erasure/concept_of.hpp>
21 #include <boost/type_erasure/derived.hpp>
22 #include <boost/type_erasure/rebind_any.hpp>
23 #include <boost/type_erasure/param.hpp>
24 #include <boost/type_erasure/check_match.hpp>
25 #include <boost/type_erasure/relaxed.hpp>
26 #include <boost/type_erasure/typeid_of.hpp>
29 namespace type_erasure {
31 template<class Concept, class Placeholder>
35 #define BOOST_TYPE_ERASURE_UNARY_INPLACE_OPERATOR(name, op) \
36 template<class T = _self> \
39 static void apply(T& arg) { op arg; } \
42 template<class T, class Base> \
43 struct concept_interface<name<T>, Base, T, \
44 typename ::boost::enable_if< \
45 detail::should_be_non_const<T, Base> \
49 typedef typename ::boost::type_erasure::derived<Base>::type _derived; \
50 _derived& operator op() \
52 ::boost::type_erasure::call(name<T>(), *this); \
53 return static_cast<_derived&>(*this); \
55 typename ::boost::type_erasure::rebind_any<Base, T>::type operator op(int) \
57 typename ::boost::type_erasure::rebind_any<Base, T>::type result( \
58 static_cast<_derived&>(*this)); \
59 ::boost::type_erasure::call(name<T>(), *this); \
64 template<class T, class Base> \
65 struct concept_interface<name<T>, Base, T, \
66 typename ::boost::enable_if< \
67 detail::should_be_const<T, Base> \
71 typedef typename ::boost::type_erasure::derived<Base>::type _derived; \
72 const _derived& operator op() const \
74 ::boost::type_erasure::call(name<T>(), *this); \
75 return static_cast<const _derived&>(*this); \
77 typename ::boost::type_erasure::rebind_any<Base, T>::type operator op(int) const \
79 typename ::boost::type_erasure::rebind_any<Base, T>::type result( \
80 static_cast<const _derived&>(*this)); \
81 ::boost::type_erasure::call(name<T>(), *this); \
87 * The @ref incrementable concept allow pre and
88 * post increment on an @ref any. The contained
89 * type must provide a pre-increment operator.
91 BOOST_TYPE_ERASURE_UNARY_INPLACE_OPERATOR(incrementable, ++)
93 * The @ref decrementable concept allow pre and
94 * post decrement on an @ref any. The contained
95 * type must provide a pre-decrement operator.
97 BOOST_TYPE_ERASURE_UNARY_INPLACE_OPERATOR(decrementable, --)
99 #undef BOOST_TYPE_ERASURE_UNARY_INPLACE_OPERATOR
102 #define BOOST_TYPE_ERASURE_UNARY_OPERATOR(name, op) \
103 template<class T = _self, class R = T> \
106 static R apply(const T& arg) { return op arg; } \
109 template<class T, class R, class Base> \
110 struct concept_interface<name<T, R>, Base, T> : Base \
112 typename ::boost::type_erasure::rebind_any<Base, R>::type operator op() const \
114 return ::boost::type_erasure::call(name<T, R>(), *this); \
119 * The @ref complementable concept allow use of the bitwise
120 * complement operator on an @ref any.
122 BOOST_TYPE_ERASURE_UNARY_OPERATOR(complementable, ~)
124 * The @ref negatable concept allow use of the unary
125 * minus operator on an @ref any.
127 BOOST_TYPE_ERASURE_UNARY_OPERATOR(negatable, -)
129 #undef BOOST_TYPE_ERASURE_UNARY_OPERATOR
131 template<class R, class T = _self>
132 struct dereferenceable
134 static R apply(const T& arg) { return *arg; }
137 /// \cond show_operators
139 template<class R, class T, class Base>
140 struct concept_interface<dereferenceable<R, T>, Base, T> : Base
142 typename ::boost::type_erasure::rebind_any<Base, R>::type operator*() const
144 return ::boost::type_erasure::call(dereferenceable<R, T>(), *this);
151 #define BOOST_TYPE_ERASURE_BINARY_OPERATOR(name, op) \
152 template<class T = _self, class U = T, class R = T> \
155 static R apply(const T& lhs, const U& rhs) { return lhs op rhs; } \
158 template<class T, class U, class R, class Base> \
159 struct concept_interface<name<T, U, R>, Base, T> : Base \
161 friend typename rebind_any<Base, R>::type \
162 operator op(const typename derived<Base>::type& lhs, \
163 typename as_param<Base, const U&>::type rhs) \
165 return ::boost::type_erasure::call(name<T, U, R>(), lhs, rhs); \
169 template<class T, class U, class R, class Base> \
170 struct concept_interface< \
174 typename ::boost::disable_if< \
175 ::boost::type_erasure::is_placeholder<T> >::type \
178 friend typename rebind_any<Base, R>::type \
179 operator op(const T& lhs, \
180 const typename derived<Base>::type& rhs) \
182 return ::boost::type_erasure::call(name<T, U, R>(), lhs, rhs); \
186 BOOST_TYPE_ERASURE_BINARY_OPERATOR(addable, +)
187 BOOST_TYPE_ERASURE_BINARY_OPERATOR(subtractable, -)
188 BOOST_TYPE_ERASURE_BINARY_OPERATOR(multipliable, *)
189 BOOST_TYPE_ERASURE_BINARY_OPERATOR(dividable, /)
190 BOOST_TYPE_ERASURE_BINARY_OPERATOR(modable, %)
191 BOOST_TYPE_ERASURE_BINARY_OPERATOR(left_shiftable, <<)
192 BOOST_TYPE_ERASURE_BINARY_OPERATOR(right_shiftable, >>)
193 BOOST_TYPE_ERASURE_BINARY_OPERATOR(bitandable, &)
194 BOOST_TYPE_ERASURE_BINARY_OPERATOR(bitorable, |)
195 BOOST_TYPE_ERASURE_BINARY_OPERATOR(bitxorable, ^)
197 #undef BOOST_TYPE_ERASURE_BINARY_OPERATOR
200 #define BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(name, op) \
201 template<class T = _self, class U = T> \
204 static void apply(T& lhs, const U& rhs) { lhs op rhs; } \
207 template<class T, class U, class Base> \
208 struct concept_interface<name<T, U>, Base, T, \
209 typename ::boost::disable_if< \
211 typename ::boost::type_erasure::placeholder_of<Base>::type, \
217 friend typename detail::non_const_this_param<Base>::type& \
218 operator op(typename detail::non_const_this_param<Base>::type& lhs, \
219 typename as_param<Base, const U&>::type rhs) \
221 ::boost::type_erasure::call(name<T, U>(),lhs, rhs); \
226 template<class T, class U, class Base> \
227 struct concept_interface< \
231 typename ::boost::disable_if< \
232 ::boost::type_erasure::is_placeholder<T> >::type \
236 operator op(T& lhs, const typename derived<Base>::type& rhs) \
238 ::boost::type_erasure::call(name<T, U>(),lhs, rhs); \
243 BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(add_assignable, +=)
244 BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(subtract_assignable, -=)
245 BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(multiply_assignable, *=)
246 BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(divide_assignable, /=)
247 BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(mod_assignable, %=)
248 BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(left_shift_assignable, <<=)
249 BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(right_shift_assignable, >>=)
250 BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(bitand_assignable, &=)
251 BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(bitor_assignable, |=)
252 BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(bitxor_assignable, ^=)
254 #undef BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR
256 template<class T = _self, class U = T>
257 struct equality_comparable
259 static bool apply(const T& lhs, const U& rhs) { return lhs == rhs; }
262 /// \cond show_operators
264 template<class T, class U, class Base>
265 struct concept_interface<equality_comparable<T, U>, Base, T> : Base
267 friend bool operator==(const typename derived<Base>::type& lhs,
268 typename as_param<Base, const U&>::type rhs)
270 if(::boost::type_erasure::check_match(equality_comparable<T, U>(), lhs, rhs)) {
271 return ::boost::type_erasure::unchecked_call(equality_comparable<T, U>(), lhs, rhs);
276 friend bool operator!=(const typename derived<Base>::type& lhs,
277 typename as_param<Base, const U&>::type rhs)
279 return !(lhs == rhs);
283 template<class T, class U, class Base>
284 struct concept_interface<
285 equality_comparable<T, U>,
288 typename ::boost::disable_if< ::boost::type_erasure::is_placeholder<T> >::type
291 friend bool operator==(const T& lhs, const typename derived<Base>::type& rhs)
293 return ::boost::type_erasure::call(equality_comparable<T, U>(), lhs, rhs);
295 friend bool operator!=(const T& lhs, const typename derived<Base>::type& rhs)
297 return !(lhs == rhs);
303 template<class T = _self, class U = T>
304 struct less_than_comparable
306 static bool apply(const T& lhs, const U& rhs) { return lhs < rhs; }
311 template<class F, class T, class U>
312 bool less_impl(const F& f, const T& lhs, const U& rhs, ::boost::mpl::true_)
314 if(::boost::type_erasure::check_match(f, lhs, rhs)) {
315 return ::boost::type_erasure::unchecked_call(f, lhs, rhs);
317 return ::boost::type_erasure::typeid_of(
318 static_cast<const typename derived<T>::type&>(lhs)
320 ::boost::type_erasure::typeid_of(
321 static_cast<const typename derived<U>::type&>(rhs)
327 template<class F, class T, class U>
328 bool less_impl(const F& f, const T& lhs, const U& rhs, ::boost::mpl::false_)
330 return ::boost::type_erasure::call(f, lhs, rhs);
335 /// \cond show_operators
337 template<class T, class Base>
338 struct concept_interface<less_than_comparable<T, T>, Base, T> : Base
340 friend bool operator<(const typename derived<Base>::type& lhs,
341 typename as_param<Base, const T&>::type rhs)
343 return ::boost::type_erasure::detail::less_impl(
344 less_than_comparable<T, T>(),
346 ::boost::type_erasure::is_relaxed<
347 typename ::boost::type_erasure::concept_of<Base>::type>());
349 friend bool operator>=(const typename derived<Base>::type& lhs,
350 typename as_param<Base, const T&>::type rhs)
354 friend bool operator>(typename as_param<Base, const T&>::type lhs,
355 const typename derived<Base>::type& rhs)
359 friend bool operator<=(typename as_param<Base, const T&>::type lhs,
360 const typename derived<Base>::type& rhs)
366 template<class T, class U, class Base>
367 struct concept_interface<less_than_comparable<T, U>, Base, T> : Base
369 friend bool operator<(const typename derived<Base>::type& lhs,
370 typename as_param<Base, const U&>::type rhs)
372 return ::boost::type_erasure::call(less_than_comparable<T, U>(), lhs, rhs);
374 friend bool operator>=(const typename derived<Base>::type& lhs,
375 typename as_param<Base, const U&>::type rhs)
379 friend bool operator>(typename as_param<Base, const U&>::type lhs,
380 const typename derived<Base>::type& rhs)
384 friend bool operator<=(typename as_param<Base, const U&>::type lhs,
385 const typename derived<Base>::type& rhs)
391 template<class T, class U, class Base>
392 struct concept_interface<
393 less_than_comparable<T, U>,
396 typename ::boost::disable_if< ::boost::type_erasure::is_placeholder<T> >::type
399 friend bool operator<(const T& lhs, const typename derived<Base>::type& rhs)
401 return ::boost::type_erasure::call(less_than_comparable<T, U>(), lhs, rhs);
403 friend bool operator>=(const T& lhs, const typename derived<Base>::type& rhs)
407 friend bool operator>(const typename derived<Base>::type& lhs, const T& rhs)
411 friend bool operator<=(const typename derived<Base>::type& lhs, const T& rhs)
419 template<class R, class T = _self, class N = std::ptrdiff_t>
422 static R apply(T& arg, const N& index) { return arg[index]; }
425 /// \cond show_operators
427 template<class R, class T, class N, class Base>
428 struct concept_interface<subscriptable<R, T, N>, Base, typename ::boost::remove_const<T>::type,
429 typename ::boost::enable_if<
430 ::boost::type_erasure::detail::should_be_non_const<T, Base>
434 typename ::boost::type_erasure::rebind_any<Base, R>::type operator[](
435 typename ::boost::type_erasure::as_param<Base, const N&>::type index)
437 return ::boost::type_erasure::call(subscriptable<R, T, N>(), *this, index);
441 template<class R, class T, class N, class Base>
442 struct concept_interface<subscriptable<R, T, N>, Base, typename ::boost::remove_const<T>::type,
443 typename ::boost::enable_if<
444 ::boost::type_erasure::detail::should_be_const<T, Base>
448 typename ::boost::type_erasure::rebind_any<Base, R>::type operator[](
449 typename ::boost::type_erasure::as_param<Base, const N&>::type index) const
451 return ::boost::type_erasure::call(subscriptable<R, const T, N>(), *this, index);
458 * The @ref ostreamable concept allows an @ref any to be
459 * written to a @c std::ostream.
461 template<class Os = std::ostream, class T = _self>
464 static void apply(Os& out, const T& arg) { out << arg; }
467 /// \cond show_operators
469 template<class Base, class Os, class T>
470 struct concept_interface<ostreamable<Os, T>, Base, Os> : Base
472 friend typename detail::non_const_this_param<Base>::type&
473 operator<<(typename detail::non_const_this_param<Base>::type& lhs,
474 typename ::boost::type_erasure::as_param<Base, const T&>::type rhs)
476 ::boost::type_erasure::call(ostreamable<Os, T>(), lhs, rhs);
481 template<class Base, class Os, class T>
482 struct concept_interface<
486 typename ::boost::disable_if< ::boost::type_erasure::is_placeholder<Os> >::type
491 const typename ::boost::type_erasure::derived<Base>::type& rhs)
493 ::boost::type_erasure::call(ostreamable<Os, T>(), lhs, rhs);
501 * The @ref istreamable concept allows an @ref any to be
502 * read from a @c std::istream.
504 template<class Is = std::istream, class T = _self>
507 static void apply(Is& out, T& arg) { out >> arg; }
510 /// \cond show_operators
513 template<class Base, class Is, class T>
514 struct concept_interface<istreamable<Is, T>, Base, Is> : Base
516 friend typename detail::non_const_this_param<Base>::type&
517 operator>>(typename detail::non_const_this_param<Base>::type& lhs,
518 typename ::boost::type_erasure::as_param<Base, T&>::type rhs)
520 ::boost::type_erasure::call(istreamable<Is, T>(), lhs, rhs);
525 template<class Base, class Is, class T>
526 struct concept_interface<
530 typename ::boost::disable_if< ::boost::type_erasure::is_placeholder<Is> >::type
535 typename ::boost::type_erasure::derived<Base>::type& rhs)
537 ::boost::type_erasure::call(istreamable<Is, T>(), lhs, rhs);