Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

base/Time

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)1996 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: Time 1046 2004-02-27 19:20:16Z jungd $
00019   $Revision: 1.6 $
00020   $Date: 2004-02-27 14:20:16 -0500 (Fri, 27 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #ifndef _BASE_TIME_
00026 #define _BASE_TIME_
00027 
00028 #include <base/base>
00029 #include <base/Object>
00030 
00031 namespace base {
00032 
00033 class Serializer;
00034 
00035 
00036 class Time : public Object
00037 {
00038 public:
00039   Time();
00040   Time(const Time& t) { secs=t.secs; micros=t.micros; }
00041   Time(long secs, long micros) { this->secs=secs; this->micros=micros; }
00042   Time(Real secs);
00043   virtual ~Time();
00044 
00045   virtual String className() const { return String("Time"); }
00046 
00047   bool isPast() const { return less(*this,now()); }
00048 
00049   Time& operator=(const Time& t) { secs=t.secs; micros=t.micros; return *this; }
00050   bool operator==(const Time& t) const { return equal(*this,t); }
00051   Time& operator-=(const Time& t);
00052   Time& operator+=(const Time& t);
00053   Time& operator*=(Real s);
00054   Time& operator/=(Real s) { return operator*=(1.0/s); }
00055 
00056   Real seconds() const { return Real(secs) + Real(micros)/Real(microsPerSec); }
00057 
00058   static const long microsPerSec = 1000000;
00059   static const Time& now();
00060   static bool isPast(const Time& t) { return less(t,now()); }
00061 
00062   bool equals(const Time& t, Time eps = Time(0,10));
00063 
00064   static bool less(const Time& t1, const Time& t2);
00065   static bool greater(const Time& t1, const Time& t2);
00066 
00067   static void sleep(const Time& t);
00068   static void sleep(long secs, long micros);
00069   static void sleep(Real secs) { sleep(Time(secs)); }
00070 
00071   static long timeResolution() { return resolution; } // us
00072 
00073   void serialize(Serializer& s); ///< read or write object state to Serializer
00074 
00075   long secs, micros;
00076 
00077 protected:
00078   static Time* time;
00079 
00080   static long baseSecs;
00081   static long baseMicros;
00082   static Time lastNow;
00083   static long resolution;
00084 
00085   static bool equal(const Time& t1, const Time& t2) { return (t1.secs==t2.secs) && (t1.micros==t2.micros); }
00086   void normalize();
00087 
00088 };
00089 
00090 std::ostream& operator<<(std::ostream& out, const Time& t); // Output
00091 
00092 inline Time operator-(const Time& t1, const Time& t2)
00093 { Time r(t1); r-=t2; return r; }
00094 
00095 inline Time operator+(const Time& t1, const Time& t2)
00096 { Time r(t1); r+=t2; return r; }
00097 
00098 inline Time operator*(const Time& t, Real s)
00099 { Time r(t); r*=s; return r; }
00100 
00101 inline Time operator*(Real s, const Time& t)
00102 { Time r(t); r*=s; return r; }
00103 
00104 inline Time operator/(const Time& t, Real s)
00105 { Time r(t); r/=s; return r; }
00106 
00107 inline bool Time::equals(const Time& t, Time eps)
00108 {
00109   if (Time::equal(*this, t)) return true;
00110 
00111   if (Time::greater(*this, t)) 
00112     return Time::less(*this - t, eps);
00113   return Time::less(t - *this, eps);
00114 
00115 }
00116 
00117 inline bool operator<(const Time& t1, const Time& t2)
00118 { return Time::less(t1,t2); }
00119 
00120 inline bool operator>(const Time& t1, const Time& t2)
00121 { return Time::greater(t1,t2); }
00122 
00123 
00124 
00125 } // base
00126 
00127 #endif

Generated on Thu Jul 29 15:56:15 2004 for OpenSim by doxygen 1.3.6