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"
11 OptionalTable<T>::OptionalTable(Name&& name)
12 : OptionalTable{std::move(name), Comment("")}
16 OptionalTable<T>::OptionalTable(Name&& name,
18 : TableBase{std::move(name),std::move(comment), par_style::OPTIONAL, detail::AlwaysUse()}
19 , RegisterIfTableMember{this}
21 NameStackRegistry::end_of_ctor();
25 OptionalTable<T>::OptionalTable(Name&& name,
27 std::function<bool()> maybeUse)
28 : TableBase{std::move(name),std::move(comment), par_style::OPTIONAL_CONDITIONAL, maybeUse}
29 , RegisterIfTableMember{this}
31 NameStackRegistry::end_of_ctor();
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}
40 validate_ParameterSet( pset, keysToIgnore );
41 NameStackRegistry::end_of_ctor();
46 OptionalTable<T>::validate_ParameterSet(ParameterSet const& pset,
47 std::set<std::string> const & keysToIgnore)
50 detail::ValidateThenSet vs{pset_, keysToIgnore};
51 cet::for_all(members(), [&vs](auto m){ vs(m.get()); });
57 OptionalTable<T>::print_allowed_configuration(std::ostream& os,
58 std::string const& tab) const
60 os << '\n' << tab << detail::optional_parameter_message() << '\n';
61 detail::PrintAllowedConfiguration pc{os};
67 OptionalTable<T>::do_set_value(fhicl::ParameterSet const& pset, bool const /*trimParent*/)
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
77 std::string const& rkey = key();
78 std::string const& nkey = detail::strip_first_containing_name(rkey);
84 has_value_ = pset.get_if_present<fhicl::ParameterSet>(nkey, pset_);