123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- #pragma once
- #include <Array.h>
- #include <cstdlib>
- #include <JSON.h>
- #include <Trie.h>
- #include <typeinfo>
- class TypeRegistry;
- #define JSONObjectValidationBuilder \
- Framework::JSON::Validator::ObjectValidationBuilder< \
- Framework::JSON::Validator::JSONValidator>
- /*
- * Used to convert an object of type T to a JSONValue and vice versa.
- * Can be registered at the TypeRegistry to be used by the JSON system
- */
- template<typename T> class TypeFactory : public Framework::ReferenceCounter
- {
- public:
- TypeFactory()
- : ReferenceCounter(){};
- virtual T* createValue(Framework::JSON::JSONObject* zJson) const = 0;
- virtual void fromJson(T* zResult, Framework::JSON::JSONObject* zJson) const
- = 0;
- virtual void toJson(T* zObject, Framework::JSON::JSONObject* zResult) const
- = 0;
- virtual JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const
- = 0;
- virtual Framework::JSON::Validator::JSONValidator* getValidator() const
- {
- return addToValidator(
- Framework::JSON::Validator::JSONValidator::buildForObject())
- ->finishObject();
- };
- };
- /*
- * Used to convert an object of type S witch inherits from type T to a
- * JSONValue and vice versa. Can be registered at the TypeRegistry to be
- * used by the JSON
- */
- template<typename T,
- typename S,
- typename = std::enable_if<std::is_base_of<T, S>::value>>
- class SubTypeFactory : public Framework::ReferenceCounter
- {
- public:
- SubTypeFactory()
- : ReferenceCounter(){};
- virtual S* createValue(Framework::JSON::JSONObject* zJson) const = 0;
- virtual void fromJson(S* zResult, Framework::JSON::JSONObject* zJson) const
- = 0;
- virtual void toJson(S* zObject, Framework::JSON::JSONObject* zResult) const
- = 0;
- virtual JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const
- = 0;
- virtual Framework::JSON::Validator::JSONValidator* getValidator() const
- {
- return addToValidator(
- Framework::JSON::Validator::JSONValidator::buildForObject())
- ->finishObject();
- };
- virtual Framework::Text getTypeToken() const = 0;
- };
- template<typename T> class SubTypeFactoryRef
- : public Framework::ReferenceCounter
- {
- private:
- Framework::Text typetoken;
- std::function<T*(Framework::JSON::JSONObject*)> createJValueFunc;
- std::function<void(T*, Framework::JSON::JSONObject*)> fromJsonFunc;
- std::function<void(T*, Framework::JSON::JSONObject*)> toJsonFunc;
- std::function<JSONObjectValidationBuilder*(JSONObjectValidationBuilder*)>
- getValidatorFunc;
- Framework::ReferenceCounter* factory;
- public:
- SubTypeFactoryRef(Framework::Text typetoken,
- std::function<T*(Framework::JSON::JSONObject*)> createJValueFunc,
- std::function<void(T*, Framework::JSON::JSONObject*)> fromJsonFunc,
- std::function<void(T*, Framework::JSON::JSONObject*)> toJsonFunc,
- std::function<JSONObjectValidationBuilder*(
- JSONObjectValidationBuilder*)> getValidatorFunc,
- Framework::ReferenceCounter* factory)
- : ReferenceCounter(),
- createJValueFunc(createJValueFunc),
- typetoken(typetoken),
- fromJsonFunc(fromJsonFunc),
- toJsonFunc(toJsonFunc),
- getValidatorFunc(getValidatorFunc),
- factory(factory)
- {}
- ~SubTypeFactoryRef()
- {
- factory->release();
- }
- T* createValue(Framework::JSON::JSONObject* zJson) const
- {
- return createJValueFunc(zJson);
- }
- void fromJson(T* zResult, Framework::JSON::JSONObject* zJson) const
- {
- fromJsonFunc(zResult, zJson);
- }
- void toJSON(T* zObject, Framework::JSON::JSONObject* zResult) const
- {
- toJsonFunc(zObject, zResult);
- }
- JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return getValidatorFunc(builder);
- }
- const Framework::Text& getTypetoken() const
- {
- return typetoken;
- }
- };
- template<typename T> class PolymorphTypeFactory : public TypeFactory<T>
- {
- private:
- Framework::RCArray<SubTypeFactoryRef<T>> factories;
- Framework::RCArray<Framework::Text> typeNames;
- static thread_local bool insideGetValidator;
- public:
- PolymorphTypeFactory()
- : TypeFactory<T>()
- {}
- T* createValue(Framework::JSON::JSONObject* zJson) const override
- {
- for (SubTypeFactoryRef<T>* factory : factories)
- {
- if (zJson->asObject()
- ->zValue("type")
- ->asString()
- ->getString()
- .istGleich(factory->getTypetoken()))
- {
- return factory->createValue(zJson->asObject());
- }
- }
- return 0;
- }
- void fromJson(T* zResult, Framework::JSON::JSONObject* zJson) const override
- {
- for (SubTypeFactoryRef<T>* factory : factories)
- {
- if (zJson->asObject()
- ->zValue("type")
- ->asString()
- ->getString()
- .istGleich(factory->getTypetoken()))
- {
- factory->fromJson(zResult, zJson->asObject());
- }
- }
- }
- void toJson(T* zObject, Framework::JSON::JSONObject* zResult) const override
- {
- auto name = typeNames.begin();
- for (SubTypeFactoryRef<T>* factory : factories)
- {
- if (name->istGleich(typeid(*zObject).name()))
- {
- factory->toJSON(zObject, zResult);
- zResult->addValue("type",
- new Framework::JSON::JSONString(factory->getTypetoken()));
- return;
- }
- name++;
- }
- }
- JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const override
- {
- throw "Can not add polymorph json validator to an object "
- "validation builder";
- }
- Framework::JSON::Validator::JSONValidator* getValidator() const override
- {
- Framework::JSON::Validator::JSONValidator* result;
- if (!insideGetValidator)
- {
- insideGetValidator = true;
- auto validator
- = Framework::JSON::Validator::JSONValidator::buildForOneOf()
- ->typeSpecifiedByAttribute("type");
- for (SubTypeFactoryRef<T>* factory : factories)
- {
- validator = validator->addAcceptedType(
- factory
- ->addToValidator(
- Framework::JSON::Validator::JSONValidator::
- buildForObject()
- ->withRequiredString("type")
- ->withExactMatch(factory->getTypetoken())
- ->finishString()
- ->setObjectReferenceId(
- ((Framework::Text("_type_")
- += typeid(T).name())
- += "_")
- += factory->getTypetoken()))
- ->finishObject());
- }
- result = validator->finishOneOf();
- insideGetValidator = false;
- }
- else
- {
- auto validator
- = Framework::JSON::Validator::JSONValidator::buildForOneOf()
- ->typeSpecifiedByAttribute("type");
- for (SubTypeFactoryRef<T>* factory : factories)
- {
- validator = validator->addAcceptedType(Framework::JSON::
- Validator::JSONValidator::buildForObjectReference(
- ((Framework::Text("_type_") += typeid(T).name())
- += "_")
- += factory->getTypetoken()));
- }
- result = validator->finishOneOf();
- }
- return result;
- }
- template<typename S,
- typename = std::enable_if<std::is_base_of<T, S>::value>>
- void addFactory(SubTypeFactory<T, S>* factory)
- {
- factories.add(new SubTypeFactoryRef<T>(
- factory->getTypeToken(),
- [factory](Framework::JSON::JSONObject* zJson) {
- S* value = factory->createValue(zJson);
- if (value)
- {
- return dynamic_cast<T*>(value);
- }
- return (T*)0;
- },
- [factory](T* zResult, Framework::JSON::JSONObject* zJson) {
- factory->fromJson(dynamic_cast<S*>(zResult), zJson);
- },
- [factory](T* zObject, Framework::JSON::JSONObject* zResult) {
- factory->toJson(dynamic_cast<S*>(zObject), zResult);
- },
- [factory](JSONObjectValidationBuilder* builder) {
- return factory->addToValidator(builder);
- },
- dynamic_cast<Framework::ReferenceCounter*>(factory)));
- typeNames.add(new Framework::Text(typeid(S).name()));
- }
- };
- template<typename T>
- thread_local bool PolymorphTypeFactory<T>::insideGetValidator = false;
- class TypeFatoryRef : public Framework::ReferenceCounter
- {
- private:
- std::function<void*(Framework::JSON::JSONObject*)> createValueFunc;
- std::function<void(void*, Framework::JSON::JSONObject*)> fromJsonFunc;
- std::function<void(void*, Framework::JSON::JSONObject*)> toJsonFunc;
- std::function<Framework::JSON::Validator::JSONValidator*()>
- getValidatorFunc;
- std::function<JSONObjectValidationBuilder*(JSONObjectValidationBuilder*)>
- addToValidatorFunc;
- Framework::ReferenceCounter* factory;
- public:
- TypeFatoryRef(
- std::function<void*(Framework::JSON::JSONObject*)> createValueFunc,
- std::function<void(void*, Framework::JSON::JSONObject*)> fromJsonFunc,
- std::function<void(void*, Framework::JSON::JSONObject*)> toJsonFunc,
- std::function<Framework::JSON::Validator::JSONValidator*()>
- getValidatorFunc,
- std::function<JSONObjectValidationBuilder*(
- JSONObjectValidationBuilder*)> addToValidatorFunc,
- Framework::ReferenceCounter* factory)
- : ReferenceCounter(),
- createValueFunc(createValueFunc),
- fromJsonFunc(fromJsonFunc),
- toJsonFunc(toJsonFunc),
- getValidatorFunc(getValidatorFunc),
- addToValidatorFunc(addToValidatorFunc),
- factory(factory)
- {}
- ~TypeFatoryRef()
- {
- factory->release();
- }
- void* createValue(Framework::JSON::JSONObject* zJson) const
- {
- return createValueFunc(zJson);
- }
- void fromJson(void* zResult, Framework::JSON::JSONObject* zJson) const
- {
- return fromJsonFunc(zResult, zJson);
- }
- void toJSON(void* zObject, Framework::JSON::JSONObject* zResult) const
- {
- return toJsonFunc(zObject, zResult);
- }
- Framework::JSON::Validator::JSONValidator* getValidator() const
- {
- return getValidatorFunc();
- }
- JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return addToValidatorFunc(builder);
- }
- template<typename T> TypeFactory<T>* zFactory() const
- {
- return (TypeFactory<T>*)(factory);
- }
- };
- class TypeRegistry : public Framework::ReferenceCounter
- {
- private:
- Framework::RCTrie<TypeFatoryRef> parsableTypes;
- Framework::RCArray<Framework::Text> parsableTypeNames;
- public:
- TypeRegistry();
- template<typename T,
- typename S,
- typename = std::enable_if<std::is_base_of<T, S>::value>>
- void registerSubType(SubTypeFactory<T, S>* factory)
- {
- Framework::Text typeId = typeid(T).name();
- TypeFatoryRef* typeFactoryRef
- = parsableTypes.z(typeId, typeId.getLength());
- if (!typeFactoryRef)
- {
- PolymorphTypeFactory<T>* polymorphFactory
- = new PolymorphTypeFactory<T>();
- registerType(polymorphFactory);
- typeFactoryRef = parsableTypes.z(typeId, typeId.getLength());
- parsableTypeNames.add(new Framework::Text(typeId));
- }
- PolymorphTypeFactory<T>* polymorphFactory
- = dynamic_cast<PolymorphTypeFactory<T>*>(
- typeFactoryRef->zFactory<T>());
- if (!polymorphFactory)
- {
- throw Framework::Text("Type not registered as Polymorphic type: ")
- + typeId;
- }
- polymorphFactory->template addFactory<S>(factory);
- }
- template<typename T> void registerType(TypeFactory<T>* factory)
- {
- Framework::Text typeId = typeid(T).name();
- TypeFatoryRef* typeFactoryRef
- = parsableTypes.z(typeId, typeId.getLength());
- if (typeFactoryRef)
- {
- throw Framework::Text("Type already registered: ") + typeId;
- }
- typeFactoryRef = new TypeFatoryRef(
- [factory](Framework::JSON::JSONObject* zJson) {
- return (void*)factory->createValue(zJson);
- },
- [factory](void* zResult, Framework::JSON::JSONObject* zJson) {
- factory->fromJson((T*)zResult, zJson);
- },
- [factory](void* zObject, Framework::JSON::JSONObject* zResult) {
- factory->toJson((T*)zObject, zResult);
- },
- [factory]() { return factory->getValidator(); },
- [factory](JSONObjectValidationBuilder* builder) {
- return factory->addToValidator(builder);
- },
- factory);
- parsableTypes.set(typeId, typeId.getLength(), typeFactoryRef);
- parsableTypeNames.add(new Framework::Text(typeId));
- }
- template<typename T> T* fromJson(Framework::JSON::JSONValue* zJson) const
- {
- Framework::Text typeId = typeid(T).name();
- TypeFatoryRef* typeFactoryRef
- = parsableTypes.z(typeId, typeId.getLength());
- if (!typeFactoryRef)
- {
- throw Framework::Text("Type not registered: ") + typeId;
- }
- T* result = (T*)(typeFactoryRef->createValue(zJson->asObject()));
- (typeFactoryRef->fromJson(result, zJson->asObject()));
- return result;
- }
- template<typename T> Framework::JSON::JSONValue* toJson(T* zObject) const
- {
- Framework::Text typeId = typeid(T).name();
- TypeFatoryRef* typeFactoryRef
- = parsableTypes.z(typeId, typeId.getLength());
- if (!typeFactoryRef)
- {
- throw Framework::Text("Type not registered: ") + typeId;
- }
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- typeFactoryRef->toJSON(zObject, result);
- return result;
- }
- template<typename T>
- Framework::JSON::Validator::JSONValidator* getValidator() const
- {
- Framework::Text typeId = typeid(T).name();
- TypeFatoryRef* typeFactoryRef
- = parsableTypes.z(typeId, typeId.getLength());
- if (!typeFactoryRef)
- {
- throw Framework::Text("Type not registered: ") + typeId;
- }
- return typeFactoryRef->getValidator();
- }
- template<typename T> JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- Framework::Text typeId = typeid(T).name();
- TypeFatoryRef* typeFactoryRef
- = parsableTypes.z(typeId, typeId.getLength());
- if (!typeFactoryRef)
- {
- throw Framework::Text("Type not registered: ") + typeId;
- }
- return typeFactoryRef->addToValidator(builder);
- }
- template<typename T> Framework::JSON::JSONValue* getValidParts(
- Framework::JSON::JSONValue* zJson) const
- {
- Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
- invalidParts;
- Framework::JSON::Validator::JSONValidator* validator
- = getValidator<T>();
- Framework::JSON::JSONValue* result
- = validator->getValidParts(zJson, &invalidParts);
- for (Framework::JSON::Validator::JSONValidationResult* invalidPart :
- invalidParts)
- {
- invalidPart->printInvalidInfo();
- }
- return result;
- }
- void writeSyntaxInfo(Framework::Text folderPath) const;
- };
|