File: operators.hpp

package info (click to toggle)
boost1.62 1.62.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 686,420 kB
  • sloc: cpp: 2,609,004; xml: 972,558; ansic: 53,674; python: 32,437; sh: 8,829; asm: 3,071; cs: 2,121; makefile: 964; perl: 859; yacc: 472; php: 132; ruby: 94; f90: 55; sql: 13; csh: 6
file content (547 lines) | stat: -rw-r--r-- 23,290 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// Boost.TypeErasure library
//
// Copyright 2011 Steven Watanabe
//
// Distributed under the Boost Software License Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// $Id$

#ifndef BOOST_TYPE_ERASURE_OPERATORS_HPP_INCLUDED
#define BOOST_TYPE_ERASURE_OPERATORS_HPP_INCLUDED

#include <iosfwd>
#include <boost/utility/enable_if.hpp>
#include <boost/type_erasure/detail/const.hpp>
#include <boost/type_erasure/call.hpp>
#include <boost/type_erasure/concept_interface.hpp>
#include <boost/type_erasure/placeholder.hpp>
#include <boost/type_erasure/concept_of.hpp>
#include <boost/type_erasure/derived.hpp>
#include <boost/type_erasure/rebind_any.hpp>
#include <boost/type_erasure/param.hpp>
#include <boost/type_erasure/check_match.hpp>
#include <boost/type_erasure/relaxed.hpp>
#include <boost/type_erasure/typeid_of.hpp>

