ParameterSet.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_ParameterSet_h
2 #define fhiclcpp_ParameterSet_h
3 
4 // ======================================================================
5 //
6 // ParameterSet
7 //
8 // ======================================================================
9 
10 #include "boost/any.hpp"
11 #include "boost/lexical_cast.hpp"
12 #include "boost/numeric/conversion/cast.hpp"
13 #include "cetlib_except/demangle.h"
15 #include "fhiclcpp/coding.h"
20 #include "fhiclcpp/exception.h"
22 #include "fhiclcpp/fwd.h"
23 
24 #include <cctype>
25 #include <map>
26 #include <sstream>
27 #include <string>
28 #include <typeinfo>
29 #include <unordered_map>
30 #include <vector>
31 
32 // ----------------------------------------------------------------------
33 
35 public:
38  using annot_t = std::unordered_map<std::string, std::string>;
39 
40  // compiler generates default c'tor, d'tor, copy c'tor, copy assignment
41 
42  // observers:
43  bool is_empty() const;
44  ParameterSetID id() const;
45 
46  std::string to_string() const;
48 
50  std::string to_indented_string(unsigned initial_indent_level) const;
51  std::string to_indented_string(unsigned initial_indent_level,
52  bool annotate) const;
53  std::string to_indented_string(unsigned initial_indent_level,
54  detail::print_mode pm) const;
55 
56  std::vector<std::string> get_names() const;
57  std::vector<std::string> get_pset_names() const;
58  std::vector<std::string> get_all_keys() const;
59 
60  // retrievers (nested key OK):
61  bool has_key(std::string const& key) const;
62  bool is_key_to_table(std::string const& key) const;
63  bool is_key_to_sequence(std::string const& key) const;
64  bool is_key_to_atom(std::string const& key) const;
65 
66  template <class T>
67  bool get_if_present(std::string const& key, T& value) const;
68  template <class T, class Via>
69  bool get_if_present(std::string const& key,
70  T& value,
71  T convert(Via const&)) const;
72 
73  template <class T>
74  T get(std::string const& key) const;
75  template <class T, class Via>
76  T get(std::string const& key, T convert(Via const&)) const;
77  template <class T>
78  T get(std::string const& key, T const& default_value) const;
79  template <class T, class Via>
80  T get(std::string const& key,
81  T const& default_value,
82  T convert(Via const&)) const;
83 
84  std::string get_src_info(std::string const& key) const;
85 
86  // Facility to traverse the ParameterSet tree
87  void walk(ParameterSetWalker& psw) const;
88 
89  // inserters (key must be local: no nesting):
90  void put(std::string const& key); // Implicit nil value.
91  template <class T> // Fail on preexisting key.
92  void put(std::string const& key, T const& value);
93  void put_or_replace(std::string const& key); // Implicit nil value.
94  template <class T> // Succeed.
95  void put_or_replace(std::string const& key, T const& value);
96  template <class T> // Fail if preexisting key of incompatible type.
97  void put_or_replace_compatible(std::string const& key, T const& value);
98 
99  // deleters:
100  bool erase(std::string const& key);
101 
102  // comparators:
103  bool operator==(ParameterSet const& other) const;
104  bool operator!=(ParameterSet const& other) const;
105 
106 private:
107  using map_t = std::map<std::string, boost::any>;
108  using map_iter_t = map_t::const_iterator;
109 
113 
114  // Private inserters.
115  void insert_(std::string const& key, boost::any const& value);
116  void insert_or_replace_(std::string const& key, boost::any const& value);
118  boost::any const& value);
119 
120  std::string to_string_(bool compact = false) const;
121  std::string stringify_(boost::any const& a, bool compact = false) const;
122 
123  bool key_is_type_(std::string const& key,
124  std::function<bool(boost::any const&)> func) const;
125 
126  // Local retrieval only.
127  template <class T>
128  bool get_one_(std::string const& key, T& value) const;
129  bool find_one_(std::string const& key) const;
130  bool descend_(std::vector<std::string> const& names, ParameterSet& ps) const;
131 
132 }; // ParameterSet
133 
134 // ======================================================================
135 
136 inline std::string
138 {
139  return to_string_();
140 }
141 
142 inline std::string
144 {
145  return to_string_(true);
146 }
147 
148 inline bool
150 {
151  return key_is_type_(key, &detail::is_table);
152 }
153 
154 inline bool
156 {
157  return key_is_type_(key, &detail::is_sequence);
158 }
159 
160 inline bool
162 {
163  return key_is_type_(key, [](boost::any const& a) {
164  return !(detail::is_sequence(a) || detail::is_table(a));
165  });
166 }
167 
168 template <class T>
169 void
170 fhicl::ParameterSet::put(std::string const& key, T const& value)
171 {
172  auto insert = [this, &value](auto const& key) {
173  using detail::encode;
174  this->insert_(key, boost::any(encode(value)));
175  };
176  detail::try_insert(insert, key);
177 }
178 
179 template <class T>
180 void
182 {
183  auto insert_or_replace = [this, &value](auto const& key) {
184  using detail::encode;
185  this->insert_or_replace_(key, boost::any(encode(value)));
186  srcMapping_.erase(key);
187  };
188  detail::try_insert(insert_or_replace, key);
189 }
190 
191 template <class T>
192 void
194  T const& value)
195 {
196  auto insert_or_replace_compatible = [this, &value](auto const& key) {
197  using detail::encode;
198  this->insert_or_replace_compatible_(key, boost::any(encode(value)));
199  srcMapping_.erase(key);
200  };
201  detail::try_insert(insert_or_replace_compatible, key);
202 }
203 
204 // ----------------------------------------------------------------------
205 
206 template <class T>
207 bool
209 {
210  auto keys = detail::get_names(key);
212  return descend_(keys.tables(), ps) ? ps.get_one_(keys.last(), value) : false;
213 }
214 
215 template <class T, class Via>
216 bool
218  T& result,
219  T convert(Via const&)) const
220 {
221  Via go_between;
222  if (!get_if_present(key, go_between)) {
223  return false;
224  }
225  result = convert(go_between);
226  return true;
227 } // get_if_present<>()
228 
229 template <class T>
230 T
232 {
233  T result;
234  return get_if_present(key, result) ? result :
235  throw fhicl::exception(cant_find, key);
236 }
237 
238 template <class T, class Via>
239 T
240 fhicl::ParameterSet::get(std::string const& key, T convert(Via const&)) const
241 {
242  T result;
243  return get_if_present(key, result, convert) ?
244  result :
245  throw fhicl::exception(cant_find, key);
246 }
247 
248 template <class T>
249 T
250 fhicl::ParameterSet::get(std::string const& key, T const& default_value) const
251 {
252  T result;
253  return get_if_present(key, result) ? result : default_value;
254 }
255 
256 template <class T, class Via>
257 T
259  T const& default_value,
260  T convert(Via const&)) const
261 {
262  T result;
263  return get_if_present(key, result, convert) ? result : default_value;
264 }
265 
266 // ----------------------------------------------------------------------
267 
268 inline bool
270 {
271  return id() == other.id();
272 }
273 
274 inline bool
276 {
277  return !operator==(other);
278 }
279 
280 // ----------------------------------------------------------------------
281 
282 template <class T>
283 bool
284 fhicl::ParameterSet::get_one_(std::string const& key, T& value) const try {
285  auto skey = detail::get_sequence_indices(key);
286 
287  map_iter_t it = mapping_.find(skey.name());
288  if (it == mapping_.end()) {
289  return false;
290  }
291 
292  auto a = it->second;
293  if (!detail::find_an_any(skey.indices().cbegin(), skey.indices().cend(), a)) {
295  }
296 
297  using detail::decode;
298  decode(a, value);
299  return true;
300 }
301 catch (fhicl::exception const& e) {
302  std::ostringstream errmsg;
303  errmsg << "\nUnsuccessful attempt to convert FHiCL parameter '" << key
304  << "' to type '" << cet::demangle_symbol(typeid(value).name())
305  << "'.\n\n"
306  << "[Specific error:]";
307  throw fhicl::exception(type_mismatch, errmsg.str(), e);
308 }
309 catch (std::exception const& e) {
310  std::ostringstream errmsg;
311  errmsg << "\nUnsuccessful attempt to convert FHiCL parameter '" << key
312  << "' to type '" << cet::demangle_symbol(typeid(value).name())
313  << "'.\n\n"
314  << "[Specific error:]\n"
315  << e.what() << "\n\n";
316  throw fhicl::exception(type_mismatch, errmsg.str());
317 }
318 
319 // ----------------------------------------------------------------------
320 
321 namespace fhicl {
322  template <>
323  void ParameterSet::put(std::string const& key,
324  fhicl::extended_value const& value);
325 }
326 
327 // ======================================================================
328 
329 #endif /* fhiclcpp_ParameterSet_h */
330 
331 // Local Variables:
332 // mode: c++
333 // End:
std::string to_string_(bool compact=false) const
const XML_Char * name
Definition: expat.h:151
bool operator!=(ParameterSet const &other) const
Definition: ParameterSet.h:275
bool get_one_(std::string const &key, T &value) const
Definition: ParameterSet.h:284
void decode(boost::any const &, std::string &)
void insert_or_replace_(std::string const &key, boost::any const &value)
keys
Reco plots.
Definition: caf_analysis.py:46
set< int >::iterator it
bool erase(std::string const &key)
void put(std::string const &key)
ps_atom_t encode(std::string const &)
bool is_empty() const
void put_or_replace(std::string const &key)
std::string get_src_info(std::string const &key) const
Keys get_names(std::string const &key)
std::unordered_map< std::string, std::string > annot_t
Definition: ParameterSet.h:38
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
bool is_sequence(boost::any const &val)
Definition: coding.h:50
bool is_table(boost::any const &val)
Definition: coding.h:56
ParameterSetID id() const
map_t::const_iterator map_iter_t
Definition: ParameterSet.h:108
void convert(std::string dir="cc_numu/C12")
Definition: convert.C:107
bool descend_(std::vector< std::string > const &names, ParameterSet &ps) const
std::vector< std::string > get_all_keys() const
void walk(ParameterSetWalker &psw) const
bool is_key_to_sequence(std::string const &key) const
Definition: ParameterSet.h:155
void try_insert(L l, std::string const &key)
Definition: try_blocks.h:13
bool is_key_to_table(std::string const &key) const
Definition: ParameterSet.h:149
void insert_or_replace_compatible_(std::string const &key, boost::any const &value)
std::string stringify_(boost::any const &a, bool compact=false) const
const XML_Char int const XML_Char * value
Definition: expat.h:331
std::map< std::string, boost::any > map_t
Definition: ParameterSet.h:107
const double a
T get(std::string const &key) const
Definition: ParameterSet.h:231
bool operator==(ParameterSet const &other) const
Definition: ParameterSet.h:269
void put_or_replace_compatible(std::string const &key, T const &value)
Definition: ParameterSet.h:193
void decode(boost::any const &a, InputTag &tag)
fhicl::detail::ps_atom_t ps_atom_t
Definition: ParameterSet.h:36
bool get_if_present(std::string const &key, T &value) const
Definition: ParameterSet.h:208
double func(double x, double y)
std::vector< std::string > get_names() const
std::string to_compact_string() const
Definition: ParameterSet.h:143
std::vector< std::string > get_pset_names() const
SequenceKey get_sequence_indices(std::string const &key)
fhicl::detail::ps_sequence_t ps_sequence_t
Definition: ParameterSet.h:37
std::string to_indented_string() const
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
bool has_key(std::string const &key) const
bool is_key_to_atom(std::string const &key) const
Definition: ParameterSet.h:161
double T
Definition: Xdiff_gwt.C:5
ParameterSetID id_
Definition: ParameterSet.h:112
bool find_one_(std::string const &key) const
Float_t e
Definition: plot.C:35
std::string ps_atom_t
Definition: coding.h:45
void insert_(std::string const &key, boost::any const &value)
bool key_is_type_(std::string const &key, std::function< bool(boost::any const &)> func) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< boost::any > ps_sequence_t
Definition: coding.h:46
bool find_an_any(cit_size_t it, cit_size_t const cend, boost::any &a)
std::string to_string() const
Definition: ParameterSet.h:137