You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Works with existing types (no base class required)
44
33
- Composable interfaces (build _views_ from parts)
45
34
- Great for embedded, plugin, and systems-level code
46
35
36
+
47
37
## Why Archetype?
38
+
Archetype provides a simple, clean, and extremely flexible way to define type erased interfaces, giving you a common way to interact with polymorphic types.
39
+
48
40
49
-
Inheritance is often overused when **composition** or **concepts** would have
41
+
Inheritance is often misused when **composition** or **concepts** would have
50
42
been more appropriate. However, alternatives to inheritance often mean losing
51
43
the ability to use clean, type erased interfaces.
52
44
@@ -56,15 +48,16 @@ Archetype fills this gap:
56
48
- Like composition, it avoids rigid hierarchies
57
49
- Like concepts, it expresses **interface intent** clearly
58
50
- Like std::function it allows **type erased binding**
59
-
- But unlike any of these **TODO**
51
+
- But unlike any of them it provides type erased, static dispatch, without dynamic memory allocation, and with composable interfaces at the same time
52
+
60
53
61
54
## The Inheritance Problem
62
55
63
-
Inheritance gives you base pointers and dynamic dispatch, but at a cost:
56
+
Inheritance gives you base pointers and dynamic dispatch but at a cost.
64
57
65
58
### Example
66
59
67
-
Suppose you want to reuse parts of `A`, `B`, `C`.
60
+
Suppose you want to reuse parts of classes `A`, `B`, and`C`.
68
61
69
62
```cpp
70
63
classA { void a(); };
@@ -75,24 +68,26 @@ class AB : public A, public B {};
75
68
class AC : public A, public C {};
76
69
class BC : public B, public C {};
77
70
```
71
+
We can refer `AB` and `AC` with an `A` base pointer (common interface).
72
+
Or `AC` and `BC` with a `C`base pointer.
78
73
79
-
We can refer `AB` and `AC` with an `A` base pointer. Or `AC` and `BC` with a `C`
80
-
base pointer. But if we want to refer to any object that implements both `A` and
81
-
`C` like `ABC` or `ACD`?
74
+
But if we want to refer to any object that implements both `A` and
75
+
`C` like `ABC` or `ACD`.
82
76
83
77
With inheritance you:
84
-
85
78
- Lose composability
86
79
- Struggle to find a common base
87
80
- Risk coupling, and rigid hierarchies
88
81
- Risk diamond problems (in multiple inheritance)
89
82
90
83
With composition:
91
-
92
84
- You can't refer to composites polymorphically
93
85
- There's no base interface, unless you add one manually
94
86
95
-
## Archetypes
87
+
With other techiques like **CRTP** or **concepts** we still loose the ability to refer
88
+
to different types polymorphically ie: we can't create `std::vector<common_interface>`
89
+
90
+
## Archetypes/Quickstart
96
91
97
92
**Archetype** gives you **type erased** views over objects that _implement_ a
98
93
particular interface without requiring them to inherit from anything.
@@ -122,13 +117,25 @@ ABD abd;
122
117
archetype_ab::view abc_view, abd_view;
123
118
abc_view.bind(abc);
124
119
abd_view.bind(abd);
120
+
```
125
121
122
+
### Use the interface:
123
+
```cpp
126
124
abc_view.a();
127
125
abd_view.b(5);
128
126
```
129
127
128
+
### Alternativley use a pointer style view:
129
+
```cpp
130
+
archetype_ab::view_ptr<> abc_view_ptr;
131
+
abc_view_ptr.bind(abc);
132
+
abc_view_ptr->a();
133
+
abc_view_ptr->b(5);
134
+
```
135
+
130
136
You now have a **type erased**, **composable**, **low overhead** reference to
131
-
any object that implements* the interface regardless of its type or hierarchy.
137
+
any object that implements* the interface regardless of its type or hierarchy.
138
+
In this case we created views that can view the common `AB` parts of `ABC` and `ABD`
132
139
133
140
## How Archetype Compares
134
141
@@ -139,13 +146,11 @@ any object that implements* the interface regardless of its type or hierarchy.
0 commit comments