namespace boost {
namespace type_erasure {

template<class Concept, class Placeholder>
class any;

/** INTERNAL ONLY */
#define BOOST_TYPE_ERASURE_UNARY_INPLACE_OPERATOR(name, op)                         \
    template<class T = _self>                                                       \
    struct name                                                                     \
    {                                                                               \
        static void apply(T& arg) { op arg; }                                       \
    };                                                                              \
                                                                                    \
    template<class T, class Base>                                                   \
    struct concept_interface<name<T>, Base, T,                                      \
        typename ::boost::enable_if<                                                \
            detail::should_be_non_const<T, Base>                                    \
        >::type                                                                     \
    > : Base                                                                        \
    {                                                                               \
        typedef typename ::boost::type_erasure::derived<Base>::type _derived;       \
        _derived& operator op()                                                     \
        {                                                                           \
            ::boost::type_erasure::call(name<T>(), *this);                          \
            return static_cast<_derived&>(*this);                                   \
        }                                                                           \
        typename ::boost::type_erasure::rebind_any<Base, T>::type operator op(int)  \
        {                                                                           \
            typename ::boost::type_erasure::rebind_any<Base, T>::type result(       \
                static_cast<_derived&>(*this));                                     \
            ::boost::type_erasure::call(name<T>(), *this);                          \
            return result;                                                          \
        }                                                                           \
    };                                                                              \
                                                                                    \
    template<class T, class Base>                                                   \
    struct concept_interface<name<T>, Base, T,                                      \
        typename ::boost::enable_if<                                                \
            detail::should_be_const<T, Base>                                        \
        >::type                                                                     \
    > : Base                                                                        \
    {                                                                               \
        typedef typename ::boost::type_erasure::derived<Base>::type _derived;       \
        const _derived& operator op() const                                         \
        {                                                                           \
            ::boost::type_erasure::call(name<T>(), *this);                          \
            return static_cast<const _derived&>(*this);                             \
        }                                                                           \
        typename ::boost::type_erasure::rebind_any<Base, T>::type operator op(int) const \
        {                                                                           \
            typename ::boost::type_erasure::rebind_any<Base, T>::type result(       \
                static_cast<const _derived&>(*this));                               \
            ::boost::type_erasure::call(name<T>(), *this);                          \
            return result;                                                          \
        }                                                                           \
    };

/**
 * The @ref incrementable concept allow pre and
 * post increment on an @ref any.  The contained
 * type must provide a pre-increment operator.
 */
BOOST_TYPE_ERASURE_UNARY_INPLACE_OPERATOR(incrementable, ++)
/**
 * The @ref decrementable concept allow pre and
 * post decrement on an @ref any.  The contained
 * type must provide a pre-decrement operator.
 */
BOOST_TYPE_ERASURE_UNARY_INPLACE_OPERATOR(decrementable, --)

#undef BOOST_TYPE_ERASURE_UNARY_INPLACE_OPERATOR

/** INTERNAL ONLY */
#define BOOST_TYPE_ERASURE_UNARY_OPERATOR(name, op)                                     \
    template<class T = _self, class R = T>                                              \
    struct name                                                                         \
    {                                                                                   \
        static R apply(const T& arg) { return op arg; }                                 \
    };                                                                                  \
                                                                                        \
    template<class T, class R, class Base>                                              \
    struct concept_interface<name<T, R>, Base, T> : Base                                \
    {                                                                                   \
        typename ::boost::type_erasure::rebind_any<Base, R>::type operator op() const   \
        {                                                                               \
            return ::boost::type_erasure::call(name<T, R>(), *this);                    \
        }                                                                               \
    };

/**
 * The @ref complementable concept allow use of the bitwise
 * complement operator on an @ref any.
 */
BOOST_TYPE_ERASURE_UNARY_OPERATOR(complementable, ~)
/**
 * The @ref negatable concept allow use of the unary
 * minus operator on an @ref any.
 */
BOOST_TYPE_ERASURE_UNARY_OPERATOR(negatable, -)

#undef BOOST_TYPE_ERASURE_UNARY_OPERATOR

template<class R, class T = _self>
struct dereferenceable
{
    static R apply(const T& arg) { return *arg; }
};

/// \cond show_operators

template<class R, class T, class Base>
struct concept_interface<dereferenceable<R, T>, Base, T> : Base
{
    typename ::boost::type_erasure::rebind_any<Base, R>::type operator*() const
    {
        return ::boost::type_erasure::call(dereferenceable<R, T>(), *this);
    }
};

/// \endcond

/** INTERNAL ONLY */
#define BOOST_TYPE_ERASURE_BINARY_OPERATOR(name, op)                        \
    template<class T = _self, class U = T, class R = T>                     \
    struct name                                                             \
    {                                                                       \
        static R apply(const T& lhs, const U& rhs) { return lhs op rhs; }   \
    };                                                                      \
                                                                            \
    template<class T, class U, class R, class Base>                         \
    struct concept_interface<name<T, U, R>, Base, T> : Base                 \
    {                                                                       \
        friend typename rebind_any<Base, R>::type                           \
        operator op(const typename derived<Base>::type& lhs,                \
                    typename as_param<Base, const U&>::type rhs)            \
        {                                                                   \
            return ::boost::type_erasure::call(name<T, U, R>(), lhs, rhs);  \
        }                                                                   \
    };                                                                      \
                                                                            \
    template<class T, class U, class R, class Base>                         \
    struct concept_interface<                                               \
        name<T, U, R>,                                                      \
        Base,                                                               \
        U,                                                                  \
        typename ::boost::disable_if<                                       \
            ::boost::type_erasure::is_placeholder<T> >::type                \
    > : Base                                                                \
    {                                                                       \
        friend typename rebind_any<Base, R>::type                           \
        operator op(const T& lhs,                                           \
                    const typename derived<Base>::type& rhs)                \
        {                                                                   \
            return ::boost::type_erasure::call(name<T, U, R>(), lhs, rhs);  \
        }                                                                   \
    };

BOOST_TYPE_ERASURE_BINARY_OPERATOR(addable, +)
BOOST_TYPE_ERASURE_BINARY_OPERATOR(subtractable, -)
BOOST_TYPE_ERASURE_BINARY_OPERATOR(multipliable, *)
BOOST_TYPE_ERASURE_BINARY_OPERATOR(dividable, /)
BOOST_TYPE_ERASURE_BINARY_OPERATOR(modable, %)
BOOST_TYPE_ERASURE_BINARY_OPERATOR(left_shiftable, <<)
BOOST_TYPE_ERASURE_BINARY_OPERATOR(right_shiftable, >>)
BOOST_TYPE_ERASURE_BINARY_OPERATOR(bitandable, &)
BOOST_TYPE_ERASURE_BINARY_OPERATOR(bitorable, |)
BOOST_TYPE_ERASURE_BINARY_OPERATOR(bitxorable, ^)

#undef BOOST_TYPE_ERASURE_BINARY_OPERATOR

/** INTERNAL ONLY */
#define BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(name, op)                    \
    template<class T = _self, class U = T>                                  \
    struct name                                                             \
    {                                                                       \
        static void apply(T& lhs, const U& rhs) { lhs op rhs; }             \
    };                                                                      \
                                                                            \
    template<class T, class U, class Base>                                  \
    struct concept_interface<name<T, U>, Base, T,                           \
        typename ::boost::disable_if<                                       \
            ::boost::is_same<                                               \
                typename ::boost::type_erasure::placeholder_of<Base>::type, \
                const T&                                                    \
            >                                                               \
        >::type                                                             \
    > : Base                                                                \
    {                                                                       \
        friend typename detail::non_const_this_param<Base>::type&           \
        operator op(typename detail::non_const_this_param<Base>::type& lhs, \
                    typename as_param<Base, const U&>::type rhs)            \
        {                                                                   \
            ::boost::type_erasure::call(name<T, U>(),lhs, rhs);             \
            return lhs;                                                     \
        }                                                                   \
    };                                                                      \
                                                                            \
    template<class T, class U, class Base>                                  \
    struct concept_interface<                                               \
        name<T, U>,                                                         \
        Base,                                                               \
        U,                                                                  \
        typename ::boost::disable_if<                                       \
            ::boost::type_erasure::is_placeholder<T> >::type                \
    > : Base                                                                \
    {                                                                       \
        friend T&                                                           \
        operator op(T& lhs, const typename derived<Base>::type& rhs)        \
        {                                                                   \
            ::boost::type_erasure::call(name<T, U>(),lhs, rhs);             \
            return lhs;                                                     \
        }                                                                   \
    };

BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(add_assignable, +=)
BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(subtract_assignable, -=)
BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(multiply_assignable, *=)
BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(divide_assignable, /=)
BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(mod_assignable, %=)
BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(left_shift_assignable, <<=)
BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(right_shift_assignable, >>=)
BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(bitand_assignable, &=)
BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(bitor_assignable, |=)
BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR(bitxor_assignable, ^=)

#undef BOOST_TYPE_ERASURE_ASSIGNMENT_OPERATOR

template<class T = _self, class U = T>
struct equality_comparable
{
    static bool apply(const T& lhs, const U& rhs) { return lhs == rhs; }
};

/// \cond show_operators

template<class T, class U, class Base>
struct concept_interface<equality_comparable<T, U>, Base, T> : Base
{
    friend bool operator==(const typename derived<Base>::type& lhs,
                           typename as_param<Base, const U&>::type rhs)
    {
        if(::boost::type_erasure::check_match(equality_comparable<T, U>(), lhs, rhs)) {
            return ::boost::type_erasure::unchecked_call(equality_comparable<T, U>(), lhs, rhs);
        } else {
            return false;
        }
    }
    friend bool operator!=(const typename derived<Base>::type& lhs,
                           typename as_param<Base, const U&>::type rhs)
    {
        return !(lhs == rhs);
    }
};

template<class T, class U, class Base>
struct concept_interface<
    equality_comparable<T, U>,
    Base,
    U,
    typename ::boost::disable_if< ::boost::type_erasure::is_placeholder<T> >::type
> : Base
{
    friend bool operator==(const T& lhs, const typename derived<Base>::type& rhs)
    {
        return ::boost::type_erasure::call(equality_comparable<T, U>(), lhs, rhs);
    }
    friend bool operator!=(const T& lhs, const typename derived<Base>::type& rhs)
    {
        return !(lhs == rhs);
    }
};

/// \endcond

template<class T = _self, class U = T>
struct less_than_comparable
{
    static bool apply(const T& lhs, const U& rhs) { return lhs < rhs; }
};

namespace detail {

template<class F, class T, class U>
bool less_impl(const F& f, const T& lhs, const U& rhs, ::boost::mpl::true_)
{
    if(::boost::type_erasure::check_match(f, lhs, rhs)) {
        return ::boost::type_erasure::unchecked_call(f, lhs, rhs);
    } else {
        return ::boost::type_erasure::typeid_of(
            static_cast<const typename derived<T>::type&>(lhs)
        ).before(
            ::boost::type_erasure::typeid_of(
                static_cast<const typename derived<U>::type&>(rhs)
            )
        ) != false;
    }
}

template<class F, class T, class U>
bool less_impl(const F& f, const T& lhs, const U& rhs, ::boost::mpl::false_)
{
    return ::boost::type_erasure::call(f, lhs, rhs);
}

}

/// \cond show_operators

template<class T, class Base>
struct concept_interface<less_than_comparable<T, T>, Base, T> : Base
{
    friend bool operator<(const typename derived<Base>::type& lhs,
                          typename as_param<Base, const T&>::type rhs)
    {
        return ::boost::type_erasure::detail::less_impl(
            less_than_comparable<T, T>(),
            lhs, rhs,
            ::boost::type_erasure::is_relaxed<
                typename ::boost::type_erasure::concept_of<Base>::type>());
    }
    friend bool operator>=(const typename derived<Base>::type& lhs,
                           typename as_param<Base, const T&>::type rhs)
    {
        return !(lhs < rhs);
    }
    friend bool operator>(typename as_param<Base, const T&>::type lhs,
                          const typename derived<Base>::type& rhs)
    {
        return rhs < lhs;
    }
    friend bool operator<=(typename as_param<Base, const T&>::type lhs,
                          const typename derived<Base>::type& rhs)
    {
        return !(rhs < lhs);
    }
};

template<class T, class U, class Base>
struct concept_interface<less_than_comparable<T, U>, Base, T> : Base
{
    friend bool operator<(const typename derived<Base>::type& lhs,
                          typename as_param<Base, const U&>::type rhs)
    {
        return ::boost::type_erasure::call(less_than_comparable<T, U>(), lhs, rhs);
    }
    friend bool operator>=(const typename derived<Base>::type& lhs,
                           typename as_param<Base, const U&>::type rhs)
    {
        return !(lhs < rhs);
    }
    friend bool operator>(typename as_param<Base, const U&>::type lhs,
                          const typename derived<Base>::type& rhs)
    {
        return rhs < lhs;
    }
    friend bool operator<=(typename as_param<Base, const U&>::type lhs,
                          const typename derived<Base>::type& rhs)
    {
        return !(rhs < lhs);
    }
};

template<class T, class U, class Base>
struct concept_interface<
    less_than_comparable<T, U>,
    Base,
    U,
    typename ::boost::disable_if< ::boost::type_erasure::is_placeholder<T> >::type
> : Base
{
    friend bool operator<(const T& lhs, const typename derived<Base>::type& rhs)
    {
        return ::boost::type_erasure::call(less_than_comparable<T, U>(), lhs, rhs);
    }
    friend bool operator>=(const T& lhs, const typename derived<Base>::type& rhs)
    {
        return !(lhs < rhs);
    }
    friend bool operator>(const typename derived<Base>::type& lhs, const T& rhs)
    {
        return rhs < lhs;
    }
    friend bool operator<=(const typename derived<Base>::type& lhs, const T& rhs)
    {
        return !(rhs < lhs);
    }
};

/// \endcond

template<class R, class T = _self, class N = std::ptrdiff_t>
struct subscriptable
{
    static R apply(T& arg, const N& index) { return arg[index]; }
};

/// \cond show_operators

template<class R, class T, class N, class Base>
struct concept_interface<subscriptable<R, T, N>, Base, typename ::boost::remove_const<T>::type,
    typename ::boost::enable_if<
        ::boost::type_erasure::detail::should_be_non_const<T, Base>
    >::type
> : Base
{
    typename ::boost::type_erasure::rebind_any<Base, R>::type operator[](
        typename ::boost::type_erasure::as_param<Base, const N&>::type index)
    {
        return ::boost::type_erasure::call(subscriptable<R, T, N>(), *this, index);
    }
};

template<class R, class T, class N, class Base>
struct concept_interface<subscriptable<R, T, N>, Base, typename ::boost::remove_const<T>::type,
    typename ::boost::enable_if<
        ::boost::type_erasure::detail::should_be_const<T, Base>
    >::type
> : Base
{
    typename ::boost::type_erasure::rebind_any<Base, R>::type operator[](
        typename ::boost::type_erasure::as_param<Base, const N&>::type index) const
    {
        return ::boost::type_erasure::call(subscriptable<R, const T, N>(), *this, index);
    }
};

/// \endcond

/**
 * The @ref ostreamable concept allows an @ref any to be
 * written to a @c std::ostream.
 */
template<class Os = std::ostream, class T = _self>
struct ostreamable
{
    static void apply(Os& out, const T& arg) { out << arg; }
};

/// \cond show_operators

template<class Base, class Os, class T>
struct concept_interface<ostreamable<Os, T>, Base, Os> : Base
{
    friend typename detail::non_const_this_param<Base>::type&
    operator<<(typename detail::non_const_this_param<Base>::type& lhs,
               typename ::boost::type_erasure::as_param<Base, const T&>::type rhs)
    {
        ::boost::type_erasure::call(ostreamable<Os, T>(), lhs, rhs);
        return lhs;
    }
};

template<class Base, class Os, class T>
struct concept_interface<
    ostreamable<Os, T>,
    Base,
    T,
    typename ::boost::disable_if< ::boost::type_erasure::is_placeholder<Os> >::type
> : Base
{
    friend Os&
    operator<<(Os& lhs,
               const typename ::boost::type_erasure::derived<Base>::type& rhs)
    {
        ::boost::type_erasure::call(ostreamable<Os, T>(), lhs, rhs);
        return lhs;
    }
};

/// \endcond

/**
 * The @ref istreamable concept allows an @ref any to be
 * read from a @c std::istream.
 */
template<class Is = std::istream, class T = _self>
struct istreamable
{
    static void apply(Is& out, T& arg) { out >> arg; }
};

/// \cond show_operators


template<class Base, class Is, class T>
struct concept_interface<istreamable<Is, T>, Base, Is> : Base
{
    friend typename detail::non_const_this_param<Base>::type&
    operator>>(typename detail::non_const_this_param<Base>::type& lhs,
               typename ::boost::type_erasure::as_param<Base, T&>::type rhs)
    {
        ::boost::type_erasure::call(istreamable<Is, T>(), lhs, rhs);
        return lhs;
    }
};

template<class Base, class Is, class T>
struct concept_interface<
    istreamable<Is, T>,
    Base,
    T,
    typename ::boost::disable_if< ::boost::type_erasure::is_placeholder<Is> >::type
> : Base
{
    friend Is&
    operator>>(Is& lhs,
               typename ::boost::type_erasure::derived<Base>::type& rhs)
    {
        ::boost::type_erasure::call(istreamable<Is, T>(), lhs, rhs);
        return lhs;
    }
};

/// \endcond

}
}

#endif