LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
Atom.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_types_Atom_h
2 #define fhiclcpp_types_Atom_h
3 
5 #include "fhiclcpp/type_traits.h"
8 #include "fhiclcpp/types/Name.h"
15 
16 #include <memory>
17 #include <sstream>
18 #include <string>
19 
20 namespace fhicl {
21 
22  //========================================================
23  template <typename T>
24  class Atom final : public detail::AtomBase,
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_ATOM);
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 Atom(Name&& name);
36  explicit Atom(Name&& name, Comment&& comment);
37  explicit Atom(Name&& name, Comment&& comment, std::function<bool()> useIf);
38 
39  // ... c'tors supporting defaults
40  explicit Atom(Name&& name, T const& dflt_value);
41  explicit Atom(Name&& name, Comment&& comment, T const& dflt_value);
42  explicit Atom(Name&& name,
43  Comment&& comment,
44  std::function<bool()> useIf,
45  T const& dflt_value);
46 
47  // ... Accessors
48  auto const&
49  operator()() const
50  {
51  return value_;
52  }
53 
54  // Expert-only
55  using default_type = T;
56  using value_type = T;
57 
58  private:
60 
61  std::string get_stringified_value() const override;
62  void do_set_value(fhicl::ParameterSet const& pset) override;
63  };
64 }
65 
66 // Implementation
69 
70 namespace fhicl {
71 
72  template <typename T>
74  : AtomBase{std::move(name),
75  std::move(comment),
78  , RegisterIfTableMember{this}
79  {
81  }
82 
83  template <typename T>
84  Atom<T>::Atom(Name&& name, Comment&& comment, std::function<bool()> maybeUse)
85  : AtomBase{std::move(name),
86  std::move(comment),
88  maybeUse}
89  , RegisterIfTableMember{this}
90  {
92  }
93 
94  template <typename T>
95  Atom<T>::Atom(Name&& name, Comment&& comment, T const& dflt_value)
96  : AtomBase{std::move(name),
97  std::move(comment),
100  , RegisterIfTableMember{this}
101  , value_{dflt_value}
102  {
104  }
105 
106  template <typename T>
108  Comment&& comment,
109  std::function<bool()> maybeUse,
110  T const& dflt_value)
111  : AtomBase{std::move(name),
112  std::move(comment),
114  maybeUse}
115  , RegisterIfTableMember{this}
116  , value_{dflt_value}
117  {
119  }
120 
121  template <typename T>
122  Atom<T>::Atom(Name&& name) : Atom{std::move(name), Comment("")}
123  {}
124 
125  template <typename T>
126  Atom<T>::Atom(Name&& name, T const& dflt_value)
127  : Atom{std::move(name), Comment(""), dflt_value}
128  {}
129 
130  template <typename T>
131  std::string
133  {
134  std::stringstream oss;
135  if (has_default()) {
136  using namespace detail::yes_defaults;
137  oss << maybe_quotes<T>(value_);
138  } else {
139  using namespace detail::no_defaults;
140  oss << expected_types<T>();
141  }
142  return oss.str();
143  }
144 
145  template <typename T>
146  void
148  {
149  auto const trimmed_key = detail::strip_first_containing_name(key());
150  if (has_default()) {
151  // Override default value if the key is present
152  pset.get_if_present<T>(trimmed_key, value_);
153  } else
154  value_ = pset.get<T>(trimmed_key);
155  }
156 }
157 
158 #endif /* fhiclcpp_types_Atom_h */
159 
160 // Local variables:
161 // mode: c++
162 // End:
auto const & operator()() const
Definition: Atom.h:49
std::string get_stringified_value() const override
Definition: Atom.h:132
AtomBase(Name const &name, Comment const &comment, par_style const vt, std::function< bool()> maybeUse)
Definition: AtomBase.h:10
#define NO_DELEGATED_PARAMETERS
std::function< bool()> AlwaysUse()
std::string strip_first_containing_name(std::string const &key)
void do_set_value(fhicl::ParameterSet const &pset) override
Definition: Atom.h:147
parameter set interface
T get(std::string const &key) const
Definition: ParameterSet.h:314
#define NO_STD_CONTAINERS
Atom(Name &&name)
Definition: Atom.h:122
std::string const & name() const
std::string const & key() const
Definition: ParameterBase.cc:6
raw::ChannelID_t value_type
Definition: Atom.h:56
value_type value_
Definition: Atom.h:59
#define NO_NESTED_TABLE_FRAGMENTS
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:267
std::string const & comment() const
#define NO_NESTED_FHICL_TYPES_IN_ATOM
raw::ChannelID_t default_type
Definition: Atom.h:55