LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
OptionalTupleAs.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_types_OptionalTupleAs_h
2 #define fhiclcpp_types_OptionalTupleAs_h
3 
4 #include "fhiclcpp/type_traits.h"
10 
11 #include <memory>
12 #include <string>
13 #include <utility>
14 
15 namespace fhicl {
16 
17  //==================================================================
18  // e.g. OptionalTupleAs<T,int,double,bool> ====> T(int,double,bool)
19  //
20  template <typename T, typename... ARGS>
21  class OptionalTupleAs;
22 
23  template <typename T, typename... ARGS>
24  class OptionalTupleAs<T(ARGS...)> {
25  public:
26  explicit OptionalTupleAs(Name&& name);
27  explicit OptionalTupleAs(Name&& name, Comment&& comment);
28  explicit OptionalTupleAs(Name&& name,
29  Comment&& comment,
30  std::function<bool()> maybeUse);
31 
32  template <std::size_t... I>
33  T
35  std::index_sequence<I...>) const
36  {
37  return T{std::get<I>(via)...};
38  }
39 
40  bool
41  operator()(T& result) const
42  {
43  typename OptionalTuple<ARGS...>::value_type via;
44  if (tupleObj_(via)) {
45  auto tmp = fill(via, std::index_sequence_for<ARGS...>{});
46  std::swap(result, tmp);
47  return true;
48  }
49  return false;
50  }
51 
52  bool
53  hasValue() const
54  {
55  return tupleObj_.hasValue();
56  }
57 
58  private:
60 
61  Comment
62  conversion_comment(Comment&& comment) const
63  {
64  std::string const preface =
65  "N.B. The following sequence is converted to type:";
66  std::string const name =
67  " '" + cet::demangle_symbol(typeid(T).name()) + "'";
68  std::string const user_comment =
69  comment.value.empty() ? "" : "\n\n" + comment.value;
70 
71  std::ostringstream oss;
72  oss << preface << '\n' << name << user_comment;
73 
74  return Comment{oss.str().c_str()};
75  }
76  };
77 
78  //==================================================================
79  // IMPLEMENTATION
80 
81  template <typename T, typename... ARGS>
83  : OptionalTupleAs{std::move(name), Comment("")}
84  {}
85 
86  template <typename T, typename... ARGS>
87  OptionalTupleAs<T(ARGS...)>::OptionalTupleAs(Name&& name, Comment&& comment)
88  : tupleObj_{std::move(name), conversion_comment(std::move(comment))}
89  {}
90 
91  template <typename T, typename... ARGS>
93  Comment&& comment,
94  std::function<bool()> maybeUse)
95  : tupleObj_{std::move(name),
96  conversion_comment(std::move(comment)),
97  maybeUse}
98  {}
99 }
100 
101 #endif /* fhiclcpp_types_OptionalTupleAs_h */
102 
103 // Local variables:
104 // mode: c++
105 // End:
Comment conversion_comment(Comment &&comment) const
Float_t tmp
Definition: plot.C:37
bool operator()(T &result) const
parameter set interface
OptionalTuple< ARGS... > tupleObj_
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
std::string value
Definition: Comment.h:37
T fill(typename OptionalTuple< ARGS... >::value_type const &via, std::index_sequence< I... >) const