LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
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;
47  std::string to_compact_string() const;
48 
49  std::string to_indented_string() const;
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>;
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);
117  void insert_or_replace_compatible_(std::string const& key,
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
149 fhicl::ParameterSet::is_key_to_table(std::string const& key) const
150 {
151  return key_is_type_(key, &detail::is_table);
152 }
153 
154 inline bool
155 fhicl::ParameterSet::is_key_to_sequence(std::string const& key) const
156 {
157  return key_is_type_(key, &detail::is_sequence);
158 }
159 
160 inline bool
161 fhicl::ParameterSet::is_key_to_atom(std::string const& key) const
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
181 fhicl::ParameterSet::put_or_replace(std::string const& key, T const& value)
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
208 fhicl::ParameterSet::get_if_present(std::string const& key, T& value) const
209 {
210  auto keys = detail::get_names(key);
211  ParameterSet ps;
212  return descend_(keys.tables(), ps) ? ps.get_one_(keys.last(), value) : false;
213 }
214 
215 template <class T, class Via>
216 bool
217 fhicl::ParameterSet::get_if_present(std::string const& key,
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
231 fhicl::ParameterSet::get(std::string const& key) const
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
258 fhicl::ParameterSet::get(std::string const& key,
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 stringify_(boost::any const &a, bool compact=false) const
Definition: ParameterSet.cc:67
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 &)
ps_atom_t encode(std::string const &)
Definition: coding.cc:87
void insert_or_replace_compatible_(std::string const &key, boost::any const &value)
Keys get_names(std::string const &key)
std::unordered_map< std::string, std::string > annot_t
Definition: ParameterSet.h:38
bool is_sequence(boost::any const &val)
Definition: coding.h:50
bool is_table(boost::any const &val)
Definition: coding.h:56
map_t::const_iterator map_iter_t
Definition: ParameterSet.h:108
bool is_key_to_sequence(std::string const &key) const
Definition: ParameterSet.h:155
std::vector< std::string > get_pset_names() const
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
intermediate_table::const_iterator const_iterator
std::map< std::string, boost::any > map_t
Definition: ParameterSet.h:107
parameter set interface
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 insert_(std::string const &key, boost::any const &value)
std::string to_indented_string() const
void decode(boost::any const &a, InputTag &tag)
Definition: InputTag.cc:64
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
void walk(ParameterSetWalker &psw) const
void insert_or_replace_(std::string const &key, boost::any const &value)
bool has_key(std::string const &key) const
std::string to_compact_string() const
Definition: ParameterSet.h:143
SequenceKey get_sequence_indices(std::string const &key)
std::vector< std::string > get_all_keys() const
fhicl::detail::ps_sequence_t ps_sequence_t
Definition: ParameterSet.h:37
ParameterSetID id() const
bool is_empty() const
Definition: ParameterSet.cc:97
bool key_is_type_(std::string const &key, std::function< bool(boost::any const &)> func) const
std::string value(boost::any const &)
bool descend_(std::vector< std::string > const &names, ParameterSet &ps) const
std::string get_src_info(std::string const &key) const
std::vector< std::string > get_names() const
bool is_key_to_atom(std::string const &key) const
Definition: ParameterSet.h:161
bool erase(std::string const &key)
ParameterSetID id_
Definition: ParameterSet.h:112
bool find_one_(std::string const &key) const
Float_t e
Definition: plot.C:34
void put_or_replace(std::string const &key)
std::string ps_atom_t
Definition: coding.h:45
void put(std::string const &key)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< boost::any > ps_sequence_t
Definition: coding.h:46
std::string to_string_(bool compact=false) const
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