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;
65 Config(istream&& is,
char const* section =
nullptr) {
67 read_json(is, fallback_);
68 (ptree&)*
this = fallback_.get_child(section);
70 read_json(is, (ptree&)*
this);
82 Config(istream& is,
char const* section =
nullptr) {
84 read_json(is, fallback_);
85 (ptree&)*
this = fallback_.get_child(section);
87 read_json(is, (ptree&)*
this);
100 Config(
char const* json,
char const* section =
nullptr)
101 :
Config(istringstream(json), section)
112 Config(ptree
const& t,
char const* section =
nullptr) {
115 (ptree&)*
this = fallback_.get_child(section);
129 Config(ptree
const& t, ptree
const& section) {
131 (ptree&)*
this = section;
141 defaultUserConfig_.reset(
new Config(c));
155 auto res = get_child_optional(param);
157 res = fallback_.get_child_optional(param);
159 if (defaultUserConfig_) {
160 return defaultUserConfig_->getChildExt(param);
162 throw ptree_bad_path(
"invalid param and no default user Config set", param);
179 template <
typename T>
181 auto res = get_optional<T>(param);
183 res = fallback_.get_optional<T>(param);
185 if (defaultUserConfig_) {
186 return defaultUserConfig_->getExt<T>(param);
188 throw ptree_bad_path(
"invalid param and no default user Config set", param);
205 template <
typename T>
206 T
getHex(ptree::path_type
const& param)
const {
207 istringstream iss(getExt<string>(param));
222 template <
typename T>
224 to = getExt<T>(param);
237 template <
typename T>
240 auto s = getExt<string>(param);
241 istringstream iss(s);
243 for (
auto iit = istream_iterator<T>(iss)
244 ; iit != istream_iterator<T>()
249 throw (ptree_bad_data(
"not space separated items in Config ", param));
264 template <
typename T>
267 auto s = getExt<string>(param);
268 istringstream iss(s);
270 for (
auto iit = istream_iterator<T>(iss)
271 ; iit != istream_iterator<T>()
273 to.emplace_back(*iit);
276 throw (ptree_bad_data(
"not space separated items in Config ", param));
290 unordered_set<string>
const& skipThese = unordered_set<string>())
const {
291 list<pair<string, string>> res;
292 unordered_set<string> history(skipThese);
293 for (
auto& p : *
this) {
294 if (history.find(p.first) == history.end()) {
295 history.insert(p.first);
296 if (p.second.empty()) {
297 res.push_back(make_pair(p.first, p.second.get_value<
string>()));
307 for (
auto& p : fallback_) {
308 if (history.find(p.first) == history.end()) {
309 history.insert(p.first);
310 if (p.second.empty()) {
311 res.push_back(make_pair(p.first, p.second.get_value<
string>()));
321 if (defaultUserConfig_) {
322 auto more = defaultUserConfig_->content(history);
323 res.insert(res.end(), more.begin(), more.end());
336 friend ostream& operator << (ostream& os,
Config const& cfg) {
337 for (
auto& r : cfg.
content()) {
338 os << r.first <<
'=' << r.second << endl;
363 std::shared_ptr<Config> defaultUserConfig_;
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:238
void setDefaultUserConfig(Config const &c)
internal use
Definition: Config.hpp:140
class to hold an hmbdc configuration
Definition: Config.hpp:46
T getExt(const path_type ¶m) const
get a value from the config
Definition: Config.hpp:180
Definition: TypedString.hpp:74
Config(istream &&is, char const *section=nullptr)
construct using stream, optionally specifying the section name
Definition: Config.hpp:65
T getHex(ptree::path_type const ¶m) const
get a number value in hex format
Definition: Config.hpp:206
Config(char const *json, char const *section=nullptr)
construct using a string, optionally specifying the section name
Definition: Config.hpp:100
Config const & operator()(T &to, const path_type ¶m) const
fill in a variable with a configured value retrieved using getExt
Definition: Config.hpp:223
ptree const & getChildExt(const path_type ¶m)
Gets the child from the config.
Definition: Config.hpp:154
Config(ptree const &t, ptree const §ion)
construct using a fallback ptree, and specify the section ptree
Definition: Config.hpp:129
Config(istream &is, char const *section=nullptr)
construct using stream, optionally specifying the section name
Definition: Config.hpp:82
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:112
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:289
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:265