1 #include "hmbdc/Copyright.hpp" 5 #include <boost/property_tree/ptree.hpp> 6 #include <boost/property_tree/json_parser.hpp> 12 #include <unordered_set> 20 namespace hmbdc {
namespace app {
22 namespace config_detail {
49 using Base = boost::property_tree::ptree;
61 , section_(other.section_)
62 , fallbackConfig_(other.fallbackConfig_
63 ?new
Config(*other.fallbackConfig_)
72 section_ = other.section_;
73 fallbackConfig_.reset(other.fallbackConfig_
74 ?
new Config(*other.fallbackConfig_)
88 Config(istream&& is,
char const* section =
nullptr)
89 : section_(section?section:
"") {
90 read_json(is, (Base&)*
this);
103 Config(istream& is,
char const* section =
nullptr)
104 : section_(section?section:
"") {
105 read_json(is, (Base&)*
this);
118 Config(
char const* json,
char const* section =
nullptr)
119 :
Config(istringstream(json), section)
130 Config(ptree
const& t,
char const* section =
nullptr)
132 , section_(section?section:
"") {
144 Config(ptree
const& dft, ptree
const& section)
146 , fallbackConfig_(new
Config(dft)) {
156 if (!fallbackConfig_) {
157 fallbackConfig_.reset(
new Config(c));
159 fallbackConfig_->setAdditionalFallbackConfig(c);
166 setAdditionalFallbackConfig(c);
179 if (sectionExists) get_child(section);
180 section_ = section?section:
"";
181 if (fallbackConfig_) {
182 fallbackConfig_->resetSection(section,
false);
199 auto res = get_child_optional(sec/=param);
201 res = get_child_optional(param);
203 if (fallbackConfig_) {
204 return fallbackConfig_->getChildExt(param);
206 throw ptree_bad_path(
"invalid param and no default user Config set", param);
224 template <
typename T>
225 T
getExt(
const path_type& param,
bool throwIfMissing =
true)
const {
227 auto res = get_optional<T>(sec/=param);
229 res = get_optional<T>(param);
231 if (fallbackConfig_) {
232 return fallbackConfig_->getExt<T>(param, throwIfMissing);
233 }
else if (throwIfMissing) {
234 throw ptree_bad_path(
"invalid param and no default user Config set", param);
253 template <
typename T>
254 T
getHex(ptree::path_type
const& param)
const {
255 istringstream iss(getExt<string>(param));
270 template <
typename T>
272 to = getExt<T>(param);
285 template <
typename T>
288 auto s = getExt<string>(param);
289 istringstream iss(s);
291 for (
auto iit = istream_iterator<T>(iss)
292 ; iit != istream_iterator<T>()
297 throw (ptree_bad_data(
"not space separated items in Config ", param));
312 template <
typename T>
315 auto s = getExt<string>(param);
316 istringstream iss(s);
318 for (
auto iit = istream_iterator<T>(iss)
319 ; iit != istream_iterator<T>()
321 to.emplace_back(*iit);
324 throw (ptree_bad_data(
"not space separated items in Config ", param));
338 unordered_set<string>
const& skipThese = unordered_set<string>())
const {
339 list<pair<string, string>> res;
340 unordered_set<string> history(skipThese);
341 auto secTree = get_child_optional(section_);
343 for (
auto& p : *secTree) {
344 if (history.find(p.first) == history.end()) {
345 history.insert(p.first);
346 if (p.second.empty()) {
347 res.push_back(make_pair(p.first, p.second.get_value<
string>()));
358 for (
auto& p : *
this) {
359 if (history.find(p.first) == history.end()) {
360 history.insert(p.first);
361 if (p.second.empty()) {
362 res.push_back(make_pair(p.first, p.second.get_value<
string>()));
372 if (fallbackConfig_) {
373 auto more = fallbackConfig_->content(history);
374 res.insert(res.end(), more.begin(), more.end());
387 friend ostream& operator << (ostream& os,
Config const& cfg) {
388 for (
auto& r : cfg.
content()) {
389 os << r.first <<
'=' << r.second << endl;
391 if (cfg.fallbackConfig_) {
392 os <<
"next in line fallback" << endl;
393 os << *cfg.fallbackConfig_ << endl;
418 std::unique_ptr<Config> fallbackConfig_;
T getExt(const path_type ¶m, bool throwIfMissing=true) const
get a value from the config
Definition: Config.hpp:225
Config const & operator()(std::unordered_set< T > &to, const path_type ¶m) const
fill an unordered_set with a configured value retrieved using getExt
Definition: Config.hpp:286
Config(Config const &other)
copt ctor
Definition: Config.hpp:59
void setDefaultUserConfig(Config const &c)
depracated
Definition: Config.hpp:165
class to hold an hmbdc configuration
Definition: Config.hpp:46
void setAdditionalFallbackConfig(Config const &c)
set additional defaults
Definition: Config.hpp:155
Definition: TypedString.hpp:76
Config & resetSection(char const *section, bool sectionExists=true)
change section name
Definition: Config.hpp:178
Config(istream &&is, char const *section=nullptr)
construct using stream, optionally specifying the section name
Definition: Config.hpp:88
T getHex(ptree::path_type const ¶m) const
get a number value in hex format
Definition: Config.hpp:254
Config(char const *json, char const *section=nullptr)
construct using a string, optionally specifying the section name
Definition: Config.hpp:118
Config(ptree const &dft, ptree const §ion)
construct using a fallback ptree, and specify the section ptree
Definition: Config.hpp:144
Config const & operator()(T &to, const path_type ¶m) const
fill in a variable with a configured value retrieved using getExt
Definition: Config.hpp:271
ptree const & getChildExt(const path_type ¶m)
Gets the child from the config.
Definition: Config.hpp:197
Config(istream &is, char const *section=nullptr)
construct using stream, optionally specifying the section name
Definition: Config.hpp:103
Config()
empty config
Definition: Config.hpp:54
Config(ptree const &t, char const *section=nullptr)
construct using another ptree as fallbacks, optionally specifying the section name ...
Definition: Config.hpp:130
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:337
Config const & operator()(std::vector< T > &to, const path_type ¶m) const
fill an list with a configured value retrieved using getExt
Definition: Config.hpp:313