LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
Table.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 #include "fhiclcpp/types/detail/validationException.h"
8 
9 namespace fhicl {
10 
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)...} {}
15 
16  template <typename T, typename KeysToIgnore>
17  template <typename... TCARGS>
18  Table<T, KeysToIgnore>::Table(Name&& name,
19  Comment&& comment,
20  TCARGS && ... tcargs)
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)...)}
24  {
25  maybe_implicitly_default();
26  NameStackRegistry::end_of_ctor();
27  }
28 
29  template <typename T, typename KeysToIgnore>
30  template <typename... TCARGS>
31  Table<T, KeysToIgnore>::Table(Name&& name,
32  Comment&& comment,
33  MaybeUseFunction maybeUse,
34  TCARGS && ... tcargs)
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)...)}
38  {
39  maybe_implicitly_default();
40  NameStackRegistry::end_of_ctor();
41  }
42 
43 
44  template <typename T, typename KeysToIgnore>
45  template <typename, typename>
46  Table<T, KeysToIgnore>::Table(ParameterSet const& pset)
47  : Table{pset, KeysToIgnore{}(), Impl{}}
48  {}
49 
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{}}
54  {}
55 
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}
60  {
61  maybe_implicitly_default();
62  validate_ParameterSet(pset, keysToIgnore);
63  NameStackRegistry::end_of_ctor();
64  }
65 
66  template <typename T, typename KeysToIgnore>
67  void
68  Table<T, KeysToIgnore>::validate_ParameterSet(ParameterSet const& pset, std::set<std::string> const& keysToIgnore)
69  {
70  pset_ = pset;
71  detail::ValidateThenSet vs {pset_, keysToIgnore};
72  cet::for_all(members(), [&vs](auto m){ vs.walk_over(*m); });
73 
74  try {
75  vs.check_keys();
76  }
77  catch(fhicl::detail::validationException const&) {
78  NameStackRegistry::instance().clear();
79  throw;
80  }
81  }
82 
83  template <typename T, typename KeysToIgnore>
84  void
85  Table<T, KeysToIgnore>::print_allowed_configuration(std::ostream& os,
86  std::string const& tab) const
87  {
88  os << '\n' << tab << detail::optional_parameter_message() << '\n';
89  detail::PrintAllowedConfiguration pc {os, false, tab};
90  pc.walk_over(*this);
91  }
92 
93  template <typename T, typename KeysToIgnore>
94  void
95  Table<T, KeysToIgnore>::do_set_value(fhicl::ParameterSet const& pset, bool const /*trimParent*/)
96  {
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);
107  }
108 
109  template <typename T, typename KeysToIgnore>
110  void
111  Table<T, KeysToIgnore>::maybe_implicitly_default()
112  {
113  bool const implicitly_default = std::all_of(members_.begin(),
114  members_.end(),
115  [](auto p){
116  return p->has_default() || p->is_optional();
117  });
118  if (implicitly_default)
119  set_par_style(par_style::DEFAULT);
120  }
121 
122 }
123 
124 // Local variables:
125 // mode: c++
126 // End: