LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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 <string>
14 #include <set>
15 #include <map>
16 #include <ostream>
17 #include <initializer_list>
18 
19 // ... and more in the template implementation section below
20 
21 namespace testing {
22 
36  class NameSelector {
37 
38  public:
39  using Name_t = std::string;
40  using Names_t = std::set<Name_t>;
41 
43  typedef enum {
48  } Response_t;
49 
50 
51  using NameList = std::initializer_list<Name_t>;
52 
53 
55  NameSelector(Response_t default_answer = rsDefault)
56  { SetDefaultResponse(default_answer); }
57 
59  template <typename LIST>
60  NameSelector(LIST const& items, Response_t default_answer = rsDefault)
61  : NameSelector(default_answer)
62  { Parse(items); }
63 
65  void SetDefaultResponse(Response_t default_answer)
66  { known_names[DefaultName] = { default_answer }; }
67 
76  template <typename LIST>
77  void Parse(LIST const& items) { BuildNameSet(known_names, items); }
78 
79 
86  void ParseName(Name_t name);
87 
96  template <typename... NAMES>
97  void ParseNames(NAMES... names) { AddFirstName(known_names, names...); }
98 
101 
113  template <typename LIST>
114  void Define(std::string set_name, LIST const& items);
115 
126  template <typename... NAMES>
127  void AddToDefinition(std::string set_name, NAMES... names)
128  { AddFirstName(definitions[set_name], names...); }
129 
131  Response_t Query(Name_t name) const;
132 
134  bool Accepted(Name_t name) const;
135 
137  bool Rejected(Name_t name) const { return !Accepted(name); }
138 
140  Response_t DefaultResponse() const
141  { return known_names.find(DefaultName)->second.response; }
142 
144  bool operator() (Name_t name) const { return Accepted(name); }
145 
147  void PrintConfiguration(std::ostream&) const;
148 
151 
154 
156  void ClearQueryRegistry() { query_registry.clear(); }
157 
159  bool CheckQueryRegistry() const { return DoCheckQueryRegistry(); }
161  bool CheckQueryRegistry(std::ostream& out) const
162  { return DoCheckQueryRegistry(&out); }
164 
165 
166  static Name_t const DefaultName;
167  static Name_t const ClearAllName;
168 
169  protected:
170 
172  typedef struct {
173  Response_t response;
174  } NameResponse_t;
175 
177  using KnownNames_t = std::map<Name_t, NameResponse_t>;
178 
180  using Definitions_t = std::map<Name_t, KnownNames_t>;
181 
183  using QueryRegistry_t = std::map<Name_t, size_t>;
184 
186 
188 
190 
192  Response_t LookupResponse(Name_t name) const;
193 
205  template <typename LIST>
206  void BuildNameSet(KnownNames_t& name_set, LIST const& items) const;
207 
209  template <typename... NAMES>
210  void AddFirstName
211  (KnownNames_t& name_set, Name_t name, NAMES... other_names);
212 
214  void InsertItem
215  (KnownNames_t& name_set, Name_t item, Response_t response) const;
216 
218  void InsertItem(
219  KnownNames_t& name_set, KnownNames_t::value_type item,
220  Response_t response
221  ) const;
222 
249  void ProcessItem(KnownNames_t& name_set, Name_t item) const;
250 
253 
255  void ClearNameSet(KnownNames_t& name_set) const;
256 
258  Names_t QueriedWithStatus(Response_t answer) const;
259 
260  // Performs the actual registry check, optionally printing a message
261  bool DoCheckQueryRegistry(std::ostream* out = nullptr) const;
262 
264  static Response_t ParseMode
265  (Name_t& item, Response_t default_answer = rsAccepted);
266 
267  }; // class NameSelector
268 
269 } // namespace testing
270 
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  KnownNames_t name_set;
283  BuildNameSet(name_set, items);
284  definitions[set_name] = std::move(name_set);
285 } // testing::NameSelector::Define()
286 
287 
288 //------------------------------------------------------------------------------
289 template <typename LIST>
291  (KnownNames_t& name_set, LIST const& items) const
292 {
293  for (Name_t item: items) ProcessItem(name_set, item);
294 } // testing::NameSelector::BuildNameSet()
295 
296 
297 //------------------------------------------------------------------------------
298 namespace testing {
299  // forward declaration of specialization
300  template <>
301  void NameSelector::AddFirstName<>(KnownNames_t& name_set, Name_t name);
302 } // namespace testing
303 
304 template <typename... NAMES>
306  (KnownNames_t& name_set, Name_t name, NAMES... other_names)
307 {
308  AddFirstName(name_set, name);
309  AddFirstName(name_set, other_names...);
310 } // testing::NameSelector::AddFirstName()
311 
312 
313 //------------------------------------------------------------------------------
314 
315 #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:167
LArSoft test utilities.
std::set< Name_t > Names_t
list of names
Definition: NameSelector.h:40
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:177
void AddToDefinition(std::string set_name, NAMES...names)
Parses a list of names and add them to the specified definition.
Definition: NameSelector.h:127
Response_t DefaultResponse() const
Returns the default answer for names that are not registered.
Definition: NameSelector.h:140
Names_t RejectedNames() const
Returns a list of names that were rejected.
Definition: NameSelector.h:153
void BuildNameSet(KnownNames_t &name_set, LIST const &items) const
Fills name_set with a list of items.
Definition: NameSelector.h:291
bool Accepted(Name_t name) const
Returns whether the name is accepted as good.
void ParseNames(NAMES...names)
Parses a list of names and adds them to the selector.
Definition: NameSelector.h:97
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:144
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:306
NameSelector(LIST const &items, Response_t default_answer=rsDefault)
Constructor: Parse()s the specified items.
Definition: NameSelector.h:60
void ParseName(Name_t name)
Parses a name and adds it to the selector.
Manages a set of names.
Definition: NameSelector.h:36
A data structure containing how to react to a name.
Definition: NameSelector.h:172
Definitions_t definitions
a set of definitions
Definition: NameSelector.h:187
bool Rejected(Name_t name) const
Returns whether the name is rejected as bad.
Definition: NameSelector.h:137
Names_t AcceptedNames() const
Returns a list of names that were accepted.
Definition: NameSelector.h:150
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:55
Response_t
Possible responses.
Definition: NameSelector.h:43
std::initializer_list< Name_t > NameList
Definition: NameSelector.h:51
intermediate_table::const_iterator const_iterator
std::map< Name_t, KnownNames_t > Definitions_t
Type of list of definitions.
Definition: NameSelector.h:180
void Parse(LIST const &items)
Parses the names in the list and adds them to the selector.
Definition: NameSelector.h:77
std::map< Name_t, size_t > QueryRegistry_t
Type of query counters.
Definition: NameSelector.h:183
bool CheckQueryRegistry() const
Checks that no known element with valid response was left unqueried.
Definition: NameSelector.h:160
void ClearQueryRegistry()
Reests the query registry.
Definition: NameSelector.h:156
KnownNames_t known_names
list of known names, with category
Definition: NameSelector.h:185
Response_t response
the response
Definition: NameSelector.h:173
static Name_t const DefaultName
name representing the default
Definition: NameSelector.h:166
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:100
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:189
throw art::Exception (art::errors::Configuration)
Definition: NameSelector.h:46
bool CheckQueryRegistry(std::ostream &out) const
Checks that no known element with valid response was left unqueried.
Definition: NameSelector.h:161
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:39
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)