| 1 | /*
 | 
|---|
| 2 |  * Chronos.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Mar 14, 2011
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef CHRONOS_HPP_
 | 
|---|
| 9 | #define CHRONOS_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | // include config.h
 | 
|---|
| 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 13 | #include <config.h>
 | 
|---|
| 14 | #endif
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <iosfwd>
 | 
|---|
| 17 | #include <map>
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include "Singleton.hpp"
 | 
|---|
| 20 | 
 | 
|---|
| 21 | class ChronosTest;
 | 
|---|
| 22 | 
 | 
|---|
| 23 | class Chronos : public Singleton<Chronos>{
 | 
|---|
| 24 |   //!> Singleton pattern needs private access
 | 
|---|
| 25 |   friend class Singleton<Chronos>;
 | 
|---|
| 26 |   //!> unit tests needs access to private members
 | 
|---|
| 27 |   friend class ChronosTest;
 | 
|---|
| 28 |   //!> operator needs to access some internals
 | 
|---|
| 29 |   friend std::ostream& operator<<(std::ostream &ost, const Chronos &_time);
 | 
|---|
| 30 | public :
 | 
|---|
| 31 | 
 | 
|---|
| 32 |   /** Returns current kept time of function \a _name.
 | 
|---|
| 33 |    *
 | 
|---|
| 34 |    * @param _name name of function
 | 
|---|
| 35 |    * @return current amount of time passed for this function, 0 if unknown, -1 if currently running
 | 
|---|
| 36 |    */
 | 
|---|
| 37 |   double getTime(const std::string _name) const;
 | 
|---|
| 38 | 
 | 
|---|
| 39 |   /** Resets time counter for this function \a _name to zero.
 | 
|---|
| 40 |    *
 | 
|---|
| 41 |    * @param _name name of function
 | 
|---|
| 42 |    */
 | 
|---|
| 43 |   void resetTime(const std::string _name);
 | 
|---|
| 44 | 
 | 
|---|
| 45 |   /** Starts Timing for this function \a _name.
 | 
|---|
| 46 |    *
 | 
|---|
| 47 |    * @param _name name of function
 | 
|---|
| 48 |    */
 | 
|---|
| 49 |   void startTiming(const std::string _name);
 | 
|---|
| 50 | 
 | 
|---|
| 51 |   /** Finishes Timing for this function \a _name.
 | 
|---|
| 52 |    *
 | 
|---|
| 53 |    * @param _name name of function
 | 
|---|
| 54 |    */
 | 
|---|
| 55 |   void endTiming(const std::string _name);
 | 
|---|
| 56 | 
 | 
|---|
| 57 |   /** Sums up total time accounted for.
 | 
|---|
| 58 |    *
 | 
|---|
| 59 |    * @return total time in seconds
 | 
|---|
| 60 |    */
 | 
|---|
| 61 |   double SumUpTotalTime() const;
 | 
|---|
| 62 | 
 | 
|---|
| 63 |   /** Sums up all functions accounted.
 | 
|---|
| 64 |    *
 | 
|---|
| 65 |    * @return number of functions
 | 
|---|
| 66 |    */
 | 
|---|
| 67 |   size_t SumUpTotalFunctions() const;
 | 
|---|
| 68 | 
 | 
|---|
| 69 | protected:
 | 
|---|
| 70 |   /** Do not call this constructor directly, use getInstance() instead. */
 | 
|---|
| 71 |   Chronos();
 | 
|---|
| 72 |   /** Do not call this destructor directly, use purgeInstance() instead. */
 | 
|---|
| 73 |   ~Chronos();
 | 
|---|
| 74 | 
 | 
|---|
| 75 | private:
 | 
|---|
| 76 |   /** Returns the current timer in seconds.
 | 
|---|
| 77 |    *
 | 
|---|
| 78 |    * This function is present to allow for changing of time measurements
 | 
|---|
| 79 |    * without the other functions noticing.
 | 
|---|
| 80 |    *
 | 
|---|
| 81 |    * \note We convert clock() by CLOCKS_PER_SEC.
 | 
|---|
| 82 |    *
 | 
|---|
| 83 |    * @return current time in unit of seconds
 | 
|---|
| 84 |    */
 | 
|---|
| 85 |   double getCurrentTime() const;
 | 
|---|
| 86 | 
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   typedef std::map<const std::string, double> TimekeepingMap;
 | 
|---|
| 89 |   typedef std::map<const std::string, double> TimerStatusMap;
 | 
|---|
| 90 | 
 | 
|---|
| 91 |   TimekeepingMap AccountedTime;
 | 
|---|
| 92 |   TimerStatusMap TimeRunning;
 | 
|---|
| 93 | };
 | 
|---|
| 94 | 
 | 
|---|
| 95 | /** Output for Chronos.
 | 
|---|
| 96 |  *
 | 
|---|
| 97 |  * @param ost output stream
 | 
|---|
| 98 |  * @param _time Chronos to print
 | 
|---|
| 99 |  * @return reference to given output stream for concatenation
 | 
|---|
| 100 |  */
 | 
|---|
| 101 | std::ostream& operator<<(std::ostream &ost, const Chronos &_time);
 | 
|---|
| 102 | 
 | 
|---|
| 103 | #endif /* CHRONOS_HPP_ */
 | 
|---|