]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/variant/test/test3.cpp
1 //-----------------------------------------------------------------------------
2 // boost-libs variant/test/test3.cpp source file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
7 // Eric Friedman, Itay Maman
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 #include "boost/test/minimal.hpp"
14 #include "boost/variant.hpp"
20 /////////////////////////////////////////////////////////////////////
23 using boost::recursive_wrapper
;
27 /////////////////////////////////////////////////////////////////////
28 /////////////////////////////////////////////////////////////////////
33 typedef variant
<int, recursive_wrapper
<Add
>, recursive_wrapper
<Sub
> > Expr
;
38 Sub(const Expr
& l
, const Expr
& r
);
39 Sub(const Sub
& other
);
48 Add(const Expr
& l
, const Expr
& r
) : lhs_(l
), rhs_(r
) { }
49 Add(const Add
& other
) : lhs_(other
.lhs_
), rhs_(other
.rhs_
) { }
56 Sub::Sub(const Expr
& l
, const Expr
& r
) : lhs_(l
), rhs_(r
) { }
57 Sub::Sub(const Sub
& other
) : lhs_(other
.lhs_
), rhs_(other
.rhs_
) { }
61 // insert-to operators
63 std::ostream
& operator<<(std::ostream
& out
, const Sub
& a
);
65 std::ostream
& operator<<(std::ostream
& out
, const Add
& a
)
67 out
<< '(' << a
.lhs_
<< '+' << a
.rhs_
<< ')';
71 std::ostream
& operator<<(std::ostream
& out
, const Sub
& a
)
73 out
<< '(' << a
.lhs_
<< '-' << a
.rhs_
<< ')';
78 // Expression evaluation visitor
80 struct Calculator
: boost::static_visitor
<int>
84 int operator()(Add
& x
) const
87 int n1
= boost::apply_visitor(calc
, x
.lhs_
);
88 int n2
= boost::apply_visitor(calc
, x
.rhs_
);
93 int operator()(Sub
& x
) const
95 return boost::apply_visitor(Calculator(), x
.lhs_
)
96 - boost::apply_visitor(Calculator(), x
.rhs_
);
99 int operator()(Expr
& x
) const
102 return boost::apply_visitor(calc
, x
);
105 int operator()(int x
) const
113 /////////////////////////////////////////////////////////////////////
116 int test_main(int, char* [])
120 Expr
e1( Add(n
, Sub(Add(40,2),Add(10,4))) ); //n + (40+2)-(10+14) = n+28
122 std::ostringstream e1_str
;
125 BOOST_CHECK(e1
.type() == boost::typeindex::type_id
<Add
>());
126 BOOST_CHECK(e1_str
.str() == "(13+((40+2)-(10+4)))");
128 //Evaluate expression
129 int res
= boost::apply_visitor(Calculator(), e1
);
130 BOOST_CHECK(res
== n
+ 28);