NIST Biometric Evaluation Framework
Software components for biometric technology evaluations
be_framework_enumeration.h
Go to the documentation of this file.
1/*
2 * This software was developed at the National Institute of Standards and
3 * Technology (NIST) by employees of the Federal Government in the course
4 * of their official duties. Pursuant to title 17 Section 105 of the
5 * United States Code, this software is not subject to copyright protection
6 * and is in the public domain. NIST assumes no responsibility whatsoever for
7 * its use by other parties, and makes no guarantees, expressed or implied,
8 * about its quality, reliability, or any other characteristic.
9 */
10
11#ifndef BE_FRAMEWORK_ENUMERATION_H_
12#define BE_FRAMEWORK_ENUMERATION_H_
13
14#include <map>
15#include <string>
16#include <ostream>
17#include <type_traits>
18
19#include <be_error_exception.h>
20
21/*
22 * These empty namespaces are here so that you can issue
23 * using namespace BiometricEvaluation::Framework::Enumeration;
24 * after including this file.
25 */
26namespace BiometricEvaluation
27{
28 namespace Framework
29 {
30 namespace Enumeration
31 {
32
33 }
34 }
35}
36
49#define BE_FRAMEWORK_ENUMERATION_DECLARATIONS(BE_ENUMERATED_TYPE_, \
50 BE_ENUMERATED_TYPE_ENUM_TO_STRING_MAP_) \
51extern const \
52std::map<BE_ENUMERATED_TYPE_, std::string> \
53BE_ENUMERATED_TYPE_ENUM_TO_STRING_MAP_;\
54\
55namespace BiometricEvaluation \
56{ \
57 namespace Framework \
58 { \
59 namespace Enumeration \
60 { \
61 \
77 bool \
78 operator==( \
79 const std::string &strVal, \
80 const BE_ENUMERATED_TYPE_ &enumVal); \
81\
82 \
98 bool \
99 operator==( \
100 const BE_ENUMERATED_TYPE_ &enumVal, \
101 const std::string &strVal); \
102\
103 \
119 bool \
120 operator!=( \
121 const std::string &strVal, \
122 const BE_ENUMERATED_TYPE_ &enumVal); \
123\
124 \
140 bool \
141 operator!=( \
142 const BE_ENUMERATED_TYPE_ &enumVal, \
143 const std::string &strVal); \
144\
159 std::ostream& \
160 operator<<( \
161 std::ostream &stream, \
162 const BE_ENUMERATED_TYPE_ &enumVal); \
163\
164 \
179 std::string \
180 operator+( \
181 const std::string &strVal, \
182 const BE_ENUMERATED_TYPE_ &enumVal); \
183\
184 \
199 std::string \
200 operator+( \
201 const BE_ENUMERATED_TYPE_ &enumVal, \
202 const std::string &strVal); \
203\
204 \
216 std::underlying_type<BE_ENUMERATED_TYPE_>::type \
217 to_int_type( \
218 const BE_ENUMERATED_TYPE_ &enumVal) \
219 noexcept; \
220\
221 \
238 std::string \
239 to_string( \
240 const BE_ENUMERATED_TYPE_ &enumVal); \
241\
242 \
258 template<typename T> \
259 T \
260 to_enum( \
261 const typename \
262 std::underlying_type<T>::type &iVal); \
263\
264 \
289 template<typename T> \
290 T \
291 to_enum( \
292 const std::string &strVal); \
293 } \
294 } \
295}\
296/* This is here to require a semicolon after macro instantation */\
297static_assert(true, "")
298
299
312#define BE_FRAMEWORK_ENUMERATION_DEFINITIONS(BE_ENUMERATED_TYPE_, \
313 BE_ENUMERATED_TYPE_ENUM_TO_STRING_MAP_) \
314/* \
315 * Template specializations for to_enum() must come before they are used, \
316 * or functions that rely on them, like operator==, will implicitly \
317 * instantiate them, disallowing the later specialization. \
318 *
319 * Additional, a long-standing g++ bug originating from a defect in the \
320 * standard requires that the template specializations be enclosed in the same \
321 * namespace: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480 \
322 */ \
323namespace BiometricEvaluation{ \
324 namespace Framework { \
325 namespace Enumeration{ \
326 template<> \
327 BE_ENUMERATED_TYPE_ \
328 to_enum( \
329 const typename std::underlying_type<\
330 BE_ENUMERATED_TYPE_>::type &iVal) \
331 { \
332 for (const auto &i : \
333 BE_ENUMERATED_TYPE_ENUM_TO_STRING_MAP_) \
334 if (static_cast<std::underlying_type<\
335 BE_ENUMERATED_TYPE_>::type>(\
336 i.first) == iVal) \
337 return (i.first); \
338 \
339 throw BiometricEvaluation::Error::\
340 ObjectDoesNotExist(std::to_string(iVal)); \
341 } \
342\
343 template<> \
344 BE_ENUMERATED_TYPE_ \
345 to_enum ( \
346 const std::string &strVal) \
347 { \
348 for (const auto &i : \
349 BE_ENUMERATED_TYPE_ENUM_TO_STRING_MAP_) \
350 if (i.second == strVal) \
351 return (i.first); \
352 \
353 throw BiometricEvaluation::Error::\
354 ObjectDoesNotExist(strVal); \
355 } \
356 } \
357 } \
358} \
359\
360bool \
361BiometricEvaluation::Framework::Enumeration::operator==( \
362 const std::string &strVal, \
363 const BE_ENUMERATED_TYPE_ &enumVal) \
364{ \
365 return (to_enum<BE_ENUMERATED_TYPE_>(strVal) == enumVal); \
366} \
367\
368bool \
369BiometricEvaluation::Framework::Enumeration::operator==( \
370 const BE_ENUMERATED_TYPE_ &enumVal, \
371 const std::string &strVal) \
372{ \
373 return (strVal == enumVal); \
374} \
375\
376bool \
377BiometricEvaluation::Framework::Enumeration::operator!=( \
378 const std::string &strVal, \
379 const BE_ENUMERATED_TYPE_ &enumVal) \
380{ \
381 return (!(strVal == enumVal)); \
382} \
383\
384bool \
385BiometricEvaluation::Framework::Enumeration::operator!=( \
386 const BE_ENUMERATED_TYPE_ &enumVal, \
387 const std::string &strVal) \
388{ \
389 return (!(strVal == enumVal)); \
390} \
391\
392std::ostream& \
393BiometricEvaluation::Framework::Enumeration::operator<<( \
394 std::ostream &stream, \
395 const BE_ENUMERATED_TYPE_ &enumVal) \
396{ \
397 return (stream << to_string(enumVal)); \
398} \
399\
400std::string \
401BiometricEvaluation::Framework::Enumeration::operator+( \
402 const std::string &strVal, \
403 const BE_ENUMERATED_TYPE_ &enumVal) \
404{ \
405 return (strVal + to_string(enumVal)); \
406} \
407\
408std::string \
409BiometricEvaluation::Framework::Enumeration::operator+( \
410 const BE_ENUMERATED_TYPE_ &enumVal, \
411 const std::string &strVal) \
412{ \
413 return (to_string(enumVal) + strVal); \
414} \
415\
416std::string \
417BiometricEvaluation::Framework::Enumeration::to_string( \
418 const BE_ENUMERATED_TYPE_ &enumVal) \
419{ \
420 return (BE_ENUMERATED_TYPE_ENUM_TO_STRING_MAP_.at(enumVal)); \
421} \
422\
423std::underlying_type<BE_ENUMERATED_TYPE_>::type \
424BiometricEvaluation::Framework::Enumeration::to_int_type( \
425 const BE_ENUMERATED_TYPE_ &enumVal) \
426 noexcept \
427{ \
428 return (static_cast<typename std::underlying_type< \
429 BE_ENUMERATED_TYPE_>::type>(enumVal)); \
430}\
431/* This is here to require a semicolon after macro instantation */\
432static_assert(true, "")
433
434#endif /* BE_FRAMEWORK_ENUMERATION_H_ */
This software was developed at the National Institute of Standards and Technology (NIST) by employees...