LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
OptionalTable.icc
Go to the documentation of this file.
1 #include "cetlib/container_algorithms.h"
2 #include "fhiclcpp/detail/printing_helpers.h"
3 #include "fhiclcpp/types/detail/PrintAllowedConfiguration.h"
4 #include "fhiclcpp/types/detail/ValidateThenSet.h"
5 #include "fhiclcpp/types/detail/optional_parameter_message.h"
6 #include "fhiclcpp/types/detail/strip_containing_names.h"
7 
8 namespace fhicl {
9 
10  template <typename T>
11  OptionalTable<T>::OptionalTable(Name&& name)
12  : OptionalTable{std::move(name), Comment("")}
13  {}
14 
15  template <typename T>
16  OptionalTable<T>::OptionalTable(Name&& name,
17  Comment&& comment)
18  : TableBase{std::move(name),std::move(comment), par_style::OPTIONAL, detail::AlwaysUse()}
19  , RegisterIfTableMember{this}
20  {
21  NameStackRegistry::end_of_ctor();
22  }
23 
24  template <typename T>
25  OptionalTable<T>::OptionalTable(Name&& name,
26  Comment&& comment,
27  std::function<bool()> maybeUse)
28  : TableBase{std::move(name),std::move(comment), par_style::OPTIONAL_CONDITIONAL, maybeUse}
29  , RegisterIfTableMember{this}
30  {
31  NameStackRegistry::end_of_ctor();
32  }
33 
34  template <typename T>
35  OptionalTable<T>::OptionalTable(ParameterSet const& pset,
36  std::set<std::string> const & keysToIgnore)
37  : TableBase{Name("<top_level>"), Comment(""), par_style::OPTIONAL, detail::AlwaysUse()}
38  , RegisterIfTableMember{this}
39  {
40  validate_ParameterSet( pset, keysToIgnore );
41  NameStackRegistry::end_of_ctor();
42  }
43 
44  template <typename T>
45  void
46  OptionalTable<T>::validate_ParameterSet(ParameterSet const& pset,
47  std::set<std::string> const & keysToIgnore)
48  {
49  pset_ = pset;
50  detail::ValidateThenSet vs{pset_, keysToIgnore};
51  cet::for_all(members(), [&vs](auto m){ vs(m.get()); });
52  vs.check_keys();
53  }
54 
55  template <typename T>
56  void
57  OptionalTable<T>::print_allowed_configuration(std::ostream& os,
58  std::string const& tab) const
59  {
60  os << '\n' << tab << detail::optional_parameter_message() << '\n';
61  detail::PrintAllowedConfiguration pc{os};
62  pc.walk_over(*this);
63  }
64 
65  template <typename T>
66  void
67  OptionalTable<T>::do_set_value(fhicl::ParameterSet const& pset, bool const /*trimParent*/)
68  {
69  // Kind of tricky: we do not have the name of the current
70  // parameter set. A placeholder is often used
71  // (e.g. "<top_level>"). Fortunately, since the pset is passed
72  // in, we can just assign to it for a top-level ParameterSet.
73  // However, for nested parameter sets, we need to trim off the
74  // placeholder, and then the key we send
75  // pset.get<fhicl::ParameterSet>(key) is the key relative to the
76  // top-level pset.
77  std::string const& rkey = key();
78  std::string const& nkey = detail::strip_first_containing_name(rkey);
79  if (nkey == rkey) {
80  pset_ = pset;
81  has_value_ = true;
82  }
83  else {
84  has_value_ = pset.get_if_present<fhicl::ParameterSet>(nkey, pset_);
85  }
86  }
87 
88 }
89 
90 // Local variables:
91 // mode: c++
92 // End: