#ifndef __CONFIG_H #define __CONFIG_H #include #include "error.h" DefineError(JsonPathNotFoundError, -512) class Args; class Config { public: Config(const std::string& file=""){_init(file);} Config(Args& args); virtual ~Config(){} static void init(Args& args); void check(); static const std::string get_string(const std::string& path){ Json::Value v = m_instance->_get(path); return v.asString(); } static int get_int(const std::string& path){ return m_instance->_get(path).asInt(); } static double get_double(const std::string& path){ return m_instance->_get(path).asDouble(); } static bool get_bool(const std::string& path){ return m_instance->_get(path).asBool(); } static const Json::Value get(const std::string& path){ return m_instance->_get(path); } static void set(const std::string& path, Json::Value& v){ return m_instance->_set(path, v); } static void set(const std::string& path, int v){ return m_instance->_set(path, Json::Value(v)); } static void set(const std::string& path, double v){ return m_instance->_set(path, Json::Value(v)); } static void set(const std::string& path, bool v){ return m_instance->_set(path, Json::Value(v)); } static void set(const std::string& path, const std::string& v){ return m_instance->_set(path, Json::Value(v)); } static const std::string APP_DIR(const std::string& x=""){ std::string dir = get_string("dirs.app"); if(x.size()) return dir+"/"+x; return "./"+x; } static const std::string PAD_DIR(const std::string& x=""){ std::string dir = get_string("dirs.pads"); if(x.size()){ if(dir[0]=='/') return dir+"/"+x; return APP_DIR(dir+"/"+x); } if(dir[0]=='/') return dir+"/"; return APP_DIR(x); } protected: void _init(const std::string& =""); Json::Value _get(const std::string&); void _set(const std::string&, Json::Value); Json::Value m_root; static Config* m_instance; static std::string m_defaut_config; }; #endif