TimeValue.h

説明を見る。
00001 // -*- C++ -*-
00019 #ifndef TimeValue_h
00020 #define TimeValue_h
00021 
00022 #include <iostream>
00023 #include <ace/OS_NS_time.h>
00024 
00025 #define TIMEVALUE_ONE_SECOND_IN_USECS 1000000 // 1 [sec] = 1000000 [usec]
00026 
00027 
00050 struct TimeValue
00051   : public timeval
00052 {
00076   TimeValue(long sec=0, long usec=0)
00077   {
00078     tv_sec = sec;
00079     tv_usec = usec;
00080     normalize();
00081   }
00082   
00107   TimeValue operator-(TimeValue& tm)
00108   {
00109     TimeValue res;
00110     if (tv_sec >= tm.tv_sec) // +
00111       {
00112         if (tv_usec >= tm.tv_usec) // 繰り下がり無し
00113           {
00114             res.tv_sec  = tv_sec  - tm.tv_sec;  // -
00115             res.tv_usec = tv_usec - tm.tv_usec; // +
00116           }
00117         else // tv_usec < tm.tv_usec 繰り下がり有り
00118           {
00119             res.tv_sec  = tv_sec  - tm.tv_sec - 1;
00120             res.tv_usec = (tv_usec + 1000000) - tm.tv_usec;
00121           }
00122       }
00123     else // tv_sec < tm.tv_sec // -
00124       {
00125         if (tm.tv_usec >= tv_usec) // 繰り下がり無し
00126           {
00127             res.tv_sec  = - (tm.tv_sec  - tv_sec); // +
00128             res.tv_usec = - (tm.tv_usec - tv_usec);  // +
00129           }
00130         else // tm.tv_usec < tv_usec 繰り下がり有り
00131           {
00132             res.tv_sec  = - (tm.tv_sec - tv_sec  - 1);
00133             res.tv_usec = - (tm.tv_usec + 1000000) + tv_usec;
00134           }
00135       }
00136     res.normalize();
00137     return res;
00138   }
00139   
00163   TimeValue operator+(TimeValue& tm)
00164   {
00165     TimeValue res;
00166     res.tv_sec  = tv_sec  + tm.tv_sec;
00167     res.tv_usec = tv_usec + tm.tv_usec;
00168     if (res.tv_usec >= 1000000)
00169       {
00170         ++res.tv_sec;
00171         res.tv_usec -= 1000000;
00172       }
00173     res.normalize();
00174     return res;
00175   }
00176   
00200   TimeValue operator=(double time)
00201   {
00202     tv_sec = (long)time;
00203     tv_usec = (long)((time - (double)tv_sec)*1000000.0 + 0.5);
00204     normalize();
00205     return *this;
00206   }
00207   
00227   double toDouble()
00228   {
00229     normalize();
00230     return (double)tv_sec + ((double)tv_usec/1000000.0);
00231   }
00232   
00252   int sign()
00253   {
00254     if (tv_sec > 0) return 1;
00255     if (tv_sec < 0) return -1;
00256     if (tv_usec > 0) return 1;
00257     if (tv_usec < 0) return -1;
00258     return 0;
00259   }
00260   
00261 private:
00262   
00278   void normalize()
00279   {
00280     if (tv_usec >= TIMEVALUE_ONE_SECOND_IN_USECS)
00281       {
00282         do
00283           {
00284             ++tv_sec;
00285             tv_usec -= TIMEVALUE_ONE_SECOND_IN_USECS;
00286           }
00287         while (tv_usec >= TIMEVALUE_ONE_SECOND_IN_USECS);
00288       }
00289     else if (tv_usec <= -TIMEVALUE_ONE_SECOND_IN_USECS)
00290       {
00291         do
00292           {
00293             --tv_sec;
00294             tv_usec += TIMEVALUE_ONE_SECOND_IN_USECS;
00295           }
00296         while (tv_usec <= -TIMEVALUE_ONE_SECOND_IN_USECS);
00297       }
00298     
00299     if (tv_sec >= 1 && tv_usec < 0)
00300       {
00301         --tv_sec;
00302         tv_usec += TIMEVALUE_ONE_SECOND_IN_USECS;
00303       }
00304     else if (tv_sec < 0 && tv_usec > 0)
00305       {
00306         ++tv_sec;
00307         tv_usec -= TIMEVALUE_ONE_SECOND_IN_USECS;
00308       }
00309   }
00310   
00311   //  friend std::ostream& operator<<(std::ostream& lhs, const TimeValue& rhs);
00312 };
00313 
00314 /***
00315     std::ostream& operator<<(std::ostream& lhs, const TimeValue& rhs)
00316     {
00317     double res;
00318     res = (double)rhs.tv_sec + ((double)rhs.tv_usec/1000000.0);
00319     lhs << res;
00320     return lhs;
00321     }
00322 */
00323 
00324 #endif // TimeValue_h

OpenRTMに対してWed May 21 05:17:25 2008に生成されました。  doxygen 1.5.3