Giter Site home page Giter Site logo

reflect's People

Contributors

humorly avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

15831944

reflect's Issues

实现c++编译期任意类型类型反射

原理:在编译期如果能实现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;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.