All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PassCounter.h
Go to the documentation of this file.
1 /**
2  * @file icarusalg/Utilities/PassCounter.h
3  * @brief Class to keep count of a pass/fail result.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date February 10, 2021
6  *
7  * This library is header-only.
8  */
9 
10 #ifndef ICARUSALG_UTILITIES_PASSCOUNTER_H
11 #define ICARUSALG_UTILITIES_PASSCOUNTER_H
12 
13 // -----------------------------------------------------------------------------
14 namespace icarus::ns::util
15  { template <typename Count = unsigned int> class PassCounter; }
16 /**
17  * @brief Class counting pass/fail events.
18  * @tparam Count (default: `unsigned int`) type of counter
19  *
20  * The class keeps track of events which may fall in one of two categories,
21  * called "passed" and "failed".
22  * Example of usage:
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
24  * icarus::ns::util::PassCounter<> OddCounter;
25  *
26  * for (int i = 0; i < 15; ++i) OddCounter.add(i % 2 == 1);
27  *
28  * std::cout << "Counted " << OddCounter.passed() << " odd entries and "
29  * << OddCounter.failed() << " even entries, "
30  * << OddCounter.total() << " in total." << std::endl;
31  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32  * will print: `Counted 7 odd entries and 8 even entries, 15 in total.`.
33  *
34  * The type `Count` must support:
35  * * increment `operator++()`
36  * * default construction which initializes to a "zero" value
37  * * usual copy and assignment
38  * * difference `operator-(Count)`
39  *
40  */
41 template <typename Count /* = unsigned int */>
43 
44  public:
45  using Count_t = Count; ///< Type used for counters.
46 
47  // constructors are all default
48 
49  // --- BEGIN -- Access -------------------------------------------------------
50  /// @name Access
51  /// @{
52 
53  /// Returns the number of events which "passed".
54  Count_t passed() const { return fPassed; }
55 
56  /// Returns the number of events which "failed".
57  Count_t failed() const { return total() - passed(); }
58 
59  /// Returns the total number of registered events.
60  Count_t total() const { return fTotal; }
61 
62  /// Returns whether there is no event recorded yet.
63  bool empty() const { return total() == Count_t{}; }
64 
65  /// @}
66  // --- END ---- Access -------------------------------------------------------
67 
68  // --- BEGIN -- Registration and reset ---------------------------------------
69  /// @name Registration and reset
70  /// @{
71 
72  /// Adds a single event, specifying whether it "passes" or not.
73  void add(bool pass);
74 
75  /// Adds a single event which did not "pass".
76  void addFailed() { add(false); }
77 
78  /// Adds a single event which did "pass".
79  void addPassed() { add(true); }
80 
81  /// Resets all counts.
82  void reset();
83 
84  /// @}
85  // --- END ---- Registration and reset ---------------------------------------
86 
87  protected:
88 
89  /// Direct read-only access to data members for derived classes.
90  Count_t const& totalRef() const { return fTotal; }
91 
92  /// Direct read-only access to data members for derived classes.
93  Count_t const& passedRef() const { return fPassed; }
94 
95  private:
96 
97  // --- BEGIN -- Data members -------------------------------------------------
98 
99  Count_t fTotal{}; ///< Total entries.
100  Count_t fPassed{}; ///< Entries which "passed".
101 
102  // --- END ---- Data members -------------------------------------------------
103 
104 }; // icarus::ns::util::PassCounter<>
105 
106 
107 
108 // -----------------------------------------------------------------------------
109 // --- template implementation
110 // -----------------------------------------------------------------------------
111 template <typename Count>
113 
114  ++fTotal;
115  if (pass) ++fPassed;
116 
117 } // icarus::ns::util::PassCounter<>::add()
118 
119 
120 // -----------------------------------------------------------------------------
121 template <typename Count>
123 
124  fTotal = Count_t{};
125  fPassed = Count_t{};
126 
127 } // icarus::ns::util::PassCounter<>::reset()
128 
129 
130 // -----------------------------------------------------------------------------
131 
132 #endif // ICARUSALG_UTILITIES_PASSCOUNTER_H
void reset()
Resets all counts.
Definition: PassCounter.h:122
Count_t total() const
Returns the total number of registered events.
Definition: PassCounter.h:60
Count_t const & totalRef() const
Direct read-only access to data members for derived classes.
Definition: PassCounter.h:90
void addFailed()
Adds a single event which did not &quot;pass&quot;.
Definition: PassCounter.h:76
bool empty() const
Returns whether there is no event recorded yet.
Definition: PassCounter.h:63
Count_t fTotal
Total entries.
Definition: PassCounter.h:99
void add(bool pass)
Adds a single event, specifying whether it &quot;passes&quot; or not.
Definition: PassCounter.h:112
void addPassed()
Adds a single event which did &quot;pass&quot;.
Definition: PassCounter.h:79
Count_t failed() const
Returns the number of events which &quot;failed&quot;.
Definition: PassCounter.h:57
std::atomic< Count > Count_t
Type used for counters.
Definition: PassCounter.h:45
Count_t passed() const
Returns the number of events which &quot;passed&quot;.
Definition: PassCounter.h:54
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
Count_t fPassed
Entries which &quot;passed&quot;.
Definition: PassCounter.h:100