LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
TableBase.cc
Go to the documentation of this file.
7 
8 #include <ostream>
9 
10 namespace fhicl::detail {
11 
13  Comment const& comment,
14  par_style const vt,
15  std::function<bool()> maybeUse)
16  : ParameterBase{name, comment, vt, par_type::TABLE, std::move(maybeUse)}
17  {}
18 
19  TableBase::~TableBase() = default;
20 
21  void
23  std::string const& tab) const
24  {
25  os << '\n' << tab << optional_parameter_message() << '\n';
26  PrintAllowedConfiguration pc{os, false, tab};
27  pc.walk_over(*this);
28  }
29 
30  std::vector<cet::exempt_ptr<ParameterBase>> const&
32  {
33  return members_;
34  }
35 
36  void
38  {
39  // This must be called in a derived class **after** the table's
40  // values has been set.
42  }
43 
44  void
46  {
47  bool const implicitly_default =
48  std::all_of(members_.begin(), members_.end(), [](auto p) {
49  return p->has_default() || p->is_optional();
50  });
51  if (implicitly_default)
53  }
54 
55  void
57  std::set<std::string> const& ignorable_keys)
58  {
59  pset_ = std::make_optional(pset);
60  ValidateThenSet vs{pset, ignorable_keys};
61  cet::for_all(members_, [&vs](auto m) { vs.walk_over(*m); });
62 
63  try {
64  vs.check_keys();
65  }
66  catch (detail::validationException const&) {
68  throw;
69  }
70  }
71 
72  bool
74  {
75  return pset_.has_value();
76  }
77 
78  ParameterSet const&
80  {
81  return pset_.value();
82  }
83 
84  ParameterSet const&
86  {
87  static ParameterSet const empty_pset;
88  return pset_.has_value() ? *pset_ : empty_pset;
89  }
90 
91  std::optional<ParameterSet>
93  {
94  // Kind of tricky: we do not have the name of the current
95  // parameter set. A placeholder is often used (e.g. "<top_level>").
96  // Fortunately, since the pset is passed in, we can just assign to
97  // it for a top-level ParameterSet. However, for nested parameter
98  // sets, we need to trim off the placeholder, and then the key we
99  // send pset.get<fhicl::ParameterSet>(key) is the key relative to
100  // the top-level pset.
101  std::string const& rkey = key();
102  std::string const& nkey = strip_first_containing_name(rkey);
103  if (nkey == rkey) {
104  return std::make_optional(pset);
105  }
106  return pset.get<ParameterSet>(nkey);
107  }
108 
109  std::optional<ParameterSet>
111  {
112  // Kind of tricky: we do not have the name of the current
113  // parameter set. A placeholder is often used
114  // (e.g. "<top_level>"). Fortunately, since the pset is passed
115  // in, we can just assign to it for a top-level ParameterSet.
116  // However, for nested parameter sets, we need to trim off the
117  // placeholder, and then the key we send
118  // pset.get<fhicl::ParameterSet>(key) is the key relative to the
119  // top-level pset.
120  std::string const& rkey = key();
121  std::string const& nkey = strip_first_containing_name(rkey);
122  if (nkey == rkey) {
123  return std::make_optional(pset);
124  }
125  return pset.get_if_present<ParameterSet>(nkey);
126  }
127 
128  void
130  {
131  if (is_optional()) {
132  pset_ = maybe_select_pset(pset);
133  } else {
134  pset_ = select_pset(pset);
135  }
136  }
137 
138  std::ostream&
139  operator<<(std::ostream& os, TableBase const& t)
140  {
141  std::ostringstream config;
142  t.print_allowed_configuration(config);
143  return os << config.str();
144  }
145 } // namespace fhicl::detail
ParameterSet const & get_pset() const
Definition: TableBase.cc:79
std::vector< cet::exempt_ptr< ParameterBase > > const & members() const
Definition: TableBase.cc:31
void validate(ParameterSet const &pset, std::set< std::string > const &ignorable_keys={})
Definition: TableBase.cc:56
std::string optional_parameter_message(bool const with_comment=true)
std::string strip_first_containing_name(std::string const &key)
void walk_over(tt::maybe_const_t< ParameterBase, C > &)
std::optional< ParameterSet > select_pset(ParameterSet const &pset) const
Definition: TableBase.cc:92
std::vector< cet::exempt_ptr< ParameterBase > > members_
Definition: TableBase.h:45
static std::vector< base_ptr > release_members()
T get(std::string const &key) const
Definition: ParameterSet.h:314
std::string const & name() const
std::optional< ParameterSet > maybe_select_pset(ParameterSet const &pset) const
Definition: TableBase.cc:110
std::string const & key() const
Definition: ParameterBase.cc:6
ParameterSet const & guarantee_pset() const
Definition: TableBase.cc:85
void print_allowed_configuration(std::ostream &os, std::string const &tab=std::string(3, ' ')) const
Definition: TableBase.cc:22
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:267
void do_set_value(ParameterSet const &pset) final
Definition: TableBase.cc:129
std::string const & comment() const
bool has_value() const
Definition: TableBase.cc:73
void set_par_style(par_style const vt)
std::optional< ParameterSet > pset_
Definition: TableBase.h:44
std::ostream & operator<<(std::ostream &os, TableBase const &t)
Definition: TableBase.cc:139
TableBase(Name const &name, Comment const &comment, par_style vt, std::function< bool()> maybeUse)
Definition: TableBase.cc:12