OpenMesh
OpenMesh/Tools/Utils/Timer.hh
Go to the documentation of this file.
00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                               OpenMesh                                    *
00004  *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openmesh.org                                *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------* 
00008  *  This file is part of OpenMesh.                                           *
00009  *                                                                           *
00010  *  OpenMesh is free software: you can redistribute it and/or modify         * 
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenMesh is distributed in the hope that it will be useful,              *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenMesh.  If not,                                    *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/ 
00034 
00035 /*===========================================================================*\
00036  *                                                                           *             
00037  *   $Revision: 501 $                                                         *
00038  *   $Date: 2012-01-16 11:40:50 +0100 (Mo, 16 Jan 2012) $                   *
00039  *                                                                           *
00040 \*===========================================================================*/
00041 
00042 #ifndef TIMER_HH
00043 #define TIMER_HH
00044 // ----------------------------------------------------------------------------
00045 
00051 // ----------------------------------------------------------------------------
00052 
00053 #include <OpenMesh/Core/System/config.hh>
00054 //
00055 #include <iostream>
00056 #include <string>
00057 #if defined(OM_CC_MIPS)
00058 #  include <assert.h>
00059 #else
00060 #  include <cassert>
00061 #endif
00062 
00063 
00064 // ------------------------------------------------------------- namespace ----
00065 
00066 namespace OpenMesh {
00067 namespace Utils {
00068 
00069 
00070 // -------------------------------------------------------------- forwards ----
00071 
00072 
00073 class TimerImpl;
00074 
00075 
00076 // ----------------------------------------------------------------- class ----
00077 
00080 class Timer
00081 {
00082 public:
00083 
00085   enum Format {
00086     Automatic,
00087     Long,
00088     Hours,
00089     Minutes,
00090     Seconds,
00091     HSeconds,
00092     MSeconds,
00093     MicroSeconds,
00094     NanoSeconds
00095   };
00096 
00097   Timer(void);
00098   ~Timer(void);
00099 
00101   bool is_valid() const { return state_!=Invalid; }
00102 
00103   bool is_stopped() const { return state_==Stopped; }
00104 
00106   void reset(void);
00107 
00109   void start(void);
00110 
00112   void stop(void);
00113 
00115   void cont(void);
00116 
00118   float resolution() const;
00119     
00121   double seconds(void) const;
00122 
00124   double hseconds(void) const { return seconds()*1e2; }
00125 
00127   double mseconds(void) const { return seconds()*1e3; }
00128 
00130   double useconds(void) const { return seconds()*1e6; }
00131   
00135   std::string as_string(Format format = Automatic);
00136   
00140   static std::string as_string(double seconds, Format format = Automatic);
00141 
00142 public:
00143 
00145 
00146   bool operator < (const Timer& t2) const 
00147   { 
00148     assert( is_stopped() && t2.is_stopped() ); 
00149     return (seconds() < t2.seconds()); 
00150   }
00151 
00152   bool operator > (const Timer& t2) const
00153   { 
00154     assert( is_stopped() && t2.is_stopped() ); 
00155     return (seconds() > t2.seconds()); 
00156   }
00157 
00158   bool operator == (const Timer& t2) const
00159   { 
00160     assert( is_stopped() && t2.is_stopped() ); 
00161     return (seconds() == t2.seconds()); 
00162   }
00163 
00164   bool operator <= (const Timer& t2) const
00165   {
00166     assert( is_stopped() && t2.is_stopped() ); 
00167     return (seconds() <= t2.seconds()); 
00168   }
00169 
00170   bool operator >=(const Timer& t2) const
00171   { 
00172     assert( is_stopped() && t2.is_stopped() ); 
00173     return (seconds() >= t2.seconds()); 
00174   }
00176 
00177 protected:
00178 
00179   TimerImpl *impl_;
00180 
00181   enum {
00182     Invalid = -1,
00183     Stopped =  0,
00184     Running =  1
00185   } state_;
00186 
00187 };
00188 
00189 
00194 inline std::ostream& operator << (std::ostream& _o, const Timer& _t)
00195 {
00196    return (_o << _t.seconds());
00197 }
00198 
00199 
00200 // ============================================================================
00201 } // END_NS_UTILS
00202 } // END_NS_OPENMESH
00203 // ============================================================================
00204 #endif
00205 // end of Timer.hh
00206 // ===========================================================================
00207 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines