Skip to content

Commit db280c8

Browse files
Starting a how it works explanation
1 parent e73e920 commit db280c8

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

docs/how_it_works.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
```cpp
2+
ARCHETYPE_DEFINE(basic_interface, (
3+
ARCHETYPE_METHOD(int, func0, const char *)
4+
))
5+
```
6+
7+
This expands to a structure that contains four substructures. These are:
8+
9+
- **A protected component structure.** This implements the vtable as a stackable layer, but does not have a public interface.
10+
- **A public view class.** Stacks the component on top of a base, and provides the public bind() function while keeping the internal component layer private.
11+
- **A public ptr class.** This gives an alternative public interface that allows using pointer syntax.
12+
- **A public templated check<T> structure.** This is used for testing and verifying that type T meets the interface specification using SFINAE
13+
14+
## Component structure:
15+
Almost all of the core functionality exists within the protected component that exists within the `basic_interface` struct. I'll start with a stripped down version, and explain how the functionality is build up.
16+
17+
The component itself is templated on a base class. It will inherit from a particular base, and stack its functionality on top without using any virtuals. All the component cares about is being able to access the `B::_obj` that is a `void *` protected member variable defined by the base class. This `B::_obj` is going to store a `void *` of the object that will get bound.
18+
19+
```cpp
20+
template <typename B = archetype::Base>
21+
class component : public B {
22+
protected:
23+
using B::_obj;
24+
};
25+
```
26+
27+
For the component to actully be useful, it needs to implement the interface that we've defined. It does this by implementing a `vtable` through function pointers and functions. In this case we have a protected function pointer: `int (*_func0_stub)(void *obj, const char *);`
28+
29+
```cpp
30+
template <typename B = archetype::Base>
31+
class component : public B {
32+
public:
33+
int func0(const char *arg0) { return _func0_stub(_obj, arg0); }
34+
35+
protected:
36+
int (*_func0_stub)(void *obj, const char *);
37+
using B::_obj;
38+
};
39+
```

0 commit comments

Comments
 (0)