LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
Sequence.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_types_Sequence_h
2 #define fhiclcpp_types_Sequence_h
3 
4 #include "cetlib/container_algorithms.h"
5 #include "fhiclcpp/type_traits.h"
6 #include "fhiclcpp/types/Atom.h"
14 
15 #include <array>
16 #include <initializer_list>
17 #include <memory>
18 #include <string>
19 #include <type_traits>
20 
21 namespace fhicl {
22 
23  namespace sequence_detail {
24 
25  // Auxiliary struct for accepting either
26  //
27  // (1) {1, 3, 5} or
28  // (2) std::array<int,3>{2, 4, 6}
29  //
30  // default values for Sequence<T,N>
31 
32  template <typename T>
33  class ValueHolder {
34  public:
35  ValueHolder(std::initializer_list<T> list) : holder_{list} {}
36 
37  template <std::size_t N>
38  ValueHolder(std::array<T, N> const& array)
39  : holder_(array.cbegin(), array.cend())
40  {}
41 
42  auto
43  begin() const
44  {
45  return holder_.cbegin();
46  }
47  auto
48  end() const
49  {
50  return holder_.cend();
51  }
52  auto
53  cbegin() const
54  {
55  return holder_.cbegin();
56  }
57  auto
58  cend() const
59  {
60  return holder_.cend();
61  }
62 
63  private:
64  std::vector<T> holder_;
65  };
66  }
67 
68  class ParameterSet;
69 
70  //==================================================================
71  // e.g. Sequence<int,4> ====> std::array<int,4>
72  //
73  template <typename T, std::size_t N = -1ull>
74  class Sequence final : public detail::SequenceBase,
76  public:
81 
82  using default_type =
84  using ftype = std::array<std::shared_ptr<tt::fhicl_type<T>>, N>;
85  using value_type = std::array<tt::return_type<T>, N>;
86 
87  explicit Sequence(Name&& name);
88  explicit Sequence(Name&& name, Comment&& comment);
89  explicit Sequence(Name&& name,
90  Comment&& comment,
91  std::function<bool()> maybeUse);
92 
93  // c'tors that support defaults
94  explicit Sequence(Name&& name, default_type const& defaults);
95  explicit Sequence(Name&& name,
96  Comment&& comment,
97  default_type const& defaults);
98  explicit Sequence(Name&& name,
99  Comment&& comment,
100  std::function<bool()> maybeUse,
101  default_type const& defaults);
102 
103  auto
104  operator()() const
105  {
106  value_type result = {{tt::return_type<T>()}};
107  cet::transform_all(
108  value_, result.begin(), [](auto const& elem) { return (*elem)(); });
109  return result;
110  }
111 
112  auto
113  operator()(std::size_t i) const
114  {
115  return (*value_.at(i))();
116  }
117 
118  private:
120 
121  std::size_t
122  get_size() const override
123  {
124  return value_.size();
125  }
126 
127  void
128  do_prepare_elements_for_validation(std::size_t const n) override
129  {
130  detail::check_nargs_for_bounded_sequences(key(), get_size(), n);
131  }
132 
133  void
136  {
137  cet::for_all(value_, [&pw](auto& e) { pw.walk_over(*e); });
138  }
139 
140  void
142  pw) const override
143  {
144  cet::for_all(value_, [&pw](auto const& e) { pw.walk_over(*e); });
145  }
146 
147  void
148  do_set_value(fhicl::ParameterSet const&, bool /*trimParents*/) override
149  {}
150  };
151 
152  //==================================================================
153  // e.g. Sequence<int> ====> std::vector<int>
154  //
155  template <typename T>
156  class Sequence<T, -1ull> final : public detail::SequenceBase,
158  public:
163 
164  using default_type = std::vector<typename tt::fhicl_type<T>::default_type>;
165  using ftype = std::vector<std::shared_ptr<tt::fhicl_type<T>>>;
166  using value_type = std::vector<tt::return_type<T>>;
167 
168  explicit Sequence(Name&& name);
169  explicit Sequence(Name&& name, Comment&& comment);
170  explicit Sequence(Name&& name,
171  Comment&& comment,
172  std::function<bool()> maybeUse);
173 
174  // c'tors supporting default values
175  explicit Sequence(Name&& name, default_type const& defaults);
176  explicit Sequence(Name&& name,
177  Comment&& comment,
178  default_type const& defaults);
179  explicit Sequence(Name&& name,
180  Comment&& comment,
181  std::function<bool()> maybeUse,
182  default_type const& defaults);
183 
184  auto
185  operator()() const
186  {
187  value_type result;
188  cet::transform_all(value_, std::back_inserter(result), [](auto const& e) {
189  return (*e)();
190  });
191  return result;
192  }
193 
194  auto
195  operator()(std::size_t i) const
196  {
197  return (*value_.at(i))();
198  }
199 
200  private:
202 
203  void
204  do_prepare_elements_for_validation(std::size_t const n) override
205  {
206  // For an unbounded sequence, we need to resize it so that any
207  // nested parameters of the elements can be checked.
208  if (n < value_.size()) {
209  value_.resize(n);
210  } else if (n > value_.size()) {
211  std::string key_fragment{key()};
212  // When emplacing a new element, do not include in the key
213  // argument the current name-stack stem--it will automatically
214  // be prepended.
215  auto const& nsr = NameStackRegistry::instance();
216  if (!nsr.empty()) {
217  std::string const& current_stem = nsr.current();
218  std::size_t const pos =
219  key_fragment.find(current_stem) != std::string::npos ?
220  current_stem.size() + 1ul : // + 1ul to account for the '.'
221  0ul;
222  key_fragment.replace(0ul, pos, "");
223  }
224 
225  for (auto i = value_.size(); i != n; ++i) {
226  value_.emplace_back(
227  new tt::fhicl_type<T>{Name::sequence_element(key_fragment, i)});
228  }
229  }
230  }
231 
232  std::size_t
233  get_size() const override
234  {
235  return value_.size();
236  }
237 
238  void
241  {
242  cet::for_all(value_, [&pw](auto& e) { pw.walk_over(*e); });
243  }
244 
245  void
247  pw) const override
248  {
249  cet::for_all(value_, [&pw](auto const& e) { pw.walk_over(*e); });
250  }
251 
252  void
253  do_set_value(fhicl::ParameterSet const&, bool /*trimParents*/) override
254  {}
255  };
256 }
257 
259 
260 #endif /* fhiclcpp_types_Sequence_h */
261 
262 // Local variables:
263 // mode: c++
264 // End:
std::size_t get_size() const override
Definition: Sequence.h:122
std::array< tt::return_type< float >, N > value_type
Definition: Sequence.h:85
void do_set_value(fhicl::ParameterSet const &, bool) override
Definition: Sequence.h:253
static NameStackRegistry & instance()
void do_walk_elements(detail::ParameterWalker< tt::const_flavor::require_const > &pw) const override
Definition: Sequence.h:246
void do_prepare_elements_for_validation(std::size_t const n) override
Definition: Sequence.h:128
#define NO_DELEGATED_PARAMETERS
std::size_t get_size() const override
Definition: Sequence.h:233
void do_set_value(fhicl::ParameterSet const &, bool) override
Definition: Sequence.h:148
void check_nargs_for_bounded_sequences(std::string const &key, std::size_t expected, std::size_t provided)
auto operator()() const
Definition: Sequence.h:185
#define NO_OPTIONAL_TYPES
ValueHolder(std::array< T, N > const &array)
Definition: Sequence.h:38
void do_walk_elements(detail::ParameterWalker< tt::const_flavor::require_non_const > &pw) override
Definition: Sequence.h:239
void do_prepare_elements_for_validation(std::size_t const n) override
Definition: Sequence.h:204
void walk_over(tt::maybe_const_t< ParameterBase, C > &)
auto operator()() const
Definition: Sequence.h:104
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:228
typename fhicl_type_impl< T >::type fhicl_type
Definition: type_traits.h:305
auto operator()(std::size_t i) const
Definition: Sequence.h:113
std::vector< typename tt::fhicl_type< T >::default_type > default_type
Definition: Sequence.h:164
std::vector< std::shared_ptr< tt::fhicl_type< T >>> ftype
Definition: Sequence.h:165
parameter set interface
void do_walk_elements(detail::ParameterWalker< tt::const_flavor::require_const > &pw) const override
Definition: Sequence.h:141
void do_walk_elements(detail::ParameterWalker< tt::const_flavor::require_non_const > &pw) override
Definition: Sequence.h:134
#define NO_NESTED_TABLE_FRAGMENTS
std::vector< tt::return_type< T >> value_type
Definition: Sequence.h:166
std::array< std::shared_ptr< tt::fhicl_type< float >>, N > ftype
Definition: Sequence.h:84
static Name sequence_element(std::size_t const i)
Definition: Name.h:17
ValueHolder(std::initializer_list< T > list)
Definition: Sequence.h:35
Char_t n[5]
typename return_type_impl< ARGS... >::value_type return_type
Definition: type_traits.h:342
Float_t e
Definition: plot.C:34
auto operator()(std::size_t i) const
Definition: Sequence.h:195