Jpp  19.1.0-rc.1
the software that should make you happy
JTimeval.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JTIMEVAL__
2 #define __JLANG__JTIMEVAL__
3 
4 #include <sys/time.h>
5 #include <istream>
6 #include <ostream>
7 #include <limits>
8 
9 #include "JLang/JComparable.hh"
10 
11 
12 /**
13  * \author mdejong
14  */
15 
16 namespace JLANG {}
17 namespace JPP { using namespace JLANG; }
18 
19 namespace JLANG {
20 
21 
22  /**
23  * Auxiliary class for time values.
24  * This class encapsulates the <tt>timeval</tt> data structure.
25  */
26  class JTimeval :
27  public timeval,
28  public JComparable<JTimeval>
29  {
30  public:
31  /**
32  * Default constructor.
33  */
35  {
36  this->tv_sec = 0;
37  this->tv_usec = 0;
38  }
39 
40 
41  /**
42  * Constructor.
43  *
44  * \param tv_us time [us]
45  */
46  JTimeval(const int tv_us)
47  {
48  this->tv_sec = 0;
49  this->tv_usec = tv_us;
50  }
51 
52 
53  /**
54  * Constructor.
55  *
56  * \param tv_s time [s]
57  * \param tv_us time [us]
58  */
59  JTimeval(const int tv_s, const int tv_us)
60  {
61  this->tv_sec = tv_s;
62  this->tv_usec = tv_us;
63  }
64 
65 
66  /**
67  * Get time value.
68  *
69  * \return time value
70  */
71  const JTimeval& getTimeval() const
72  {
73  return static_cast<const JTimeval&>(*this);
74  }
75 
76 
77  /**
78  * Get time value.
79  *
80  * \return time value
81  */
83  {
84  return static_cast<JTimeval&>(*this);
85  }
86 
87 
88  /**
89  * Set time value.
90  *
91  * \param timeval time value
92  */
93  void setTimeval(const JTimeval& timeval)
94  {
95  static_cast<JTimeval&>(*this) = timeval;
96  }
97 
98 
99  /**
100  * Less than method.
101  *
102  * \param value time value
103  * \result true if this time value less than given time value; else false
104  */
105  inline bool less(const JTimeval& value) const
106  {
107  if (this->tv_sec == value.tv_sec)
108  return this->tv_usec < value.tv_usec;
109  else
110  return this->tv_sec < value.tv_sec;
111  }
112 
113 
114  /**
115  * Get minimal time value.
116  *
117  * \return time value
118  */
119  static JTimeval min()
120  {
121  return JTimeval(0, 0);
122  }
123 
124 
125 
126  /**
127  * Get maximal time value.
128  *
129  * \return time value
130  */
131  static JTimeval max()
132  {
133  return JTimeval(std::numeric_limits<int>::max(), std::numeric_limits<int>::max());
134  }
135 
136 
137 
138  /**
139  * Get pointer to time value.
140  *
141  * \return pointer to time value
142  */
143  const timeval* get() const
144  {
145  return static_cast<const timeval*>(this);
146  }
147 
148 
149  /**
150  * Get pointer to time value.
151  *
152  * \return pointer to time value
153  */
154  timeval* get()
155  {
156  return static_cast<timeval*>(this);
157  }
158 
159 
160  /**
161  * Address of operator.
162  *
163  * \return pointer to time value
164  */
165  const timeval* operator &() const
166  {
167  return get();
168  }
169 
170 
171  /**
172  * Address of operator.
173  *
174  * \return pointer to time value
175  */
176  timeval* operator &()
177  {
178  return get();
179  }
180 
181 
182  /**
183  * Read time value from input.
184  *
185  * \param in input stream
186  * \param time time value
187  * \return input stream
188  */
189  friend inline std::istream& operator>>(std::istream& in, JTimeval& time)
190  {
191  return in >> time.tv_sec >> time.tv_usec;
192  }
193 
194 
195  /**
196  * Write time value to output.
197  *
198  * \param out output stream
199  * \param time time value
200  * \return output stream
201  */
202  friend inline std::ostream& operator<<(std::ostream& out, const JTimeval& time)
203  {
204  return out << time.tv_sec << ' ' << time.tv_usec;
205  }
206  };
207 }
208 
209 #endif
Auxiliary class for time values.
Definition: JTimeval.hh:29
friend std::istream & operator>>(std::istream &in, JTimeval &time)
Read time value from input.
Definition: JTimeval.hh:189
JTimeval(const int tv_s, const int tv_us)
Constructor.
Definition: JTimeval.hh:59
void setTimeval(const JTimeval &timeval)
Set time value.
Definition: JTimeval.hh:93
friend std::ostream & operator<<(std::ostream &out, const JTimeval &time)
Write time value to output.
Definition: JTimeval.hh:202
const timeval * operator&() const
Address of operator.
Definition: JTimeval.hh:165
JTimeval()
Default constructor.
Definition: JTimeval.hh:34
JTimeval(const int tv_us)
Constructor.
Definition: JTimeval.hh:46
JTimeval & getTimeval()
Get time value.
Definition: JTimeval.hh:82
const timeval * get() const
Get pointer to time value.
Definition: JTimeval.hh:143
const JTimeval & getTimeval() const
Get time value.
Definition: JTimeval.hh:71
static JTimeval min()
Get minimal time value.
Definition: JTimeval.hh:119
timeval * get()
Get pointer to time value.
Definition: JTimeval.hh:154
bool less(const JTimeval &value) const
Less than method.
Definition: JTimeval.hh:105
static JTimeval max()
Get maximal time value.
Definition: JTimeval.hh:131
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Template definition of auxiliary base class for comparison of data structures.
Definition: JComparable.hh:139