| [93abe8] | 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 | 
 | 
|---|
| [9b8fa4] | 19 | #include "CodePatterns/Singleton.hpp"
 | 
|---|
| [93abe8] | 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;
 | 
|---|
| [8e24ef] | 28 |   //!> operator needs to access some internals
 | 
|---|
 | 29 |   friend std::ostream& operator<<(std::ostream &ost, const Chronos &_time);
 | 
|---|
| [93abe8] | 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 |    */
 | 
|---|
| [8e24ef] | 37 |   double getTime(const std::string _name) const;
 | 
|---|
| [93abe8] | 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 |    *
 | 
|---|
| [8e24ef] | 59 |    * @return total time in seconds
 | 
|---|
| [93abe8] | 60 |    */
 | 
|---|
| [8e24ef] | 61 |   double SumUpTotalTime() const;
 | 
|---|
| [93abe8] | 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:
 | 
|---|
| [8e24ef] | 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.
 | 
|---|
| [760f97c] | 82 |    * \note We convert struct tms::tms_utime by _SC_CLK_TCK via sysconf().
 | 
|---|
| [8e24ef] | 83 |    *
 | 
|---|
 | 84 |    * @return current time in unit of seconds
 | 
|---|
 | 85 |    */
 | 
|---|
 | 86 |   double getCurrentTime() const;
 | 
|---|
 | 87 | 
 | 
|---|
| [93abe8] | 88 | 
 | 
|---|
| [8e24ef] | 89 |   typedef std::map<const std::string, double> TimekeepingMap;
 | 
|---|
 | 90 |   typedef std::map<const std::string, double> TimerStatusMap;
 | 
|---|
| [93abe8] | 91 | 
 | 
|---|
 | 92 |   TimekeepingMap AccountedTime;
 | 
|---|
| [8e24ef] | 93 |   TimerStatusMap TimeRunning;
 | 
|---|
| [93abe8] | 94 | };
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 | /** Output for Chronos.
 | 
|---|
 | 97 |  *
 | 
|---|
 | 98 |  * @param ost output stream
 | 
|---|
 | 99 |  * @param _time Chronos to print
 | 
|---|
 | 100 |  * @return reference to given output stream for concatenation
 | 
|---|
 | 101 |  */
 | 
|---|
 | 102 | std::ostream& operator<<(std::ostream &ost, const Chronos &_time);
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 | #endif /* CHRONOS_HPP_ */
 | 
|---|