From 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 20:24:20 +0200 Subject: Adding upstream version 14.2.21. Signed-off-by: Daniel Baumann --- src/boost/libs/proto/example/calc1.cpp | 68 ++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/boost/libs/proto/example/calc1.cpp (limited to 'src/boost/libs/proto/example/calc1.cpp') 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 +#include +#include +namespace proto = boost::proto; +using proto::_; + +template 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 + double operator ()(proto::tag::terminal, placeholder) const + { + return d[ I - 1 ]; + } +}; + +template +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; +} +//] -- cgit v1.2.3