|
| 1 | +// Copyright (c) 2018-2025 Jean-Louis Leroy |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// See accompanying file LICENSE_1_0.txt |
| 4 | +// or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | + |
| 6 | +#include <iostream> |
| 7 | + |
| 8 | +#include <boost/openmethod/initialize.hpp> |
| 9 | +#include <boost/openmethod/core.hpp> |
| 10 | + |
| 11 | +struct Node { |
| 12 | + virtual ~Node() { |
| 13 | + } |
| 14 | + virtual int value() const = 0; |
| 15 | +}; |
| 16 | + |
| 17 | +struct Variable : Node { |
| 18 | + Variable(int value) : v(value) { |
| 19 | + } |
| 20 | + int value() const override { |
| 21 | + return v; |
| 22 | + } |
| 23 | + int v; |
| 24 | +}; |
| 25 | + |
| 26 | +struct Plus : Node { |
| 27 | + Plus(const Node& left, const Node& right) : left(left), right(right) { |
| 28 | + } |
| 29 | + int value() const override { |
| 30 | + return left.value() + right.value(); |
| 31 | + } |
| 32 | + const Node& left; |
| 33 | + const Node& right; |
| 34 | +}; |
| 35 | + |
| 36 | +struct Times : Node { |
| 37 | + Times(const Node& left, const Node& right) : left(left), right(right) { |
| 38 | + } |
| 39 | + int value() const override { |
| 40 | + return left.value() * right.value(); |
| 41 | + } |
| 42 | + const Node& left; |
| 43 | + const Node& right; |
| 44 | +}; |
| 45 | + |
| 46 | +using namespace boost::openmethod; |
| 47 | + |
| 48 | +// tag::method[] |
| 49 | +#include <boost/openmethod/macros.hpp> |
| 50 | + |
| 51 | +struct BOOST_OPENMETHOD_ID(postfix); |
| 52 | + |
| 53 | +using postfix = method< |
| 54 | + BOOST_OPENMETHOD_ID(postfix), |
| 55 | + void(virtual_ptr<const Node> node, std::ostream& os)>; |
| 56 | +// end::method[] |
| 57 | + |
| 58 | +// tag::variable_overrider[] |
| 59 | +auto postfix_variable(virtual_ptr<const Variable> node, std::ostream& os) { |
| 60 | + os << node->v; |
| 61 | +} |
| 62 | + |
| 63 | +static postfix::override<postfix_variable> override_postfix_variable; |
| 64 | +// end::variable_overrider[] |
| 65 | + |
| 66 | +// tag::binary_overriders[] |
| 67 | +#include <boost/openmethod/macros.hpp> |
| 68 | + |
| 69 | +auto postfix_plus(virtual_ptr<const Plus> node, std::ostream& os) { |
| 70 | + postfix::fn(node->left, os); |
| 71 | + os << ' '; |
| 72 | + postfix::fn(node->right, os); |
| 73 | + os << " +"; |
| 74 | +} |
| 75 | + |
| 76 | +auto postfix_times(virtual_ptr<const Times> node, std::ostream& os) { |
| 77 | + postfix::fn(node->left, os); |
| 78 | + os << ' '; |
| 79 | + postfix::fn(node->right, os); |
| 80 | + os << " *"; |
| 81 | +} |
| 82 | + |
| 83 | +BOOST_OPENMETHOD_REGISTER(postfix::override<postfix_plus, postfix_times>); |
| 84 | +// end::binary_overriders[] |
| 85 | + |
| 86 | +// tag::use_classes[] |
| 87 | +BOOST_OPENMETHOD_REGISTER(use_classes<Node, Variable, Plus, Times>); |
| 88 | +// end::use_classes[] |
| 89 | + |
| 90 | +// tag::main[] |
| 91 | +auto main() -> int { |
| 92 | + boost::openmethod::initialize(); |
| 93 | + Variable a{2}, b{3}, c{4}; |
| 94 | + Plus d{a, b}; |
| 95 | + Times e{d, c}; |
| 96 | + postfix::fn(e, std::cout); |
| 97 | + std::cout << " = " << e.value() << "\n"; // 2 3 + 4 * = 20 |
| 98 | +} |
| 99 | +// end::main[] |
0 commit comments