/** * @file di/Configuration.h * @brief Configuration loading from JSON file * @date June 2, 2015 * @author J.J.J. Jacobs, R.W.A. van der Heijden * @copyright 2015 Dual Inventive Technology Centre B.V. */ #ifndef INCLUDE_DI_CONFIGURATION_H_ #define INCLUDE_DI_CONFIGURATION_H_ #include #include #include #include namespace Di { /** * @class Configuration * * loads settings from a JSON file. * It supports documenting the configuration with C99-style single * and multiline comments. * Searches automatic in the following paths (in the given order): * - ../ * - /etc/di * - /usr/local/etc/di * - /opt/di/etc/ * - /tmp/di/etc */ class Configuration { public: Configuration(); /** Try to load filename, will search in default paths */ bool load(std::string filename); /** Try to load filename, search can be enabled/disabled */ bool load(std::string filename, bool search); /** Save default configuration to file */ bool save(std::string filename); bool getProperty(const std::string &key, bool *value); bool getProperty(const std::string &key, std::string *value); bool getProperty(const std::string &key, double *value); bool getProperty(const std::string &key, int *value); bool getProperty(const std::string &key, int64_t *value); bool getArrayProperty(const std::string &key, std::shared_ptr *value); void setDefaultProperty(const std::string &key, bool value); void setDefaultProperty(const std::string &key, const char *value); void setDefaultProperty(const std::string &key, const std::string &value); void setDefaultProperty(const std::string &key, double value); void setDefaultProperty(const std::string &key, int64_t value); void setDefaultProperty(const std::string &key, const std::shared_ptr &value); private: std::shared_ptr loaded; std::shared_ptr defaults; bool __getProperty(std::shared_ptr root, const std::string &key, bool *value); bool __getProperty(std::shared_ptr root, const std::string &key, std::string *value); bool __getProperty(std::shared_ptr root, const std::string &key, double *value); bool __getProperty(std::shared_ptr root, const std::string &key, int *value); bool __getProperty(std::shared_ptr root, const std::string &key, int64_t *value); bool __getArrayProperty(std::shared_ptr root, const std::string &key, std::shared_ptr *value); bool __searchFile(const std::string &filename, const std::vector &paths, std::string *filepath); bool __fileExists(const std::string &filepath); }; } // namespace Di #endif // INCLUDE_DI_CONFIGURATION_H_