Skip to content

Commit 0ae0ac4

Browse files
committed
more doc
1 parent f0a679c commit 0ae0ac4

File tree

9 files changed

+317
-113
lines changed

9 files changed

+317
-113
lines changed

doc/modules/ROOT/examples/ast.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,11 @@ int main() {
6767
postfix(e, std::cout);
6868
std::cout << " = " << e.value() << "\n"; // 2 3 + 4 * = 20
6969
}
70+
71+
void call_via_ref(const Node& node, std::ostream& os) {
72+
postfix(node, os);
73+
}
74+
75+
void call_via_virtual_ptr(virtual_ptr<const Node> node, std::ostream& os) {
76+
postfix(node, os);
77+
}
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: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,73 @@
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>
16-
1711
using namespace boost::openmethod::aliases;
1812

1913
struct Node {
2014
virtual ~Node() {}
15+
virtual int value() const = 0;
2116
};
2217

2318
struct Variable : Node {
24-
Variable(int value) : value(value) {}
25-
26-
int value;
19+
Variable(int value) : v(value) {}
20+
int value() const override { return v; }
21+
int v;
2722
};
2823

2924
struct Plus : Node {
30-
Plus(unique_virtual_ptr<Node> left, unique_virtual_ptr<Node> right)
25+
Plus(unique_virtual_ptr<const Node> left, unique_virtual_ptr<const Node> right)
3126
: left(std::move(left)), right(std::move(right)) {}
32-
33-
unique_virtual_ptr<Node> left, right;
27+
int value() const override { return left->value() + right->value(); }
28+
unique_virtual_ptr<const Node> left, right;
3429
};
3530

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

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

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

48-
BOOST_OPENMETHOD_OVERRIDE(value, (virtual_ptr<Plus> node), int) {
49-
return value(node->left) + value(node->right);
42+
BOOST_OPENMETHOD_OVERRIDE(
43+
postfix, (virtual_ptr<const Variable> var, std::ostream& os), void) {
44+
os << var->v;
5045
}
5146

52-
BOOST_OPENMETHOD_OVERRIDE(value, (virtual_ptr<Negate> node), int) {
53-
return -value(node->child);
47+
BOOST_OPENMETHOD_OVERRIDE(
48+
postfix, (virtual_ptr<const Plus> plus, std::ostream& os), void) {
49+
postfix(plus->left, os);
50+
os << ' ';
51+
postfix(plus->right, os);
52+
os << " +";
5453
}
5554

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

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

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

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

doc/modules/ROOT/examples/ast_virtual_function_2.cpp

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

doc/modules/ROOT/nav.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
* xref:introduction.adoc[Introduction]
22
* xref:basics.adoc[Basics]
3-
* xref:smart_pointers.adoc[Smart Pointers]
43
* xref:performance.adoc[Performance]
4+
* xref:smart_pointers.adoc[Smart Pointers]
55
* xref:headers_namespaces.adoc[Headers and Namespaces]
66
* xref:friendship.adoc[Friendship]
77
* xref:multiple_dispatch.adoc[Multiple Dispatch]

doc/modules/ROOT/pages/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Boost.OpenMethod implements open-(multi-)methods in C++17 and above.
66

77
== Features
88

9-
* Single dispatch as fast as equivalent virtual function calls.
9+
* Single dispatch can be as fast as equivalent virtual function calls.
1010

1111
* Multiple dispatch in constant time (for a given number of virtual parameters).
1212

0 commit comments

Comments
 (0)