reflect.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #ifndef XG_REFLECT_H
  2. #define XG_REFLECT_H
  3. //////////////////////////////////////////////////////////////////////////////
  4. #include <set>
  5. #include <map>
  6. #include <list>
  7. #include <cmath>
  8. #include <ctime>
  9. #include <mutex>
  10. #include <atomic>
  11. #include <vector>
  12. #include <string>
  13. #include <cctype>
  14. #include <cstdio>
  15. #include <cstdarg>
  16. #include <cstdlib>
  17. #include <cassert>
  18. #include <cstring>
  19. #include <memory>
  20. #include <iostream>
  21. #include <typeinfo>
  22. #define reflect_object(type, name) \
  23. type name; \
  24. ReflectHelper __##name = ReflectHelper(this, &this->name, #type, #name)
  25. #define reflect_attr(type, name, defval) \
  26. type name = defval; \
  27. ReflectHelper __##name = ReflectHelper(this, &this->name, #type, #name)
  28. #define rint(name) reflect_attr(int, name, 0)
  29. #define rbool(name) reflect_attr(bool, name, false)
  30. #define rfloat(name) reflect_attr(float, name, 0.0F)
  31. #define rdouble(name) reflect_attr(double, name, 0.0F)
  32. #define reflect_int(name) reflect_attr(int, name, 0)
  33. #define reflect_bool(name) reflect_attr(bool, name, false)
  34. #define reflect_float(name) reflect_attr(float, name, 0.0F)
  35. #define reflect_double(name) reflect_attr(double, name, 0.0F)
  36. #define rstring(name) reflect_object(string, name)
  37. #define robject(type, name) reflect_object(type, name)
  38. #define reflect_string(name) reflect_object(string, name)
  39. #define sp shared_ptr
  40. #define newsp make_shared
  41. #define ARR_LEN(arr) (sizeof(arr)/sizeof(arr[0]))
  42. #define CHECK_FALSE_RETURN(FUNC) if (FUNC){} else return false
  43. #ifdef _MSC_VER
  44. #define strcasecmp stricmp
  45. #pragma warning(disable:4996)
  46. #endif
  47. using namespace std;
  48. typedef unsigned long long ReflectKey;
  49. class Object
  50. {
  51. public:
  52. virtual ~Object();
  53. virtual string toString() const;
  54. virtual const char* getClassName() const;
  55. };
  56. class SpinMutex : public Object
  57. {
  58. private:
  59. atomic_flag flag = ATOMIC_FLAG_INIT;
  60. public:
  61. void lock()
  62. {
  63. while (flag.test_and_set(memory_order_acquire));
  64. }
  65. void unlock()
  66. {
  67. flag.clear(std::memory_order_release);
  68. }
  69. };
  70. typedef lock_guard<SpinMutex> SpinLocker;
  71. class ReflectItem
  72. {
  73. friend class JsonReflect;
  74. protected:
  75. int offset;
  76. const char* name;
  77. const char* type;
  78. public:
  79. ReflectItem() : offset(0), type(NULL)
  80. {
  81. }
  82. ReflectItem(int _offset, const char* _type, const char* _name) : offset(_offset), type(_type), name(_name)
  83. {
  84. }
  85. public:
  86. bool operator < (const ReflectItem& obj) const
  87. {
  88. return offset < obj.offset;
  89. }
  90. bool operator == (const ReflectItem& obj) const
  91. {
  92. return offset == obj.offset;
  93. }
  94. public:
  95. string get(const void* obj) const;
  96. bool set(void* obj, int val) const;
  97. bool set(void* obj, double val) const;
  98. bool set(void* obj, const char* val) const;
  99. bool canUse() const
  100. {
  101. return type ? true : false;
  102. }
  103. int getOffset() const
  104. {
  105. return offset;
  106. }
  107. const char* getName() const
  108. {
  109. return name;
  110. }
  111. const char* getType() const
  112. {
  113. return type;
  114. }
  115. bool set(void* obj, bool val) const
  116. {
  117. return set(obj, val ? 1 : 0);
  118. }
  119. bool set(void* obj, float val) const
  120. {
  121. return set(obj, (double)(val));
  122. }
  123. bool set(void* obj, const string& val) const
  124. {
  125. return set(obj, val.c_str());
  126. }
  127. };
  128. class ReflectHelper
  129. {
  130. public:
  131. static string GetAttrString(ReflectKey key);
  132. static vector<ReflectItem> GetAttrList(ReflectKey key);
  133. ReflectHelper(Object* self, void* data, const char* type, const char* name);
  134. ReflectHelper(Object* self, Object* data, const char* type, const char* name);
  135. static ReflectKey GetKey(const Object* obj)
  136. {
  137. return (ReflectKey)(typeid(*obj).name());
  138. }
  139. template<class REFLECT_TYPE> static ReflectKey GetKey()
  140. {
  141. return (ReflectKey)(typeid(REFLECT_TYPE).name());
  142. }
  143. static string GetAttrString(const Object* obj)
  144. {
  145. return GetAttrString(GetKey(obj));
  146. }
  147. template<class REFLECT_TYPE> static string GetAttrString()
  148. {
  149. string res = GetAttrString(GetKey<REFLECT_TYPE>());
  150. if (res.length() > 0) return res;
  151. REFLECT_TYPE item;
  152. return GetAttrString(&item);
  153. }
  154. static vector<ReflectItem> GetAttrList(const Object* obj)
  155. {
  156. return GetAttrList(GetKey(obj));
  157. }
  158. template<class REFLECT_TYPE> static vector<ReflectItem> GetAttrList()
  159. {
  160. vector<ReflectItem> vec = GetAttrList(GetKey<REFLECT_TYPE>());
  161. if (vec.size() > 0) return std::move(vec);
  162. REFLECT_TYPE item;
  163. return GetAttrList(&item);
  164. }
  165. };
  166. //////////////////////////////////////////////////////////////////////////////
  167. #endif