g++/final: don't add extra code to check for buffer overflows (-fno-stack-protector)
[senf.git] / senf / Utils / StatisticAccumulator.hh
1 // $Id$
2 //
3 // Copyright (C) 2010
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Mathias Kretschmer <mtk@berlios.de>
27 //   Jens Moedeker <jmo@berlios.de>
28
29 /** \file
30     \brief StatisticAccumulator public header */
31
32 #ifndef HH_SENF_Utils_StatisticAccumulator_
33 #define HH_SENF_Utils_StatisticAccumulator_ 1
34
35 // Custom includes
36 #include <math.h>
37 #include <boost/cstdint.hpp>
38 #include <senf/Utils/Format.hh>
39 ///////////////////////////////hh.p////////////////////////////////////////
40 namespace senf {
41
42      struct StatisticsData
43      {
44          StatisticsData(float min_, float avg_, float max_, float stddev_, boost::uint32_t count_)
45              : min(min_), avg(avg_), max(max_), stddev(stddev_), count(count_) {};
46          StatisticsData(StatisticsData const & other)
47              : min(other.min), avg(other.avg), max(other.max), stddev(other.stddev), count(other.count) {};
48          StatisticsData()
49              : min(0.0), avg(0.0), max(0.0), stddev(0.0), count(0) {};
50
51          float min;
52          float avg;
53          float max;
54          float stddev;
55          boost::uint32_t count;
56      };
57
58      std::ostream & operator<<(std::ostream & os, StatisticsData const & _data);
59
60      /** \brief Accumulate measurement values
61
62          The accumulator mainly do the prelimenary work for the senf::Statistic class.
63          It accumulates certain values with in an interval to be used by senf::Statistics
64
65          \li the senf::Statistics class
66          \li statistics sources
67
68          This class provides the average, standard deviation, min, max values over one
69          interval, means until clear() is called. It rather directly calculates the results
70          then collection all single values provided by calling accumulate().
71          \see senf::Statistics to process accumulated values
72          \ingroup senf_statistics
73       */
74     template <class T>
75     class StatisticAccumulator
76     {
77     public:
78         StatisticAccumulator();
79
80         void clear();                   ///< Reset accumulated values.
81                                         /**< This member reset all values. */
82         void accumulate(T value);       ///< Gather value to be accumulated.
83                                         /**< This method accumulates a value.
84                                              \param[in] value to be accumulated value */
85         void setLastAvg(T value);       ///< If no real data was collected, this method specifies the min/avg/max value to be returned.
86                                         /**< This method specifies the the min/avg/max value to
87                                              be returned if no real data was collected
88                                              \param[in] value to be returned */
89
90         T     min() const;              ///< Returns current minimal value.
91                                         /**< This method returns the minimal value of the
92                                              current accumulation. */
93         T     max() const;              ///< Returns current maximal value.
94                                         /**< This method returns the maximal value of the
95                                              current accumulation.*/
96         float avg() const;              ///< Returns average value.
97                                         /**< This method returns the average value of the
98                                              current accumulation.*/
99         float last_avg() const;         ///< Returns former average value.
100                                         /**< This method returns the average value of the
101                                              former accumulation period.*/
102         float stddev() const;           ///< Returns standard deviation value.
103                                         /**< This method returns the standard deviation
104                                              value of the current accumulation.*/
105         boost::uint32_t count() const;  ///< Returns count of accumulated values.
106                                         /**< This method returns count of accumulated
107                                              values of the current accumulation.*/
108         void data(StatisticsData & data_) const;
109                                         ///< Returns the accumulated data as a tuple
110                                         /**< This method returns the accumulated information
111                                              as a tuple.*/
112     private:
113         T sum_squared_;
114         T sum_;
115         T min_;
116         T max_;
117         float last_avg_;
118         boost::uint32_t count_;
119     };
120
121 }
122 ///////////////////////////////hh.e////////////////////////////////////////
123 //#include "StatisticAccumulator.cci"
124 #include "StatisticAccumulator.ct"
125 #include "StatisticAccumulator.cti"
126 #endif
127