Skip to content

Commit 92c756d

Browse files
author
Simon Ninon
committed
comment code
1 parent 0fd33cc commit 92c756d

File tree

6 files changed

+43
-4
lines changed

6 files changed

+43
-4
lines changed

includes/cpp_reflection/callable.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ namespace cpp_reflection {
99
template <typename T>
1010
class callable;
1111

12+
//! callable class
13+
//! contains an std::function corresponding to the class template
14+
//!
15+
//! simple wrapper of std::function, but inheriting from callable_base
16+
//! useful for storing std::function of different types in the same container
17+
//!
18+
//! uses partial template specialization for std::function like syntax (callable<void(int)>)
1219
template <typename ReturnType, typename... Params>
1320
class callable<ReturnType(Params...)> : public callable_base {
1421
public:
@@ -19,6 +26,7 @@ class callable<ReturnType(Params...)> : public callable_base {
1926
callable& operator=(const callable&) = default;
2027

2128
public:
29+
//! functor for calling internal std::function
2230
ReturnType operator()(Params... params) {
2331
return m_fct(params...);
2432
}

includes/cpp_reflection/callable_base.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace cpp_reflection {
44

5+
//! callable_base class
6+
//! empty base class inherited by callable class
57
class callable_base {
68
public:
79
virtual ~callable_base(void) = default;

includes/cpp_reflection/cpp_reflection.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ namespace cpp_reflection {
1010
template <typename T>
1111
struct make_reflection;
1212

13+
//! partial template specialization is not available for functions
14+
//! so we can't have an std::function like syntax for reflection_manager::make_reflection<>
15+
//! we must make reflection_manager::make_reflection<void, int, int> and can't do reflection_manager::make_reflection<void(int, int)>
16+
//! this is not really convenient...
17+
//!
18+
//! we get around of this limitation by wrapping this function call inside a simple struct
19+
//! this struct is partially specialized
20+
//! this way, we can do make_reflection<void(int, int)>::call(...)
1321
template <typename ReturnType, typename... Params>
1422
struct make_reflection<ReturnType(Params...)> {
1523
static ReturnType call(const std::string& class_name, const std::string& function_name, Params... params) {

includes/cpp_reflection/reflectable.hpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
namespace cpp_reflection {
1818

19+
//! reflectable class
20+
//! contains information about registered class (class name, member functions name and member functions pointers)
21+
//! inherits from reflectable_base in order to store reflectables of different types in the same container
22+
//!
23+
//! This class registered itself in the reflection_manager and is used by the manager during reflection
1924
template <typename Type>
2025
class reflectable : public reflectable_base {
2126
public:
@@ -34,14 +39,17 @@ class reflectable : public reflectable_base {
3439
reflectable& operator=(const reflectable& reflectable) = default;
3540

3641
public:
37-
// TO DO: COMMENT
42+
//! unpack Fcts... variadic template
3843
template <typename Fct, typename... Fcts>
3944
void register_member_function(const std::pair<std::string, Fct> fct, Fcts... fcts) {
4045
register_member_function(fct);
4146
register_member_function(fcts...);
4247
}
4348

44-
// TO DO: COMMENT
49+
//! register_member_function
50+
//! in order to have an equivalent of std::bind with default std::placeholders, we use closures
51+
//! this closure has the same signature than the registered function and simply forwards its parameters to the function
52+
//! the closure is stored inside a callable<> object
4553
template <typename ReturnType, typename... Params>
4654
void register_member_function(const std::pair<std::string, ReturnType (Type::*)(Params...)> fct) {
4755
m_member_functions[fct.first] = std::make_shared<callable<ReturnType(Params...)>>([=] (Params... params) -> ReturnType {
@@ -73,11 +81,12 @@ class reflectable : public reflectable_base {
7381
std::string m_name;
7482

7583
//! list of functions for this object
84+
//! associate function name to a callable<> object
7685
std::map<std::string, std::shared_ptr<callable_base>> m_member_functions;
7786
};
7887

7988
//! this define creates a static reflectable
80-
//! REGISTER_REFLECTABLE(type, (fct)) will generates a static reflectable<type> reflectable_int("type", { { "fct", &type::fct } }) var
89+
//! REGISTER_REFLECTABLE(type, (fct)(other_fct)) will generates a static reflectable<type> reflectable_int("type", { "fct", &type::fct }, { "other_fct", &type::other_fct }) var
8190
//! since it is static, the type is registered at program startup
8291
#define TO_STRING(val) #val
8392
#define MAKE_REGISTERABLE_FUNCTION(r, type, i, function) BOOST_PP_COMMA_IF(i) std::make_pair( std::string(TO_STRING(function)), &type::function )

includes/cpp_reflection/reflectable_base.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace cpp_reflection {
88

9+
//! reflectable<> base class
10+
//! used for storing reflectable<> of different types in the same container
911
class reflectable_base {
1012
public:
1113
//! ctor & dotr

includes/cpp_reflection/reflection_manager.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,31 @@ class reflection_manager {
4141
//! implementation
4242
void _register_reflectable(const reflectable_base* reflectable);
4343

44-
//! TODO: COMMENT
44+
//! _make_reflection
45+
//! this is where the reflection is done
4546
template <typename ReturnType, typename... Params>
4647
ReturnType _make_reflection(const std::string& class_name, const std::string& function_name, Params... params) {
48+
//! we first search for the reflectable object matching the class name
4749
auto it = std::find_if(m_types.begin(), m_types.end(), [class_name](const auto& type) {
4850
return type->get_name() == class_name;
4951
});
5052

53+
//! if nothing is found, class is not registered
5154
if (it == m_types.end())
5255
throw reflection_exception("Class " + class_name + " is not registered as reflectable.");
5356

57+
//! we ask the reflectable object for a callable<> object matching the function name
58+
//! get_function will throw a reflection_exception if function is not registered
59+
//!
60+
//! it also returns a generic std::shared_ptr<callable_base>
61+
//! we dynamically cast this pointer to a callable<> object templated on the expected function signature
5462
auto fct = std::dynamic_pointer_cast<callable<ReturnType(Params...)>>((*it)->get_function(function_name));
5563

64+
//! if dynamic_cast failed, it means that the registered function has a different signature than the expected signature
5665
if (not fct)
5766
throw reflection_exception("invalid function signature for " + class_name + "::" + function_name);
5867

68+
//! if everything is ok, we call the function
5969
return (*fct)(params...);
6070
}
6171

0 commit comments

Comments
 (0)