LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
NameSelector.h
Go to the documentation of this file.
1 
9 #ifndef TEST_GEOMETRY_NAMESELECTOR_H
10 #define TEST_GEOMETRY_NAMESELECTOR_H 1
11 
12 // C/C++ standard library
13 #include <initializer_list>
14 #include <iosfwd>
15 #include <map>
16 #include <set>
17 #include <stddef.h>
18 #include <string>
19 
20 // ... and more in the template implementation section below
21 
22 namespace testing {
23 
37  class NameSelector {
38 
39  public:
40  using Name_t = std::string;
41  using Names_t = std::set<Name_t>;
42 
44  typedef enum {
49  } Response_t;
50 
51  using NameList = std::initializer_list<Name_t>;
52 
54  NameSelector(Response_t default_answer = rsDefault) { SetDefaultResponse(default_answer); }
55 
57  template <typename LIST>
58  NameSelector(LIST const& items, Response_t default_answer = rsDefault)
59  : NameSelector(default_answer)
60  {
61  Parse(items);
62  }
63 
65  void SetDefaultResponse(Response_t default_answer)
66  {
67  known_names[DefaultName] = {default_answer};
68  }
69 
78  template <typename LIST>
79  void Parse(LIST const& items)
80  {
81  BuildNameSet(known_names, items);
82  }
83 
90  void ParseName(Name_t name);
91 
100  template <typename... NAMES>
101  void ParseNames(NAMES... names)
102  {
103  AddFirstName(known_names, names...);
104  }
105 
108 
120  template <typename LIST>
121  void Define(std::string set_name, LIST const& items);
122 
133  template <typename... NAMES>
134  void AddToDefinition(std::string set_name, NAMES... names)
135  {
136  AddFirstName(definitions[set_name], names...);
137  }
138 
140  Response_t Query(Name_t name) const;
141 
143  bool Accepted(Name_t name) const;
144 
146  bool Rejected(Name_t name) const { return !Accepted(name); }
147 
149  Response_t DefaultResponse() const { return known_names.find(DefaultName)->second.response; }
150 
152  bool operator()(Name_t name) const { return Accepted(name); }
153 
155  void PrintConfiguration(std::ostream&) const;
156 
159 
162 
164  void ClearQueryRegistry() { query_registry.clear(); }
165 
167  bool CheckQueryRegistry() const { return DoCheckQueryRegistry(); }
169  bool CheckQueryRegistry(std::ostream& out) const { return DoCheckQueryRegistry(&out); }
171 
172  static Name_t const DefaultName;
173  static Name_t const ClearAllName;
174 
175  protected:
177  typedef struct {
178  Response_t response;
179  } NameResponse_t;
180 
182  using KnownNames_t = std::map<Name_t, NameResponse_t>;
183 
185  using Definitions_t = std::map<Name_t, KnownNames_t>;
186 
188  using QueryRegistry_t = std::map<Name_t, size_t>;
189 
191 
193 
195 
197  Response_t LookupResponse(Name_t name) const;
198 
210  template <typename LIST>
211  void BuildNameSet(KnownNames_t& name_set, LIST const& items) const;
212 
214  template <typename... NAMES>
215  void AddFirstName(KnownNames_t& name_set, Name_t name, NAMES... other_names);
216 
218  void InsertItem(KnownNames_t& name_set, Name_t item, Response_t response) const;
219 
221  void InsertItem(KnownNames_t& name_set,
222  KnownNames_t::value_type item,
223  Response_t response) const;
224 
251  void ProcessItem(KnownNames_t& name_set, Name_t item) const;
252 
255 
257  void ClearNameSet(KnownNames_t& name_set) const;
258 
260  Names_t QueriedWithStatus(Response_t answer) const;
261 
262  // Performs the actual registry check, optionally printing a message
263  bool DoCheckQueryRegistry(std::ostream* out = nullptr) const;
264 
266  static Response_t ParseMode(Name_t& item, Response_t default_answer = rsAccepted);
267 
268  }; // class NameSelector
269 
270 } // namespace testing
271 
272 //------------------------------------------------------------------------------
273 //--- Template implementation
274 //---
275 
276 // C/C++ standard library
277 #include <utility> // std::move()
278 
279 //------------------------------------------------------------------------------
280 template <typename LIST>
281 void testing::NameSelector::Define(std::string set_name, LIST const& items)
282 {
283  KnownNames_t name_set;
284  BuildNameSet(name_set, items);
285  definitions[set_name] = std::move(name_set);
286 } // testing::NameSelector::Define()
287 
288 //------------------------------------------------------------------------------
289 template <typename LIST>
290 void testing::NameSelector::BuildNameSet(KnownNames_t& name_set, LIST const& items) const
291 {
292  for (Name_t item : items)
293  ProcessItem(name_set, item);
294 } // testing::NameSelector::BuildNameSet()
295 
296 //------------------------------------------------------------------------------
297 namespace testing {
298  // forward declaration of specialization
299  template <>
300  void NameSelector::AddFirstName<>(KnownNames_t& name_set, Name_t name);
301 } // namespace testing
302 
303 template <typename... NAMES>
304 void testing::NameSelector::AddFirstName(KnownNames_t& name_set, Name_t name, NAMES... other_names)
305 {
306  AddFirstName(name_set, name);
307  AddFirstName(name_set, other_names...);
308 } // testing::NameSelector::AddFirstName()
309 
310 //------------------------------------------------------------------------------
311 
312 #endif // TEST_GEOMETRY_NAMESELECTOR_H
void Define(std::string set_name, LIST const &items)
Defines a set.
Definition: NameSelector.h:281
static Name_t const ClearAllName
name instructing to delete all names
Definition: NameSelector.h:173
LArSoft test utilities.
std::set< Name_t > Names_t
list of names
Definition: NameSelector.h:41
bool DoCheckQueryRegistry(std::ostream *out=nullptr) const
static Response_t ParseMode(Name_t &item, Response_t default_answer=rsAccepted)
Strips the mode specifier from item and returns the insertion mode.
Definitions_t::const_iterator FindDefinition(Name_t &item) const
Strips set specifier and returns iterator to the definition, or end()
std::map< Name_t, NameResponse_t > KnownNames_t
Information about known names.
Definition: NameSelector.h:182
void AddToDefinition(std::string set_name, NAMES...names)
Parses a list of names and add them to the specified definition.
Definition: NameSelector.h:134
Response_t DefaultResponse() const
Returns the default answer for names that are not registered.
Definition: NameSelector.h:149
Names_t RejectedNames() const
Returns a list of names that were rejected.
Definition: NameSelector.h:161
void BuildNameSet(KnownNames_t &name_set, LIST const &items) const
Fills name_set with a list of items.
Definition: NameSelector.h:290
bool Accepted(Name_t name) const
Returns whether the name is accepted as good.
intermediate_table::const_iterator const_iterator
void ParseNames(NAMES...names)
Parses a list of names and adds them to the selector.
Definition: NameSelector.h:101
void PrintConfiguration(std::ostream &) const
Prints the configuration into a stream.
bool operator()(Name_t name) const
Returns whether the name is accepted as good (alias for accept())
Definition: NameSelector.h:152
Response_t LookupResponse(Name_t name) const
Returns the response for the specified name (does not register query)
void AddFirstName(KnownNames_t &name_set, Name_t name, NAMES...other_names)
Parses the first of the provided names, and recurs.
Definition: NameSelector.h:304
NameSelector(LIST const &items, Response_t default_answer=rsDefault)
Constructor: Parse()s the specified items.
Definition: NameSelector.h:58
void ParseName(Name_t name)
Parses a name and adds it to the selector.
Manages a set of names.
Definition: NameSelector.h:37
A data structure containing how to react to a name.
Definition: NameSelector.h:177
Definitions_t definitions
a set of definitions
Definition: NameSelector.h:192
bool Rejected(Name_t name) const
Returns whether the name is rejected as bad.
Definition: NameSelector.h:146
Names_t AcceptedNames() const
Returns a list of names that were accepted.
Definition: NameSelector.h:158
void ProcessItem(KnownNames_t &name_set, Name_t item) const
Fills name_set with an item.
NameSelector(Response_t default_answer=rsDefault)
Constructor: an empty selector with a default answer for unknown names.
Definition: NameSelector.h:54
Response_t
Possible responses.
Definition: NameSelector.h:44
std::initializer_list< Name_t > NameList
Definition: NameSelector.h:51
std::map< Name_t, KnownNames_t > Definitions_t
Type of list of definitions.
Definition: NameSelector.h:185
void Parse(LIST const &items)
Parses the names in the list and adds them to the selector.
Definition: NameSelector.h:79
std::map< Name_t, size_t > QueryRegistry_t
Type of query counters.
Definition: NameSelector.h:188
bool CheckQueryRegistry() const
Checks that no known element with valid response was left unqueried.
Definition: NameSelector.h:168
void ClearQueryRegistry()
Reests the query registry.
Definition: NameSelector.h:164
KnownNames_t known_names
list of known names, with category
Definition: NameSelector.h:190
Response_t response
the response
Definition: NameSelector.h:178
static Name_t const DefaultName
name representing the default
Definition: NameSelector.h:172
Names_t QueriedWithStatus(Response_t answer) const
Returns the list of queried names whose response is answer.
void Clear()
Erases all the names in the selector (default answer is unchanged)
Definition: NameSelector.h:107
void ClearNameSet(KnownNames_t &name_set) const
Erases all the names in the selector (default answer is unchanged)
QueryRegistry_t query_registry
record of all the queries
Definition: NameSelector.h:194
throw art::Exception (art::errors::Configuration)
Definition: NameSelector.h:47
bool CheckQueryRegistry(std::ostream &out) const
Checks that no known element with valid response was left unqueried.
Definition: NameSelector.h:169
void InsertItem(KnownNames_t &name_set, Name_t item, Response_t response) const
Adds an item to the name set, working in specified mode.
std::string Name_t
type representing a name
Definition: NameSelector.h:40
void SetDefaultResponse(Response_t default_answer)
Sets the default answer for names that are not registered.
Definition: NameSelector.h:65
Response_t Query(Name_t name) const
Returns the response for the specified name (does not throw)