Skip to content

Commit 9ecf915

Browse files
committed
basics.adoc: include cpp fragments
1 parent c7bc263 commit 9ecf915

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

doc/modules/ROOT/examples/ast.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,22 @@ using boost::openmethod::virtual_ptr;
4343

4444
BOOST_OPENMETHOD(postfix, (virtual_ptr<const Node> node, std::ostream& os), void);
4545

46+
// tag::variable_overrider[]
4647
BOOST_OPENMETHOD_OVERRIDE(
4748
postfix, (virtual_ptr<const Variable> var, std::ostream& os), void) {
4849
os << var->v;
4950
}
51+
// end::variable_overrider[]
5052

53+
// tag::plus_overrider[]
5154
BOOST_OPENMETHOD_OVERRIDE(
5255
postfix, (virtual_ptr<const Plus> plus, std::ostream& os), void) {
5356
postfix(plus->left, os);
5457
os << ' ';
5558
postfix(plus->right, os);
5659
os << " +";
5760
}
61+
// end::plus_overrider[]
5862

5963
BOOST_OPENMETHOD_OVERRIDE(
6064
postfix, (virtual_ptr<const Times> times, std::ostream& os), void) {
@@ -64,7 +68,9 @@ BOOST_OPENMETHOD_OVERRIDE(
6468
os << " *";
6569
}
6670

71+
// tag::class_registration[]
6772
BOOST_OPENMETHOD_CLASSES(Node, Variable, Plus, Times);
73+
// end::class_registration[]
6874

6975
int main() {
7076
boost::openmethod::initialize();

doc/modules/ROOT/pages/basics.adoc

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ inline auto postfix(virtual_ptr<const Node> node, std::ostream& os) -> void {
3737
Before we can call the method, we need to define overriders. For that we use the
3838
xref:BOOST_OPENMETHOD_OVERRIDE.adoc[BOOST_OPENMETHOD_OVERRIDE] macro:
3939
40-
```c++
41-
BOOST_OPENMETHOD_OVERRIDE(postfix, (virtual_ptr<const Variable> var, std::ostream& os), void) {
42-
os << var->v;
43-
}
44-
```
40+
[source,cpp]
41+
----
42+
include::{examplesdir}/ast.cpp[tag=variable_overrider]
43+
----
4544
4645
The overrider must have virtual parameters in the same positions as in the
4746
method. The classes in the method's virtual parameters must be accessible,
@@ -53,32 +52,31 @@ The non-virtual parameters must have exactly the same types as in the method.
5352
5453
Let's look at another overrider:
5554
56-
```c++
57-
BOOST_OPENMETHOD_OVERRIDE(postfix, (virtual_ptr<const Plus> plus, std::ostream& os), void) {
58-
postfix(plus->left, os);
59-
os << ' ';
60-
postfix(plus->right, os);
61-
}
62-
```
55+
[source,cpp]
56+
----
57+
include::{examplesdir}/ast.cpp[tag=plus_overrider]
58+
----
6359
6460
This one calls `postfix` recursively to print the left and right
6561
sub-expressions. Note that we call the method just like an ordinary function.
6662
6763
`postfix` expects a `virtual_ptr<const Node>`, and we are passing it a _plain_
6864
_reference_ to a `Plus` object. This works because `virtual_ptr` has conversion
6965
constructors from plain references or pointers to an object, or from other
70-
`virtual_ptr` to compatible classes.
66+
`virtual_ptr`{empty}s to compatible classes.
7167
7268
There are two more things we need to do.
7369
7470
OpenMethod is a library, not a compiler. It needs to be made aware of all the
7571
classes that may be used as virtual parameters, and in method calls, and their
76-
inheritance relationships. We do this using the
72+
inheritance relationships. We do this with the
7773
xref:BOOST_OPENMETHOD_CLASSES.adoc[BOOST_OPENMETHOD_CLASSES] macro:
7874
79-
```c++
80-
BOOST_OPENMETHOD_CLASSES(Node, Variable, Plus, Times);
81-
```
75+
76+
[source,cpp]
77+
----
78+
include::{examplesdir}/ast.cpp[tag=class_registration]
79+
----
8280
8381
Classes can be registered multiple times, in any order, and incrementally. Every
8482
direct base of a class must appear together with it in at least one call to

0 commit comments

Comments
 (0)