]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/spirit/home/support/utree/operators.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / spirit / home / support / utree / operators.hpp
1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
4 Copyright (c) 2011 Bryce Lelbach
5
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #if !defined(BOOST_SPIRIT_UTREE_OPERATORS)
10 #define BOOST_SPIRIT_UTREE_OPERATORS
11
12 #if defined(BOOST_MSVC)
13 # pragma warning(push)
14 # pragma warning(disable: 4804)
15 # pragma warning(disable: 4805)
16 #endif
17
18 #include <exception>
19 #if !defined(BOOST_SPIRIT_DISABLE_UTREE_IO)
20 #include <ios>
21 #include <boost/io/ios_state.hpp>
22 #endif
23 #include <boost/spirit/home/support/utree/utree.hpp>
24 #include <boost/preprocessor/cat.hpp>
25 #include <boost/throw_exception.hpp>
26 #include <boost/type_traits/is_arithmetic.hpp>
27 #include <boost/type_traits/is_integral.hpp>
28
29 namespace boost { namespace spirit
30 {
31 // Relational operators
32 bool operator==(utree const& a, utree const& b);
33 bool operator<(utree const& a, utree const& b);
34 bool operator!=(utree const& a, utree const& b);
35 bool operator>(utree const& a, utree const& b);
36 bool operator<=(utree const& a, utree const& b);
37 bool operator>=(utree const& a, utree const& b);
38
39 #if !defined(BOOST_SPIRIT_DISABLE_UTREE_IO)
40 // output
41 std::ostream& operator<<(std::ostream& out, utree const& x);
42 std::ostream& operator<<(std::ostream& out, utree::invalid_type const& x);
43 std::ostream& operator<<(std::ostream& out, utree::nil_type const& x);
44 #endif
45
46 // Logical operators
47 utree operator&&(utree const& a, utree const& b);
48 utree operator||(utree const& a, utree const& b);
49 utree operator!(utree const& a);
50
51 // Arithmetic operators
52 utree operator+(utree const& a, utree const& b);
53 utree operator-(utree const& a, utree const& b);
54 utree operator*(utree const& a, utree const& b);
55 utree operator/(utree const& a, utree const& b);
56 utree operator%(utree const& a, utree const& b);
57 utree operator-(utree const& a);
58
59 // Bitwise operators
60 utree operator&(utree const& a, utree const& b);
61 utree operator|(utree const& a, utree const& b);
62 utree operator^(utree const& a, utree const& b);
63 utree operator<<(utree const& a, utree const& b);
64 utree operator>>(utree const& a, utree const& b);
65 utree operator~(utree const& a);
66
67 // Implementation
68 struct utree_is_equal
69 {
70 typedef bool result_type;
71
72 template <typename A, typename B>
73 bool dispatch(const A&, const B&, boost::mpl::false_) const
74 {
75 return false; // cannot compare different types by default
76 }
77
78 template <typename A, typename B>
79 bool dispatch(const A& a, const B& b, boost::mpl::true_) const
80 {
81 return a == b; // for arithmetic types
82 }
83
84 template <typename A, typename B>
85 bool operator()(const A& a, const B& b) const
86 {
87 return dispatch(a, b,
88 boost::mpl::and_<
89 boost::is_arithmetic<A>,
90 boost::is_arithmetic<B> >());
91 }
92
93 template <typename T>
94 bool operator()(const T& a, const T& b) const
95 {
96 // This code works for lists
97 return a == b;
98 }
99
100 template <typename Base, utree_type::info type_>
101 bool operator()(
102 basic_string<Base, type_> const& a,
103 basic_string<Base, type_> const& b) const
104 {
105 return static_cast<Base const&>(a) == static_cast<Base const&>(b);
106 }
107
108 bool operator()(utree::invalid_type, utree::invalid_type) const
109 {
110 return true;
111 }
112
113 bool operator()(utree::nil_type, utree::nil_type) const
114 {
115 return true;
116 }
117
118 bool operator()(function_base const&, function_base const&) const
119 {
120 return false; // just don't allow comparison of functions
121 }
122 };
123
124 struct utree_is_less_than
125 {
126 typedef bool result_type;
127
128 template <typename A, typename B>
129 bool dispatch(const A&, const B&, boost::mpl::false_) const
130 {
131 return false; // cannot compare different types by default
132 }
133
134 template <typename A, typename B>
135 bool dispatch(const A& a, const B& b, boost::mpl::true_) const
136 {
137 return a < b; // for arithmetic types
138 }
139
140 template <typename A, typename B>
141 bool operator()(const A& a, const B& b) const
142 {
143 return dispatch(a, b,
144 boost::mpl::and_<
145 boost::is_arithmetic<A>,
146 boost::is_arithmetic<B> >());
147 }
148
149 template <typename T>
150 bool operator()(const T& a, const T& b) const
151 {
152 // This code works for lists
153 return a < b;
154 }
155
156 template <typename Base, utree_type::info type_>
157 bool operator()(
158 basic_string<Base, type_> const& a,
159 basic_string<Base, type_> const& b) const
160 {
161 return static_cast<Base const&>(a) < static_cast<Base const&>(b);
162 }
163
164 bool operator()(utree::invalid_type, utree::invalid_type) const
165 {
166 BOOST_THROW_EXCEPTION(bad_type_exception
167 ("no less-than comparison for this utree type",
168 utree_type::invalid_type));
169 return false; // no less than comparison for nil
170 }
171
172 bool operator()(utree::nil_type, utree::nil_type) const
173 {
174 BOOST_THROW_EXCEPTION(bad_type_exception
175 ("no less-than comparison for this utree type",
176 utree_type::nil_type));
177 return false; // no less than comparison for nil
178 }
179
180 bool operator()(any_ptr const&, any_ptr const&) const
181 {
182 BOOST_THROW_EXCEPTION(bad_type_exception
183 ("no less-than comparison for this utree type",
184 utree_type::any_type));
185 return false; // no less than comparison for any_ptr
186 }
187
188 bool operator()(function_base const&, function_base const&) const
189 {
190 BOOST_THROW_EXCEPTION(bad_type_exception
191 ("no less-than comparison for this utree type",
192 utree_type::function_type));
193 return false; // no less than comparison of functions
194 }
195 };
196
197 #if !defined(BOOST_SPIRIT_DISABLE_UTREE_IO)
198 struct utree_print
199 {
200 typedef void result_type;
201
202 std::ostream& out;
203 utree_print(std::ostream& out) : out(out) {}
204
205 void operator()(utree::invalid_type) const
206 {
207 out << "<invalid> ";
208 }
209
210 void operator()(utree::nil_type) const
211 {
212 out << "<nil> ";
213 }
214
215 template <typename T>
216 void operator()(T val) const
217 {
218 out << val << ' ';
219 }
220
221 void operator()(bool b) const
222 {
223 out << (b ? "true" : "false") << ' ';
224 }
225
226 void operator()(binary_range_type const& b) const
227 {
228 boost::io::ios_all_saver saver(out);
229 out << "#";
230 out.width(2);
231 out.fill('0');
232
233 typedef binary_range_type::const_iterator iterator;
234 for (iterator i = b.begin(); i != b.end(); ++i)
235 out << std::hex << int((unsigned char)*i);
236 out << "# ";
237 }
238
239 void operator()(utf8_string_range_type const& str) const
240 {
241 typedef utf8_string_range_type::const_iterator iterator;
242 iterator i = str.begin();
243 out << '"';
244 for (; i != str.end(); ++i)
245 out << *i;
246 out << "\" ";
247 }
248
249 void operator()(utf8_symbol_range_type const& str) const
250 {
251 typedef utf8_symbol_range_type::const_iterator iterator;
252 iterator i = str.begin();
253 for (; i != str.end(); ++i)
254 out << *i;
255 out << ' ';
256 }
257
258 template <typename Iterator>
259 void operator()(boost::iterator_range<Iterator> const& range) const
260 {
261 typedef typename boost::iterator_range<Iterator>::const_iterator iterator;
262 (*this)('(');
263 for (iterator i = range.begin(); i != range.end(); ++i)
264 {
265 boost::spirit::utree::visit(*i, *this);
266 }
267 (*this)(')');
268 }
269
270 void operator()(any_ptr const&) const
271 {
272 return (*this)("<pointer>");
273 }
274
275 void operator()(function_base const&) const
276 {
277 return (*this)("<function>");
278 }
279 };
280 #endif
281
282 template <typename Base>
283 struct logical_function
284 {
285 typedef utree result_type;
286
287 // We assume anything except false is true
288
289 // binary
290 template <typename A, typename B>
291 utree operator()(A const& a, B const& b) const
292 {
293 return dispatch(a, b
294 , boost::is_arithmetic<A>()
295 , boost::is_arithmetic<B>());
296 }
297
298 // binary
299 template <typename A, typename B>
300 utree dispatch(A const& a, B const& b, mpl::true_, mpl::true_) const
301 {
302 return Base::eval(a, b); // for arithmetic types
303 }
304
305 // binary
306 template <typename A, typename B>
307 utree dispatch(A const&, B const& b, mpl::false_, mpl::true_) const
308 {
309 return Base::eval(true, b);
310 }
311
312 // binary
313 template <typename A, typename B>
314 utree dispatch(A const& a, B const&, mpl::true_, mpl::false_) const
315 {
316 return Base::eval(a, true);
317 }
318
319 // binary
320 template <typename A, typename B>
321 utree dispatch(A const&, B const&, mpl::false_, mpl::false_) const
322 {
323 return Base::eval(true, true);
324 }
325
326 // unary
327 template <typename A>
328 utree operator()(A const& a) const
329 {
330 return dispatch(a, boost::is_arithmetic<A>());
331 }
332
333 // unary
334 template <typename A>
335 utree dispatch(A const& a, mpl::true_) const
336 {
337 return Base::eval(a);
338 }
339
340 // unary
341 template <typename A>
342 utree dispatch(A const&, mpl::false_) const
343 {
344 return Base::eval(true);
345 }
346 };
347
348 template <typename Base>
349 struct arithmetic_function
350 {
351 typedef utree result_type;
352
353 template <typename A, typename B>
354 utree dispatch(A const&, B const&, boost::mpl::false_) const
355 {
356 return utree(); // cannot apply to non-arithmetic types
357 }
358
359 template <typename A, typename B>
360 utree dispatch(A const& a, B const& b, boost::mpl::true_) const
361 {
362 return Base::eval(a, b); // for arithmetic types
363 }
364
365 // binary
366 template <typename A, typename B>
367 utree operator()(A const& a, B const& b) const
368 {
369 return dispatch(a, b,
370 boost::mpl::and_<
371 boost::is_arithmetic<A>,
372 boost::is_arithmetic<B> >());
373 }
374
375 template <typename A>
376 utree dispatch(A const&, boost::mpl::false_) const
377 {
378 return utree(); // cannot apply to non-arithmetic types
379 }
380
381 template <typename A>
382 utree dispatch(A const& a, boost::mpl::true_) const
383 {
384 return Base::eval(a); // for arithmetic types
385 }
386
387 // unary
388 template <typename A>
389 utree operator()(A const& a) const
390 {
391 return dispatch(a, boost::is_arithmetic<A>());
392 }
393 };
394
395 template <typename Base>
396 struct integral_function
397 {
398 typedef utree result_type;
399
400 template <typename A, typename B>
401 utree dispatch(A const&, B const&, boost::mpl::false_) const
402 {
403 return utree(); // cannot apply to non-integral types
404 }
405
406 template <typename A, typename B>
407 utree dispatch(A const& a, B const& b, boost::mpl::true_) const
408 {
409 return Base::eval(a, b); // for integral types
410 }
411
412 // binary
413 template <typename A, typename B>
414 utree operator()(A const& a, B const& b) const
415 {
416 return dispatch(a, b,
417 boost::mpl::and_<
418 boost::is_integral<A>,
419 boost::is_integral<B> >());
420 }
421
422 template <typename A>
423 utree dispatch(A const&, boost::mpl::false_) const
424 {
425 return utree(); // cannot apply to non-integral types
426 }
427
428 template <typename A>
429 utree dispatch(A const& a, boost::mpl::true_) const
430 {
431 return Base::eval(a); // for integral types
432 }
433
434 // unary
435 template <typename A>
436 utree operator()(A const& a) const
437 {
438 return dispatch(a, boost::is_integral<A>());
439 }
440 };
441
442 #define BOOST_SPIRIT_UTREE_CREATE_FUNCTION_BINARY \
443 template <typename Lhs, typename Rhs> \
444 static utree eval(Lhs const& a, Rhs const& b) \
445 /***/
446
447 #define BOOST_SPIRIT_UTREE_CREATE_FUNCTION_UNARY \
448 template <typename Operand> \
449 static utree eval(Operand const& a) \
450 /***/
451
452 #define BOOST_SPIRIT_UTREE_CREATE_FUNCTION(arity, name, expr, base) \
453 struct BOOST_PP_CAT(function_impl_, name) \
454 { \
455 BOOST_SPIRIT_UTREE_CREATE_FUNCTION_##arity \
456 { \
457 return utree(expr); \
458 } \
459 }; \
460 base<BOOST_PP_CAT(function_impl_, name)> const \
461 BOOST_PP_CAT(base, BOOST_PP_CAT(_, name)) = {}; \
462 /***/
463
464 #define BOOST_SPIRIT_UTREE_CREATE_ARITHMETIC_FUNCTION(arity, name, expr) \
465 BOOST_SPIRIT_UTREE_CREATE_FUNCTION(arity, name, expr, arithmetic_function)\
466 /***/
467
468 #define BOOST_SPIRIT_UTREE_CREATE_INTEGRAL_FUNCTION(arity, name, expr) \
469 BOOST_SPIRIT_UTREE_CREATE_FUNCTION(arity, name, expr, integral_function) \
470 /***/
471
472 #define BOOST_SPIRIT_UTREE_CREATE_LOGICAL_FUNCTION(arity, name, expr) \
473 BOOST_SPIRIT_UTREE_CREATE_FUNCTION(arity, name, expr, logical_function) \
474 /***/
475
476 inline bool operator==(utree const& a, utree const& b)
477 {
478 return utree::visit(a, b, utree_is_equal());
479 }
480
481 inline bool operator<(utree const& a, utree const& b)
482 {
483 return utree::visit(a, b, utree_is_less_than());
484 }
485
486 inline bool operator!=(utree const& a, utree const& b)
487 {
488 return !(a == b);
489 }
490
491 inline bool operator>(utree const& a, utree const& b)
492 {
493 return b < a;
494 }
495
496 inline bool operator<=(utree const& a, utree const& b)
497 {
498 return !(b < a);
499 }
500
501 inline bool operator>=(utree const& a, utree const& b)
502 {
503 return !(a < b);
504 }
505
506 #if !defined(BOOST_SPIRIT_DISABLE_UTREE_IO)
507 inline std::ostream& operator<<(std::ostream& out, utree const& x)
508 {
509 utree::visit(x, utree_print(out));
510 return out;
511 }
512
513 inline std::ostream& operator<<(std::ostream& out, utree::invalid_type const&)
514 {
515 return out;
516 }
517
518 inline std::ostream& operator<<(std::ostream& out, utree::nil_type const&)
519 {
520 return out;
521 }
522 #endif
523
524 BOOST_SPIRIT_UTREE_CREATE_LOGICAL_FUNCTION(BINARY, and_, a&&b)
525 BOOST_SPIRIT_UTREE_CREATE_LOGICAL_FUNCTION(BINARY, or_, a||b)
526 BOOST_SPIRIT_UTREE_CREATE_LOGICAL_FUNCTION(UNARY, not_, !a)
527
528 BOOST_SPIRIT_UTREE_CREATE_ARITHMETIC_FUNCTION(BINARY, plus, a+b)
529 BOOST_SPIRIT_UTREE_CREATE_ARITHMETIC_FUNCTION(BINARY, minus, a-b)
530 BOOST_SPIRIT_UTREE_CREATE_ARITHMETIC_FUNCTION(BINARY, times, a*b)
531 BOOST_SPIRIT_UTREE_CREATE_ARITHMETIC_FUNCTION(BINARY, divides, a/b)
532 BOOST_SPIRIT_UTREE_CREATE_INTEGRAL_FUNCTION (BINARY, modulus, a%b)
533 BOOST_SPIRIT_UTREE_CREATE_ARITHMETIC_FUNCTION(UNARY, negate, -a)
534
535 BOOST_SPIRIT_UTREE_CREATE_INTEGRAL_FUNCTION(BINARY, bitand_, a&b)
536 BOOST_SPIRIT_UTREE_CREATE_INTEGRAL_FUNCTION(BINARY, bitor_, a|b)
537 BOOST_SPIRIT_UTREE_CREATE_INTEGRAL_FUNCTION(BINARY, bitxor_, a^b)
538 BOOST_SPIRIT_UTREE_CREATE_INTEGRAL_FUNCTION(BINARY, shift_left, a<<b)
539 BOOST_SPIRIT_UTREE_CREATE_INTEGRAL_FUNCTION(BINARY, shift_right, a>>b)
540 BOOST_SPIRIT_UTREE_CREATE_INTEGRAL_FUNCTION(UNARY, invert, ~a)
541
542 // avoid `'~' on an expression of type bool` warning
543 template <> utree function_impl_invert::eval<bool>(bool const& a)
544 {
545 return utree(!a);
546 }
547
548 #undef BOOST_SPIRIT_UTREE_CREATE_FUNCTION_BINARY
549 #undef BOOST_SPIRIT_UTREE_CREATE_FUNCTION_UNARY
550 #undef BOOST_SPIRIT_UTREE_CREATE_FUNCTION
551 #undef BOOST_SPIRIT_UTREE_CREATE_LOGICAL_FUNCTION
552 #undef BOOST_SPIRIT_UTREE_CREATE_INTEGRAL_FUNCTION
553 #undef BOOST_SPIRIT_UTREE_CREATE_ARITHMETIC_FUNCTION
554
555 inline utree operator&&(utree const& a, utree const& b)
556 {
557 return utree::visit(a, b, logical_function_and_);
558 }
559
560 inline utree operator||(utree const& a, utree const& b)
561 {
562 return utree::visit(a, b, logical_function_or_);
563 }
564
565 inline utree operator!(utree const& a)
566 {
567 return utree::visit(a, logical_function_not_);
568 }
569
570 inline utree operator+(utree const& a, utree const& b)
571 {
572 utree r = utree::visit(a, b, arithmetic_function_plus);
573 if (r.which() == utree_type::invalid_type)
574 {
575 BOOST_THROW_EXCEPTION(bad_type_exception
576 ("addition performed on non-arithmetic utree types",
577 a.which(), b.which()));
578 }
579 return r;
580 }
581
582 inline utree operator-(utree const& a, utree const& b)
583 {
584 utree r = utree::visit(a, b, arithmetic_function_minus);
585 if (r.which() == utree_type::invalid_type)
586 {
587 BOOST_THROW_EXCEPTION(bad_type_exception
588 ("subtraction performed on non-arithmetic utree types",
589 a.which(), b.which()));
590 }
591 return r;
592 }
593
594 inline utree operator*(utree const& a, utree const& b)
595 {
596 utree r = utree::visit(a, b, arithmetic_function_times);
597 if (r.which() == utree_type::invalid_type)
598 {
599 BOOST_THROW_EXCEPTION(bad_type_exception
600 ("multiplication performed on non-arithmetic utree types",
601 a.which(), b.which()));
602 }
603 return r;
604 }
605
606 inline utree operator/(utree const& a, utree const& b)
607 {
608 utree r = utree::visit(a, b, arithmetic_function_divides);
609 if (r.which() == utree_type::invalid_type)
610 {
611 BOOST_THROW_EXCEPTION(bad_type_exception
612 ("division performed on non-arithmetic utree types",
613 a.which(), b.which()));
614 }
615 return r;
616 }
617
618 inline utree operator%(utree const& a, utree const& b)
619 {
620 utree r = utree::visit(a, b, integral_function_modulus);
621 if (r.which() == utree_type::invalid_type)
622 {
623 BOOST_THROW_EXCEPTION(bad_type_exception
624 ("modulos performed on non-integral utree types",
625 a.which(), b.which()));
626 }
627 return r;
628 }
629
630 inline utree operator-(utree const& a)
631 {
632 utree r = utree::visit(a, arithmetic_function_negate);
633 if (r.which() == utree_type::invalid_type)
634 {
635 BOOST_THROW_EXCEPTION(bad_type_exception
636 ("negation performed on non-arithmetic utree type",
637 a.which()));
638 }
639 return r;
640 }
641
642 inline utree operator&(utree const& a, utree const& b)
643 {
644 utree r = utree::visit(a, b, integral_function_bitand_);
645 if (r.which() == utree_type::invalid_type)
646 {
647 BOOST_THROW_EXCEPTION(bad_type_exception
648 ("bitwise and performed on non-integral utree types",
649 a.which(), b.which()));
650 }
651 return r;
652 }
653
654 inline utree operator|(utree const& a, utree const& b)
655 {
656 utree r = utree::visit(a, b, integral_function_bitor_);
657 if (r.which() == utree_type::invalid_type)
658 {
659 BOOST_THROW_EXCEPTION(bad_type_exception
660 ("bitwise or performed on non-integral utree types",
661 a.which(), b.which()));
662 }
663 return r;
664 }
665
666 inline utree operator^(utree const& a, utree const& b)
667 {
668 utree r = utree::visit(a, b, integral_function_bitxor_);
669 if (r.which() == utree_type::invalid_type)
670 {
671 BOOST_THROW_EXCEPTION(bad_type_exception
672 ("bitwise xor performed on non-integral utree types",
673 a.which(), b.which()));
674 }
675 return r;
676 }
677
678 inline utree operator<<(utree const& a, utree const& b)
679 {
680 utree r = utree::visit(a, b, integral_function_shift_left);
681 if (r.which() == utree_type::invalid_type)
682 {
683 BOOST_THROW_EXCEPTION(bad_type_exception
684 ("left shift performed on non-integral utree types",
685 a.which(), b.which()));
686 }
687 return r;
688 }
689
690 inline utree operator>>(utree const& a, utree const& b)
691 {
692 utree r = utree::visit(a, b, integral_function_shift_right);
693 if (r.which() == utree_type::invalid_type)
694 {
695 BOOST_THROW_EXCEPTION(bad_type_exception
696 ("right shift performed on non-integral utree types",
697 a.which(), b.which()));
698 }
699 return r;
700 }
701
702 inline utree operator~(utree const& a)
703 {
704 utree r = utree::visit(a, integral_function_invert);
705 if (r.which() == utree_type::invalid_type)
706 {
707 BOOST_THROW_EXCEPTION(bad_type_exception
708 ("inversion performed on non-integral utree type",
709 a.which()));
710 }
711 return r;
712 }
713 }}
714
715 #if defined(BOOST_MSVC)
716 # pragma warning(pop)
717 #endif
718
719 #endif