ewmscp  ..
statCollector.h
Go to the documentation of this file.
1 #ifndef __statCollector_h__
2 #define __statCollector_h__
3 
4 namespace statCollector {
5  class base {
6  private:
7  std::string name;
8  std::string unit;
9  protected:
10  unsigned long long n;
11  public:
12  base(const std::string& aName,
13  const std::string& aUnit = "s") : name(aName), unit(aUnit) {};
14  virtual void print(std::ostream&) const = 0;
15  virtual void reset() = 0;
16  friend std::ostream& operator<<(std::ostream& stream, const base& obj) {
17  obj.print(stream);
18  return stream;
19  }
20  const std::string& getName() const {
21  return name;
22  };
23  const std::string& getUnit() const {
24  return unit;
25  };
26  decltype(n) getN() const {
27  return n;
28  };
29  };
30 
31 
32  template <typename T> class typed: public base {
33  public:
34  T sum;
35  T min;
36  T max;
37  void addValue(T value) {
38  sum += value;
39  if (value < min) {
40  min = value;
41  }
42  if (value > max) {
43  max = value;
44  }
45  this->n++;
46  };
47  template <typename TT = T, typename std::enable_if <std::is_arithmetic<TT>::value>::type..., class ... Types>
48  typed(Types ... args):
49  base(args...),
50  sum(0),
51  min(std::numeric_limits<T>::max()),
52  max(std::numeric_limits<T>::lowest()) {
53  };
54  template < typename TT = T, typename std::enable_if < !std::is_arithmetic<TT>::value >::type..., class ... Types >
55  typed(Types ... args) :
56  base(args...),
57  sum(T::zero()),
58  min(T::max()),
59  max(T::min()) {
60  };
61 
62  private:
63  template<typename TT = T>
64  typename std::enable_if < !std::is_arithmetic<TT>::value >::type
65  _print(std::ostream& stream) const {
66  stream << this->getName() << " avg: " << std::scientific
67  << std::chrono::duration_cast<std::chrono::duration<double>>(sum / this->n).count()
68  << this->getUnit() << ", min: "
69  << std::chrono::duration_cast<std::chrono::duration<double>>(min).count()
70  << this->getUnit() << ", max: "
71  << std::chrono::duration_cast<std::chrono::duration<double>>(max).count()
72  << this->getUnit();
73  }
74  template<typename TT = T>
75  typename std::enable_if <std::is_arithmetic<TT>::value>::type
76  _print(std::ostream& stream) const {
77  stream << this->getName() << " avg: "
78  << sum / this->n
79  << this->getUnit() << ", min: "
80  << min
81  << this->getUnit() << ", max: "
82  << max
83  << this->getUnit();
84  }
85  public:
86  void print(std::ostream& stream) const override {
87  if (n) {
88  _print(stream);
89  } else {
90  stream << this->getName() << " no values yet ";
91  }
92  }
93  private:
94  template<typename TT = T>
95  typename std::enable_if < !std::is_arithmetic<TT>::value >::type
96  _reset() {
97  sum = T::zero();
98  min =T::max();
99  max = T::min();
100  }
101  template<typename TT = T>
102  typename std::enable_if <std::is_arithmetic<TT>::value>::type
103  _reset() {
104  sum=0;
105  min=std::numeric_limits<T>::max();
106  max=std::numeric_limits<T>::lowest();
107  }
108  public:
109  void reset() override {
110  n=0;
111  _reset();
112  }
113  };
114 
115  inline std::chrono::system_clock::time_point timePoint(const struct timespec& spec) {
116  return std::chrono::system_clock::time_point(std::chrono::seconds(spec.tv_sec) +
117  std::chrono::nanoseconds(spec.tv_nsec));
118 
119  }
120 } // end of name space statCollector
121 #endif
statCollector::typed::reset
void reset() override
Definition: statCollector.h:109
statCollector::typed::max
T max
Definition: statCollector.h:36
statCollector::typed::print
void print(std::ostream &stream) const override
Definition: statCollector.h:86
statCollector
Definition: statCollector.h:4
statCollector::base::name
std::string name
Definition: statCollector.h:7
statCollector::base::n
unsigned long long n
Definition: statCollector.h:10
statCollector::typed::_reset
std::enable_if< std::is_arithmetic< TT >::value >::type _reset()
Definition: statCollector.h:103
statCollector::base::getN
decltype(n) getN() const
Definition: statCollector.h:26
statCollector::base::print
virtual void print(std::ostream &) const =0
statCollector::typed::_print
std::enable_if< std::is_arithmetic< TT >::value >::type _print(std::ostream &stream) const
Definition: statCollector.h:76
statCollector::timePoint
std::chrono::system_clock::time_point timePoint(const struct timespec &spec)
Definition: statCollector.h:115
statCollector::typed::min
T min
Definition: statCollector.h:35
statCollector::base::base
base(const std::string &aName, const std::string &aUnit="s")
Definition: statCollector.h:12
statCollector::typed
Definition: statCollector.h:32
statCollector::base
Definition: statCollector.h:5
statCollector::typed::addValue
void addValue(T value)
Definition: statCollector.h:37
statCollector::base::unit
std::string unit
Definition: statCollector.h:8
statCollector::typed::_reset
std::enable_if< !std::is_arithmetic< TT >::value >::type _reset()
Definition: statCollector.h:96
statCollector::typed::_print
std::enable_if< !std::is_arithmetic< TT >::value >::type _print(std::ostream &stream) const
Definition: statCollector.h:65
statCollector::base::operator<<
friend std::ostream & operator<<(std::ostream &stream, const base &obj)
Definition: statCollector.h:16
statCollector::typed::sum
T sum
Definition: statCollector.h:34
statCollector::base::reset
virtual void reset()=0
statCollector::base::getUnit
const std::string & getUnit() const
Definition: statCollector.h:23
statCollector::base::getName
const std::string & getName() const
Definition: statCollector.h:20