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
|
//-----------------------------------------------------------------------------
// boost-libs variant/test/test3.cpp source file
// See http://www.boost.org for updates, documentation, and revision history.
//-----------------------------------------------------------------------------
//
// Copyright (c) 2003
// Eric Friedman, Itay Maman
//
// 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)
#include "boost/test/minimal.hpp"
#include "boost/variant.hpp"
#include <iostream>
#include <sstream>
#include <string>
/////////////////////////////////////////////////////////////////////
using boost::variant;
using boost::recursive_wrapper;
using std::cout;
using std::endl;
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
struct Add;
struct Sub;
typedef variant<int, recursive_wrapper<Add>, recursive_wrapper<Sub> > Expr;
struct Sub
{
Sub();
Sub(const Expr& l, const Expr& r);
Sub(const Sub& other);
Expr lhs_;
Expr rhs_;
};
struct Add
{
Add() { }
Add(const Expr& l, const Expr& r) : lhs_(l), rhs_(r) { }
Add(const Add& other) : lhs_(other.lhs_), rhs_(other.rhs_) { }
Expr lhs_;
Expr rhs_;
};
Sub::Sub() { }
Sub::Sub(const Expr& l, const Expr& r) : lhs_(l), rhs_(r) { }
Sub::Sub(const Sub& other) : lhs_(other.lhs_), rhs_(other.rhs_) { }
//
// insert-to operators
//
std::ostream& operator<<(std::ostream& out, const Sub& a);
std::ostream& operator<<(std::ostream& out, const Add& a)
{
out << '(' << a.lhs_ << '+' << a.rhs_ << ')';
return out;
}
std::ostream& operator<<(std::ostream& out, const Sub& a)
{
out << '(' << a.lhs_ << '-' << a.rhs_ << ')';
return out;
}
//
// Expression evaluation visitor
//
struct Calculator : boost::static_visitor<int>
{
Calculator() { }
int operator()(Add& x) const
{
Calculator calc;
int n1 = boost::apply_visitor(calc, x.lhs_);
int n2 = boost::apply_visitor(calc, x.rhs_);
return n1 + n2;
}
int operator()(Sub& x) const
{
return boost::apply_visitor(Calculator(), x.lhs_)
- boost::apply_visitor(Calculator(), x.rhs_);
}
int operator()(Expr& x) const
{
Calculator calc;
return boost::apply_visitor(calc, x);
}
int operator()(int x) const
{
return x;
}
}; // Calculator
/////////////////////////////////////////////////////////////////////
int test_main(int, char* [])
{
int n = 13;
Expr e1( Add(n, Sub(Add(40,2),Add(10,4))) ); //n + (40+2)-(10+14) = n+28
std::ostringstream e1_str;
e1_str << e1;
BOOST_CHECK(e1.type() == typeid(Add));
BOOST_CHECK(e1_str.str() == "(13+((40+2)-(10+4)))");
//Evaluate expression
int res = boost::apply_visitor(Calculator(), e1);
BOOST_CHECK(res == n + 28);
return 0;
}
|