humorly / reflect Goto Github PK
View Code? Open in Web Editor NEW用以实现编译期的c++任意类型反射。
License: MIT License
用以实现编译期的c++任意类型反射。
License: MIT License
原理:在编译期如果能实现const char * -> long的映射,即可利用模板特化来构建对应类型对象。
#1:转换过程如下
// set special template index type
using __factor_type = std::size_t;
// make special template factor value
constexpr __factor_type __factor_value(const char * val)
{
__factor_type num = 0;
while (*val) num += *val++;
return num;
}
#2:构建特化版本
// reflect template
template <__factor_type N>
class reflect
{
public:
reflect() {};
virtual ~reflect() {}
};
// special template
#define register_type(__class) template <>\
class reflect<__factor_value(#__class)>\
{\
public:\
reflect() {};\
virtual ~reflect() {}\
typedef __class type;\
};
#3:特化pod类型
// register pod type
register_type(bool);
register_type(char);
register_type(unsigned char);
register_type(unsigned short);
register_type(short);
register_type(unsigned int);
register_type(int);
register_type(float);
register_type(unsigned long);
register_type(long);
register_type(double);
register_type(std::string);
// register pod point type
register_type(bool *);
register_type(char *);
register_type(unsigned char *);
register_type(unsigned short *);
register_type(short *);
register_type(unsigned int *);
register_type(int *);
register_type(float *);
register_type(unsigned long *);
register_type(long *);
register_type(double *);
register_type(std::string *);
#4:提供对象反射宏
// make class
#define get_register_type(x) reflect<__factor_value(x)>::type();
#5:测试代码
class test;
register_type(test);
class test
{
public:
test() {}
virtual ~test() {}
void print() { std::cout << "test" << std::endl; }
};
int main()
{
auto n1 = get_register_type("int");
auto d1 = get_register_type("double");
auto t1 = get_register_type("test");
t1.print();
return 0;
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.