1 #include "hmbdc/Copyright.hpp" 7 #include <unordered_set> 8 #include <boost/property_tree/ptree.hpp> 9 #pragma GCC diagnostic ignored "-Wreturn-type" 10 #include <boost/property_tree/json_parser.hpp> 12 namespace hmbdc {
namespace app {
52 Config(istream&& is,
char const* section =
nullptr) {
54 read_json(is, fallback_);
55 (ptree&)*
this = fallback_.get_child(section);
57 read_json(is, (ptree&)*
this);
69 Config(istream& is,
char const* section =
nullptr) {
71 read_json(is, fallback_);
72 (ptree&)*
this = fallback_.get_child(section);
74 read_json(is, (ptree&)*
this);
87 Config(
char const* json,
char const* section =
nullptr)
88 :
Config(istringstream(json), section)
99 Config(ptree
const& t,
char const* section =
nullptr) {
102 (ptree&)*
this = fallback_.get_child(section);
117 Config(ptree
const& t, ptree
const& section) {
119 (ptree&)*
this = section;
129 defaultUserConfig_.reset(
new Config(c));
142 template <
typename T>
144 auto res = get_optional<T>(param);
146 res = fallback_.get_optional<T>(param);
148 if (defaultUserConfig_) {
149 return defaultUserConfig_->getExt<T>(param);
151 throw ptree_bad_path(
"invalid param and no default user Config set", param);
168 template <
typename T>
169 T
getHex(ptree::path_type
const& param) {
170 istringstream iss(getExt<string>(param));
185 template <
typename T>
187 to = getExt<T>(param);
199 unordered_set<string>
const& skipThese = unordered_set<string>())
const {
200 list<pair<string, string>> res;
201 unordered_set<string> history(skipThese);
202 for (
auto& p : *
this) {
203 if (history.find(p.first) == history.end()) {
204 history.insert(p.first);
205 if (p.second.empty()) {
206 res.push_back(make_pair(p.first, p.second.get_value<
string>()));
210 for (
auto& p : fallback_) {
211 if (history.find(p.first) == history.end()) {
212 history.insert(p.first);
213 if (p.second.empty()) {
214 res.push_back(make_pair(p.first, p.second.get_value<
string>()));
218 if (defaultUserConfig_) {
219 auto more = defaultUserConfig_->content(history);
220 res.insert(res.end(), more.begin(), more.end());
227 std::shared_ptr<Config> defaultUserConfig_;
class to hold an hmbdc configuration
Definition: Config.hpp:35
T getHex(ptree::path_type const ¶m)
get a number value in hex format
Definition: Config.hpp:169
void setDefaultUserConfig(Config const &c)
internal use
Definition: Config.hpp:128
Config()
empty config
Definition: Config.hpp:41
Definition: TypedString.hpp:74
list< pair< string, string > > content(unordered_set< string > const &skipThese=unordered_set< string >()) const
get contents of all the effective configure in the form of list of string pairs
Definition: Config.hpp:198
Config(istream &is, char const *section=nullptr)
construct using stream, optionally specifying the section name
Definition: Config.hpp:69
Config(ptree const &t, char const *section=nullptr)
construct using another ptree as fallbacks, optionally specifying the section name ...
Definition: Config.hpp:99
Config const & operator()(T &to, const path_type ¶m) const
fill in a variable with a configured value retrieved using getExt
Definition: Config.hpp:186
Config(istream &&is, char const *section=nullptr)
construct using stream, optionally specifying the section name
Definition: Config.hpp:52
Config(ptree const &t, ptree const §ion)
construct using a fallback ptree, and specify the section ptree
Definition: Config.hpp:117
T getExt(const path_type ¶m) const
get a value from the config
Definition: Config.hpp:143
Config(char const *json, char const *section=nullptr)
construct using a string, optionally specifying the section name
Definition: Config.hpp:87
Definition: Client.hpp:11