/* * 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 <optional> #include <string> #include <vector> namespace libconfig { class Config; class Setting; } class Configuration { public: Configuration(); ~Configuration(); bool readUserConfiguration(const std::string &path); bool parse(const std::string &contents); bool writeUserConfiguration(const std::string &path); bool writeIfNeeded(); bool parseDefaultConfiguration(const std::string &contents); std::vector<std::string> childrenSettings(const char *name = ""); std::vector<std::string> childrenGroups(const char *name = ""); void resetValue(const char *path); template <typename T> std::optional<T> value(const char *path) const; template <typename T> bool setValue(std::string path, const T &val); private: bool changed = false; std::string m_userCfgPath; libconfig::Config *m_userCfg, *m_defaultCfg; }; // replace ~ with home std::string &patchHome(std::string &path, const std::string &home); // instantiate functions // this needs to be done because the implementation is in the cpp file // Settings::value<> extern template std::optional<int> Configuration::value<int>(const char *path) const; extern template std::optional<unsigned int> Configuration::value<unsigned int>(const char *path) const; extern template std::optional<long> Configuration::value<long>(const char *path) const; extern template std::optional<unsigned long> Configuration::value<unsigned long>(const char *path) const; extern template std::optional<long long> Configuration::value<long long>(const char *path) const; extern template std::optional<unsigned long long> Configuration::value<unsigned long long>(const char *path) const; extern template std::optional<float> Configuration::value<float>(const char *path) const; extern template std::optional<double> Configuration::value<double>(const char *path) const; extern template std::optional<bool> Configuration::value<bool>(const char *path) const; extern template std::optional<std::string> Configuration::value<std::string>(const char *path) const; // Settings::setValue<> extern template bool Configuration::setValue<int>(std::string path, const int &val); extern template bool Configuration::setValue<unsigned int>(std::string path, const unsigned int &val); extern template bool Configuration::setValue<long>(std::string path, const long &val); extern template bool Configuration::setValue<unsigned long>(std::string path, const unsigned long &val); extern template bool Configuration::setValue<long long>(std::string path, const long long &val); extern template bool Configuration::setValue<unsigned long long>(std::string path, const unsigned long long &val); extern template bool Configuration::setValue<float>(std::string path, const float &val); extern template bool Configuration::setValue<double>(std::string path, const double &val); extern template bool Configuration::setValue<bool>(std::string path, const bool &val); extern template bool Configuration::setValue<std::string>(std::string path, const std::string &val); #endif // CONFIGURATION_H