]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/proto/example/calc2.cpp
2 // Copyright 2008 Eric Niebler. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 // This example enhances the simple arithmetic expression evaluator
7 // in calc1.cpp by using proto::extends to make arithmetic
8 // expressions immediately evaluable with operator (), a-la a
12 #include <boost/proto/core.hpp>
13 #include <boost/proto/context.hpp>
14 namespace proto
= boost::proto
;
17 template<typename Expr
>
18 struct calculator_expression
;
20 // Tell proto how to generate expressions in the calculator_domain
21 struct calculator_domain
22 : proto::domain
<proto::generator
<calculator_expression
> >
25 // Will be used to define the placeholders _1 and _2
26 template<int I
> struct placeholder
{};
28 // Define a calculator context, for evaluating arithmetic expressions
29 // (This is as before, in calc1.cpp)
30 struct calculator_context
31 : proto::callable_context
< calculator_context
const >
33 // The values bound to the placeholders
36 // The result of evaluating arithmetic expressions
37 typedef double result_type
;
39 explicit calculator_context(double d1
= 0., double d2
= 0.)
45 // Handle the evaluation of the placeholder terminals
47 double operator ()(proto::tag::terminal
, placeholder
<I
>) const
53 // Wrap all calculator expressions in this type, which defines
54 // operator () to evaluate the expression.
55 template<typename Expr
>
56 struct calculator_expression
57 : proto::extends
<Expr
, calculator_expression
<Expr
>, calculator_domain
>
59 explicit calculator_expression(Expr
const &expr
= Expr())
60 : calculator_expression::proto_extends(expr
)
63 BOOST_PROTO_EXTENDS_USING_ASSIGN(calculator_expression
<Expr
>)
65 // Override operator () to evaluate the expression
66 double operator ()() const
68 calculator_context
const ctx
;
69 return proto::eval(*this, ctx
);
72 double operator ()(double d1
) const
74 calculator_context
const ctx(d1
);
75 return proto::eval(*this, ctx
);
78 double operator ()(double d1
, double d2
) const
80 calculator_context
const ctx(d1
, d2
);
81 return proto::eval(*this, ctx
);
85 // Define some placeholders (notice they're wrapped in calculator_expression<>)
86 calculator_expression
<proto::terminal
< placeholder
< 1 > >::type
> const _1
;
87 calculator_expression
<proto::terminal
< placeholder
< 2 > >::type
> const _2
;
89 // Now, our arithmetic expressions are immediately executable function objects:
93 std::cout
<< (_1
+ 2.0)( 3.0 ) << std::endl
;
96 std::cout
<< ( _1
* _2
)( 3.0, 2.0 ) << std::endl
;
99 std::cout
<< ( (_1
- _2
) / _2
)( 3.0, 2.0 ) << std::endl
;