Config.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef __CONFIG_H
  2. #define __CONFIG_H
  3. #include <json/json.h>
  4. #include "error.h"
  5. DefineError(JsonPathNotFoundError, -512)
  6. class Args;
  7. class Config
  8. {
  9. public:
  10. Config(const std::string& file=""){_init(file);}
  11. Config(Args& args);
  12. virtual ~Config(){}
  13. static void init(Args& args);
  14. void check();
  15. static const std::string get_string(const std::string& path){
  16. Json::Value v = m_instance->_get(path);
  17. return v.asString();
  18. }
  19. static int get_int(const std::string& path){
  20. return m_instance->_get(path).asInt();
  21. }
  22. static double get_double(const std::string& path){
  23. return m_instance->_get(path).asDouble();
  24. }
  25. static bool get_bool(const std::string& path){
  26. return m_instance->_get(path).asBool();
  27. }
  28. static const Json::Value get(const std::string& path){
  29. return m_instance->_get(path);
  30. }
  31. static void set(const std::string& path, Json::Value& v){
  32. return m_instance->_set(path, v);
  33. }
  34. static void set(const std::string& path, int v){
  35. return m_instance->_set(path, Json::Value(v));
  36. }
  37. static void set(const std::string& path, double v){
  38. return m_instance->_set(path, Json::Value(v));
  39. }
  40. static void set(const std::string& path, bool v){
  41. return m_instance->_set(path, Json::Value(v));
  42. }
  43. static void set(const std::string& path, const std::string& v){
  44. return m_instance->_set(path, Json::Value(v));
  45. }
  46. static const std::string APP_DIR(const std::string& x=""){
  47. std::string dir = get_string("dirs.app");
  48. if(x.size()) return dir+"/"+x;
  49. return "./"+x;
  50. }
  51. static const std::string PAD_DIR(const std::string& x=""){
  52. std::string dir = get_string("dirs.pads");
  53. if(x.size()){
  54. if(dir[0]=='/') return dir+"/"+x;
  55. return APP_DIR(dir+"/"+x);
  56. }
  57. if(dir[0]=='/') return dir+"/";
  58. return APP_DIR(x);
  59. }
  60. protected:
  61. void _init(const std::string& ="");
  62. Json::Value _get(const std::string&);
  63. void _set(const std::string&, Json::Value);
  64. Json::Value m_root;
  65. static Config* m_instance;
  66. static std::string m_defaut_config;
  67. };
  68. #endif