NIST Biometric Evaluation Framework
Software components for biometric technology evaluations
be_framework_api.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_API_H_
12#define BE_FRAMEWORK_API_H_
13
14#include <functional>
15#include <memory>
16
19#include <be_framework_status.h>
20#include <be_time_timer.h>
21#include <be_time_watchdog.h>
22
23namespace BiometricEvaluation
24{
25 namespace Framework
26 {
28 enum class APICurrentState
29 {
39 Running,
42 };
43 }
44}
45
48 BE_Framework_APICurrentState_EnumToStringMap);
49
50namespace BiometricEvaluation
51{
52 namespace Framework
53 {
62 template<typename T>
63 class API
64 {
65 public:
67 class Result
68 {
69 public:
71 Result();
72
74 uint64_t elapsed;
89
98 inline bool
100 const
101 {
102 return (currentState !=
104 }
105
113 inline explicit operator
114 bool()
115 const
116 {
117 return (currentState ==
119 }
120
131 std::string
133 const
134 noexcept
135 {
136 try {
137 this->rethrowException();
138 } catch (const std::exception &e) {
139 return (e.what());
140 } catch (...) {
141 return {};
142 }
143 }
144
164 [[noreturn]]
165 void
167 const
168 {
169 if (this->currentState !=
172 "No exception handled, "
173 "current state is " +
175 this->currentState)};
176 if (!this->exceptionPtr)
178 "Exception was caught, but "
179 "not saved"};
180
181 std::rethrow_exception(
182 this->exceptionPtr);
183 }
184
192 void
194 std::exception_ptr e)
195 {
196 this->exceptionPtr = e;
197 }
198
199 private:
201 std::exception_ptr exceptionPtr{};
202 };
203
205 API();
206
241 Result
242 call(
243 const std::function<T(void)> &operation,
244 const std::function<void(const Result&)>
245 &success = {},
246 const std::function<void(const Result&)>
247 &failure = {});
248
268 bool
270 const
271 {
272 return (this->willCatchExceptions() &&
273 !this->willRethrowExceptions() &&
274 this->getWatchdog()->isEnabled() &&
275 this->getSignalManager->isEnabled());
276 }
277
297 void
299 const bool protectionsEnabled)
300 {
301 this->setCatchExceptions(protectionsEnabled);
302 this->setRethrowExceptions(!protectionsEnabled);
303 this->getWatchdog()->setEnabled(
304 protectionsEnabled);
305 this->getSignalManager->setEnabled(
306 protectionsEnabled);
307 }
308
322 bool
324 const
325 {
326 return (this->_rethrowExceptions);
327 }
328
342 void
344 const bool shouldRethrow)
345 {
346 this->_rethrowExceptions = shouldRethrow;
347 }
348
358 void
360 const bool catchExceptions)
361 {
362 this->_catchExceptions = false;
363 }
364
374 bool
376 const
377 {
378 return (this->_catchExceptions);
379 }
380
388 inline std::shared_ptr<BiometricEvaluation::Time::Timer>
390 noexcept
391 {
392 return (_timer);
393 }
394
402 inline std::shared_ptr<
405 noexcept
406 {
407 return (_watchdog);
408 }
409
417 inline std::shared_ptr<
420 noexcept
421 {
422 return (_sigmgr);
423 }
424
425 private:
427 bool _catchExceptions{true};
429 bool _rethrowExceptions{false};
431 std::shared_ptr<BiometricEvaluation::Time::Timer>
432 _timer;
434 std::shared_ptr<BiometricEvaluation::Time::Watchdog>
435 _watchdog;
437 std::shared_ptr<
439 };
440 }
441}
442
443template<typename T>
445 currentState(BiometricEvaluation::Framework::APICurrentState::NeverCalled)
446{
447
448}
449
450template<typename T>
452 _catchExceptions{true},
453 _rethrowExceptions{false},
454 _timer(new BiometricEvaluation::Time::Timer()),
455 _watchdog(new BiometricEvaluation::Time::Watchdog(
456 BiometricEvaluation::Time::Watchdog::REALTIME)),
457 _sigmgr(new BiometricEvaluation::Error::SignalManager())
458{
459
460}
461
462template<typename T>
465 const std::function<T(void)> &operation,
466 const std::function<void(const Framework::API<T>::Result&)> &success,
467 const std::function<void(const Framework::API<T>::Result&)> &failure)
468{
469 Result ret;
470
471 BEGIN_SIGNAL_BLOCK(this->getSignalManager(), SM_BLOCK);
472 BEGIN_WATCHDOG_BLOCK(this->getWatchdog(), WD_BLOCK);
474 if (this->willCatchExceptions()) {
475 this->getTimer()->start();
476 try {
477 ret.status = operation();
478 } catch (...) {
479 this->getTimer()->stop();
480 ret.elapsed = this->getTimer()->elapsed();
481 ret.currentState =
483 ret.setException(std::current_exception());
484
485 if (failure)
486 failure(ret);
487
488 if (this->_rethrowExceptions)
489 throw;
490
491 return (ret);
492 }
493 } else {
494 this->getTimer()->start();
495 ret.status = operation();
496 }
497 this->getTimer()->stop();
498 END_WATCHDOG_BLOCK(this->getWatchdog(), WD_BLOCK);
499 END_SIGNAL_BLOCK(this->getSignalManager(), SM_BLOCK);
500 if (this->getSignalManager()->sigHandled()) {
501 this->getTimer()->stop();
502 ret.elapsed = this->getTimer()->elapsed();
504
505 if (failure)
506 failure(ret);
507 } else if (this->getWatchdog()->expired()) {
508 this->getTimer()->stop();
509 ret.elapsed = this->getTimer()->elapsed();
511
512 if (failure)
513 failure(ret);
514 } else {
516 ret.elapsed = this->getTimer()->elapsed();
517
518 if (success)
519 success(ret);
520 }
521
522 return (ret);
523}
524
525#endif /* BE_FRAMEWORK_API_H_ */
#define END_SIGNAL_BLOCK(_sigmgr, _blockname)
#define BEGIN_SIGNAL_BLOCK(_sigmgr, _blockname)
BE_FRAMEWORK_ENUMERATION_DECLARATIONS(BiometricEvaluation::Framework::APICurrentState, BE_Framework_APICurrentState_EnumToStringMap)
std::string to_string(const BiometricEvaluation::Memory::AutoArray< T > &aa)
Convert a uint8_t or char AutoArray to a string.
#define END_WATCHDOG_BLOCK(_watchdog, _blockname)
#define BEGIN_WATCHDOG_BLOCK(_watchdog, _blockname)
Macros that are used by applications to indicate the start and end of a watchdog timer block.
A SignalManager object is used to handle signals that come from the operating system.
A StrategyError object is thrown when the underlying implementation of this interface encounters an e...
APICurrentState currentState
Current state of operation.
bool operator!() const
Logical negation operator overload.
std::string getExceptionStr() const noexcept
Obtain the exception string.
void setException(std::exception_ptr e)
Save a thrown exception.
void rethrowException() const
Rethrow the caught exception.
T status
Value returned from operation.
uint64_t elapsed
Time elapsed while calling operation.
A convenient way to execute biometric technology evaluation API methods safely.
Result call(const std::function< T(void)> &operation, const std::function< void(const Result &)> &success={}, const std::function< void(const Result &)> &failure={})
Invoke an operation.
void setRethrowExceptions(const bool shouldRethrow)
Change whether or not exceptions caught in call() should be rethrown.
bool willCatchExceptions() const
Obtain whether or not exceptions raised in call() will be caught, triggering the failure block.
std::shared_ptr< BiometricEvaluation::Error::SignalManager > getSignalManager() noexcept
Obtain the signal manager object.
std::shared_ptr< BiometricEvaluation::Time::Watchdog > getWatchdog() noexcept
Obtain the watchdog timer object.
void setProtectionsEnabled(const bool protectionsEnabled)
Wholesale change of process protections enabled by this object.
std::shared_ptr< BiometricEvaluation::Time::Timer > getTimer() noexcept
Obtain the timer object.
bool protectionsEnabled() const
Obtain whether or not all protections enabled by this object are enabled.
bool willRethrowExceptions() const
Obtain whether or not exceptions caught in call() will be rethrown.
void setCatchExceptions(const bool catchExceptions)
Set whether or not to catch exceptions from call(), triggering the failure block.
A Watchdog object can be used by applications to limit the amount of processing time taken by a block...
APICurrentState
Reasons operations could not complete.
@ NeverCalled
Operation was never executed.
@ SignalCaught
Signal handler was invoked.
This software was developed at the National Institute of Standards and Technology (NIST) by employees...