Skip to content

Commit 41c4629

Browse files
committed
more doc
1 parent b41f40f commit 41c4629

20 files changed

Lines changed: 706 additions & 486 deletions

doc/modules/ROOT/examples/ast.cpp

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,80 +5,73 @@
55

66
// clang-format off
77

8-
// tag::ast[]
9-
10-
#include <iostream>
11-
12-
#include <boost/openmethod.hpp>
13-
#include <boost/openmethod/initialize.hpp>
14-
15-
using boost::openmethod::virtual_ptr;
16-
8+
// tag::content[]
179
struct Node {
1810
virtual ~Node() {}
11+
virtual int value() const = 0;
1912
};
2013

2114
struct Variable : Node {
22-
explicit Variable(int value) : value(value) {}
23-
24-
int value;
15+
Variable(int value) : v(value) {}
16+
int value() const override { return v; }
17+
int v;
2518
};
2619

2720
struct Plus : Node {
28-
Plus(virtual_ptr<Node> left, virtual_ptr<Node> right)
29-
: left(left), right(right) {}
21+
Plus(const Node& left, const Node& right) : left(left), right(right) {}
22+
int value() const override { return left.value() + right.value(); }
23+
const Node& left; const Node& right;
24+
};
3025

31-
virtual_ptr<Node> left, right;
26+
struct Times : Node {
27+
Times(const Node& left, const Node& right) : left(left), right(right) {}
28+
int value() const override { return left.value() * right.value(); }
29+
const Node& left; const Node& right;
3230
};
3331

34-
struct Negate : Node {
35-
explicit Negate(virtual_ptr<Node> node) : child(node) {}
32+
#include <boost/openmethod.hpp>
33+
#include <boost/openmethod/initialize.hpp>
34+
#include <iostream>
3635

37-
virtual_ptr<Node> child;
38-
};
36+
using boost::openmethod::virtual_ptr;
3937

40-
BOOST_OPENMETHOD(value, (virtual_ptr<Node>), int);
38+
BOOST_OPENMETHOD(postfix, (virtual_ptr<const Node> node, std::ostream& os), void);
4139

