LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
OptionalTable.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_types_OptionalTable_h
2 #define fhiclcpp_types_OptionalTable_h
3 
5 #include "fhiclcpp/type_traits.h"
8 #include "fhiclcpp/types/Name.h"
15 
16 #include <memory>
17 #include <set>
18 #include <string>
19 
20 namespace fhicl {
21 
22  //========================================================
23  template <typename T>
24  class OptionalTable final : public detail::TableBase,
25  private detail::RegisterIfTableMember {
26  public:
27  static_assert(!tt::is_sequence_type_v<T>, NO_STD_CONTAINERS);
28  static_assert(!tt::is_fhicl_type_v<T>, NO_NESTED_FHICL_TYPES_IN_TABLE);
29  static_assert(!tt::is_table_fragment_v<T>, NO_NESTED_TABLE_FRAGMENTS);
30  static_assert(!tt::is_delegated_parameter_v<T>, NO_DELEGATED_PARAMETERS);
31 
32  //=====================================================
33  // User-friendly
34  // ... c'tors
35  explicit OptionalTable(Name&& name);
36  explicit OptionalTable(Name&& name, Comment&& comment);
37  explicit OptionalTable(Name&& name,
38  Comment&& comment,
39  std::function<bool()> maybeUse);
40  OptionalTable(ParameterSet const& pset,
41  std::set<std::string> const& keysToIgnore);
42 
43  // ... Accessors
44  std::optional<T>
45  operator()() const
46  {
47  return hasValue() ? std::make_optional(*value_) : std::nullopt;
48  }
49 
50  // Obsolete
51  bool
52  operator()(T& value) const
53  {
54  auto opt = operator()();
55  if (opt) {
56  value = *value_;
57  }
58  return opt.has_value();
59  }
60 
61  bool
62  hasValue() const
63  {
64  return TableBase::has_value();
65  }
66 
67  ParameterSet const&
68  get_PSet() const
69  {
70  return TableBase::guarantee_pset();
71  }
72 
73  //=====================================================
74  // Expert-only
75  using value_type = T;
76 
77  OptionalTable();
78 
79  private:
80  std::shared_ptr<T> value_{std::make_shared<T>()};
81  };
82 
83  //=====================================================
84  // Implementation
85 
86  template <typename T>
88  : OptionalTable{std::move(name), Comment("")}
89  {}
90 
91  template <typename T>
93  : TableBase{std::move(name),
94  std::move(comment),
97  , RegisterIfTableMember{this}
98  {
101  }
102 
103  template <typename T>
105  Comment&& comment,
106  std::function<bool()> maybeUse)
107  : TableBase{std::move(name),
108  std::move(comment),
110  maybeUse}
111  , RegisterIfTableMember{this}
112  {
115  }
116 
117  template <typename T>
119  std::set<std::string> const& keysToIgnore)
120  : TableBase{Name("<top_level>"),
121  Comment(""),
124  , RegisterIfTableMember{this}
125  {
128  validate(pset, keysToIgnore);
129  }
130 
131 }
132 
133 #endif /* fhiclcpp_types_OptionalTable_h */
134 
135 // Local variables:
136 // mode: c++
137 // End:
#define NO_DELEGATED_PARAMETERS
std::shared_ptr< T > value_
Definition: OptionalTable.h:80
std::optional< T > operator()() const
Definition: OptionalTable.h:45
std::function< bool()> AlwaysUse()
void validate(ParameterSet const &pset, std::set< std::string > const &ignorable_keys={})
Definition: TableBase.cc:56
#define NO_NESTED_FHICL_TYPES_IN_TABLE
parameter set interface
#define NO_STD_CONTAINERS
std::string const & name() const
#define NO_NESTED_TABLE_FRAGMENTS
double value
Definition: spectrum.C:18
bool operator()(T &value) const
Definition: OptionalTable.h:52
bool hasValue() const
Definition: OptionalTable.h:62
ParameterSet const & get_PSet() const
Definition: OptionalTable.h:68
std::string const & comment() const
TableBase(Name const &name, Comment const &comment, par_style vt, std::function< bool()> maybeUse)
Definition: TableBase.cc:12