File: Config.h

package info (click to toggle)
i2pd 2.58.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,612 kB
  • sloc: cpp: 59,663; makefile: 224; sh: 138
file content (127 lines) | stat: -rw-r--r-- 3,533 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright (c) 2013-2020, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/

#ifndef CONFIG_H
#define CONFIG_H

#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().
	 * @param argv Cmdline parameters array, should be passed from main()
	 *
	 * If --help is given in parameters, shows its list with description
	 * and terminates the program with exitcode 0.
	 *
	 * In case of parameter misuse boost throws an exception.
	 * We internally handle type boost::program_options::unknown_option,
	 * and then terminate the program with exitcode 1.
	 *
	 * Other exceptions will be passed to higher level.
	 */
	void ParseCmdline(int argc, char* argv[], bool ignoreUnknown = false);

	/**
	 * @brief Load and parse given config file
	 * @param path Path to config file
	 *
	 * If error occurred when opening file path is points to,
	 * 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
	 *
	 * Example: uint16_t port; GetOption("sam.port", port);
	 */
	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;
	}

	template<typename T>
	bool GetOption(const std::string& name, T& value)
	{
		return GetOption (name.c_str (), value);
	}

	bool GetOptionAsAny(const char *name, boost::any& value);
	bool GetOptionAsAny(const std::string& name, boost::any& value);

	/**
	 * @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
	 *
	 * Example: uint16_t port = 2827; SetOption("bob.port", port);
	 */
	template<typename T>
	bool SetOption(const char *name, const T& value)
	{
		if (!m_Options.count(name))
			return false;
		m_Options.at(name).value() = value;
		notify(m_Options);
		return true;
	}

	/**
	 * @brief Check is value explicitly given or default
	 * @param name Name of checked parameter
	 * @return true if value set to default, false otherwise
	 */
	bool IsDefault(const char *name);
}
}

#endif // CONFIG_H