stime.h

00001 /*<std-header orig-src='shore' incl-file-exclusion='STIME_H'>
00002 
00003  $Id: stime.h,v 1.23 2010/12/09 15:20:12 nhall Exp $
00004 
00005 SHORE -- Scalable Heterogeneous Object REpository
00006 
00007 Copyright (c) 1994-99 Computer Sciences Department, University of
00008                       Wisconsin -- Madison
00009 All Rights Reserved.
00010 
00011 Permission to use, copy, modify and distribute this software and its
00012 documentation is hereby granted, provided that both the copyright
00013 notice and this permission notice appear in all copies of the
00014 software, derivative works or modified versions, and any portions
00015 thereof, and that both notices appear in supporting documentation.
00016 
00017 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
00018 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
00019 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
00020 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
00021 
00022 This software was developed with support by the Advanced Research
00023 Project Agency, ARPA order number 018 (formerly 8230), monitored by
00024 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
00025 Further funding for this work was provided by DARPA through
00026 Rome Research Laboratory Contract No. F30602-97-2-0247.
00027 
00028 */
00029 
00030 #ifndef STIME_H
00031 #define STIME_H
00032 
00033 #include "w_defines.h"
00034 
00035 /*  -- do not edit anything above this line --   </std-header>*/
00036 
00037 /**\cond skip */
00038 /* Skip documentation.
00039  * This isn't really for general consumption; it's used for
00040  * debugging and testing.
00041  * */
00042 
00043 #include <sys/time.h>
00044 
00045 /**\brief General-purpose time interval class.
00046 *
00047 *  It's used as a
00048 *  cast so that you can subtract two times and print the result as an
00049 *  interval rather than an absolute date.
00050 *
00051 *  Stime_t is an class that lets you build an underlying implementation
00052 *  which can utilize whatever the "best" time
00053 *  information on a system is.  
00054 *  
00055 *  The current implementation, uses either BSD 'struct timeval' or
00056 *  Posix 'struct timespec' to represent both intervals and timestamps.
00057 *  Future implementors are encouraged to use something similar if other
00058 *  high resolution timers are available.  The combination of a
00059 *  "time-of-day" portion of time, and a "high resolution" between
00060 *  time-of-day increments works well for both purposes.
00061 *
00062 */
00063 
00064 // USE_POSIX_TIME has protection from double-define so that
00065 // you can override this in shore.def
00066 #if     defined(HAVE_CLOCK_GETTIME)
00067 #ifndef USE_POSIX_TIME
00068 #define USE_POSIX_TIME 1
00069 #endif
00070 #elif   !defined(HAVE_GETTIMEOFDAY)
00071 #error  No suitable get-time system call. Cannot build.
00072 #endif
00073 
00074 class stime_t {
00075 protected:
00076 #ifdef USE_POSIX_TIME
00077     struct    timespec    _time;
00078 #else
00079     struct    timeval        _time;
00080 #endif
00081 
00082     /* better method name, PLEASE */
00083     void    gettime();
00084 
00085     void signs();
00086     void _normalize();
00087     void normalize();
00088 
00089     /* only good INSIDE, since workings exposed if public */
00090     stime_t(time_t, long);        /* time-of-day, hr-secs */
00091 
00092 public:
00093     stime_t() {
00094         _time.tv_sec = 0;
00095 #ifdef USE_POSIX_TIME
00096         _time.tv_nsec = 0;
00097 #else
00098         _time.tv_usec = 0;
00099 #endif
00100     }
00101 
00102 #ifdef USE_POSIX_TIME
00103     stime_t(const struct timespec &ts);
00104 #endif
00105     stime_t(const struct timeval &tv);
00106 
00107     // an interval in seconds.
00108     stime_t(int);
00109     stime_t(long);
00110 
00111     // an interval in floating point seconds
00112     stime_t(double);
00113 
00114     /* comparison primitives */
00115     bool    operator==(const stime_t &) const;
00116     bool    operator<(const stime_t &) const;
00117     bool    operator<=(const stime_t &) const;
00118     /* derived compares */
00119     bool    operator!=(const stime_t &r) const { return !(*this == r); }
00120     bool    operator>(const stime_t &r) const { return !(*this <= r); }
00121     bool    operator>=(const stime_t &r) const { return !(*this < r); }
00122 
00123     // negate an interval
00124     stime_t    operator-() const;
00125 
00126     /* times can be added and subtracted */
00127     stime_t    operator+(const stime_t &r) const;
00128     stime_t    operator-(const stime_t &r) const;
00129 
00130     /* adjust an interval by a factor ... an experiment in progress */
00131     /* XXX should this be confined to sinterval_t ??? */ 
00132     stime_t operator*(const int factor) const;
00133     stime_t operator/(const int factor) const;
00134     stime_t operator*(const double factor) const;
00135     stime_t operator/(const double factor) const;
00136 
00137     // operatorX= variants
00138     stime_t &operator+=(const stime_t &r);
00139     stime_t &operator-=(const stime_t &r);
00140 
00141     /* XXX need a rounding operator?? */ 
00142 
00143     /* output conversions.  int/long is not available,
00144        because it doesn't have the dynamic range */
00145     operator double() const;
00146     operator float() const;
00147 #ifdef USE_POSIX_TIME
00148     operator struct timespec() const;
00149 #endif
00150 
00151     // for type conversion:
00152     operator struct timeval() const;
00153     
00154     /* XXX really belongs in sinterval_t */
00155     /* simple output conversions for integers to eliminate fp */
00156     long    secs() const;        /* seconds */
00157     long    msecs() const;        /* milli seconds */
00158     long    usecs() const;        /* micro seconds */
00159     long    nsecs() const;        /* nano seconds */
00160 
00161     /* input conversion operators for integral types */
00162     static    stime_t    sec(int seconds); 
00163     static    stime_t    usec(int micro_seconds, int seconds = 0);
00164     static    stime_t    msec(int milli_seconds, int seconds = 0);
00165     static    stime_t    nsec(int nano_seconds, int seconds = 0);
00166 
00167     /* the Current time */
00168     static    stime_t    now();
00169 
00170     ostream&             print(ostream &s) const;
00171     ostream&             ctime(ostream &s) const;
00172 };
00173 
00174 
00175 /* Intervals are different, in some ways, from absolute
00176    times.  For now, they are to change print methods */
00177 
00178 class sinterval_t : public stime_t {
00179 public:
00180     /* XXX why do I duplicate the constructors ???  There
00181      is or was a reason for it. */
00182 
00183     sinterval_t() : stime_t() { }
00184 #ifdef USE_POSIX_TIME
00185     sinterval_t(const struct timespec &ts) : stime_t(ts) { }
00186 #endif
00187     sinterval_t(const struct timeval &tv) : stime_t(tv) { }
00188 
00189     sinterval_t(const stime_t &time) : stime_t(time) { }
00190     sinterval_t(int time) : stime_t(time) { }
00191     sinterval_t(long time) : stime_t(time) { }
00192     sinterval_t(double time) : stime_t(time) { }
00193 
00194     ostream    &print(ostream &s) const;
00195 };
00196 
00197 
00198 extern ostream &operator<<(ostream &s, const stime_t &t);
00199 extern ostream &operator<<(ostream &s, const sinterval_t &t);
00200 
00201 /**\endcond skip */
00202 
00203 /*<std-footer incl-file-exclusion='STIME_H'>  -- do not edit anything below this line -- */
00204 
00205 #endif          /*</std-footer>*/

Generated on Mon Jan 2 15:13:56 2012 for Shore Storage Manager by  doxygen 1.4.7