Config.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef CONFIG_H
  2. #define CONFIG_H
  3. #include <string>
  4. #include <boost/program_options/options_description.hpp>
  5. #include <boost/program_options/variables_map.hpp>
  6. /**
  7. * Functions to parse and store i2pd parameters
  8. *
  9. * General usage flow:
  10. * Init() -- early as possible
  11. * ParseCmdline() -- somewhere close to main()
  12. * ParseConfig() -- after detecting path to config
  13. * Finalize() -- right after all Parse*() functions called
  14. * GetOption() -- may be called after Finalize()
  15. */
  16. namespace i2p {
  17. namespace config {
  18. extern boost::program_options::variables_map m_Options;
  19. /**
  20. * @brief Initialize list of acceptable parameters
  21. *
  22. * Should be called before any Parse* functions.
  23. */
  24. void Init();
  25. /**
  26. * @brief Parse cmdline parameters, and show help if requested
  27. * @param argc Cmdline arguments count, should be passed from main().
  28. * @param argv Cmdline parameters array, should be passed from main()
  29. *
  30. * If --help is given in parameters, shows its list with description
  31. * and terminates the program with exitcode 0.
  32. *
  33. * In case of parameter misuse boost throws an exception.
  34. * We internally handle type boost::program_options::unknown_option,
  35. * and then terminate the program with exitcode 1.
  36. *
  37. * Other exceptions will be passed to higher level.
  38. */
  39. void ParseCmdline(int argc, char* argv[], bool ignoreUnknown = false);
  40. /**
  41. * @brief Load and parse given config file
  42. * @param path Path to config file
  43. *
  44. * If error occurred when opening file path is points to,
  45. * we show the error message and terminate program.
  46. *
  47. * In case of parameter misuse boost throws an exception.
  48. * We internally handle type boost::program_options::unknown_option,
  49. * and then terminate program with exitcode 1.
  50. *
  51. * Other exceptions will be passed to higher level.
  52. */
  53. void ParseConfig(const std::string& path);
  54. /**
  55. * @brief Used to combine options from cmdline, config and default values
  56. */
  57. void Finalize();
  58. /* @brief Accessor to parameters by name
  59. * @param name Name of the requested parameter
  60. * @param value Variable where to store option
  61. * @return this function returns false if parameter not found
  62. *
  63. * Example: uint16_t port; GetOption("sam.port", port);
  64. */
  65. template<typename T>
  66. bool GetOption(const char *name, T& value) {
  67. if (!m_Options.count(name))
  68. return false;
  69. value = m_Options[name].as<T>();
  70. return true;
  71. }
  72. template<typename T>
  73. bool GetOption(const std::string& name, T& value)
  74. {
  75. return GetOption (name.c_str (), value);
  76. }
  77. bool GetOptionAsAny(const char *name, boost::any& value);
  78. bool GetOptionAsAny(const std::string& name, boost::any& value);
  79. /**
  80. * @brief Set value of given parameter
  81. * @param name Name of settable parameter
  82. * @param value New parameter value
  83. * @return true if value set up successful, false otherwise
  84. *
  85. * Example: uint16_t port = 2827; SetOption("bob.port", port);
  86. */
  87. template<typename T>
  88. bool SetOption(const char *name, const T& value) {
  89. if (!m_Options.count(name))
  90. return false;
  91. m_Options.at(name).value() = value;
  92. notify(m_Options);
  93. return true;
  94. }
  95. /**
  96. * @brief Check is value explicitly given or default
  97. * @param name Name of checked parameter
  98. * @return true if value set to default, false otherwise
  99. */
  100. bool IsDefault(const char *name);
  101. }
  102. }
  103. #endif // CONFIG_H