Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TimeStamp.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef __BIASTIMESTAMP_HH__
26 #define __BIASTIMESTAMP_HH__
27 
28 #ifdef WIN32
29 #include <winsock.h>
30 #else //WIN32
31 #include <sys/time.h>
32 #endif //WIN32
33 
34 #include <Base/Debug/Debug.hh>
35 //for compatibility with Win32 (i.e. for gettimeofday)
36 #include <Base/Common/W32Compat.hh>
37 
38 namespace BIAS {
39 
40  /**
41  @class TimeStamp
42  @brief holds system time in milliseconds
43  */
44  //typedef long long TimeStamp;
45  class BIASCommon_EXPORT TimeStamp {
46  public:
47  /**
48  @brief Ctors
49  */
50  TimeStamp() {msec_ = 0;}
51  TimeStamp(long long ts) {msec_ = ts;}
53 
54  /**
55  @brief Operators for comparsion, duplication and basic arithmetics
56  */
57  bool operator>(const TimeStamp &ts) const;
58  bool operator>(const long long &ts) const;
59  bool operator<(const TimeStamp &ts) const;
60  bool operator<(const long long &ts) const;
61  bool operator>=(const TimeStamp &ts) const;
62  bool operator>=(const long long &ts) const;
63  bool operator<=(const TimeStamp &ts) const;
64  bool operator<=(const long long &ts) const;
65  bool operator==(const TimeStamp &ts) const;
66  bool operator==(const long long &ts) const;
67  bool operator!=(const TimeStamp &ts) const;
68  bool operator!=(const long long &ts) const;
69 
70  TimeStamp& operator=(const TimeStamp &ts);
71  TimeStamp& operator=(const long long &ts);
72 
73  TimeStamp operator+(const TimeStamp &ts) const;
74  TimeStamp operator+(const long long &ts) const;
75  TimeStamp operator-(const TimeStamp &ts) const;
76  TimeStamp operator-(const long long &ts) const;
77  TimeStamp operator*(const TimeStamp &ts) const;
78  TimeStamp operator*(const long long &ts) const;
79  TimeStamp operator/(const TimeStamp &ts) const;
80  TimeStamp operator/(const long long &ts) const;
81  TimeStamp operator%(const TimeStamp &ts) const;
82  TimeStamp operator%(const long long &ts) const;
83 
84  /**
85  @brief Get the timestamp as long long int
86  */
87  inline long long GetMSec() const {return msec_;}
88 
89  /**
90  @brief Returns the absolute value timestamp
91  */
92  inline TimeStamp Abs() {if (msec_<0) return TimeStamp(-msec_);
93  return *this;}
94 
95  private:
96  long long msec_;
97  };
98 
99  /**
100  @class TimeStampGen
101  @author 23.1.07 apetersen
102  @ingroup Sensors
103  @brief Generates absolute timestamps with a certain offset from
104  given base-time
105  */
106  class TimeStampGen : public BIAS::Debug {
107 
108  public:
109  /**
110  @brief Standard Ctor and Dtor
111  */
113  baseTime_.tv_sec = 0;
114  baseTime_.tv_usec = 0;
115  msecBaseTime_ = 0;
116  }
118 
119  /**
120  @brief Generate a timestamp with offset 'msec' from given base-time.
121  For msec==0 just returns base-time in ms.
122 
123  @param msec Offset from base-time for timestamp
124 
125  @return Timestamp with value 'base-time + msec'
126  */
127  inline TimeStamp GenerateTimeStamp(const long long &msec)
128  { return msecBaseTime_ + msec; }
130  { return msecBaseTime_ + ts; }
131 
132  /**
133  @brief Generate a timestamp from system time
134 
135  @return actual system time in ms
136  */
138  { timeval tmp;
139  gettimeofday(&tmp,NULL);
140  return TimeStamp(((long long) tmp.tv_sec)*1000 + (tmp.tv_usec / 1000));
141  }
142 
143  /**
144  @brief Sets system time as base-time for stamp generation.
145  'msecOffset' will be added to base-time as timing correction.
146  Timestamps are conform with (unix) gettimeofday; baseTime_ holds
147  the 'raw' timeval struct and msecBaseTime_ the (corresponding)
148  milliseconds since beginning of epoch (unix: 1.1.1970 00:00).
149 
150  @param msecOffset Offset from actual time to base-time
151  */
152  inline void SetBaseTime(const long long msecOffset=0)
153  { //get actual system time and add offset
154  gettimeofday(&baseTime_, NULL);
155  baseTime_.tv_sec = baseTime_.tv_sec + long(msecOffset/1000LL);
156  baseTime_.tv_usec = baseTime_.tv_usec + long(msecOffset%1000)*1000;
157  //combine timeval-struct to fit in one long long
158  msecBaseTime_ = ((long long)baseTime_.tv_sec) * 1000
159  + (baseTime_.tv_usec / 1000);
160  }
161 
162  protected:
163  timeval baseTime_;
165  };
166 
167  /**
168  @brief Standard output operator for TimeStamps
169  */
170  BIASCommon_EXPORT std::ostream& operator<<(std::ostream& os,
171  const BIAS::TimeStamp &ts);
172 
173  /**
174  @brief Standard input operator for TimeStamps
175  */
176  BIASCommon_EXPORT std::istream& operator>>(std::istream& is,
177  BIAS::TimeStamp &ts);
178 
179 } //namespace BIAS
180 
181 #endif //__BIASTIMESTAMP_HH__
TimeStamp msecBaseTime_
Definition: TimeStamp.hh:164
TimeStamp GenerateTimeStamp(const long long &msec)
Generate a timestamp with offset &#39;msec&#39; from given base-time.
Definition: TimeStamp.hh:127
DualQuaternion< T > operator/(const DualQuaternion< T > &l, const T &scalar)
long long GetMSec() const
Get the timestamp as long long int.
Definition: TimeStamp.hh:87
DualQuaternion< T > operator-(const DualQuaternion< T > &l, const DualQuaternion< T > &r)
void SetBaseTime(const long long msecOffset=0)
Sets system time as base-time for stamp generation.
Definition: TimeStamp.hh:152
Generates absolute timestamps with a certain offset from given base-time.
Definition: TimeStamp.hh:106
DualQuaternion< T > operator+(const DualQuaternion< T > &l, const DualQuaternion< T > &r)
TimeStamp GenerateTimeStamp(const TimeStamp &ts)
Definition: TimeStamp.hh:129
bool operator>(const BIAS::Polynom &p1, const BIAS::Polynom &p2)
Definition: Polynom.hh:302
bool operator<(const BIAS::Polynom &p1, const BIAS::Polynom &p2)
Definition: Polynom.hh:293
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
Definition: Array2D.hh:260
DualQuaternion< T > operator*(const DualQuaternion< T > &l, const T &scalar)
TimeStampGen()
Standard Ctor and Dtor.
Definition: TimeStamp.hh:112
TimeStamp(long long ts)
Definition: TimeStamp.hh:51
TimeStamp()
Ctors.
Definition: TimeStamp.hh:50
holds system time in milliseconds
Definition: TimeStamp.hh:45
TimeStamp Abs()
Returns the absolute value timestamp.
Definition: TimeStamp.hh:92
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.
Definition: TimeStamp.cpp:157
TimeStamp GenerateTimeStamp()
Generate a timestamp from system time.
Definition: TimeStamp.hh:137