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>
78  , RegisterIfTableMember{this}
79  {
81  }
82 
83  template <typename T>
88  maybeUse}
89  , RegisterIfTableMember{this}
90  {
92  }
93 
94  template <typename T>
95  Atom<T>::Atom(Name&& name, Comment&& comment, T const& dflt_value)
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)
114  maybeUse}
115  , RegisterIfTableMember{this}
116  , value_{dflt_value}
117  {
119  }
120 
121  template <typename T>
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>
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  }
154  else
155  value_ = pset.get<T>(trimmed_key);
156  }
157 }
158 
159 #endif /* fhiclcpp_types_Atom_h */
160 
161 // Local variables:
162 // mode: c++
163 // End:
auto const & operator()() const
Definition: Atom.h:49
std::string get_stringified_value() const override
Definition: Atom.h:132
std::string const & name() const
Definition: ParameterBase.h:43
AtomBase(Name const &name, Comment const &comment, par_style const vt, std::function< bool()> maybeUse)
Definition: AtomBase.h:10
#define NO_DELEGATED_PARAMETERS
std::string string
Definition: nybbler.cc:12
ChannelGroupService::Name Name
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
def move(depos, offset)
Definition: depos.py:107
T get(std::string const &key) const
Definition: ParameterSet.h:271
#define NO_STD_CONTAINERS
Atom(Name &&name)
Definition: Atom.h:122
raw::ChannelID_t value_type
Definition: Atom.h:56
value_type value_
Definition: Atom.h:59
#define NO_NESTED_TABLE_FRAGMENTS
std::string const & key() const
Definition: ParameterBase.h:38
#define Comment
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:224
std::string const & comment() const
Definition: ParameterBase.h:48
#define NO_NESTED_FHICL_TYPES_IN_ATOM
void function(int client, int *resource, int parblock, int *test, int p)
raw::ChannelID_t default_type
Definition: Atom.h:55