Advertisement
lfed255-dev

Paste From Computer

Sep 8th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.00 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <string>
  4. #include <typeinfo>
  5. #include <vector>
  6. #include <sstream>
  7. #include <regex>
  8. #include <unordered_map>
  9. #include <boost/variant.hpp>
  10.  
  11.  
  12.  
  13.  
  14.  
  15. struct IType;
  16. struct IObject;
  17.  
  18. struct FieldVar {
  19. protected:
  20. int offset;
  21. IType* type;
  22. std::string name;
  23. public:
  24. FieldVar() = default;
  25. FieldVar(int _offset,IType* _type, std::string _name) : offset(_offset), type(_type), name(_name) {}
  26. FieldVar(int offset) : FieldVar(offset,nullptr,"") {}
  27. FieldVar(IType* type) : FieldVar(0, type, "") {}
  28. FieldVar(std::string name) : FieldVar(0, nullptr, name) {}
  29.  
  30. template<typename _type>
  31. _type getValue(void* obj) {
  32. return *(_type*)((char*)obj + offset);
  33. }
  34. template<typename _type>
  35. void setValue(void* obj, _type value) {
  36. *(_type*)((char*)obj + offset) = value;
  37. }
  38.  
  39. template<typename _type>
  40. _type getValue(IObject* obj) {
  41. return getValue(obj->getObjPtr());
  42. }
  43. template<typename _type>
  44. void setValue(IObject* obj,_type value) {
  45. setValue(obj->getObjPtr(), value);
  46. }
  47. int getFieldOffset() { return offset; }
  48. int setFieldOffset(int offset) { this->offset = offset; }
  49.  
  50. IType* getType() { return this->type; }
  51. void setType(IType* type) { this->type = type; }
  52.  
  53. std::string getName() { return name; }
  54. void setName(std::string name) { this->name = name; }
  55. };
  56.  
  57. struct FieldFunc {
  58. protected:
  59. friend struct IType;
  60. public:
  61. template <typename _restype, typename... _argstypes>
  62. _restype Invoke(void* object, _argstypes... args) {
  63. const _IRFieldFunc<_restype, _argstypes...>* irff = (const _IRFieldFunc<_restypes, _argstypes...>*)this;
  64. return irff->Invoke(object, args...);
  65. }
  66. };
  67. template <typename _restype, typename... _argstypes>
  68. struct _IRFieldFunc : public FieldFunc {
  69. protected:
  70. friend struct IType;
  71. public:
  72. virtual _restype Invoke(void* object, _argstypes... args) = 0;
  73. };
  74. template <typename _ctype,typename _restype, typename... _argstypes>
  75. struct _RFieldFunc : public _IRFieldFunc<_restype,_argstypes...> {
  76. protected:
  77. friend struct IType;
  78.  
  79. public:
  80. _restype(_ctype::*funcptr)(_argstypes...);
  81. _restype Invoke(void* object, _argstypes... args) {
  82. _ctype *ct = (_ctype*)object;
  83. return ct->*funcptr(args..);
  84. }
  85. };
  86.  
  87. template <typename _ctype, typename _restype, typename... _argstypes>
  88. struct _RCFieldFunc : public _IRFieldFunc<_restype, _argstypes...> {
  89. protected:
  90. friend struct IType;
  91.  
  92. public:
  93. _restype(_ctype::*funcptr)(_argstypes...) const;
  94. _restype Invoke(void* object, _argstypes... args) {
  95. _ctype *ct = (_ctype*)object;
  96. return ct->*funcptr(args..);
  97. }
  98. };
  99.  
  100.  
  101. struct IObject;
  102.  
  103. struct IType {
  104. using FieldContainer = boost::variant<FieldFunc, FieldVar>;
  105. using FieldPair = std::pair<std::string, FieldContainer>;
  106. using FieldsArray = std::unordered_map<std::string, boost::variant<FieldFunc, FieldVar>>;
  107. virtual IObject* newInstance() = 0;
  108. virtual FieldContainer getField(std::string name) = 0;
  109. virtual std::string getName() = 0;
  110. virtual std::string getId() = 0;
  111. virtual size_t getSize() = 0;
  112. virtual IType::FieldsArray getFields() const = 0;
  113. };
  114.  
  115. struct IObject {
  116. virtual IType* getType() const = 0;
  117. virtual void* getInst() = 0;
  118. virtual IType::FieldContainer operator[](std::string) = 0;
  119. virtual IType::FieldsArray getFields() = 0;
  120. virtual IType::FieldContainer getField(std::string) = 0;
  121. };
  122.  
  123.  
  124.  
  125. namespace details {
  126.  
  127. template<typename T, typename U> constexpr size_t offsetOfCPtr(U T::*member)
  128. {
  129. return (char*)&((T*)nullptr->*member) - (char*)nullptr;
  130. }
  131.  
  132. std::vector<std::string> splitString(const std::string &s, char delim) {
  133. std::stringstream ss(s);
  134. std::string item;
  135. std::vector<std::string> elems;
  136. while (std::getline(ss, item, delim)) {
  137. elems.push_back(item);
  138. // elems.push_back(std::move(item)); // if C++11 (based on comment from @mchiasson)
  139. }
  140. return elems;
  141. }
  142.  
  143. std::string removeNamespaceName(std::string& s) {
  144. std::regex r(".*::");
  145. s = std::regex_replace(s, r, "");
  146. return s;
  147. }
  148. std::vector<std::string> removeNamespaceNames(std::vector<std::string>& s) {
  149. std::regex r(".*::");
  150. for (std::vector<std::string>::iterator it = s.begin(); it != s.end(); it++) {
  151. *it = removeNamespaceName(*it);
  152. }
  153.  
  154. return s;
  155. }
  156.  
  157.  
  158.  
  159. template<typename _ctype,typename _restype, typename ..._argstypes>
  160. IType::FieldPair makeTypeField(std::string name = "", _restype (_ctype::*ptr)(_argstypes...) = nullptr) {
  161. typename IType::FieldPair fp;
  162. auto v = new _RFieldFunc<_ctype, _restype, _argstypes...>();
  163. v.funcptr = ptr;
  164. FieldFunc *ff = v;
  165. fp.second = ff;
  166. fp.first = s;
  167. return fp;
  168. }
  169.  
  170. template<typename _ctype, typename _restype, typename ..._argstypes>
  171. IType::FieldPair makeTypeField(std::string name = "", _restype(_ctype::*ptr)(_argstypes...) const = nullptr) {
  172. typename IType::FieldPair fp;
  173. auto v = new _RCFieldFunc<_ctype, _restype, _argstypes...>;
  174. v.funcptr = ptr;
  175. FieldFunc *ff = v;
  176. fp.second = ff;
  177. fp.first = s;
  178. return fp;
  179. }
  180.  
  181. template<typename _ptrtype,
  182. typename = typename std::enable_if<std::is_member_pointer<_ptrtype>::value>::type>
  183. IType::FieldPair makeTypeField(std::string name = "", IType* t = nullptr, _ptrtype ptr = nullptr) {
  184. typename IType::FieldPair fp;
  185. FieldVar fv(details::offsetOfCPtr(ptr),t,name);
  186. fp.second = fv;
  187.  
  188. return fp;
  189. }
  190.  
  191. template<typename _ptrtype, typename = typename std::enable_if<std::is_pointer<_ptrtype>::value>::type>
  192. void wrapTypeField(IType* t,std::vector<std::string> names,int& nidx, IType::FieldsArray& fa, std::unordered_map<std::string, boost::variant<FieldFunc, FieldVar>>::iterator& curidx, _ptrtype ptr) {
  193. fa.insert(makeTypeField(names[nidx],curidx->first, t, ptr));
  194. curidx = fa.end();
  195. if (nidx + 1 != names.size() - 1) {
  196. nidx++;
  197. }
  198. }
  199.  
  200. template<typename... _ptrtypes>
  201. IType::FieldsArray finalmakeTypeFields(std::string lss, _ptrtypes... ptrs) {
  202. auto vec = details::removeNamespaceNames(details::splitString(lss));
  203. IType::FieldsArray fa;
  204. int idx;
  205. IType::FieldsArray::iterator fait;
  206. wrapTypeField(t, vec, idx, fa, fait, ptrs)...;
  207. return fa;
  208. }
  209. }
  210.  
  211.  
  212. #define _IOBJECT(name) \
  213. public: \
  214. static name##_Type type;\
  215. IType* getType() const override { static name##_Type _type = type; return &_type; }\
  216. void* getInst() override { return (void*)this; }\
  217. IType::FieldContainer operator[](std::string name) { return getField(name); } \
  218. IType::FieldsArray getFields() override { return this->type.getFields(); } \
  219. IType::FieldContainer getField(std::string name) override { return type.fields[name]; }
  220. #define IOBJECT(name) _IOBJECT(name)
  221. #define _DEFINE_TYPE_ADVANCED(name) \
  222. struct name; \
  223. struct name##_Type : public IType { \
  224. private: \
  225. static IType::FieldsArray fields; \
  226. public: \
  227. IObject* newInstance() override { return new name(); }\
  228. std::string getName() override { return #name; } \
  229. std::string getId() override { return typeid(name).name(); }\
  230. size_t getSize() { return sizeof(name); }\
  231. IType::FieldsArray getFields() const override { return this->fields; } \
  232. IType::FieldContainer getField(std::string name) { return type.fields[name]; }\
  233. }
  234.  
  235. #define _DEFINE_TYPE_AUTO(name,...) \
  236. struct name; \
  237. struct name##_Type : public IType { \
  238. private: \
  239. static IType::FieldsArray fields = details::finalmakeTypeFields(#__VA_ARGS__,__VA_ARGS__); \
  240. public: \
  241. IObject* newInstance() override { return new name(); }\
  242. std::string getName() override { return #name; } \
  243. std::string getId() override { return typeid(name).name(); }\
  244. size_t getSize() { return sizeof(name); }\
  245. IType::FieldsArray getFields() const override { return this->fields; } \
  246. IType::FieldContainer getField(std::string name) { return type.fields[name]; }\
  247. }
  248.  
  249. #define DTYPE(name,...) _DEFINE_TYPE_AUTO(name,__VA_ARGS__)
  250. #define DTYPEA(name) _DEFINE_TYPE_ADVANCED(name)
  251.  
  252.  
  253. DTYPEA(MyTest)
  254.  
  255. struct MyTest : public IObject {
  256. IOBJECT(MyTest)
  257. };
  258.  
  259. //
  260. //struct IObject;
  261. //
  262. //struct IType {
  263. // typedef std::unordered_map<std::string, FieldContainer> FieldsType;
  264. // virtual IObject* newInstance() const = 0;
  265. // virtual std::string getName() const = 0;
  266. // virtual std::string getId() const = 0;
  267. // virtual size_t getSize() const = 0;
  268. // virtual IType::FieldsType getFields() const = 0;
  269. //};
  270. //
  271. //struct FieldContainer {
  272. // enum {
  273. // FUNC,
  274. // VAR,
  275. // }type;
  276. // union {
  277. // //FieldFunc* ff;
  278. // FieldVar* fv;
  279. // }val;
  280. //};
  281. //
  282. //struct IObject {
  283. // virtual IType* getType() const = 0;
  284. // virtual void* getObjPtr() const = 0;
  285. // virtual FieldContainer& operator[](std::string) = 0;
  286. //};
  287. //
  288. //
  289. //#define _IOBJECT_AUTO(NAME,...) \
  290. //public: \
  291. // static NAME##_Type type; \
  292. // IType* getType() const override { return type; } \
  293. // void* getObjPtr() const override { return (void*)this; } \
  294. // FieldContainer& operator[](std::string name) { \
  295. // return type.getFields()[name]; \
  296. // }
  297. //
  298. //
  299. //#define _IOBJECT_ADVANCED(NAME) \
  300. //public: \
  301. //static NAME##_Type type; \
  302. //IType* getType() const override { return &type; } \
  303. //void* getObjPtr() const override { return (void*)this; } \
  304. //FieldContainer& operator[](std::string name) { \
  305. // return type.getFields()[name]; \
  306. //}
  307. //
  308. //#define TYPE(NAME) \
  309. // struct NAME; \
  310. // struct NAME##_Type : public IType { \
  311. // private: \
  312. // static IType::FieldsType fields; \
  313. // public: \
  314. // std::string getName() const override { return #NAME; } \
  315. // std::string getId() const override { return typeid(NAME).name(); } \
  316. // size_t getSize() const override { return sizeof(NAME); } \
  317. // IObject* newInstance() const override { IObject *ptr = new NAME(); return ptr; } \
  318. // IType::FieldsType getFields() const override { return fields; } \
  319. // };
  320. //
  321. //#define IOBJECT(NAME,...) _IOBJECT_AUTO(NAME,__VA_ARGS__);
  322. //#define IOBJECTA(NAME) _IOBJECT_ADVANCED(NAME);
  323. //
  324. //TYPE(Test)
  325. //
  326. //struct Test : public IObject {
  327. // IOBJECTA(Test)
  328. //public:
  329. //
  330. // Test() {
  331. // }
  332. //};
  333. //
  334. //int test() {
  335. // IObject* t = Test::type.newInstance();
  336. // t->operator[]("h");
  337. // t[""]
  338. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement