Skip to content

Commit 838dc1d

Browse files
committed
improve Core API doc
1 parent e3c76f0 commit 838dc1d

File tree

5 files changed

+264
-144
lines changed

5 files changed

+264
-144
lines changed

doc/modules/ROOT/examples/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ foreach (cpp ${cpp_files})
2626
add_dependencies(tests ${stem})
2727
endforeach()
2828

29-
if (NOT WIN32)
30-
add_subdirectory(shared_libs)
31-
endif()
32-
3329
function(add_step_by_step dir)
3430
set(add_test "")
3531
if(ARGC GREATER 1)
@@ -51,5 +47,10 @@ function(add_step_by_step dir)
5147
endforeach()
5248
endfunction()
5349

50+
add_step_by_step(core_api)
5451
add_step_by_step(rolex)
5552
add_step_by_step(ambiguities OFF)
53+
54+
if (NOT WIN32)
55+
add_subdirectory(shared_libs)
56+
endif()

doc/modules/ROOT/examples/core_api.cpp

Lines changed: 0 additions & 104 deletions
This file was deleted.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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[]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
// clang-format off
7+
8+
#include <iostream>
9+
10+
// tag::classes[]
11+
#include <boost/openmethod.hpp>
12+
#include <boost/openmethod/initialize.hpp>
13+
14+
struct Node {
15+
virtual ~Node() {}
16+
virtual int value() const = 0;
17+
};
18+
19+
struct Variable : Node {
20+
Variable(int value) : v(value) {}
21+
int value() const override { return v; }
22+
int v;
23+
};
24+
25+
template<typename Op>
26+
struct BinaryOp : Node {
27+
BinaryOp(const Node& left, const Node& right) : left(left), right(right) {}
28+
int value() const override { return Op()(left.value(), right.value()); }
29+
const Node& left;
30+
const Node& right;
31+
};
32+
// end::classes[]
33+
34+
using namespace boost::openmethod;
35+
36+
// tag::method[]
37+
BOOST_OPENMETHOD(
38+
postfix, (virtual_ptr<const Node> node, std::ostream& os), void);
39+
40+
BOOST_OPENMETHOD_OVERRIDE(
41+
postfix, (virtual_ptr<const Variable> var, std::ostream& os), void) {
42+
os << var->v;
43+
}
44+
// end::method[]
45+
46+
// tag::postfix_binary[]
47+
template<class BinaryOp, char Op>
48+
auto postfix_binary(virtual_ptr<const BinaryOp> node, std::ostream& os) {
49+
postfix(node->left, os);
50+
os << ' ';
51+
postfix(node->right, os);
52+
os << " " << Op;
53+
}
54+
// end::postfix_binary[]
55+
56+
// tag::add_postfix_binary[]
57+
BOOST_OPENMETHOD_TYPE(
58+
postfix, (virtual_ptr<const Node> node, std::ostream& os), void)::
59+
override<
60+
postfix_binary<BinaryOp<std::plus<int>>, '+'>,
61+
postfix_binary<
62+
BinaryOp<std::multiplies<int>>, '*'>> add_binary_overriders;
63+
// end::add_postfix_binary[]
64+
65+
// tag::main[]
66+
BOOST_OPENMETHOD_CLASSES(
67+
Node, Variable, BinaryOp<std::plus<int>>, BinaryOp<std::multiplies<int>>);
68+
69+
auto main() -> int {
70+
boost::openmethod::initialize();
71+
Variable a{2}, b{3}, c{4};
72+
BinaryOp<std::plus<int>> d{a, b};
73+
BinaryOp<std::multiplies<int>> e{d, c};
74+
postfix(e, std::cout);
75+
std::cout << " = " << e.value() << "\n"; // 2 3 + 4 * = 20
76+
}
77+
// end::main[]

0 commit comments

Comments
 (0)