stime.h

00001 /*<std-header orig-src='shore' incl-file-exclusion='STIME_H'>
00002 
00003  $Id: stime.h,v 1.22 2010/07/29 21:22:43 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 extern "C" {
00044 #include <sys/time.h>
00045 }
00046 
00047 /**\brief General-purpose time interval class.
00048 *
00049 *  It's used as a
00050 *  cast so that you can subtract two times and print the result as an
00051 *  interval rather than an absolute date.
00052 *
00053 *  Stime_t is an class that lets you build an underlying implementation
00054 *  which can utilize whatever the "best" time
00055 *  information on a system is.  
00056 *  
00057 *  The current implementation, uses either BSD 'struct timeval' or
00058 *  Posix 'struct timespec' to represent both intervals and timestamps.
00059 *  Future implementors are encouraged to use something similar if other
00060 *  high resolution timers are available.  The combination of a
00061 *  "time-of-day" portion of time, and a "high resolution" between
00062 *  time-of-day increments works well for both purposes.
00063 *
00064 */
00065 
00066 // USE_POSIX_TIME has protection from double-define so that
00067 // you can override this in shore.def
00068 #if     defined(HAVE_CLOCK_GETTIME)
00069 #ifndef USE_POSIX_TIME
00070 #define USE_POSIX_TIME 1
00071 #endif
00072 #elif   !defined(HAVE_GETTIMEOFDAY)
00073 #error  No suitable get-time system call. Cannot build.
00074 #endif
00075 
00076 class stime_t {
00077 protected:
00078 #ifdef USE_POSIX_TIME
00079     struct    timespec    _time;
00080 #else
00081     struct    timeval        _time;
00082 #endif
00083 
00084     /* better method name, PLEASE */
00085     void    gettime();
00086 
00087     void signs();
00088     void _normalize();
00089     void normalize();
00090 
00091     /* only good INSIDE, since workings exposed if public */
00092     stime_t(time_t, long);        /* time-of-day, hr-secs */
00093 
00094 public:
00095     stime_t() {
00096         _time.tv_sec = 0;
00097 #ifdef USE_POSIX_TIME
00098         _time.tv_nsec = 0;
00099 #else
00100         _time.tv_usec = 0;
00101 #endif
00102     }
00103 
00104 #ifdef USE_POSIX_TIME
00105     stime_t(const struct timespec &ts);
00106 #endif
00107     stime_t(const struct timeval &tv);
00108 
00109     // an interval in seconds.
00110     stime_t(int);
00111     stime_t(long);
00112 
00113     // an interval in floating point seconds
00114     stime_t(double);
00115 
00116     /* comparison primitives */
00117     bool    operator==(const stime_t &) const;
00118     bool    operator<(const stime_t &) const;
00119     bool    operator<=(const stime_t &) const;
00120     /* derived compares */
00121     bool    operator!=(const stime_t &r) const { return !(*this == r); }
00122     bool    operator>(const stime_t &r) const { return !(*this <= r); }
00123     bool    operator>=(const stime_t &r) const { return !(*this < r); }
00124 
00125     // negate an interval
00126     stime_t    operator-() const;
00127 
00128     /* times can be added and subtracted */
00129     stime_t    operator+(const stime_t &r) const;
00130     stime_t    operator-(const stime_t &r) const;
00131 
00132     /* adjust an interval by a factor ... an experiment in progress */
00133     /* XXX should this be confined to sinterval_t ??? */ 
00134     stime_t operator*(const int factor) const;
00135     stime_t operator/(const int factor) const;
00136     stime_t operator*(const double factor) const;
00137     stime_t operator/(const double factor) const;
00138 
00139     // operatorX= variants
00140     stime_t &operator+=(const stime_t &r);
00141     stime_t &operator-=(const stime_t &r);
00142 
00143     /* XXX need a rounding operator?? */ 
00144 
00145     /* output conversions.  int/long is not available,
00146        because it doesn't have the dynamic range */
00147     operator double() const;
00148     operator float() const;
00149 #ifdef USE_POSIX_TIME
00150     operator struct timespec() const;
00151 #endif
00152 
00153     // for type conversion:
00154     operator struct timeval() const;
00155     
00156     /* XXX really belongs in sinterval_t */
00157     /* simple output conversions for integers to eliminate fp */
00158     long    secs() const;        /* seconds */
00159     long    msecs() const;        /* milli seconds */
00160     long    usecs() const;        /* micro seconds */
00161     long    nsecs() const;        /* nano seconds */
00162 
00163     /* input conversion operators for integral types */
00164     static    stime_t sec(int seconds); 
00165     static    stime_t    usec(int micro_seconds, int seconds = 0);
00166     static    stime_t    msec(int milli_seconds, int seconds = 0);
00167     static    stime_t    nsec(int nano_seconds, int seconds = 0);
00168 
00169     /* the Current time */
00170     static    stime_t now();
00171 
00172     ostream    &print(ostream &s) const;
00173     ostream &ctime(ostream &s) const;
00174 };
00175 
00176 
00177 /* Intervals are different, in some ways, from absolute
00178    times.  For now, they are to change print methods */
00179 
00180 class sinterval_t : public stime_t {
00181 public:
00182     /* XXX why do I duplicate the constructors ???  There
00183      is or was a reason for it. */
00184 
00185     sinterval_t() : stime_t() { }
00186 #ifdef USE_POSIX_TIME
00187     sinterval_t(const struct timespec &ts) : stime_t(ts) { }
00188 #endif
00189     sinterval_t(const struct timeval &tv) : stime_t(tv) { }
00190 
00191     sinterval_t(const stime_t &time) : stime_t(time) { }
00192     sinterval_t(int time) : stime_t(time) { }
00193     sinterval_t(long time) : stime_t(time) { }
00194     sinterval_t(double time) : stime_t(time) { }
00195 
00196     ostream    &print(ostream &s) const;
00197 };
00198 
00199 
00200 extern ostream &operator<<(ostream &s, const stime_t &t);
00201 extern ostream &operator<<(ostream &s, const sinterval_t &t);
00202 
00203 /**\endcond skip */
00204 
00205 /*<std-footer incl-file-exclusion='STIME_H'>  -- do not edit anything below this line -- */
00206 
00207 #endif          /*</std-footer>*/

Generated on Mon Nov 8 11:12:37 2010 for Shore Storage Manager by  doxygen 1.4.7