123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- #pragma once
- #include <Array.h>
- #include <cstdlib>
- #include <JSON.h>
- #include <Trie.h>
- #include <typeinfo>
- class TypeRegistry;
- class GeneratorRuleFactory;
- class GeneratorRule;
- class DimensionGenerator;
- class JExpressionMemory;
- class DimensionFactory;
- class Dimension;
- /*
- * 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* fromJson(Framework::JSON::JSONValue* zJson) const = 0;
- virtual Framework::JSON::JSONValue* toJson(T* zObject) const = 0;
- virtual Framework::JSON::Validator::JSONValidator* getValidator() const = 0;
- };
- /*
- * 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* fromJson(Framework::JSON::JSONObject* zJson) const = 0;
- virtual Framework::JSON::JSONObject* toJson(S* zObject) const = 0;
- virtual Framework::JSON::Validator::JSONValidator* getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- = 0;
- virtual Framework::Text getTypeToken() const = 0;
- };
- template<typename T> class SubTypeFactoryRef
- : public Framework::ReferenceCounter
- {
- private:
- Framework::Text typetoken;
- std::function<T*(Framework::JSON::JSONObject*)> fromJsonFunc;
- std::function<Framework::JSON::JSONObject*(T*)> toJsonFunc;
- std::function<Framework::JSON::Validator::JSONValidator*(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>*)>
- getValidatorFunc;
- Framework::ReferenceCounter* factory;
- public:
- SubTypeFactoryRef(Framework::Text typetoken,
- std::function<T*(Framework::JSON::JSONObject*)> fromJsonFunc,
- std::function<Framework::JSON::JSONObject*(T*)> toJsonFunc,
- std::function<Framework::JSON::Validator::JSONValidator*(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>*)> getValidatorFunc,
- Framework::ReferenceCounter* factory)
- : ReferenceCounter(),
- typetoken(typetoken),
- fromJsonFunc(fromJsonFunc),
- toJsonFunc(toJsonFunc),
- getValidatorFunc(getValidatorFunc),
- factory(factory)
- {}
- ~SubTypeFactoryRef()
- {
- factory->release();
- }
- T* fromJson(Framework::JSON::JSONObject* zJson) const
- {
- return fromJsonFunc(zJson);
- }
- Framework::JSON::JSONObject* toJSON(T* zObject) const
- {
- return toJsonFunc(zObject);
- }
- Framework::JSON::Validator::JSONValidator* getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* 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;
- public:
- PolymorphTypeFactory()
- : TypeFactory<T>()
- {}
- T* fromJson(Framework::JSON::JSONValue* zJson) const override
- {
- for (SubTypeFactoryRef<T>* factory : factories)
- {
- if (zJson->asObject()
- ->zValue("type")
- ->asString()
- ->getString()
- .istGleich(factory->getTypetoken()))
- {
- return factory->fromJson(zJson->asObject());
- }
- }
- return 0;
- }
- Framework::JSON::JSONValue* toJson(T* zObject) const override
- {
- for (SubTypeFactoryRef<T>* factory : factories)
- {
- Framework::JSON::JSONObject* result = factory->toJSON(zObject);
- if (result)
- {
- result->addValue("type",
- new Framework::JSON::JSONString(factory->getTypetoken()));
- return result;
- }
- }
- return 0;
- }
- Framework::JSON::Validator::JSONValidator* getValidator() const override
- {
- auto validator
- = Framework::JSON::Validator::JSONValidator::buildForOneOf()
- ->typeSpecifiedByAttribute("type");
- for (SubTypeFactoryRef<T>* factory : factories)
- {
- validator = validator->addAcceptedType(factory->getValidator(
- Framework::JSON::Validator::JSONValidator::buildForObject()
- ->withRequiredString("type")
- ->withExactMatch(factory->getTypetoken())
- ->finishString()));
- }
- return validator->finishOneOf();
- }
- 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->fromJson(zJson);
- if (value)
- {
- return dynamic_cast<T*>(factory->fromJson(zJson));
- }
- return (T*)0;
- },
- [factory](T* zObject) {
- S* value = dynamic_cast<S*>(zObject);
- if (value)
- {
- return factory->toJson(value);
- }
- return (Framework::JSON::JSONObject*)0;
- },
- [factory](Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) {
- return factory->getValidator(builder);
- },
- dynamic_cast<Framework::ReferenceCounter*>(factory)));
- }
- };
- class TypeFatoryRef : public Framework::ReferenceCounter
- {
- private:
- std::function<void*(Framework::JSON::JSONValue*)> fromJsonFunc;
- std::function<Framework::JSON::JSONValue*(void*)> toJsonFunc;
- std::function<Framework::JSON::Validator::JSONValidator*()>
- getValidatorFunc;
- Framework::ReferenceCounter* factory;
- public:
- TypeFatoryRef(
- std::function<void*(Framework::JSON::JSONValue*)> fromJsonFunc,
- std::function<Framework::JSON::JSONValue*(void*)> toJsonFunc,
- std::function<Framework::JSON::Validator::JSONValidator*()>
- getValidatorFunc,
- Framework::ReferenceCounter* factory)
- : ReferenceCounter(),
- fromJsonFunc(fromJsonFunc),
- toJsonFunc(toJsonFunc),
- getValidatorFunc(getValidatorFunc),
- factory(factory)
- {}
- ~TypeFatoryRef()
- {
- factory->release();
- }
- void* fromJson(Framework::JSON::JSONValue* zJson) const
- {
- return fromJsonFunc(zJson);
- }
- Framework::JSON::JSONValue* toJSON(void* zObject) const
- {
- return toJsonFunc(zObject);
- }
- Framework::JSON::Validator::JSONValidator* getValidator() const
- {
- return getValidatorFunc();
- }
- template<typename T> TypeFactory<T>* zFactory() const
- {
- return (TypeFactory<T>*)(factory);
- }
- };
- class GeneratorRuleFactory : public virtual Framework::ReferenceCounter
- {
- public:
- GeneratorRuleFactory();
- virtual GeneratorRule* createRule(
- Framework::JSON::JSONValue* zConfig, JExpressionMemory* zMemory)
- = 0;
- virtual Framework::JSON::Validator::JSONValidator* getValidator() = 0;
- };
- class DimensionFactory : public virtual Framework::ReferenceCounter
- {
- private:
- int dimensionId;
- public:
- DimensionFactory(int dimensionId);
- virtual ~DimensionFactory() = default;
- virtual Dimension* createDimension() = 0;
- int getDimensionId() const;
- };
- class DimensionGeneratorFactory : public virtual Framework::ReferenceCounter
- {
- private:
- Framework::Text name;
- int dimensionId;
- public:
- DimensionGeneratorFactory(Framework::Text name, int dimensionId);
- virtual DimensionGenerator* createDimensionGenerator(
- int worldSeed, Framework::JSON::JSONValue* config)
- = 0;
- virtual Framework::JSON::Validator::JSONValidator* getConfigValidator() = 0;
- virtual Framework::JSON::JSONValue* getDefaultConfig() = 0;
- Framework::Text getName() const;
- int getDimensionId() const;
- };
- class TypeRegistry : public Framework::ReferenceCounter
- {
- private:
- Framework::RCArray<GeneratorRuleFactory> generatorRules;
- Framework::RCArray<DimensionGeneratorFactory> dimensionGenerators;
- Framework::RCArray<DimensionFactory> dimensionFactories;
- Framework::RCTrie<TypeFatoryRef> parsableTypes;
- public:
- TypeRegistry();
- void registerGeneratorRuleFactory(GeneratorRuleFactory* factory);
- GeneratorRule* createGeneratorRule(
- Framework::JSON::JSONValue* zConfig, JExpressionMemory* zMemory);
- Framework::JSON::Validator::JSONValidator* getGeneratorRuleValidator();
- void registerDimensionGeneratorFactory(DimensionGeneratorFactory* factory);
- DimensionGenerator* createDimensionGenerator(
- Framework::JSON::JSONValue* zConfig, int worldSeed);
- Framework::JSON::Validator::JSONValidator* getDimensionGeneratorValidator();
- const Framework::RCArray<DimensionGeneratorFactory>&
- getDimensionGeneratorFactories() const;
- void registerDimension(DimensionFactory* factory);
- Dimension* createDimension(int id);
- 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());
- }
- 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::JSONValue* zJson) {
- return factory->fromJson(zJson);
- },
- [factory](void* zObject) { return factory->toJson((T*)zObject); },
- [factory]() { return factory->getValidator(); },
- factory);
- parsableTypes.set(typeId, typeId.getLength(), typeFactoryRef);
- }
- 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;
- }
- return (T*)(typeFactoryRef->fromJson(zJson));
- }
- 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;
- }
- return typeFactoryRef->toJSON(zObject);
- }
- 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> 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;
- }
- };
|