TypeRegistry.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. #pragma once
  2. #include <Array.h>
  3. #include <cstdlib>
  4. #include <JSON.h>
  5. #include <Trie.h>
  6. #include <typeinfo>
  7. class TypeRegistry;
  8. #define JSONObjectValidationBuilder \
  9. Framework::JSON::Validator::ObjectValidationBuilder< \
  10. Framework::JSON::Validator::JSONValidator>
  11. /*
  12. * Used to convert an object of type T to a JSONValue and vice versa.
  13. * Can be registered at the TypeRegistry to be used by the JSON system
  14. */
  15. template<typename T> class TypeFactory : public Framework::ReferenceCounter
  16. {
  17. public:
  18. TypeFactory()
  19. : ReferenceCounter(){};
  20. virtual T* createValue(Framework::JSON::JSONObject* zJson) const = 0;
  21. virtual void fromJson(T* zResult, Framework::JSON::JSONObject* zJson) const
  22. = 0;
  23. virtual void toJson(T* zObject, Framework::JSON::JSONObject* zResult) const
  24. = 0;
  25. virtual JSONObjectValidationBuilder* addToValidator(
  26. JSONObjectValidationBuilder* builder) const
  27. = 0;
  28. virtual Framework::JSON::Validator::JSONValidator* getValidator() const
  29. {
  30. return addToValidator(
  31. Framework::JSON::Validator::JSONValidator::buildForObject())
  32. ->finishObject();
  33. };
  34. };
  35. /*
  36. * Used to convert an object of type S witch inherits from type T to a
  37. * JSONValue and vice versa. Can be registered at the TypeRegistry to be
  38. * used by the JSON
  39. */
  40. template<typename T,
  41. typename S,
  42. typename = std::enable_if<std::is_base_of<T, S>::value>>
  43. class SubTypeFactory : public Framework::ReferenceCounter
  44. {
  45. public:
  46. SubTypeFactory()
  47. : ReferenceCounter(){};
  48. virtual S* createValue(Framework::JSON::JSONObject* zJson) const = 0;
  49. virtual void fromJson(S* zResult, Framework::JSON::JSONObject* zJson) const
  50. = 0;
  51. virtual void toJson(S* zObject, Framework::JSON::JSONObject* zResult) const
  52. = 0;
  53. virtual JSONObjectValidationBuilder* addToValidator(
  54. JSONObjectValidationBuilder* builder) const
  55. = 0;
  56. virtual Framework::JSON::Validator::JSONValidator* getValidator() const
  57. {
  58. return addToValidator(
  59. Framework::JSON::Validator::JSONValidator::buildForObject())
  60. ->finishObject();
  61. };
  62. virtual Framework::Text getTypeToken() const = 0;
  63. };
  64. template<typename T> class SubTypeFactoryRef
  65. : public Framework::ReferenceCounter
  66. {
  67. private:
  68. Framework::Text typetoken;
  69. std::function<T*(Framework::JSON::JSONObject*)> createJValueFunc;
  70. std::function<void(T*, Framework::JSON::JSONObject*)> fromJsonFunc;
  71. std::function<void(T*, Framework::JSON::JSONObject*)> toJsonFunc;
  72. std::function<JSONObjectValidationBuilder*(JSONObjectValidationBuilder*)>
  73. getValidatorFunc;
  74. Framework::ReferenceCounter* factory;
  75. public:
  76. SubTypeFactoryRef(Framework::Text typetoken,
  77. std::function<T*(Framework::JSON::JSONObject*)> createJValueFunc,
  78. std::function<void(T*, Framework::JSON::JSONObject*)> fromJsonFunc,
  79. std::function<void(T*, Framework::JSON::JSONObject*)> toJsonFunc,
  80. std::function<JSONObjectValidationBuilder*(
  81. JSONObjectValidationBuilder*)> getValidatorFunc,
  82. Framework::ReferenceCounter* factory)
  83. : ReferenceCounter(),
  84. createJValueFunc(createJValueFunc),
  85. typetoken(typetoken),
  86. fromJsonFunc(fromJsonFunc),
  87. toJsonFunc(toJsonFunc),
  88. getValidatorFunc(getValidatorFunc),
  89. factory(factory)
  90. {}
  91. ~SubTypeFactoryRef()
  92. {
  93. factory->release();
  94. }
  95. T* createValue(Framework::JSON::JSONObject* zJson) const
  96. {
  97. return createJValueFunc(zJson);
  98. }
  99. void fromJson(T* zResult, Framework::JSON::JSONObject* zJson) const
  100. {
  101. fromJsonFunc(zResult, zJson);
  102. }
  103. void toJSON(T* zObject, Framework::JSON::JSONObject* zResult) const
  104. {
  105. toJsonFunc(zObject, zResult);
  106. }
  107. JSONObjectValidationBuilder* addToValidator(
  108. JSONObjectValidationBuilder* builder) const
  109. {
  110. return getValidatorFunc(builder);
  111. }
  112. const Framework::Text& getTypetoken() const
  113. {
  114. return typetoken;
  115. }
  116. };
  117. template<typename T> class PolymorphTypeFactory : public TypeFactory<T>
  118. {
  119. private:
  120. Framework::RCArray<SubTypeFactoryRef<T>> factories;
  121. Framework::RCArray<Framework::Text> typeNames;
  122. static thread_local bool insideGetValidator;
  123. public:
  124. PolymorphTypeFactory()
  125. : TypeFactory<T>()
  126. {}
  127. T* createValue(Framework::JSON::JSONObject* zJson) const override
  128. {
  129. for (SubTypeFactoryRef<T>* factory : factories)
  130. {
  131. if (zJson->asObject()
  132. ->zValue("type")
  133. ->asString()
  134. ->getString()
  135. .istGleich(factory->getTypetoken()))
  136. {
  137. return factory->createValue(zJson->asObject());
  138. }
  139. }
  140. return 0;
  141. }
  142. void fromJson(T* zResult, Framework::JSON::JSONObject* zJson) const override
  143. {
  144. for (SubTypeFactoryRef<T>* factory : factories)
  145. {
  146. if (zJson->asObject()
  147. ->zValue("type")
  148. ->asString()
  149. ->getString()
  150. .istGleich(factory->getTypetoken()))
  151. {
  152. factory->fromJson(zResult, zJson->asObject());
  153. }
  154. }
  155. }
  156. void toJson(T* zObject, Framework::JSON::JSONObject* zResult) const override
  157. {
  158. auto name = typeNames.begin();
  159. for (SubTypeFactoryRef<T>* factory : factories)
  160. {
  161. if (name->istGleich(typeid(*zObject).name()))
  162. {
  163. factory->toJSON(zObject, zResult);
  164. zResult->addValue("type",
  165. new Framework::JSON::JSONString(factory->getTypetoken()));
  166. return;
  167. }
  168. name++;
  169. }
  170. }
  171. JSONObjectValidationBuilder* addToValidator(
  172. JSONObjectValidationBuilder* builder) const override
  173. {
  174. throw "Can not add polymorph json validator to an object "
  175. "validation builder";
  176. }
  177. Framework::JSON::Validator::JSONValidator* getValidator() const override
  178. {
  179. Framework::JSON::Validator::JSONValidator* result;
  180. if (!insideGetValidator)
  181. {
  182. insideGetValidator = true;
  183. auto validator
  184. = Framework::JSON::Validator::JSONValidator::buildForOneOf()
  185. ->typeSpecifiedByAttribute("type");
  186. for (SubTypeFactoryRef<T>* factory : factories)
  187. {
  188. validator = validator->addAcceptedType(
  189. factory
  190. ->addToValidator(
  191. Framework::JSON::Validator::JSONValidator::
  192. buildForObject()
  193. ->withRequiredString("type")
  194. ->withExactMatch(factory->getTypetoken())
  195. ->finishString()
  196. ->setObjectReferenceId(
  197. ((Framework::Text("_type_")
  198. += typeid(T).name())
  199. += "_")
  200. += factory->getTypetoken()))
  201. ->finishObject());
  202. }
  203. result = validator->finishOneOf();
  204. insideGetValidator = false;
  205. }
  206. else
  207. {
  208. auto validator
  209. = Framework::JSON::Validator::JSONValidator::buildForOneOf()
  210. ->typeSpecifiedByAttribute("type");
  211. for (SubTypeFactoryRef<T>* factory : factories)
  212. {
  213. validator = validator->addAcceptedType(Framework::JSON::
  214. Validator::JSONValidator::buildForObjectReference(
  215. ((Framework::Text("_type_") += typeid(T).name())
  216. += "_")
  217. += factory->getTypetoken()));
  218. }
  219. result = validator->finishOneOf();
  220. }
  221. return result;
  222. }
  223. template<typename S,
  224. typename = std::enable_if<std::is_base_of<T, S>::value>>
  225. void addFactory(SubTypeFactory<T, S>* factory)
  226. {
  227. factories.add(new SubTypeFactoryRef<T>(
  228. factory->getTypeToken(),
  229. [factory](Framework::JSON::JSONObject* zJson) {
  230. S* value = factory->createValue(zJson);
  231. if (value)
  232. {
  233. return dynamic_cast<T*>(value);
  234. }
  235. return (T*)0;
  236. },
  237. [factory](T* zResult, Framework::JSON::JSONObject* zJson) {
  238. factory->fromJson(dynamic_cast<S*>(zResult), zJson);
  239. },
  240. [factory](T* zObject, Framework::JSON::JSONObject* zResult) {
  241. factory->toJson(dynamic_cast<S*>(zObject), zResult);
  242. },
  243. [factory](JSONObjectValidationBuilder* builder) {
  244. return factory->addToValidator(builder);
  245. },
  246. dynamic_cast<Framework::ReferenceCounter*>(factory)));
  247. typeNames.add(new Framework::Text(typeid(S).name()));
  248. }
  249. };
  250. template<typename T>
  251. thread_local bool PolymorphTypeFactory<T>::insideGetValidator = false;
  252. class TypeFatoryRef : public Framework::ReferenceCounter
  253. {
  254. private:
  255. std::function<void*(Framework::JSON::JSONObject*)> createValueFunc;
  256. std::function<void(void*, Framework::JSON::JSONObject*)> fromJsonFunc;
  257. std::function<void(void*, Framework::JSON::JSONObject*)> toJsonFunc;
  258. std::function<Framework::JSON::Validator::JSONValidator*()>
  259. getValidatorFunc;
  260. std::function<JSONObjectValidationBuilder*(JSONObjectValidationBuilder*)>
  261. addToValidatorFunc;
  262. Framework::ReferenceCounter* factory;
  263. public:
  264. TypeFatoryRef(
  265. std::function<void*(Framework::JSON::JSONObject*)> createValueFunc,
  266. std::function<void(void*, Framework::JSON::JSONObject*)> fromJsonFunc,
  267. std::function<void(void*, Framework::JSON::JSONObject*)> toJsonFunc,
  268. std::function<Framework::JSON::Validator::JSONValidator*()>
  269. getValidatorFunc,
  270. std::function<JSONObjectValidationBuilder*(
  271. JSONObjectValidationBuilder*)> addToValidatorFunc,
  272. Framework::ReferenceCounter* factory)
  273. : ReferenceCounter(),
  274. createValueFunc(createValueFunc),
  275. fromJsonFunc(fromJsonFunc),
  276. toJsonFunc(toJsonFunc),
  277. getValidatorFunc(getValidatorFunc),
  278. addToValidatorFunc(addToValidatorFunc),
  279. factory(factory)
  280. {}
  281. ~TypeFatoryRef()
  282. {
  283. factory->release();
  284. }
  285. void* createValue(Framework::JSON::JSONObject* zJson) const
  286. {
  287. return createValueFunc(zJson);
  288. }
  289. void fromJson(void* zResult, Framework::JSON::JSONObject* zJson) const
  290. {
  291. return fromJsonFunc(zResult, zJson);
  292. }
  293. void toJSON(void* zObject, Framework::JSON::JSONObject* zResult) const
  294. {
  295. return toJsonFunc(zObject, zResult);
  296. }
  297. Framework::JSON::Validator::JSONValidator* getValidator() const
  298. {
  299. return getValidatorFunc();
  300. }
  301. JSONObjectValidationBuilder* addToValidator(
  302. JSONObjectValidationBuilder* builder) const
  303. {
  304. return addToValidatorFunc(builder);
  305. }
  306. template<typename T> TypeFactory<T>* zFactory() const
  307. {
  308. return (TypeFactory<T>*)(factory);
  309. }
  310. };
  311. class TypeRegistry : public Framework::ReferenceCounter
  312. {
  313. private:
  314. Framework::RCTrie<TypeFatoryRef> parsableTypes;
  315. Framework::RCArray<Framework::Text> parsableTypeNames;
  316. public:
  317. TypeRegistry();
  318. template<typename T,
  319. typename S,
  320. typename = std::enable_if<std::is_base_of<T, S>::value>>
  321. void registerSubType(SubTypeFactory<T, S>* factory)
  322. {
  323. Framework::Text typeId = typeid(T).name();
  324. TypeFatoryRef* typeFactoryRef
  325. = parsableTypes.z(typeId, typeId.getLength());
  326. if (!typeFactoryRef)
  327. {
  328. PolymorphTypeFactory<T>* polymorphFactory
  329. = new PolymorphTypeFactory<T>();
  330. registerType(polymorphFactory);
  331. typeFactoryRef = parsableTypes.z(typeId, typeId.getLength());
  332. parsableTypeNames.add(new Framework::Text(typeId));
  333. }
  334. PolymorphTypeFactory<T>* polymorphFactory
  335. = dynamic_cast<PolymorphTypeFactory<T>*>(
  336. typeFactoryRef->zFactory<T>());
  337. if (!polymorphFactory)
  338. {
  339. throw Framework::Text("Type not registered as Polymorphic type: ")
  340. + typeId;
  341. }
  342. polymorphFactory->template addFactory<S>(factory);
  343. }
  344. template<typename T> void registerType(TypeFactory<T>* factory)
  345. {
  346. Framework::Text typeId = typeid(T).name();
  347. TypeFatoryRef* typeFactoryRef
  348. = parsableTypes.z(typeId, typeId.getLength());
  349. if (typeFactoryRef)
  350. {
  351. throw Framework::Text("Type already registered: ") + typeId;
  352. }
  353. typeFactoryRef = new TypeFatoryRef(
  354. [factory](Framework::JSON::JSONObject* zJson) {
  355. return (void*)factory->createValue(zJson);
  356. },
  357. [factory](void* zResult, Framework::JSON::JSONObject* zJson) {
  358. factory->fromJson((T*)zResult, zJson);
  359. },
  360. [factory](void* zObject, Framework::JSON::JSONObject* zResult) {
  361. factory->toJson((T*)zObject, zResult);
  362. },
  363. [factory]() { return factory->getValidator(); },
  364. [factory](JSONObjectValidationBuilder* builder) {
  365. return factory->addToValidator(builder);
  366. },
  367. factory);
  368. parsableTypes.set(typeId, typeId.getLength(), typeFactoryRef);
  369. parsableTypeNames.add(new Framework::Text(typeId));
  370. }
  371. template<typename T> T* fromJson(Framework::JSON::JSONValue* zJson) const
  372. {
  373. Framework::Text typeId = typeid(T).name();
  374. TypeFatoryRef* typeFactoryRef
  375. = parsableTypes.z(typeId, typeId.getLength());
  376. if (!typeFactoryRef)
  377. {
  378. throw Framework::Text("Type not registered: ") + typeId;
  379. }
  380. T* result = (T*)(typeFactoryRef->createValue(zJson->asObject()));
  381. (typeFactoryRef->fromJson(result, zJson->asObject()));
  382. return result;
  383. }
  384. template<typename T> Framework::JSON::JSONValue* toJson(T* zObject) const
  385. {
  386. Framework::Text typeId = typeid(T).name();
  387. TypeFatoryRef* typeFactoryRef
  388. = parsableTypes.z(typeId, typeId.getLength());
  389. if (!typeFactoryRef)
  390. {
  391. throw Framework::Text("Type not registered: ") + typeId;
  392. }
  393. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  394. typeFactoryRef->toJSON(zObject, result);
  395. return result;
  396. }
  397. template<typename T>
  398. Framework::JSON::Validator::JSONValidator* getValidator() const
  399. {
  400. Framework::Text typeId = typeid(T).name();
  401. TypeFatoryRef* typeFactoryRef
  402. = parsableTypes.z(typeId, typeId.getLength());
  403. if (!typeFactoryRef)
  404. {
  405. throw Framework::Text("Type not registered: ") + typeId;
  406. }
  407. return typeFactoryRef->getValidator();
  408. }
  409. template<typename T> JSONObjectValidationBuilder* addToValidator(
  410. JSONObjectValidationBuilder* builder) const
  411. {
  412. Framework::Text typeId = typeid(T).name();
  413. TypeFatoryRef* typeFactoryRef
  414. = parsableTypes.z(typeId, typeId.getLength());
  415. if (!typeFactoryRef)
  416. {
  417. throw Framework::Text("Type not registered: ") + typeId;
  418. }
  419. return typeFactoryRef->addToValidator(builder);
  420. }
  421. template<typename T> Framework::JSON::JSONValue* getValidParts(
  422. Framework::JSON::JSONValue* zJson) const
  423. {
  424. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  425. invalidParts;
  426. Framework::JSON::Validator::JSONValidator* validator
  427. = getValidator<T>();
  428. Framework::JSON::JSONValue* result
  429. = validator->getValidParts(zJson, &invalidParts);
  430. for (Framework::JSON::Validator::JSONValidationResult* invalidPart :
  431. invalidParts)
  432. {
  433. invalidPart->printInvalidInfo();
  434. }
  435. return result;
  436. }
  437. void writeSyntaxInfo(Framework::Text folderPath) const;
  438. };