- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Utils/Lang/Time.cpp

    00001
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00009 // Organization:        Model-based Embedded and Robotic Systems Group
    00010 //                                      Massachusetts Institute of Technology
    00011 //
    00013
    00014 // File Time.cc
    00015 // class Time
    00016
    00017 #include "Time.h"
    00018
    00019 #include <cstdlib>
    00020 #include <cctype>
    00021 #include <map>
    00022
    00024
    00025 using namespace Mers::Utils::Lang;
    00026 using std::ostream;
    00027 using std::istream;
    00028 using std::cerr;
    00029 using std::cout;
    00030 using std::endl;
    00031
    00033
    00034 // initialize static members
    00035 const Time Time::NEG_INF(-1, true);
    00036 const Time Time::POS_INF(1, true);
    00037
    00038 // Public Constructors
    00039 Time::Time() : value(0), inf_flag(false) {}
    00040
    00041 Time::Time(long val) : value(val), inf_flag(false) {}
    00042
    00043 Time::Time(int val): inf_flag(false) {value = (long) val;}
    00044
    00045 Time::Time(long val, bool inf) : value(val), inf_flag(inf) {}
    00046
    00047 Time::Time(const char * str) : value(0), inf_flag(false)
    00048 {
    00049   // check that str is not null
    00050   if (str == NULL)
    00051   {
    00052         // set to POS_INF
    00053         cerr << "warning::Time( const char*), str is NULL" << endl;
    00054         inf_flag = true;
    00055         value = 1;
    00056         return;
    00057   }
    00058
    00059   // assume that if the first character is a digit then this is a number
    00060   if (isdigit(str[0])) {
    00061         inf_flag = false;
    00062         // Convert string to an integer and assign to variable value
    00063         value = atoi(str);
    00064   }
    00065   // also handle '-' or '+' characters at beginning of string
    00066   else if ((str[0]=='-' || str[0]=='+') && isdigit(str[1])) {
    00067         inf_flag = false;
    00068         value = atoi(str);
    00069   }
    00070   // Assign value to + or - INF
    00071   else {
    00072         inf_flag = true;
    00073         if (strcmp(str,"-INF")==0)
    00074         value = -1;
    00075         else    // buffer should hold "+INF"
    00076           value = 1;
    00077   }
    00078 }
    00079
    00080
    00082 void
    00083 Time::set(long val, bool inf)
    00084 {
    00085   inf_flag = inf;
    00086   value = val;
    00087 }
    00088
    00090 Time&
    00091 Time::operator=(const char* str )
    00092 {
    00093   // check that str is not null
    00094   if (str == NULL )
    00095   {
    00096         cerr << "warning::Time::operator=(const char*), str is NULL" << endl;
    00097         inf_flag = true;
    00098         value = 1;
    00099   }
    00100
    00101   // assume that if the first character is a digit then this is a number
    00102   if (isdigit(str[0])) {
    00103         inf_flag = false;
    00104         // Convert string to an integer and assign to variable value
    00105         value = atoi(str);
    00106   }
    00107   // also handle '-' or '+' characters at beginning of string
    00108   else if ((str[0]=='-' || str[0]=='+') && isdigit(str[1])) {
    00109         inf_flag = false;
    00110         value = atoi(str);
    00111   }
    00112   // Assign value to + or - INF
    00113   else {
    00114         inf_flag = true;
    00115         if (strcmp(str,"-INF")==0)
    00116         value = -1;
    00117         else    // buffer should hold "+INF"
    00118           value = 1;
    00119   }
    00120
    00121   return *this;
    00122 }
    00123
    00124
    00126 Time&
    00127 Time::operator=(const long val)
    00128 {
    00129   inf_flag = false;
    00130   value = val;
    00131   return *this;
    00132 }
    00133
    00134
    00136 Time&
    00137 Time::operator=(const int val)
    00138 {
    00139         inf_flag = false;
    00140         value = (long) val;
    00141         return *this;
    00142 }
    00143
    00144
    00146 Time&
    00147 Time::operator=(const Time& rhs)
    00148 {
    00149   inf_flag = rhs.inf_flag;
    00150   value = rhs.value;
    00151   return *this;
    00152 }
    00153
    00154
    00155 Time
    00156 Time::operator+(const Time& rhs) const {
    00157   Time retVal;
    00158
    00159  /* if (inf_flag && rhs.inf_flag  &&
    00160       ((value>=0 && rhs.value<0) || (value<0 && rhs.value>=0)))
    00161   {
    00162     cerr << "Error: class Time - l.35 time.cc. Invalid addition operation\n";
    00163     exit(0);
    00164   }
    00165 */
    00166         if (inf_flag && rhs.inf_flag)
    00167                 if (value*rhs.value>0)
    00168                         retVal.set(value,true);
    00169                 else
    00170                 {
    00171                         cerr << "Time add error: cannot add neg.inf and pos.inf!"<<endl;
    00172                         exit(-1);
    00173                 }
    00174         else if (inf_flag && !rhs.inf_flag)
    00175                 retVal.set(value,true);
    00176         else if (!inf_flag && rhs.inf_flag)
    00177                 retVal.set(rhs.value,true);
    00178         else
    00179                 retVal.set(value+rhs.value);
    00180
    00181 /*
    00182   if (inf_flag)
    00183     retVal.set(value,true);
    00184   else if (rhs.inf_flag)
    00185     retVal.set(rhs.value,true);
    00186   else
    00187     retVal.set(value+rhs.value);
    00188 */
    00189   return retVal;
    00190 }
    00191
    00192 Time&
    00193 Time::operator+=(const Time& rhs) {
    00194   if (inf_flag == rhs.inf_flag  &&
    00195       ((value>=0 && rhs.value<0) || (value<0 && rhs.value>=0)))
    00196   {
    00197     cerr << "Error: class Time - Invalid addition operation\n";
    00198     exit(0);
    00199   }
    00200
    00201   if (inf_flag)
    00202     return *this;
    00203
    00204   if (rhs.inf_flag) {
    00205     set(rhs.value,true);
    00206   }
    00207   else {
    00208     set(value+rhs.value);
    00209   }
    00210
    00211   return *this;
    00212 }
    00213
    00214 Time
    00215 Time::operator-(const Time& rhs) const {
    00216   Time retVal;
    00217   if (inf_flag && rhs.inf_flag  &&
    00218       ((value>=0 && rhs.value>=0) || (value<0 && rhs.value<0)))
    00219   {
    00220     cerr << "Error: class Time - Invalid subtraction operation\n";
    00221     exit(0);
    00222   }
    00223
    00224   if (inf_flag)
    00225     retVal.set(value,true);
    00226   else if (rhs.inf_flag) {
    00227     if(rhs.value<0)
    00228       retVal.set(-1,true);
    00229     else
    00230       retVal.set(1,true);
    00231   }
    00232   else
    00233     retVal.set(value-rhs.value);
    00234
    00235   return retVal;
    00236 }
    00237
    00238 Time
    00239 Time::operator-() const {
    00240   if (inf_flag) {
    00241         if (value>=0)
    00242                 return Time(-1,true);
    00243         else
    00244                 return Time(1,true);
    00245   }
    00246   else {
    00247         return Time(-value,false);
    00248   }
    00249 }
    00250
    00251 bool
    00252 Time::operator>(const Time& rhs) const {
    00253         if (rhs.inf_flag) {
    00254                 if (inf_flag)
    00255                         return (value>=0 && rhs.value<0);
    00256                 else
    00257                         return (rhs.value<0);
    00258         }
    00259         else {
    00260                 if (inf_flag)
    00261                         return (value>=0);
    00262                 else
    00263                         return (value>rhs.value);
    00264         }
    00265 }
    00266
    00267 bool
    00268 Time::operator<(const Time& rhs) const {
    00269         if (rhs.inf_flag) {
    00270                 if (inf_flag)
    00271                         return (value<0 && rhs.value>=0);
    00272                 else
    00273                         return (rhs.value>=0);
    00274         }
    00275         else {
    00276                 if (inf_flag)
    00277                         return (value<0);
    00278                 else
    00279                         return (value<rhs.value);
    00280         }
    00281 }
    00282
    00283 // This a mathematical equality test
    00284 // Comparison of infinite values always returns false
    00285 bool
    00286 Time::operator==(const Time& rhs) const {
    00287         if (!inf_flag && !rhs.inf_flag)
    00288                 return (value==rhs.value);
    00289         else
    00290                 return false;
    00291 }
    00292
    00293 // This method returns true if two Time objects represent the same value
    00294 // This is different from operator==, which gives a mathematical equality test
    00295 // operator== returns false when comparing infinite values
    00296 // This method returns true if they represent the same type (positive or negative) of infinity
    00297 bool
    00298 Time::HasValue(const Time& rhs) const {
    00299         if (inf_flag && rhs.inf_flag) {
    00300                 return ( ( value>=0 && rhs.value>=0 ) || ( value<0 && rhs.value<0 ) );
    00301         }
    00302         else {
    00303                 return *this == rhs;
    00304         }
    00305 }
    00306
    00307 bool
    00308 Time::operator!=(const Time& rhs) const {
    00309         return !(*this==rhs);
    00310 }
    00311
    00312 bool
    00313 Time::operator<=(const Time& rhs) const {
    00314         return !(*this>rhs);
    00315 }
    00316
    00317 bool
    00318 Time::operator>=(const Time& rhs) const {
    00319         return !(*this<rhs);
    00320 }
    00321
    00322 long
    00323 Time::longval() const {
    00324         // If infinite then fake it
    00325         if (inf_flag && value>=0)
    00326                 return LONG_MAX;
    00327         else if (inf_flag && value<0)
    00328                 return LONG_MIN;
    00329         else
    00330                 return value;
    00331 }
    00332
    00333 bool
    00334 Time::isInfinite() const { // added by AFW
    00335         return inf_flag;
    00336 }
    00337
    00338 Time& Time::min(Time &a, Time &b)
    00339 {
    00340         if (a < b)
    00341                 return a;
    00342         else
    00343                 return b;
    00344 }
    00345
    00346 Time& Time::max(Time &a, Time &b)
    00347 {
    00348         if (a > b)
    00349                 return a;
    00350         else
    00351                 return b;
    00352 }
    00353
    00354
    00355 ostream&
    00356 Mers::Utils::Lang::operator<<(ostream& os, const Time& t) {
    00357   if (t.inf_flag) {
    00358     if (t.value>=0)
    00359       os << "+INF";
    00360     else
    00361       os << "-INF";
    00362   }
    00363   else {
    00364     os << t.value;
    00365   }
    00366   return os;
    00367 }
    00368
    00369 istream&
    00370 Mers::Utils::Lang::operator>>(istream& is, Time& t)
    00371 {
    00372   char buffer[MAX_TIME_LEN];
    00373   is >> buffer;
    00374
    00375   // assume that if the first character is a digit then this is a number
    00376   if (isdigit(buffer[0])) {
    00377         t.inf_flag = false;
    00378         t.value = atoi(buffer);
    00379   }
    00380   // also handle '-' or '+' characters
    00381   else if ((buffer[0]=='-' || buffer[0]=='+') && isdigit(buffer[1])) {
    00382         t.inf_flag = false;
    00383         t.value = atoi(buffer);
    00384   }
    00385   else {
    00386         t.inf_flag = true;
    00387         if (strcmp(buffer,"-INF")==0)
    00388       t.value = -1;
    00389         else    // buffer should hold "+INF"
    00390           t.value = 1;
    00391   }
    00392
    00393   return is;
    00394 }
    00395
    00396
    00397 #ifdef _DEBUG
    00398
    00399 void time_check() {
    00400   // check assignment
    00401   Time t1;
    00402   t1 = 10;
    00403   Time t2 = 2984225;
    00404   Time t3(1,true);
    00405   Time t4(-1,true);
    00406   cout << "t1 = " << t1 << endl;
    00407   cout << "t2 = " << t2 << endl;
    00408   cout << "t3 = " << t3 << endl;
    00409   cout << "t4 = " << t4 << endl;
    00410
    00411   cout << "t1 + t2 = " << t1+t2 << endl;
    00412   cout << "t1 + t3 = " << t1+t3 << endl;
    00413   cout << "t1 = " << t1 << endl;
    00414   cout << "t2 = " << t2 << endl;
    00415
    00416   t1 += t2;
    00417   cout << "t1 += t2 -> t1 = " << t1 << endl;
    00418
    00419         cout << "POS_INF: " << Time::POS_INF << endl;
    00420         cout << "NEG_INF: " << Time::NEG_INF << endl;
    00421 }
    00422
    00423 #endif
    00424