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  bool
34  operator()(T& result) const
35  {
36  typename OptionalTuple<ARGS...>::value_type via;
37  if (tupleObj_(via)) {
38  result = std::make_from_tuple(via);
39  return true;
40  }
41  return false;
42  }
43 
44  bool
45  hasValue() const
46  {
47  return tupleObj_.hasValue();
48  }
49 
50  private:
52 
53  Comment
54  conversion_comment(Comment&& comment) const
55  {
56  std::string const preface =
57  "N.B. The following sequence is converted to type:";
58  std::string const name =
59  " '" + cet::demangle_symbol(typeid(T).name()) + "'";
60  std::string const user_comment =
61  comment.value.empty() ? "" : "\n\n" + comment.value;
62 
63  std::ostringstream oss;
64  oss << preface << '\n' << name << user_comment;
65 
66  return Comment{oss.str().c_str()};
67  }
68  };
69 
70  //==================================================================
71  // IMPLEMENTATION
72 
73  template <typename T, typename... ARGS>
75  : OptionalTupleAs{std::move(name), Comment("")}
76  {}
77 
78  template <typename T, typename... ARGS>
80  : tupleObj_{std::move(name), conversion_comment(std::move(comment))}
81  {}
82 
83  template <typename T, typename... ARGS>
85  Comment&& comment,
86  std::function<bool()> maybeUse)
87  : tupleObj_{std::move(name),
88  conversion_comment(std::move(comment)),
89  maybeUse}
90  {}
91 }
92 
93 #endif /* fhiclcpp_types_OptionalTupleAs_h */
94 
95 // Local variables:
96 // mode: c++
97 // End:
const XML_Char * name
Definition: expat.h:151
Comment conversion_comment(Comment &&comment) const
bool operator()(T &result) const
OptionalTuple< ARGS... > tupleObj_
double T
Definition: Xdiff_gwt.C:5
enum BeamMode string