i2pd/libi2pd/Config.h

117 lines
3.4 KiB
C
Raw Normal View History

2016-01-20 00:00:00 +00:00
#ifndef CONFIG_H
#define CONFIG_H
2016-01-20 00:00:00 +00:00
#include <string>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/variables_map.hpp>
/**
* Functions to parse and store i2pd parameters
*
* General usage flow:
* Init() -- early as possible
* ParseCmdline() -- somewhere close to main()
* ParseConfig() -- after detecting path to config
* Finalize() -- right after all Parse*() functions called
* GetOption() -- may be called after Finalize()
*/
namespace i2p {
namespace config {
extern boost::program_options::variables_map m_Options;
/**
* @brief Initialize list of acceptable parameters
*
* Should be called before any Parse* functions.
*/
void Init();
/**
* @brief Parse cmdline parameters, and show help if requested
* @param argc Cmdline arguments count, should be passed from main().
2016-03-10 17:23:17 +00:00
* @param argv Cmdline parameters array, should be passed from main()
2016-01-20 00:00:00 +00:00
*
2019-04-08 19:22:42 +00:00
* If --help is given in parameters, shows its list with description
* and terminates the program with exitcode 0.
2016-01-20 00:00:00 +00:00
*
* In case of parameter misuse boost throws an exception.
* We internally handle type boost::program_options::unknown_option,
2019-04-08 19:22:42 +00:00
* and then terminate the program with exitcode 1.
2016-01-20 00:00:00 +00:00
*
* Other exceptions will be passed to higher level.
*/
2017-03-29 14:51:32 +00:00
void ParseCmdline(int argc, char* argv[], bool ignoreUnknown = false);
2016-01-20 00:00:00 +00:00
/**
* @brief Load and parse given config file
* @param path Path to config file
*
2017-12-07 13:26:28 +00:00
* If error occurred when opening file path is points to,
2016-01-20 00:00:00 +00:00
* we show the error message and terminate program.
*
* In case of parameter misuse boost throws an exception.
* We internally handle type boost::program_options::unknown_option,
* and then terminate program with exitcode 1.
*
* Other exceptions will be passed to higher level.
*/
void ParseConfig(const std::string& path);
/**
* @brief Used to combine options from cmdline, config and default values
*/
void Finalize();
/* @brief Accessor to parameters by name
* @param name Name of the requested parameter
* @param value Variable where to store option
* @return this function returns false if parameter not found
*
2016-05-31 00:00:00 +00:00
* Example: uint16_t port; GetOption("sam.port", port);
2016-01-20 00:00:00 +00:00
*/
template<typename T>
bool GetOption(const char *name, T& value) {
if (!m_Options.count(name))
return false;
value = m_Options[name].as<T>();
return true;
}
2016-11-03 19:28:33 +00:00
template<typename T>
2018-01-06 03:48:51 +00:00
bool GetOption(const std::string& name, T& value)
2016-11-03 19:28:33 +00:00
{
return GetOption (name.c_str (), value);
2018-01-06 03:48:51 +00:00
}
2016-11-03 19:28:33 +00:00
2017-03-14 12:34:01 +00:00
bool GetOptionAsAny(const char *name, boost::any& value);
bool GetOptionAsAny(const std::string& name, boost::any& value);
2016-01-20 00:00:00 +00:00
/**
* @brief Set value of given parameter
* @param name Name of settable parameter
* @param value New parameter value
* @return true if value set up successful, false otherwise
*
2016-05-31 00:00:00 +00:00
* Example: uint16_t port = 2827; SetOption("bob.port", port);
2016-01-20 00:00:00 +00:00
*/
template<typename T>
bool SetOption(const char *name, const T& value) {
if (!m_Options.count(name))
return false;
2016-03-10 17:23:17 +00:00
m_Options.at(name).value() = value;
2016-01-20 00:00:00 +00:00
notify(m_Options);
return true;
2016-11-03 19:28:33 +00:00
}
2016-02-10 00:00:00 +00:00
/**
* @brief Check is value explicitly given or default
* @param name Name of checked parameter
2019-04-08 19:22:42 +00:00
* @return true if value set to default, false otherwise
2016-02-10 00:00:00 +00:00
*/
bool IsDefault(const char *name);
2016-01-20 00:00:00 +00:00
}
}
2016-01-20 00:00:00 +00:00
#endif // CONFIG_H