Ocean
Loading...
Searching...
No Matches
DataTimestamp.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_IO_SERIALIZATION_DATA_TIMESTAMP_H
9#define META_OCEAN_IO_SERIALIZATION_DATA_TIMESTAMP_H
10
12
13#include "ocean/io/Bitstream.h"
14
15namespace Ocean
16{
17
18namespace IO
19{
20
21namespace Serialization
22{
23
24/**
25 * This class implements a data timestamp which can hold either a double or int64_t value.
26 * The dual-type design allows the timestamp to represent both floating-point time values (e.g., seconds with fractional precision) and integer-based timestamps (e.g., nanoseconds or frame indices).<br>
27 * Arithmetic and comparison operations are supported, but both operands must have the same value type.<br>
28 * The class provides serialization support through static read/write functions for use with bitstreams.
29 * @ingroup ioserialization
30 */
31class OCEAN_IO_SERIALIZATION_EXPORT DataTimestamp
32{
33 public:
34
35 /**
36 * Definition of the value types.
37 */
38 enum ValueType : uint8_t
39 {
40 /// Invalid value type.
41 VT_INVALID = 0u,
42 /// Double value type.
44 /// Int64 value type.
45 VT_INT64
46 };
47
48 public:
49
50 /**
51 * Creates a new invalid data timestamp.
52 */
53 DataTimestamp() = default;
54
55 /**
56 * Creates a new data timestamp with a double value.
57 * @param value The double value
58 */
59 explicit inline DataTimestamp(const double value);
60
61 /**
62 * Creates a new data timestamp with an int64 value.
63 * @param value The int64 value
64 */
65 explicit inline DataTimestamp(const int64_t value);
66
67 /**
68 * Returns whether this timestamp holds a double value.
69 * @return True, if so
70 */
71 inline bool isDouble() const;
72
73 /**
74 * Returns whether this timestamp holds an int64 value.
75 * @return True, if so
76 */
77 inline bool isInt() const;
78
79 /**
80 * Returns the double value of this timestamp.
81 * @return The double value
82 */
83 inline double asDouble() const;
84
85 /**
86 * Returns the int64 value of this timestamp.
87 * @return The int64 value
88 */
89 inline int64_t asInt() const;
90
91 /**
92 * Returns the timestamp as a double value regardless of the actual value type.
93 * In case the value type is int64, the value will be converted to a double value.
94 * @return The timestamp as a double value
95 */
96 inline double forceDouble() const;
97
98 /**
99 * Returns whether this timestamp is valid.
100 * @return True, if so
101 */
102 inline bool isValid() const;
103
104 /**
105 * Adds two timestamps and returns the result.
106 * Both timestamps must have the same value type.
107 * @param dataTimestamp The second timestamp to add
108 * @return The sum of both timestamps
109 */
110 inline DataTimestamp operator+(const DataTimestamp& dataTimestamp) const;
111
112 /**
113 * Adds another timestamp to this timestamp.
114 * Both timestamps must have the same value type.
115 * @param dataTimestamp The timestamp to add
116 * @return Reference to this timestamp
117 */
118 inline DataTimestamp& operator+=(const DataTimestamp& dataTimestamp);
119
120 /**
121 * Returns whether two timestamps are equal.
122 * @param dataTimestamp The second timestamp to compare
123 * @return True, if so
124 */
125 bool operator==(const DataTimestamp& dataTimestamp) const;
126
127 /**
128 * Returns whether this timestamp is less than another timestamp.
129 * @param dataTimestamp The second timestamp to compare
130 * @return True, if so
131 */
132 bool operator<(const DataTimestamp& dataTimestamp) const;
133
134 /**
135 * Returns whether this timestamp is less than or equal to another timestamp.
136 * @param dataTimestamp The second timestamp to compare
137 * @return True, if so
138 */
139 bool operator<=(const DataTimestamp& dataTimestamp) const;
140
141 /**
142 * Returns whether this timestamp is greater than another timestamp.
143 * @param dataTimestamp The second timestamp to compare
144 * @return True, if so
145 */
146 bool operator>(const DataTimestamp& dataTimestamp) const;
147
148 /**
149 * Returns whether this timestamp is greater than or equal to another timestamp.
150 * @param dataTimestamp The second timestamp to compare
151 * @return True, if so
152 */
153 bool operator>=(const DataTimestamp& dataTimestamp) const;
154
155 /**
156 * Reads a data timestamp from an input bitstream.
157 * @param inputBitstream The input bitstream from which the timestamp will be read
158 * @param dataTimestamp The resulting data timestamp
159 * @return True, if succeeded
160 */
161 static bool read(InputBitstream& inputBitstream, DataTimestamp& dataTimestamp);
162
163 /**
164 * Writes a data timestamp to an output bitstream.
165 * @param outputBitstream The output bitstream to which the timestamp will be written
166 * @param dataTimestamp The data timestamp to write
167 * @return True, if succeeded
168 */
169 static bool write(OutputBitstream& outputBitstream, const DataTimestamp& dataTimestamp);
170
171 protected:
172
173 /**
174 * The union holding the value.
175 */
176 union
177 {
178 /// The double value.
180 /// The int64 value.
181 int64_t intValue_;
182 } value_;
183
184 /// The value type.
185 ValueType valueType_ = VT_INVALID;
186};
187
188inline DataTimestamp::DataTimestamp(const double value) :
189 valueType_(VT_DOUBLE)
190{
191 value_.doubleValue_ = value;
192}
193
194inline DataTimestamp::DataTimestamp(const int64_t value) :
195 valueType_(VT_INT64)
196{
197 value_.intValue_ = value;
198}
199
200inline bool DataTimestamp::isDouble() const
201{
202 return valueType_ == VT_DOUBLE;
203}
204
205inline bool DataTimestamp::isInt() const
206{
207 return valueType_ == VT_INT64;
208}
209
210inline double DataTimestamp::asDouble() const
211{
212 ocean_assert(isValid());
213 ocean_assert(valueType_ == VT_DOUBLE);
214
215 return value_.doubleValue_;
216}
217
218inline int64_t DataTimestamp::asInt() const
219{
220 ocean_assert(isValid());
221 ocean_assert(valueType_ == VT_INT64);
222
223 return value_.intValue_;
224}
225
226inline double DataTimestamp::forceDouble() const
227{
228 ocean_assert(isValid());
229
230 if (valueType_ == VT_DOUBLE)
231 {
232 return asDouble();
233 }
234 else
235 {
236 ocean_assert(valueType_ == VT_INT64);
237
238 return double(asInt());
239 }
240}
241
242inline bool DataTimestamp::isValid() const
243{
244 return valueType_ != VT_INVALID;
245}
246
247inline DataTimestamp DataTimestamp::operator+(const DataTimestamp& dataTimestamp) const
248{
249 ocean_assert(isValid() && dataTimestamp.isValid());
250
251 if (valueType_ != dataTimestamp.valueType_)
252 {
253 ocean_assert(false && "Value types do not match");
254 return DataTimestamp();
255 }
256
257 if (valueType_ == VT_DOUBLE)
258 {
259 return DataTimestamp(value_.doubleValue_ + dataTimestamp.value_.doubleValue_);
260 }
261 else
262 {
263 ocean_assert(valueType_ == VT_INT64);
264 return DataTimestamp(value_.intValue_ + dataTimestamp.value_.intValue_);
265 }
266}
267
269{
270 ocean_assert(isValid() && dataTimestamp.isValid());
271
272 if (valueType_ != dataTimestamp.valueType_)
273 {
274 ocean_assert(false && "Value types do not match");
275 return *this;
276 }
277
278 if (valueType_ == VT_DOUBLE)
279 {
280 value_.doubleValue_ += dataTimestamp.value_.doubleValue_;
281 }
282 else
283 {
284 ocean_assert(valueType_ == VT_INT64);
285 value_.intValue_ += dataTimestamp.value_.intValue_;
286 }
287
288 return *this;
289}
290
291}
292
293}
294
295}
296
297#endif // META_OCEAN_IO_SERIALIZATION_DATA_TIMESTAMP_H
This class implements an input bitstream.
Definition Bitstream.h:51
This class implements an output bitstream.
Definition Bitstream.h:215
This class implements a data timestamp which can hold either a double or int64_t value.
Definition DataTimestamp.h:32
int64_t asInt() const
Returns the int64 value of this timestamp.
Definition DataTimestamp.h:218
double doubleValue_
The double value.
Definition DataTimestamp.h:179
double forceDouble() const
Returns the timestamp as a double value regardless of the actual value type.
Definition DataTimestamp.h:226
union Ocean::IO::Serialization::DataTimestamp::@2 value_
The union holding the value.
static bool write(OutputBitstream &outputBitstream, const DataTimestamp &dataTimestamp)
Writes a data timestamp to an output bitstream.
double asDouble() const
Returns the double value of this timestamp.
Definition DataTimestamp.h:210
ValueType valueType_
The value type.
Definition DataTimestamp.h:185
bool isDouble() const
Returns whether this timestamp holds a double value.
Definition DataTimestamp.h:200
bool isInt() const
Returns whether this timestamp holds an int64 value.
Definition DataTimestamp.h:205
bool isValid() const
Returns whether this timestamp is valid.
Definition DataTimestamp.h:242
bool operator==(const DataTimestamp &dataTimestamp) const
Returns whether two timestamps are equal.
DataTimestamp operator+(const DataTimestamp &dataTimestamp) const
Adds two timestamps and returns the result.
Definition DataTimestamp.h:247
bool operator>=(const DataTimestamp &dataTimestamp) const
Returns whether this timestamp is greater than or equal to another timestamp.
bool operator<=(const DataTimestamp &dataTimestamp) const
Returns whether this timestamp is less than or equal to another timestamp.
bool operator>(const DataTimestamp &dataTimestamp) const
Returns whether this timestamp is greater than another timestamp.
DataTimestamp()=default
Creates a new invalid data timestamp.
DataTimestamp & operator+=(const DataTimestamp &dataTimestamp)
Adds another timestamp to this timestamp.
Definition DataTimestamp.h:268
bool operator<(const DataTimestamp &dataTimestamp) const
Returns whether this timestamp is less than another timestamp.
int64_t intValue_
The int64 value.
Definition DataTimestamp.h:181
ValueType
Definition of the value types.
Definition DataTimestamp.h:39
@ VT_DOUBLE
Double value type.
Definition DataTimestamp.h:43
@ VT_INT64
Int64 value type.
Definition DataTimestamp.h:45
@ VT_INVALID
Invalid value type.
Definition DataTimestamp.h:41
static bool read(InputBitstream &inputBitstream, DataTimestamp &dataTimestamp)
Reads a data timestamp from an input bitstream.
The namespace covering the entire Ocean framework.
Definition Accessor.h:15
AutomaticDifferentiationT< T1, TNumeric1 > operator+(const T2 &left, const AutomaticDifferentiationT< T1, TNumeric1 > &right)
Definition AutomaticDifferentiation.h:417