Timer.hh 3.81 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
// ============================================================================
// $Id: Timer.hh,v 1.1.1.1 2006-10-25 16:14:15 habbecke Exp $
// ----------------------------------------------------------------------------
#ifndef TIMER_HH
#define TIMER_HH
// ----------------------------------------------------------------------------

/** \file Timer.hh
    A timer class
*/


// ----------------------------------------------------------------------------

#include <OpenMesh/Core/System/config.hh>
//
#include <iostream>
#include <string>
#if defined(OM_CC_MIPS)
#  include <assert.h>
#else
#  include <cassert>
#endif


// ------------------------------------------------------------- namespace ----

namespace OpenMesh {
namespace Utils {


// -------------------------------------------------------------- forwards ----


class TimerImpl;


// ----------------------------------------------------------------- class ----

/** Timer class
 */
class Timer
{
public:

  /// Formatting options for member Timer::as_string()
  enum Format {
    Automatic,
    Long,
    Hours,
    Minutes,
    Seconds,
    HSeconds,
    MSeconds,
    MicroSeconds,
    NanoSeconds
  };

  Timer(void);
  ~Timer(void);

  /// Returns true if self is in a valid state!
  bool is_valid() const { return state_!=Invalid; }

  bool is_stopped() const { return state_==Stopped; }

  /// Reset the timer
  void reset(void);

  /// Start measurement
  void start(void);

  /// Stop measurement
  void stop(void);

  /// Continue measurement
  void cont(void);

  /// Give resolution of timer. Depends on the underlying measurement method.
  float resolution() const;
    
  /// Returns measured time in seconds, if the timer is in state 'Stopped'
  double seconds(void) const;

  /// Returns measured time in hundredth seconds, if the timer is in state 'Stopped'
  double hseconds(void) const { return seconds()*1e2; }

  /// Returns measured time in milli seconds, if the timer is in state 'Stopped'
  double mseconds(void) const { return seconds()*1e3; }

  /// Returns measured time in micro seconds, if the timer is in state 'Stopped'
  double useconds(void) const { return seconds()*1e6; }
  
  /** Returns the measured time as a string. Use the format flags to specify
      a wanted resolution.
   */
  std::string as_string(Format format = Automatic);
  
  /** Returns a given measured time as a string. Use the format flags to 
      specify a wanted resolution.
   */
  static std::string as_string(double seconds, Format format = Automatic);

public:

  //@{
  /// Compare timer values
  bool operator < (const Timer& t2) const 
  { 
    assert( is_stopped() && t2.is_stopped() ); 
    return (seconds() < t2.seconds()); 
  }

  bool operator > (const Timer& t2) const
  { 
    assert( is_stopped() && t2.is_stopped() ); 
    return (seconds() > t2.seconds()); 
  }

  bool operator == (const Timer& t2) const
  { 
    assert( is_stopped() && t2.is_stopped() ); 
    return (seconds() == t2.seconds()); 
  }

  bool operator <= (const Timer& t2) const
  {
    assert( is_stopped() && t2.is_stopped() ); 
    return (seconds() <= t2.seconds()); 
  }

  bool operator >=(const Timer& t2) const
  { 
    assert( is_stopped() && t2.is_stopped() ); 
    return (seconds() >= t2.seconds()); 
  }
  //@}

protected:

  TimerImpl *impl_;

  enum {
    Invalid = -1,
    Stopped =  0,
    Running =  1
  } state_;

};


/** Write seconds to output stream. 
 *  Timer must be stopped before.
 *  \relates Timer
 */
inline std::ostream& operator << (std::ostream& _o, const Timer& _t)
{
   return (_o << _t.seconds());
}


// ============================================================================
} // END_NS_UTILS
} // END_NS_OPENMESH
// ============================================================================
#endif
// end of Timer.hh
// ===========================================================================