An enhanced version of FlexibleReflection.
The function provided by FlexibleReflection is serializing data but lacking of deserializing. Therefore, based on FlexibleReflection, this developping project is an enhanced version including deserializing function and so on.
This project is developped using VisualStudio2019. Clone the source code and create an C++ empty project, then copy these .h and .cpp files into your project.
$ git clone https://github.com/pisuto/EnhancedReflection.git
Include the header file ref_define.h
and enable reflection feature, for example Node
.
struct Node {
std::string key;
int value;
std::vector<Node> children;
REFLECT(Node) // enable reflection
};
// define related type descriptor
REFLECT_STRUCT_BEGIN(Node)
REFLECT_STRUCT_MEMBER(key)
REFLECT_STRUCT_MEMBER(value)
REFLECT_STRUCT_MEMBER(children)
REFLECT_STRUCT_END
After finishing defining the struct, then construct ref::format_helper
and choose the specific parser tool for the file to be resolved. This project provides three simple parser ref::xml_parser
ref::txt_parser
ref::console_parser
whose names imply their corresponding formats. In general, the three tools lack the functions that a powerful parser should have. So as to achieve special functions you want, you have to construct your own parser based on class file_parser. Finally, using member functions write
and read
provided by the helper achieves serializing and deserializing effects.
/* choose one of the parsers by the format of file */
ref::format_helper helper(new ref::xml_parser("setting.xml"));
/* serialize */
{
Base base = { false, {{"water", 4, {}}}};
Node node = { "apple", 3, {{"banana", 7, {}}, {"cherry", 11, {}}} };
helper.write(base, node);
}
/* deserialize */
{
Base base;
Node node;
helper.read(base, node);
}
Noted: the order of these types when read must be consistent with these when wrote,otherwise the result will be abnormal.
- FlexibleReflection - a small, flexible runtime reflection system using C++11 language features.
- TestCppReflect - test application for using C++ reflection
MIT © pisuto