Table.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_types_Table_h
2 #define fhiclcpp_types_Table_h
3 
5 #include "fhiclcpp/type_traits.h"
9 #include "fhiclcpp/types/Name.h"
16 
17 #include <memory>
18 #include <ostream>
19 #include <set>
20 #include <string>
21 
22 namespace fhicl {
23 
24  //========================================================
25  template <typename T, typename KeysToIgnore = void>
26  class Table final : public detail::TableBase,
27  private detail::RegisterIfTableMember {
28  public:
29  static_assert(!tt::is_sequence_type_v<T>, NO_STD_CONTAINERS);
30  static_assert(!tt::is_fhicl_type_v<T>, NO_NESTED_FHICL_TYPES_IN_TABLE);
31  static_assert(!tt::is_table_fragment_v<T>, NO_NESTED_TABLE_FRAGMENTS);
32  static_assert(!tt::is_delegated_parameter_v<T>, NO_DELEGATED_PARAMETERS);
33 
34  //=====================================================
35  // User-friendly
36  // ... c'tors
37  template <typename... TCARGS>
38  explicit Table(Name&& name, TCARGS&&... tcargs);
39  template <typename... TCARGS>
40  explicit Table(Name&& name, Comment&& comment, TCARGS&&... tcargs);
41  template <typename... TCARGS>
42  explicit Table(Name&& name,
43  Comment&& comment,
44  MaybeUseFunction maybeUse,
45  TCARGS&&... tcargs);
46 
47  // Choose this c'tor if user specifies the second 'KeysToIgnore' template
48  // argument.
49  template <typename K = KeysToIgnore,
51  Table(ParameterSet const& pset);
52 
53  // If user does not specify the second 'KeysToIgnore' template argument,
54  // then choose this c'tor.
55  template <typename K = KeysToIgnore,
57  Table(ParameterSet const& pset,
58  std::set<std::string> const& keysToIgnore = {});
59 
60  // ... Accessors
61  auto const&
62  operator()() const
63  {
64  return *value_;
65  }
66 
67  ParameterSet const&
68  get_PSet() const
69  {
70  return pset_;
71  }
72 
73  void validate_ParameterSet(ParameterSet const& pset,
74  std::set<std::string> const& keysToIgnore = {});
75 
77  std::ostream& os,
78  std::string const& tab = std::string(3, ' ')) const;
79 
80  //=====================================================
81  // Expert-only
82  using default_type = T;
83  using value_type = T;
84 
85  private:
86  using members_t = std::vector<cet::exempt_ptr<ParameterBase>>;
87 
88  std::shared_ptr<T> value_{std::make_shared<T>()};
92 
93  struct Impl {};
94  Table(ParameterSet const&, std::set<std::string> const&, Impl);
96 
97  members_t const&
98  get_members() const override
99  {
100  return members_;
101  }
102  void do_set_value(fhicl::ParameterSet const& pset,
103  bool const trimParents) override;
104  };
105 
106  template <typename T>
107  inline std::ostream&
108  operator<<(std::ostream& os, Table<T> const& t)
109  {
110  std::ostringstream config;
111  t.print_allowed_configuration(config);
112  return os << config.str();
113  }
114 }
115 
116 // Implementation
117 
125 
126 namespace fhicl {
127 
128  template <typename T, typename KeysToIgnore>
129  template <typename... TCARGS>
130  Table<T, KeysToIgnore>::Table(Name&& name, TCARGS&&... tcargs)
131  : Table{std::move(name), Comment(""), std::forward<TCARGS>(tcargs)...}
132  {}
133 
134  template <typename T, typename KeysToIgnore>
135  template <typename... TCARGS>
137  Comment&& comment,
138  TCARGS&&... tcargs)
143  , RegisterIfTableMember{this}
144  , value_{std::make_shared<T>(std::forward<TCARGS>(tcargs)...)}
145  {
148  }
149 
150  template <typename T, typename KeysToIgnore>
151  template <typename... TCARGS>
153  Comment&& comment,
154  MaybeUseFunction maybeUse,
155  TCARGS&&... tcargs)
159  maybeUse}
160  , RegisterIfTableMember{this}
161  , value_{std::make_shared<T>(std::forward<TCARGS>(tcargs)...)}
162  {
165  }
166 
167  template <typename T, typename KeysToIgnore>
168  template <typename, typename>
170  : Table{pset, KeysToIgnore{}(), Impl{}}
171  {}
172 
173  template <typename T, typename KeysToIgnore>
174  template <typename, typename>
176  std::set<std::string> const& keysToIgnore)
177  : Table{pset, keysToIgnore, Impl{}}
178  {}
179 
180  template <typename T, typename KeysToIgnore>
182  std::set<std::string> const& keysToIgnore,
183  Impl)
184  : TableBase{Name("<top_level>"),
185  Comment(""),
188  , RegisterIfTableMember{this}
189  {
191  validate_ParameterSet(pset, keysToIgnore);
193  }
194 
195  template <typename T, typename KeysToIgnore>
196  void
198  ParameterSet const& pset,
199  std::set<std::string> const& keysToIgnore)
200  {
201  pset_ = pset;
202  detail::ValidateThenSet vs{pset_, keysToIgnore};
203  cet::for_all(members(), [&vs](auto m) { vs.walk_over(*m); });
204 
205  try {
206  vs.check_keys();
207  }
208  catch (fhicl::detail::validationException const&) {
210  throw;
211  }
212  }
213 
214  template <typename T, typename KeysToIgnore>
215  void
217  std::ostream& os,
218  std::string const& tab) const
219  {
220  os << '\n' << tab << detail::optional_parameter_message() << '\n';
221  detail::PrintAllowedConfiguration pc{os, false, tab};
222  pc.walk_over(*this);
223  }
224 
225  template <typename T, typename KeysToIgnore>
226  void
228  bool const /*trimParent*/)
229  {
230  // Kind of tricky: we do not have the name of the current
231  // parameter set. A placeholder is often used (e.g. "<top_level>").
232  // Fortunately, since the pset is passed in, we can just assign to
233  // it for a top-level ParameterSet. However, for nested parameter
234  // sets, we need to trim off the placeholder, and then the key we
235  // send pset.get<fhicl::ParameterSet>(key) is the key relative to
236  // the top-level pset.
237  std::string const& rkey = key();
239  pset_ = (nkey == rkey) ? pset : pset.get<fhicl::ParameterSet>(nkey);
240  }
241 
242  template <typename T, typename KeysToIgnore>
243  void
245  {
246  bool const implicitly_default =
247  std::all_of(members_.begin(), members_.end(), [](auto p) {
248  return p->has_default() || p->is_optional();
249  });
250  if (implicitly_default)
252  }
253 }
254 
255 #endif /* fhiclcpp_types_Table_h */
256 
257 // Local variables:
258 // mode: c++
259 // End:
void print_allowed_configuration(std::ostream &os, std::string const &tab=std::string(3, ' ')) const
Definition: Table.h:216
static const double m
Definition: Units.h:79
std::string const & name() const
Definition: ParameterBase.h:49
static NameStackRegistry & instance()
trkf::TrackStatePropagator::trkmkr::KalmanFilterFitTrackMaker::Config default_type
Definition: Table.h:82
#define NO_DELEGATED_PARAMETERS
std::string string
Definition: nybbler.cc:12
trkf::TrackStatePropagator::trkmkr::KalmanFilterFitTrackMaker::Config value_type
Definition: Table.h:83
ParameterSet const & get_PSet() const
Definition: Table.h:68
ParameterSet pset_
Definition: Table.h:89
std::function< bool()> AlwaysUse()
void validate_ParameterSet(ParameterSet const &pset, std::set< std::string > const &keysToIgnore={})
Definition: Table.h:197
#define NO_NESTED_FHICL_TYPES_IN_TABLE
std::string optional_parameter_message(bool const with_comment=true)
static TableMemberRegistry & instance()
std::string strip_first_containing_name(std::string const &key)
Table(Name &&name, TCARGS &&...tcargs)
Definition: Table.h:130
static Config * config
Definition: config.cpp:1054
members_t members_
Definition: Table.h:90
parameter set interface
std::vector< cet::exempt_ptr< ParameterBase > > const & members() const
Definition: TableBase.h:24
def move(depos, offset)
Definition: depos.py:107
T get(std::string const &key) const
Definition: ParameterSet.h:231
#define NO_STD_CONTAINERS
void do_set_value(fhicl::ParameterSet const &pset, bool const trimParents) override
Definition: Table.h:227
#define NO_NESTED_TABLE_FRAGMENTS
std::vector< base_ptr > release_members()
std::string const & key() const
Definition: ParameterBase.h:44
p
Definition: test.py:223
auto const & operator()() const
Definition: Table.h:62
AdcCodeMitigator::Name Name
#define Comment
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
members_t const & get_members() const override
Definition: Table.h:98
auto for_all(FwdCont &, Func)
std::shared_ptr< T > value_
Definition: Table.h:88
void maybe_implicitly_default()
Definition: Table.h:244
std::string const & comment() const
Definition: ParameterBase.h:54
TableBase(Name const &name, Comment const &comment, par_style const vt, std::function< bool()> maybeUse)
Definition: TableBase.h:16
void set_par_style(par_style const vt)