42-
BOOST_OPENMETHOD_OVERRIDE(value, (virtual_ptr<Variable> node), int) {
43-
return node->value;
40+
BOOST_OPENMETHOD_OVERRIDE(
41+
postfix, (virtual_ptr<const Variable> var, std::ostream& os), void) {
42+
os << var->v;
4443
}
4544

46-
BOOST_OPENMETHOD_OVERRIDE(value, (virtual_ptr<Plus> node), int) {
47-
return value(node->left) + value(node->right);
45+
BOOST_OPENMETHOD_OVERRIDE(
46+
postfix, (virtual_ptr<const Plus> plus, std::ostream& os), void) {
47+
postfix(plus->left, os);
48+
os << ' ';
49+
postfix(plus->right, os);
50+
os << " +";
4851
}
4952

50-
BOOST_OPENMETHOD_OVERRIDE(value, (virtual_ptr<Negate> node), int) {
51-
return -value(node->child);
53+
BOOST_OPENMETHOD_OVERRIDE(
54+
postfix, (virtual_ptr<const Times> times, std::ostream& os), void) {
55+
postfix(times->left, os);
56+
os << ' ';
57+
postfix(times->right, os);
58+
os << " *";
5259
}
5360

54-
BOOST_OPENMETHOD_CLASSES(Node, Variable, Plus, Negate);
61+
BOOST_OPENMETHOD_CLASSES(Node, Variable, Plus, Times);
5562

56-
auto main() -> int {
63+
int main() {
5764
boost::openmethod::initialize();
58-
59-
Variable one(1), two(2);
60-
Plus sum(one, two);
61-
Negate neg(sum);
62-
63-
std::cout << value(neg) << "\n"; // -3
64-
65-
return 0;
65+
Variable a{2}, b{3}, c{4};
66+
Plus d{a, b}; Times e{d, c};
67+
postfix(e, std::cout);
68+
std::cout << " = " << e.value() << "\n"; // 2 3 + 4 * = 20
6669
}
67-
// end::ast[]
6870

69-
auto negate(virtual_ptr<Node> node) -> int {
70-
return -value(node);
71+
void call_via_ref(const Node& node, std::ostream& os) {
72+
postfix(node, os);
7173
}
7274

73-
#define main alt_main
74-
75-
auto main() -> int {
76-
// tag::final[]
77-
Variable one(1);
78-
Negate neg(boost::openmethod::final_virtual_ptr(one));
79-
// end::final[]
80-
81-
std::cout << value(boost::openmethod::final_virtual_ptr(neg)) << "\n"; // -3
82-
83-
return 0;
75+
void call_via_virtual_ptr(virtual_ptr<const Node> node, std::ostream& os) {
76+
postfix(node, os);
8477
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
// tag::content[]
9+
#include <boost/openmethod.hpp>
10+
using namespace boost::openmethod::aliases;
11+
12+
struct Node {
13+
};
14+
15+
struct Variable : Node {
16+
Variable(int value) : v(value) {}
17+
int v;
18+
};
19+
20+
struct Plus : Node {
21+
Plus(virtual_ptr<const Node> left, virtual_ptr<const Node> right)
22+
: left(left), right(right) {}
23+
virtual_ptr<const Node> left, right;
24+
};
25+
26+
struct Times : Node {
27+
Times(virtual_ptr<const Node> left, virtual_ptr<const Node> right)
28+
: left(left), right(right) {}
29+
virtual_ptr<const Node> left, right;
30+
};
31+
32+
#include <iostream>
33+
34+
using boost::openmethod::virtual_ptr;
35+
36+
BOOST_OPENMETHOD(value, (virtual_ptr<const Node>), int);
37+
38+
BOOST_OPENMETHOD_OVERRIDE(value, (virtual_ptr<const Variable> node), int) {
39+
return node->v;
40+
}
41+
42+
BOOST_OPENMETHOD_OVERRIDE(value, (virtual_ptr<const Plus> node), int) {
43+
return value(node->left) + value(node->right);
44+
}
45+
46+
BOOST_OPENMETHOD_OVERRIDE(value, (virtual_ptr<const Times> node), int) {
47+
return value(node->left) * value(node->right);
48+
}
49+
50+
BOOST_OPENMETHOD(postfix, (virtual_ptr<const Node> node, std::ostream& os), void);
51+
52+
BOOST_OPENMETHOD_OVERRIDE(
53+
postfix, (virtual_ptr<const Variable> var, std::ostream& os), void) {
54+
os << var->v;
55+
}
56+
57+
BOOST_OPENMETHOD_OVERRIDE(
58+
postfix, (virtual_ptr<const Plus> plus, std::ostream& os), void) {
59+
postfix(plus->left, os);
60+
os << ' ';
61+
postfix(plus->right, os);
62+
os << " +";
63+
}
64+
65+
BOOST_OPENMETHOD_OVERRIDE(
66+
postfix, (virtual_ptr<const Times> times, std::ostream& os), void) {
67+
postfix(times->left, os);
68+
os << ' ';
69+
postfix(times->right, os);
70+
os << " *";
71+
}
72+
73+
BOOST_OPENMETHOD_CLASSES(Node, Variable, Plus, Times);
74+
75+
#include <boost/openmethod/initialize.hpp>
76+
77+
int main() {
78+
boost::openmethod::initialize();
79+
Variable a{2}, b{3}, c{4};
80+
Plus d{final_virtual_ptr(a), final_virtual_ptr(b)};
81+
Times e{final_virtual_ptr(d), final_virtual_ptr(c)};
82+
auto root = final_virtual_ptr(e);
83+
postfix(root, std::cout);
84+
std::cout << " = " << value(root) << "\n"; // 2 3 + 4 * = 20
85+
}
86+
// end::content[]

doc/modules/ROOT/examples/ast_unique_ptr.cpp

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,77 @@
55

66
// clang-format off
77

8-
// tag::ast[]
9-
10-
#include <iostream>
11-
#include <memory>
12-
8+
// tag::content[]
139
#include <boost/openmethod.hpp>
1410
#include <boost/openmethod/interop/std_unique_ptr.hpp>
15-
#include <boost/openmethod/initialize.hpp>
1611

1712
using namespace boost::openmethod::aliases;
1813

14+
// NOTE: unique_virtual_ptr<Class> is an alias for
15+
// virtual_ptr<std::unique_ptr<Class>>
16+
1917
struct Node {
2018
virtual ~Node() {}
19+
virtual int value() const = 0;
2120
};
2221

2322
struct Variable : Node {
24-
Variable(int value) : value(value) {}
25-
26-
int value;
23+
Variable(int value) : v(value) {}
24+
int value() const override { return v; }
25+
int v;
2726
};
2827

2928
struct Plus : Node {
30-
Plus(unique_virtual_ptr<Node> left, unique_virtual_ptr<Node> right)
29+
Plus(unique_virtual_ptr<const Node> left, unique_virtual_ptr<const Node> right)
3130
: left(std::move(left)), right(std::move(right)) {}
32-
33-
unique_virtual_ptr<Node> left, right;
31+
int value() const override { return left->value() + right->value(); }
32+
unique_virtual_ptr<const Node> left, right;
3433
};
3534

36-
struct Negate : Node {
37-
Negate(unique_virtual_ptr<Node> node) : child(std::move(node)) {}
38-
39-
unique_virtual_ptr<Node> child;
35+
struct Times : Node {
36+
Times(unique_virtual_ptr<const Node> left, unique_virtual_ptr<const Node> right)
37+
: left(std::move(left)), right(std::move(right)) {}
38+
int value() const override { return left->value() * right->value(); }
39+
unique_virtual_ptr<const Node> left, right;
4040
};
4141

42-
BOOST_OPENMETHOD(value, (virtual_ptr<Node>), int);
42+
#include <iostream>
4343

44-
BOOST_OPENMETHOD_OVERRIDE(value, (virtual_ptr<Variable> node), int) {
45-
return node->value;
46-
}
44+
BOOST_OPENMETHOD(postfix, (virtual_ptr<const Node> node, std::ostream& os), void);
4745

48-
BOOST_OPENMETHOD_OVERRIDE(value, (virtual_ptr<Plus> node), int) {
49-
return value(node->left) + value(node->right);
46+
BOOST_OPENMETHOD_OVERRIDE(
47+
postfix, (virtual_ptr<const Variable> var, std::ostream& os), void) {
48+
os << var->v;
5049
}
5150

52-
BOOST_OPENMETHOD_OVERRIDE(value, (virtual_ptr<Negate> node), int) {
53-
return -value(node->child);
51+
BOOST_OPENMETHOD_OVERRIDE(
52+
postfix, (virtual_ptr<const Plus> plus, std::ostream& os), void) {
53+
postfix(plus->left, os);
54+
os << ' ';
55+
postfix(plus->right, os);
56+
os << " +";
5457
}
5558

56-
BOOST_OPENMETHOD_CLASSES(Node, Variable, Plus, Negate);
57-
58-
auto main() -> int {
59-
boost::openmethod::initialize();
59+
BOOST_OPENMETHOD_OVERRIDE(
60+
postfix, (virtual_ptr<const Times> times, std::ostream& os), void) {
61+
postfix(times->left, os);
62+
os << ' ';
63+
postfix(times->right, os);
64+
os << " *";
65+
}
6066

61-
auto expr = make_unique_virtual<Negate>(
62-
make_unique_virtual<Plus>(
63-
make_unique_virtual<Variable>(1),
64-
make_unique_virtual<Variable>(2)));
67+
BOOST_OPENMETHOD_CLASSES(Node, Variable, Plus, Times);
6568

66-
std::cout << value(expr) << "\n"; // -3
69+
#include <boost/openmethod/initialize.hpp>
6770

68-
return 0;
71+
int main() {
72+
boost::openmethod::initialize();
73+
auto a = std::make_unique<Variable>(2);
74+
auto b = std::make_unique<Variable>(3);
75+
auto c = std::make_unique<Variable>(4);
76+
auto d = make_unique_virtual<Plus>(std::move(a), std::move(b));
77+
auto e = make_unique_virtual<Times>(std::move(d), std::move(c));
78+
postfix(e, std::cout);
79+
std::cout << " = " << e->value() << "\n"; // 2 3 + 4 * = 20
6980
}
70-
// end::ast[]
81+
// end::content[]

doc/modules/ROOT/examples/ast_virtual_function.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55

66
// clang-format off
77

8+
// tag::content[]
89
#include <iostream>
910

10-
// tag::all[]
11-
1211
struct Node {
1312
virtual ~Node() {}
1413
virtual int value() const = 0;
@@ -35,6 +34,6 @@ struct Times : Node {
3534
int main() {
3635
Variable a{2}, b{3}, c{4};
3736
Plus d{a, b}; Times e{d, c};
38-
std::cout << e.value() << "\n"; // prints "20"
37+
std::cout << e.value() << "\n"; // 20
3938
}
40-
// end::all[]
39+
// end::content[]

doc/modules/ROOT/examples/ast_virtual_function_2.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
// clang-format off
77

8-
#include <iostream>
9-
108
// tag::all[]
9+
#include <iostream>
1110

1211
struct Node {
1312
virtual ~Node() {}
@@ -47,3 +46,7 @@ int main() {
4746
std::cout << " = " << e.value() << "\n"; // 2 3 + 4 * = 20
4847
}
4948
// end::all[]
49+
50+
void call_virtual_function(const Node& node, std::ostream& os) {
51+
node.postfix(os);
52+
}

0 commit comments

Comments
 (0)