| Fast DDS Version 3.3.0 Fast DDS

Loading...
Searching...
No Matches
Time_t.hpp
1// Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
18
19#ifndef FASTDDS_RTPS_COMMON__TIME_T_HPP
20#define FASTDDS_RTPS_COMMON__TIME_T_HPP
21
22#include <fastdds/dds/core/Time_t.hpp>
23#include <fastdds/fastdds_dll.hpp>
24
25#include <cmath>
26#include <cstdint>
27#include <iostream>
28
29 namespace eprosima {
30 namespace fastdds {
31 namespace rtps {
32
37 class FASTDDS_EXPORTED_API Time_t
38{
39 public:
40
42 Time_t() = default;
43
48 Time_t(
49 int32_t sec,
50 uint32_t frac);
51
55 Time_t(
56 long double sec);
57
61 Time_t(
62 const eprosima::fastdds::dds::Time_t& time);
63
67 int64_t to_ns() const;
68
72 void from_ns(
73 int64_t nanosecs);
74
78 int32_t seconds() const;
79
83 int32_t& seconds();
84
88 void seconds(
89 int32_t sec);
90
94 uint32_t nanosec() const;
95
99 void nanosec(
100 uint32_t nanos);
101
105 uint32_t fraction() const;
106
110 uint32_t& fraction();
111
115 void fraction(
116 uint32_t frac);
117
118 eprosima::fastdds::dds::Duration_t to_duration_t() const;
119
120 void from_duration_t(
121 const eprosima::fastdds::dds::Duration_t& duration);
122
128 static void now(
129 Time_t& ret);
130
131 private:
132
134 int32_t seconds_ = 0;
135
137 uint32_t fraction_ = 0;
138
140 uint32_t nanosec_ = 0;
141
142 void set_fraction(
143 uint32_t frac);
144
145 void set_nanosec(
146 uint32_t nanos);
147};
148
149#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
150
157 static inline bool operator ==(
158 const Time_t& t1,
159 const Time_t& t2)
160{
161 if (t1.seconds() != t2.seconds())
162 {
163 return false;
164 }
165 if (t1.fraction() != t2.fraction())
166 {
167 return false;
168 }
169 return true;
170}
171
178 static inline bool operator !=(
179 const Time_t& t1,
180 const Time_t& t2)
181{
182 if (t1.seconds() != t2.seconds())
183 {
184 return true;
185 }
186 if (t1.fraction() != t2.fraction())
187 {
188 return true;
189 }
190 return false;
191}
192
199 static inline bool operator <(
200 const Time_t& t1,
201 const Time_t& t2)
202{
203 if (t1.seconds() < t2.seconds())
204 {
205 return true;
206 }
207 else if (t1.seconds() > t2.seconds())
208 {
209 return false;
210 }
211 else
212 {
213 if (t1.fraction() < t2.fraction())
214 {
215 return true;
216 }
217 else
218 {
219 return false;
220 }
221 }
222}
223
230 static inline bool operator >(
231 const Time_t& t1,
232 const Time_t& t2)
233{
234 if (t1.seconds() > t2.seconds())
235 {
236 return true;
237 }
238 else if (t1.seconds() < t2.seconds())
239 {
240 return false;
241 }
242 else
243 {
244 if (t1.fraction() > t2.fraction())
245 {
246 return true;
247 }
248 else
249 {
250 return false;
251 }
252 }
253}
254
261 static inline bool operator <=(
262 const Time_t& t1,
263 const Time_t& t2)
264{
265 if (t1.seconds() < t2.seconds())
266 {
267 return true;
268 }
269 else if (t1.seconds() > t2.seconds())
270 {
271 return false;
272 }
273 else
274 {
275 if (t1.fraction() <= t2.fraction())
276 {
277 return true;
278 }
279 else
280 {
281 return false;
282 }
283 }
284}
285
292 static inline bool operator >=(
293 const Time_t& t1,
294 const Time_t& t2)
295{
296 if (t1.seconds() > t2.seconds())
297 {
298 return true;
299 }
300 else if (t1.seconds() < t2.seconds())
301 {
302 return false;
303 }
304 else
305 {
306 if (t1.fraction() >= t2.fraction())
307 {
308 return true;
309 }
310 else
311 {
312 return false;
313 }
314 }
315}
316
317 inline std::ostream& operator <<(
318 std::ostream& output,
319 const Time_t& t)
320{
321 return output << t.seconds() << "." << t.nanosec();
322}
323
324 inline std::istream& operator >>(
325 std::istream& input,
326 Time_t& t)
327{
328 std::istream::sentry s(input);
329
330 if (s)
331 {
332 char point;
333 int32_t sec = 0;
334 uint32_t nano = 0;
335 std::ios_base::iostate excp_mask = input.exceptions();
336
337 try
338 {
339 input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
340
341 input >> sec;
342 input >> point >> nano;
343 // nano could not be bigger or equal than 1 sec
344 if ( point != '.' || nano >= 1000000000 )
345 {
346 input.setstate(std::ios_base::failbit);
347 nano = 0;
348 }
349 }
350 catch (std::ios_base::failure& )
351 {
352 }
353
354 t.seconds(sec);
355 t.nanosec(nano);
356
357 input.exceptions(excp_mask);
358 }
359
360 return input;
361}
362
369 static inline Time_t operator +(
370 const Time_t& ta,
371 const Time_t& tb)
372{
373 Time_t result(ta.seconds() + tb.seconds(), ta.fraction() + tb.fraction());
374 if (result.fraction() < ta.fraction()) // Overflow is detected by any of them
375 {
376 ++result.seconds();
377 }
378 return result;
379}
380
387 static inline Time_t operator -(
388 const Time_t& ta,
389 const Time_t& tb)
390{
391 Time_t result(ta.seconds() - tb.seconds(), ta.fraction() - tb.fraction());
392 if (result.fraction() > ta.fraction()) // Overflow is detected by ta
393 {
394 --result.seconds();
395 }
396 return result;
397}
398
399#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
400
401 const Time_t c_RTPSTimeInfinite{0x7fffffff, 0xffffffff};
402 const Time_t c_RTPSTimeZero{0, 0};
403 const Time_t c_RTPSTimeInvalid{-1, 0xffffffff};
404
405} // namespace rtps
406} // namespace fastdds
407} // namespace eprosima
408
409// defines to avoid the "static initialization order fiasco"
410#define TIME_T_INFINITE_SECONDS (eprosima::fastdds::dds::Time_t::INFINITE_SECONDS)
411#define TIME_T_INFINITE_NANOSECONDS (eprosima::fastdds::dds::Time_t::INFINITE_NANOSECONDS)
412
413#endif // FASTDDS_RTPS_COMMON__TIME_T_HPP
eprosima::fastdds::rtps::Time_t
Structure Time_t, used to describe times at RTPS protocol.
Definition Time_t.hpp:38
eprosima::fastdds::rtps::Time_t::nanosec
void nanosec(uint32_t nanos)
Sets nanoseconds field and updates the fraction.
eprosima::fastdds::rtps::Time_t::Time_t
Time_t(long double sec)
eprosima::fastdds::rtps::Time_t::to_duration_t
eprosima::fastdds::dds::Duration_t to_duration_t() const
eprosima::fastdds::rtps::Time_t::seconds
int32_t & seconds()
Retrieve the seconds field by ref.
eprosima::fastdds::rtps::Time_t::fraction
void fraction(uint32_t frac)
Sets fraction field and updates the nanoseconds.
eprosima::fastdds::rtps::Time_t::nanosec
uint32_t nanosec() const
Retrieve the nanosec field.
eprosima::fastdds::rtps::Time_t::from_ns
void from_ns(int64_t nanosecs)
eprosima::fastdds::rtps::Time_t::now
static void now(Time_t &ret)
Fills a Time_t struct with a representation of the current time.
eprosima::fastdds::rtps::Time_t::fraction
uint32_t fraction() const
Retrieve the fraction field.
eprosima::fastdds::rtps::Time_t::from_duration_t
void from_duration_t(const eprosima::fastdds::dds::Duration_t &duration)
eprosima::fastdds::rtps::Time_t::Time_t
Time_t()=default
Default constructor. Sets values to zero.
eprosima::fastdds::rtps::Time_t::seconds
int32_t seconds() const
Retrieve the seconds field.
eprosima::fastdds::rtps::Time_t::seconds
void seconds(int32_t sec)
Sets seconds field.
eprosima::fastdds::rtps::Time_t::Time_t
Time_t(int32_t sec, uint32_t frac)
eprosima::fastdds::rtps::Time_t::to_ns
int64_t to_ns() const
Returns stored time as nanoseconds (including seconds).
eprosima::fastdds::rtps::Time_t::Time_t
Time_t(const eprosima::fastdds::dds::Time_t &time)
eprosima::fastdds::rtps::Time_t::fraction
uint32_t & fraction()
Retrieve the fraction field by ref.
eprosima::fastdds::dds::Duration_t
Time_t Duration_t
Definition Time_t.hpp:91
eprosima::fastdds::rtps
Contains the RTPS protocol implementation.
eprosima::fastdds::rtps::operator>>
std::istream & operator>>(std::istream &input, EntityId_t &enP)
Definition EntityId_t.hpp:289
eprosima::fastdds::rtps::c_RTPSTimeInfinite
const Time_t c_RTPSTimeInfinite
Definition Time_t.hpp:401
eprosima::fastdds::rtps::operator==
bool operator==(const BuiltinTransportsOptions &bto1, const BuiltinTransportsOptions &bto2)
Equal to operator.
Definition BuiltinTransports.hpp:79
eprosima::fastdds::rtps::operator<<
std::ostream & operator<<(std::ostream &output, BuiltinTransports transports)
Definition BuiltinTransports.hpp:118
eprosima::fastdds::rtps::operator+
SequenceNumber_t operator+(const SequenceNumber_t &seq, const uint32_t inc) noexcept
Add one uint32_t to a SequenceNumber_t.
Definition SequenceNumber.hpp:258
eprosima::fastdds::rtps::operator<=
bool operator<=(const SequenceNumber_t &seq1, const SequenceNumber_t &seq2) noexcept
Checks if a SequenceNumber_t is less or equal than other.
Definition SequenceNumber.hpp:218
eprosima::fastdds::rtps::c_RTPSTimeInvalid
const Time_t c_RTPSTimeInvalid
Definition Time_t.hpp:403
eprosima::fastdds::rtps::operator!=
bool operator!=(const EntityId_t &id1, const EntityId_t &id2)
Guid prefix comparison operator.
Definition EntityId_t.hpp:267
eprosima::fastdds::rtps::operator<
bool operator<(const GUID_t &g1, const GUID_t &g2)
Definition Guid.hpp:192
eprosima::fastdds::rtps::operator-
SequenceNumber_t operator-(const SequenceNumber_t &seq, const uint32_t inc) noexcept
Subtract one uint32_t from a SequenceNumber_t.
Definition SequenceNumber.hpp:236
eprosima::fastdds::rtps::operator>=
bool operator>=(const SequenceNumber_t &seq1, const SequenceNumber_t &seq2) noexcept
Checks if a SequenceNumber_t is greater or equal than other.
Definition SequenceNumber.hpp:200
eprosima::fastdds::rtps::c_RTPSTimeZero
const Time_t c_RTPSTimeZero
Definition Time_t.hpp:402
eprosima::fastdds::rtps::operator>
bool operator>(const SequenceNumber_t &seq1, const SequenceNumber_t &seq2) noexcept
Checks if a SequenceNumber_t is greater than other.
Definition SequenceNumber.hpp:164
eprosima::fastdds
eprosima
eProsima namespace.
eprosima::fastdds::dds::Time_t
Structure Time_t, used to describe times at a DDS level.
Definition Time_t.hpp:36