All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AtomicPassCounter.h
Go to the documentation of this file.
1 /**
2  * @file icarusalg/Utilities/AtomicPassCounter.h
3  * @brief Class to keep count of a pass/fail result (thread-safe).
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date September 17, 2021
6  *
7  * This library is header-only.
8  */
9 
10 #ifndef ICARUSALG_UTILITIES_ATOMICPASSCOUNTER_H
11 #define ICARUSALG_UTILITIES_ATOMICPASSCOUNTER_H
12 
13 // ICARUS libraries
15 
16 // C/C++ standard libraries
17 #include <atomic>
18 
19 
20 // -----------------------------------------------------------------------------
21 namespace icarus::ns::util {
22  template <typename Count> class AtomicPassCounter;
23 }
24 /**
25  * @brief Class counting pass/fail events.
26  * @tparam Count (default: `unsigned int`) type of counter
27  * @see icarus::ns::util::PassCounter
28  *
29  * This is an implementation of `icarus::ns::util::PassCounter` using atomic
30  * counters, inherently thread-safe.
31  * Only `Count` types that are lock-free (``) are supported.
32  *
33  * This class exposes an interface equivalent to `PassCounter`: see its
34  * documentation for usage details.
35  *
36  */
37 template <typename Count = unsigned int>
39  : public icarus::ns::util::PassCounter<std::atomic<Count>>
40 {
41  static_assert(std::atomic<Count>::is_always_lock_free,
42  "Only types whose atomic type is non-blocking are supported."
43  );
44 
46 
47  public:
48  using Count_t = Count; ///< Type used for counters.
49 
50  // constructors are all default
51 
52  // --- BEGIN -- Access -------------------------------------------------------
53  /// @name Access
54  /// @{
55 
56  /// Returns the number of events which "passed".
57  Count_t passed() const { return Base_t::passedRef().load(); }
58 
59  /// Returns the number of events which "failed".
60  Count_t failed() const { return total() - passed(); }
61 
62  /// Returns the total number of registered events.
63  Count_t total() const { return Base_t::totalRef().load(); }
64 
65  /// Returns whether there is no event recorded yet.
66  bool empty() const { return total() == Count_t{}; }
67 
68  /// @}
69  // --- END ---- Access -------------------------------------------------------
70 
71 }; // icarus::ns::util::AtomicPassCounter<>
72 
73 
74 // -----------------------------------------------------------------------------
75 
76 #endif // ICARUSALG_UTILITIES_ATOMICPASSCOUNTER_H
Count_t total() const
Returns the total number of registered events.
Count_t passed() const
Returns the number of events which &quot;passed&quot;.
Class to keep count of a pass/fail result.
Count_t failed() const
Returns the number of events which &quot;failed&quot;.
Count_t const & totalRef() const
Direct read-only access to data members for derived classes.
Definition: PassCounter.h:90
Count Count_t
Type used for counters.
bool empty() const
Returns whether there is no event recorded yet.
Class counting pass/fail events.
Definition: PassCounter.h:15
Count_t const & passedRef() const
Direct read-only access to data members for derived classes.
Definition: PassCounter.h:93
Class counting pass/fail events.