LArSoft  v09_90_00
Liquid Argon Software toolkit - https://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 <tuple>
14 #include <utility>
15 
16 namespace fhicl {
17 
18  //==================================================================
19  // e.g. OptionalTupleAs<T,int,double,bool> ====> T(int,double,bool)
20  //
21  template <typename T, typename... ARGS>
22  class OptionalTupleAs;
23 
24  template <typename T, typename... ARGS>
25  class OptionalTupleAs<T(ARGS...)> {
26  public:
27  explicit OptionalTupleAs(Name&& name);
28  explicit OptionalTupleAs(Name&& name, Comment&& comment);
29  explicit OptionalTupleAs(Name&& name,
30  Comment&& comment,
31  std::function<bool()> maybeUse);
32 
33  std::optional<T>
34  operator()() const
35  {
36  if (auto via = tupleObj_()) {
37  return make_optional(std::make_from_tuple(*via));
38  }
39  return std::nullopt;
40  }
41 
42  // Obsolete
43  bool
44  operator()(T& result) const
45  {
46  auto t = operator()();
47  if (t) {
48  result = *t;
49  }
50  return t.has_value();
51  }
52 
53  bool
54  hasValue() const
55  {
56  return tupleObj_.hasValue();
57  }
58 
59  private:
61 
62  Comment
63  conversion_comment(Comment&& comment) const
64  {
65  std::string const preface =
66  "N.B. The following sequence is converted to type:";
67  std::string const name =
68  " '" + cet::demangle_symbol(typeid(T).name()) + "'";
69  std::string const user_comment =
70  comment.value.empty() ? "" : "\n\n" + comment.value;
71 
72  std::ostringstream oss;
73  oss << preface << '\n' << name << user_comment;
74 
75  return Comment{oss.str().c_str()};
76  }
77  };
78 
79  //==================================================================
80  // IMPLEMENTATION
81 
82  template <typename T, typename... ARGS>
84  : OptionalTupleAs{std::move(name), Comment("")}
85  {}
86 
87  template <typename T, typename... ARGS>
88  OptionalTupleAs<T(ARGS...)>::OptionalTupleAs(Name&& name, Comment&& comment)
89  : tupleObj_{std::move(name), conversion_comment(std::move(comment))}
90  {}
91 
92  template <typename T, typename... ARGS>
94  Comment&& comment,
95  std::function<bool()> maybeUse)
96  : tupleObj_{std::move(name),
97  conversion_comment(std::move(comment)),
98  maybeUse}
99  {}
100 }
101 
102 #endif /* fhiclcpp_types_OptionalTupleAs_h */
103 
104 // Local variables:
105 // mode: c++
106 // End:
Comment conversion_comment(Comment &&comment) const
bool operator()(T &result) const
parameter set interface
OptionalTuple< ARGS... > tupleObj_
std::optional< T > operator()() const
std::string value
Definition: Comment.h:37