#ifndef SIBS_CONF_HPP #define SIBS_CONF_HPP #include "Result.hpp" #include "StringView.hpp" #include "utils.hpp" #include "Dependency.hpp" #include "Package.hpp" #include #include #include namespace sibs { class ConfigValue { public: enum class Type { NONE, SINGLE, LIST }; ConfigValue() : type(Type::NONE) {} ConfigValue(StringView value) : type(Type::SINGLE) { values.push_back(value); } ConfigValue(const std::vector &_values) : type(Type::LIST), values(_values) { } bool isSingle() const { return type == Type::SINGLE; } bool isList() const { return type == Type::LIST; } StringView asSingle() const { assert(isSingle()); return values[0]; } const std::vector asList() const { assert(isList()); return values; } private: Type type; std::vector values; }; class Parser; class ParserException : public std::runtime_error { public: ParserException(const std::string &errMsg) : runtime_error(errMsg) { } }; class ConfigCallback { friend class Parser; protected: virtual void processObject(StringView name) = 0; virtual void processField(StringView name, const ConfigValue &value) = 0; virtual void finished() = 0; }; class Config { public: static Result readFromFile(const char *filepath, const ConfigCallback &callback); }; class SibsConfig : public ConfigCallback { public: SibsConfig() : finishedProcessing(false), packageType((PackageType)-1) {} const std::string& getPackageName() const { assert(finishedProcessing); return packageName; } PackageType getPackageType() const { assert(finishedProcessing); return packageType; } const std::vector& getDependencies() const { return dependencies; } protected: void processObject(StringView name) override; void processField(StringView name, const ConfigValue &value) override; void finished() override; private: StringView currentObject; std::string packageName; PackageType packageType; std::vector dependencies; bool finishedProcessing; }; } #endif //SIBS_CONF_HPP