1 #include "hmbdc/Copyright.hpp" 8 #include <unordered_set> 10 #include <boost/property_tree/ptree.hpp> 11 #include <boost/property_tree/json_parser.hpp> 18 namespace hmbdc {
namespace app {
20 namespace config_detail {
47 using Base = boost::property_tree::ptree;
63 Config(istream&& is,
char const* section =
nullptr) {
65 read_json(is, fallback_);
66 (ptree&)*
this = fallback_.get_child(section);
68 read_json(is, (ptree&)*
this);
80 Config(istream& is,
char const* section =
nullptr) {
82 read_json(is, fallback_);
83 (ptree&)*
this = fallback_.get_child(section);
85 read_json(is, (ptree&)*
this);
98 Config(
char const* json,
char const* section =
nullptr)
99 :
Config(istringstream(json), section)
110 Config(ptree
const& t,
char const* section =
nullptr) {
113 (ptree&)*
this = fallback_.get_child(section);
128 Config(ptree
const& t, ptree
const& section) {
130 (ptree&)*
this = section;
140 defaultUserConfig_.reset(
new Config(c));
153 template <
typename T>
155 auto res = get_optional<T>(param);
157 res = fallback_.get_optional<T>(param);
159 if (defaultUserConfig_) {
160 return defaultUserConfig_->getExt<T>(param);
162 throw ptree_bad_path(
"invalid param and no default user Config set", param);
179 template <
typename T>
180 T
getHex(ptree::path_type
const& param) {
181 istringstream iss(getExt<string>(param));
196 template <
typename T>
198 to = getExt<T>(param);
211 template <
typename T>
214 auto s = getExt<string>(param);
215 istringstream iss(s);
217 for (
auto iit = istream_iterator<T>(iss)
218 ; iit != istream_iterator<T>()
223 throw (ptree_bad_data(
"not space separated uint16_t's in Config", param));
238 template <
typename T>
240 auto a = this->get_child_optional(param);
242 a = fallback_.get_child_optional(param);
245 if (defaultUserConfig_) {
246 return (*defaultUserConfig_)(to, param);
248 throw ptree_bad_path(
"invalid param and no default user Config set", param);
251 for (
auto const& v : *a) {
252 to.push_back(v.second.get_value<T>());
265 unordered_set<string>
const& skipThese = unordered_set<string>())
const {
266 list<pair<string, string>> res;
267 unordered_set<string> history(skipThese);
268 for (
auto& p : *
this) {
269 if (history.find(p.first) == history.end()) {
270 history.insert(p.first);
271 if (p.second.empty()) {
272 res.push_back(make_pair(p.first, p.second.get_value<
string>()));
276 for (
auto& p : fallback_) {
277 if (history.find(p.first) == history.end()) {
278 history.insert(p.first);
279 if (p.second.empty()) {
280 res.push_back(make_pair(p.first, p.second.get_value<
string>()));
284 if (defaultUserConfig_) {
285 auto more = defaultUserConfig_->content(history);
286 res.insert(res.end(), more.begin(), more.end());
293 std::shared_ptr<Config> defaultUserConfig_;
void setDefaultUserConfig(Config const &c)
internal use
Definition: Config.hpp:139
class to hold an hmbdc configuration
Definition: Config.hpp:44
T getExt(const path_type ¶m) const
get a value from the config
Definition: Config.hpp:154
Definition: TypedString.hpp:74
Config(istream &&is, char const *section=nullptr)
construct using stream, optionally specifying the section name
Definition: Config.hpp:63
Config(char const *json, char const *section=nullptr)
construct using a string, optionally specifying the section name
Definition: Config.hpp:98
Config(ptree const &t, ptree const §ion)
construct using a fallback ptree, and specify the section ptree
Definition: Config.hpp:128
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:239
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:212
Config(istream &is, char const *section=nullptr)
construct using stream, optionally specifying the section name
Definition: Config.hpp:80
Config()
empty config
Definition: Config.hpp:52
Config(ptree const &t, char const *section=nullptr)
construct using another ptree as fallbacks, optionally specifying the section name ...
Definition: Config.hpp:110
T getHex(ptree::path_type const ¶m)
get a number value in hex format
Definition: Config.hpp:180
Config const & operator()(T &to, const path_type ¶m) const
fill in a variable with a configured value retrieved using getExt
Definition: Config.hpp:197
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:264