/* * This file is part of smolbote. It's copyrighted by the contributors recorded * in the version control history of the file, available from its original * location: git://neueland.iserlohn-fortress.net/smolbote.git * * SPDX-License-Identifier: GPL-3.0 */ #ifndef CONFIGURATION_H #define CONFIGURATION_H #include #include #include #include class Configuration { public: explicit Configuration(const std::string &path, const std::string &home); ~Configuration(); bool read(); bool parse(const std::string &contents); bool writeIfNeeded(const std::string &path = std::string()); bool parseDefaultConfiguration(const std::string &contents); std::vector childrenSettings(const char *name = ""); std::vector childrenGroups(const char *name = ""); void resetValue(const char *path); template std::optional value(const char *path) const { // if setting doesn't exist, give back a nullopt if(!m_userCfg->exists(path)) { const_cast(this)->resetValue(path); return value(path); } const libconfig::Setting &v = m_userCfg->lookup(path); if constexpr(std::is_same_v) return std::optional(castToString(v)); else return std::optional(static_cast(v)); } template bool setValue(std::string path, const T &val); private: std::string castToString(const libconfig::Setting &v) const; bool changed = false; std::string m_homePath; std::string m_userCfgPath; libconfig::Config *m_userCfg, *m_defaultCfg; }; // replace ~ with home std::string patchHome(const std::string &path, const std::string &home); // instantiate functions // this needs to be done because the implementation is in the cpp file // Settings::setValue<> extern template bool Configuration::setValue(std::string path, const int &val); extern template bool Configuration::setValue(std::string path, const unsigned int &val); extern template bool Configuration::setValue(std::string path, const long &val); extern template bool Configuration::setValue(std::string path, const unsigned long &val); extern template bool Configuration::setValue(std::string path, const long long &val); extern template bool Configuration::setValue(std::string path, const unsigned long long &val); extern template bool Configuration::setValue(std::string path, const float &val); extern template bool Configuration::setValue(std::string path, const double &val); extern template bool Configuration::setValue(std::string path, const bool &val); extern template bool Configuration::setValue(std::string path, const std::string &val); #endif // CONFIGURATION_H