76 lines
2.6 KiB
C++
76 lines
2.6 KiB
C++
/**
|
|
* @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 <string>
|
|
#include <vector>
|
|
#include <memory>
|
|
|
|
#include <di/Json.h>
|
|
|
|
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<Json> *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<Json> &value);
|
|
|
|
private:
|
|
std::shared_ptr<Json> loaded;
|
|
std::shared_ptr<Json> defaults;
|
|
|
|
bool __getProperty(std::shared_ptr<Json> root, const std::string &key, bool *value);
|
|
bool __getProperty(std::shared_ptr<Json> root, const std::string &key, std::string *value);
|
|
bool __getProperty(std::shared_ptr<Json> root, const std::string &key, double *value);
|
|
bool __getProperty(std::shared_ptr<Json> root, const std::string &key, int *value);
|
|
bool __getProperty(std::shared_ptr<Json> root, const std::string &key, int64_t *value);
|
|
bool __getArrayProperty(std::shared_ptr<Json> root, const std::string &key, std::shared_ptr<Json> *value);
|
|
|
|
bool __searchFile(const std::string &filename, const std::vector<std::string> &paths, std::string *filepath);
|
|
bool __fileExists(const std::string &filepath);
|
|
};
|
|
|
|
} // namespace Di
|
|
|
|
|
|
#endif // INCLUDE_DI_CONFIGURATION_H_
|