-
Notifications
You must be signed in to change notification settings - Fork 23
Struct Class template manual reflection
Julien SOYSOUVANH edited this page Oct 28, 2021
·
1 revision
For the example, we will manually reflect std::vector.
Just like structs and classes manual reflection, it is unfortunately not possible to completely reflect a third party class template since code injection is not possible. Class template instantiations depends on code injection, and there is no way to do it manually (or write each instantiation manually, which makes no sense).
That being said, it is possible to have a relatively satisfactory result using inheritance.
Let's create a new class template that inherits from std::vector. The advantage of using inheritance over composition is to keep access to all public members without any further manipulation.
Then, we just have to write a proxy method for each method we want to reflect:
//Vector.h
#include <vector>
#include "Generated/Vector.rfkh.h"
//Don't put the allocator template param for example simplicity
template <typename T>
class CLASS() Vector : public std::vector<T>
{
public:
METHOD()
void push_back(T const& elem)
{
std::vector<T>::push_back(elem);
}
METHOD()
T& operator[](std::size_t index)
{
return std::vector<T>::operator[](index);
}
Vector_GENERATED
};
File_Vector_GENERATED
//Vector.cpp
#include "Generated/Vector.rfks.h"