Skip to content

Commit 8ce3a0b

Browse files
author
Simon Ninon
committed
update README.md
1 parent 92c756d commit 8ce3a0b

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ In order to enable reflection, we must explicitly "register" our class.
2424

2525
For this, `cpp_reflection` provides a simple macro: `REGISTER_REFLECTABLE(type, list of functions)`.
2626
* `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).
2828

2929
The list of functions must be formatted in a specific way: `(fct1)(fct2)(fct3)...`.
3030
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:
3434
```cpp
3535
class SomeClass {
3636
public:
37-
void fct(void) {
37+
int fct(void) {
3838
std::cout << "fct()" << std::endl;
3939
}
4040

41-
void other_fct(void) {
41+
void other_fct(const std::string&, float) {
4242
std::cout << "other_fct()" << std::endl;
4343
}
4444
};
@@ -49,9 +49,11 @@ We just need to do `REGISTER_REFLECTABLE(SomeClass, (fct)(other_fct))` and this
4949
## Making Reflection
5050
Each time we register a type and its member functions, it stores this type into the `reflection_manager` singleton class.
5151

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).
5353

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`.
5557

5658
# How does it work
5759
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>`.
6062
For example `REGISTER_REFLECTABLE(SomeClass, (fct)(other_fct))` will generates the following code:
6163

6264
```cpp
63-
static reflectable<SomeClass> reflectable_int("SomeClass", {
65+
static reflectable<SomeClass> reflectable_int(
66+
"SomeClass",
6467
{ "fct", &SomeClass::fct },
6568
{ "other_fct", &SomeClass::other_fct }
66-
});
69+
);
6770
```
6871

6972
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
7477
```cpp
7578
#include <iostream>
7679

77-
#include "cpp_reflection/reflectable.hpp"
78-
#include "cpp_reflection/reflection_manager.hpp"
80+
#include "cpp_reflection/cpp_reflection.hpp"
7981

8082
class SomeClass {
8183
public:
82-
void fct(void) {
84+
int fct(void) {
8385
std::cout << "fct()" << std::endl;
8486
}
8587

86-
void other_fct(void) {
88+
void other_fct(const std::string&, float) {
8789
std::cout << "other_fct()" << std::endl;
8890
}
8991
};
9092

9193
REGISTER_REFLECTABLE(SomeClass, (fct)(other_fct))
9294

9395
int main(void) {
94-
cpp_reflection::reflection_manager::make_reflection("SomeClass", "fct");
95-
cpp_reflection::reflection_manager::make_reflection("SomeClass", "other_fct");
96+
cpp_reflection::make_reflection<int()>::call("SomeClass", "fct");
97+
cpp_reflection::make_reflection<void(const std::string&, float)>::call("SomeClass", "other_fct", std::string("hello"), 4.2);
9698

9799
return 0;
98100
}

0 commit comments

Comments
 (0)