diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/proto/example/calc1.cpp | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/proto/example/calc1.cpp')
-rw-r--r-- | src/boost/libs/proto/example/calc1.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/boost/libs/proto/example/calc1.cpp b/src/boost/libs/proto/example/calc1.cpp new file mode 100644 index 00000000..d3fefa62 --- /dev/null +++ b/src/boost/libs/proto/example/calc1.cpp @@ -0,0 +1,68 @@ +//[ Calc1 +// Copyright 2008 Eric Niebler. 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) +// +// This is a simple example of how to build an arithmetic expression +// evaluator with placeholders. + +#include <iostream> +#include <boost/proto/core.hpp> +#include <boost/proto/context.hpp> +namespace proto = boost::proto; +using proto::_; + +template<int I> struct placeholder {}; + +// Define some placeholders +proto::terminal< placeholder< 1 > >::type const _1 = {{}}; +proto::terminal< placeholder< 2 > >::type const _2 = {{}}; + +// Define a calculator context, for evaluating arithmetic expressions +struct calculator_context + : proto::callable_context< calculator_context const > +{ + // The values bound to the placeholders + double d[2]; + + // The result of evaluating arithmetic expressions + typedef double result_type; + + explicit calculator_context(double d1 = 0., double d2 = 0.) + { + d[0] = d1; + d[1] = d2; + } + + // Handle the evaluation of the placeholder terminals + template<int I> + double operator ()(proto::tag::terminal, placeholder<I>) const + { + return d[ I - 1 ]; + } +}; + +template<typename Expr> +double evaluate( Expr const &expr, double d1 = 0., double d2 = 0. ) +{ + // Create a calculator context with d1 and d2 substituted for _1 and _2 + calculator_context const ctx(d1, d2); + + // Evaluate the calculator expression with the calculator_context + return proto::eval(expr, ctx); +} + +int main() +{ + // Displays "5" + std::cout << evaluate( _1 + 2.0, 3.0 ) << std::endl; + + // Displays "6" + std::cout << evaluate( _1 * _2, 3.0, 2.0 ) << std::endl; + + // Displays "0.5" + std::cout << evaluate( (_1 - _2) / _2, 3.0, 2.0 ) << std::endl; + + return 0; +} +//] |