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 #include "fhiclcpp/types/detail/validationException.h"
11 template <typename T, typename KeysToIgnore>
12 template <typename... TCARGS>
13 Table<T, KeysToIgnore>::Table(Name&& name, TCARGS && ... tcargs)
14 : Table{std::move(name), Comment(""), std::forward<TCARGS>(tcargs)...} {}
16 template <typename T, typename KeysToIgnore>
17 template <typename... TCARGS>
18 Table<T, KeysToIgnore>::Table(Name&& name,
21 : TableBase{std::move(name), std::move(comment), par_style::REQUIRED, detail::AlwaysUse()}
22 , RegisterIfTableMember{this}
23 , value_{std::make_shared<T>(std::forward<TCARGS>(tcargs)...)}
25 maybe_implicitly_default();
26 NameStackRegistry::end_of_ctor();
29 template <typename T, typename KeysToIgnore>
30 template <typename... TCARGS>
31 Table<T, KeysToIgnore>::Table(Name&& name,
33 MaybeUseFunction maybeUse,
35 : TableBase{std::move(name), std::move(comment), par_style::REQUIRED_CONDITIONAL, maybeUse}
36 , RegisterIfTableMember{this}
37 , value_{std::make_shared<T>(std::forward<TCARGS>(tcargs)...)}
39 maybe_implicitly_default();
40 NameStackRegistry::end_of_ctor();
44 template <typename T, typename KeysToIgnore>
45 template <typename, typename>
46 Table<T, KeysToIgnore>::Table(ParameterSet const& pset)
47 : Table{pset, KeysToIgnore{}(), Impl{}}
50 template <typename T, typename KeysToIgnore>
51 template <typename, typename>
52 Table<T, KeysToIgnore>::Table(ParameterSet const& pset, std::set<std::string> const& keysToIgnore)
53 : Table{pset, keysToIgnore, Impl{}}
56 template <typename T, typename KeysToIgnore>
57 Table<T, KeysToIgnore>::Table(ParameterSet const& pset, std::set<std::string> const& keysToIgnore, Impl)
58 : TableBase{Name("<top_level>"), Comment(""), par_style::REQUIRED, detail::AlwaysUse()}
59 , RegisterIfTableMember{this}
61 maybe_implicitly_default();
62 validate_ParameterSet(pset, keysToIgnore);
63 NameStackRegistry::end_of_ctor();
66 template <typename T, typename KeysToIgnore>
68 Table<T, KeysToIgnore>::validate_ParameterSet(ParameterSet const& pset, std::set<std::string> const& keysToIgnore)
71 detail::ValidateThenSet vs {pset_, keysToIgnore};
72 cet::for_all(members(), [&vs](auto m){ vs.walk_over(*m); });
77 catch(fhicl::detail::validationException const&) {
78 NameStackRegistry::instance().clear();
83 template <typename T, typename KeysToIgnore>
85 Table<T, KeysToIgnore>::print_allowed_configuration(std::ostream& os,
86 std::string const& tab) const
88 os << '\n' << tab << detail::optional_parameter_message() << '\n';
89 detail::PrintAllowedConfiguration pc {os, false, tab};
93 template <typename T, typename KeysToIgnore>
95 Table<T, KeysToIgnore>::do_set_value(fhicl::ParameterSet const& pset, bool const /*trimParent*/)
97 // Kind of tricky: we do not have the name of the current
98 // parameter set. A placeholder is often used (e.g. "<top_level>").
99 // Fortunately, since the pset is passed in, we can just assign to
100 // it for a top-level ParameterSet. However, for nested parameter
101 // sets, we need to trim off the placeholder, and then the key we
102 // send pset.get<fhicl::ParameterSet>(key) is the key relative to
103 // the top-level pset.
104 std::string const& rkey = key();
105 std::string const& nkey = detail::strip_first_containing_name(rkey);
106 pset_ = (nkey == rkey) ? pset : pset.get<fhicl::ParameterSet>(nkey);
109 template <typename T, typename KeysToIgnore>
111 Table<T, KeysToIgnore>::maybe_implicitly_default()
113 bool const implicitly_default = std::all_of(members_.begin(),
116 return p->has_default() || p->is_optional();
118 if (implicitly_default)
119 set_par_style(par_style::DEFAULT);