1 #include "hmbdc/Copyright.hpp" 7 #include <unordered_set> 9 #include <boost/property_tree/ptree.hpp> 10 #include <boost/property_tree/json_parser.hpp> 17 namespace hmbdc {
namespace app {
19 namespace config_detail {
60 Config(istream&& is,
char const* section =
nullptr) {
62 read_json(is, fallback_);
63 (ptree&)*
this = fallback_.get_child(section);
65 read_json(is, (ptree&)*
this);
77 Config(istream& is,
char const* section =
nullptr) {
79 read_json(is, fallback_);
80 (ptree&)*
this = fallback_.get_child(section);
82 read_json(is, (ptree&)*
this);
95 Config(
char const* json,
char const* section =
nullptr)
96 :
Config(istringstream(json), section)
107 Config(ptree
const& t,
char const* section =
nullptr) {
110 (ptree&)*
this = fallback_.get_child(section);
125 Config(ptree
const& t, ptree
const& section) {
127 (ptree&)*
this = section;
137 defaultUserConfig_.reset(
new Config(c));
150 template <
typename T>
152 auto res = get_optional<T>(param);
154 res = fallback_.get_optional<T>(param);
156 if (defaultUserConfig_) {
157 return defaultUserConfig_->getExt<T>(param);
159 throw ptree_bad_path(
"invalid param and no default user Config set", param);
176 template <
typename T>
177 T
getHex(ptree::path_type
const& param) {
178 istringstream iss(getExt<string>(param));
193 template <
typename T>
195 to = getExt<T>(param);
208 template <
typename T>
211 auto s = getExt<string>(param);
212 istringstream iss(s);
214 for (
auto iit = istream_iterator<T>(iss)
215 ; iit != istream_iterator<T>()
220 throw (ptree_bad_data(
"not space separated uint16_t's in Config", param));
234 unordered_set<string>
const& skipThese = unordered_set<string>())
const {
235 list<pair<string, string>> res;
236 unordered_set<string> history(skipThese);
237 for (
auto& p : *
this) {
238 if (history.find(p.first) == history.end()) {
239 history.insert(p.first);
240 if (p.second.empty()) {
241 res.push_back(make_pair(p.first, p.second.get_value<
string>()));
245 for (
auto& p : fallback_) {
246 if (history.find(p.first) == history.end()) {
247 history.insert(p.first);
248 if (p.second.empty()) {
249 res.push_back(make_pair(p.first, p.second.get_value<
string>()));
253 if (defaultUserConfig_) {
254 auto more = defaultUserConfig_->content(history);
255 res.insert(res.end(), more.begin(), more.end());
262 std::shared_ptr<Config> defaultUserConfig_;
void setDefaultUserConfig(Config const &c)
internal use
Definition: Config.hpp:136
class to hold an hmbdc configuration
Definition: Config.hpp:43
T getExt(const path_type ¶m) const
get a value from the config
Definition: Config.hpp:151
Definition: TypedString.hpp:74
Config(istream &&is, char const *section=nullptr)
construct using stream, optionally specifying the section name
Definition: Config.hpp:60
Config(char const *json, char const *section=nullptr)
construct using a string, optionally specifying the section name
Definition: Config.hpp:95
Config(ptree const &t, ptree const §ion)
construct using a fallback ptree, and specify the section ptree
Definition: Config.hpp:125
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:209
Config(istream &is, char const *section=nullptr)
construct using stream, optionally specifying the section name
Definition: Config.hpp:77
Config()
empty config
Definition: Config.hpp:49
Config(ptree const &t, char const *section=nullptr)
construct using another ptree as fallbacks, optionally specifying the section name ...
Definition: Config.hpp:107
T getHex(ptree::path_type const ¶m)
get a number value in hex format
Definition: Config.hpp:177
Config const & operator()(T &to, const path_type ¶m) const
fill in a variable with a configured value retrieved using getExt
Definition: Config.hpp:194
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:233