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
Copy file name to clipboardExpand all lines: README.md
+15-13Lines changed: 15 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ In order to enable reflection, we must explicitly "register" our class.
24
24
25
25
For this, `cpp_reflection` provides a simple macro: `REGISTER_REFLECTABLE(type, list of functions)`.
26
26
*`Type` is the type on which we want to enable reflection
27
-
*`List of functions` is the list of member functions we want to be able to call during reflection.
27
+
*`List of functions` is the list of member functions we want to be able to call during reflection. These functions can have any signature (there is no restriction concerning the return value type, the number of parameters and the types of these parameters).
28
28
29
29
The list of functions must be formatted in a specific way: `(fct1)(fct2)(fct3)...`.
30
30
This format is the format used by Boost.PP (which is used in this library) and must be respected in order to compile.
@@ -34,11 +34,11 @@ For example, if we have the following class:
34
34
```cpp
35
35
classSomeClass {
36
36
public:
37
-
void fct(void) {
37
+
int fct(void) {
38
38
std::cout << "fct()" << std::endl;
39
39
}
40
40
41
-
void other_fct(void) {
41
+
void other_fct(const std::string&, float) {
42
42
std::cout << "other_fct()" << std::endl;
43
43
}
44
44
};
@@ -49,9 +49,11 @@ We just need to do `REGISTER_REFLECTABLE(SomeClass, (fct)(other_fct))` and this
49
49
## Making Reflection
50
50
Each time we register a type and its member functions, it stores this type into the `reflection_manager` singleton class.
51
51
52
-
This class is the class which does the reflection. By calling `reflection_manager::make_reflection("class_name", "function_name")`, this will call `class_name::function_name` (on a new object).
52
+
This class is the class which does the reflection. By calling `reflection_manager::make_reflection<RetVal, Params...>("class_name", "function_name", ...)`, this will call `class_name::function_name` (on a new object).
53
53
54
-
If we take the previous example, by calling `cpp_reflection::reflection_manager::make_reflection("SomeClass", "fct");`, we will call `SomeClass::fct`.
54
+
A facility function with a more elegant syntax is also provided: `make_reflection<RetVal(Params...)>::call("class_name", "function_name, ...")`. It provides the std::function template syntax which is more readable.
55
+
56
+
If we take the previous example, by calling `cpp_reflection::make_reflection<void(const std::string&, float)>::call("SomeClass", "other_fct", some_str, some_float);`, we will call `SomeClass::fct`.
55
57
56
58
# How does it work
57
59
The `REGISTER_REFLECTABLE` macro uses variadic macro parameters and works with Boost.PP in order to iterate through the list of member functions.
@@ -60,10 +62,11 @@ This macro will create a static object of type `reflectable<SomeClass>`.
60
62
For example `REGISTER_REFLECTABLE(SomeClass, (fct)(other_fct))` will generates the following code:
This generation is done at compile time but the registration is only effective at runtime at the beginning of the execution. Registering a class for reflection will impact on the compilation time and at program startup (when all static variables are initialized).
@@ -74,25 +77,24 @@ The constructor of a reflectable object registers itself into the reflectable_ma
0 commit comments