Complete C++ reflect library in less than 200 lines code.
中文介绍:
#include <iostream>
#include "reflect.h"
class Base {
public:
Base() {
std::cout << "Base" << std::endl;
}
};
RE_class(Class1, Base)
{
public:
Class1() {
std::cout << "Class1" << std::endl;
}
};
RE_class(Class2, Base)
{
public:
Class2() {
std::cout << "Class2" << std::endl;
}
};
int main(int argc, const char * argv[]) {
Reflect(Base)["Class1"]->create();
return 0;
}
And the result:
Base
Class1
#include <iostream>
#include "reflect-enum.h"
ENUM_CLASS(Test,
TEST1,
TEST2,
TEST3
);
int main()
{
std::cout << enum_reflection<Test>()->toString(Test::TEST1) << std::endl;
std::cout << enum_reflection<Test>()->toString(
enum_reflection<Test>()->fromString("Test::TEST1")
) << std::endl;
std::cout << enum_reflection<Test>()->toString(
enum_reflection<Test>()->fromString("TEST1")
) << std::endl;
}
And the result:
Test::TEST1
Test::TEST1
Test::TEST1
This library is a header-only library, include the header wherever you need.
This library supports multi-object-file projects, but has no effect on the efficiency of that program. Neither does this library need a non-standard preprocessor.
Defined a class to reflect with in TAG. This macro is to replace
class ClassName : public BaseClass
.
Return a reference to a map<string, factory>
, aka. the reflect map.
A tag is a empty struct that defines a group of reflects, and each group of reflects will not affect others.
Defined a enum class to reflect, for example:
ENUM_CLASS( MyEnum // enum class MyEnum {
Member1, // Member1,
Member2 // Member2
); // };
map EnumValue to string
Reflect from string to EnumValue, Accept EnumMember with or without EnumName
Define REFLECT_NS
to your namespace before including reflect-enum.h