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 * @ingroup ioserialization
27 */
28class OCEAN_IO_SERIALIZATION_EXPORT DataTimestamp
29{
30 public:
31
32 /**
33 * Definition of the value types.
34 */
35 enum ValueType : uint8_t
36 {
37 /// Invalid value type.
38 VT_INVALID = 0u,
39 /// Double value type.
41 /// Int64 value type.
42 VT_INT64
43 };
44
45 public:
46
47 /**
48 * Creates a new invalid data timestamp.
49 */
50 DataTimestamp() = default;
51
52 /**
53 * Creates a new data timestamp with a double value.
54 * @param value The double value
55 */
56 explicit inline DataTimestamp(const double value);
57
58 /**
59 * Creates a new data timestamp with an int64 value.
60 * @param value The int64 value
61 */
62 explicit inline DataTimestamp(const int64_t value);
63
64 /**
65 * Returns whether this timestamp holds a double value.
66 * @return True, if so
67 */
68 inline bool isDouble() const;
69
70 /**
71 * Returns whether this timestamp holds an int64 value.
72 * @return True, if so
73 */
74 inline bool isInt() const;
75
76 /**
77 * Returns the double value of this timestamp.
78 * @return The double value
79 */
80 inline double asDouble() const;
81
82 /**
83 * Returns the int64 value of this timestamp.
84 * @return The int64 value
85 */
86 inline int64_t asInt() const;
87
88 /**
89 * Returns the timestamp as a double value regardless of the actual value type.
90 * In case the value type is int64, the value will be converted to a double value.
91 * @return The timestamp as a double value
92 */
93 inline double forceDouble() const;
94
95 /**
96 * Returns whether this timestamp is valid.
97 * @return True, if so
98 */
99 inline bool isValid() const;
100
101 /**
102 * Adds two timestamps and returns the result.
103 * Both timestamps must have the same value type.
104 * @param dataTimestamp The second timestamp to add
105 * @return The sum of both timestamps
106 */
107 inline DataTimestamp operator+(const DataTimestamp& dataTimestamp) const;
108
109 /**
110 * Adds another timestamp to this timestamp.
111 * Both timestamps must have the same value type.
112 * @param dataTimestamp The timestamp to add
113 * @return Reference to this timestamp
114 */
115 inline DataTimestamp& operator+=(const DataTimestamp& dataTimestamp);
116
117 /**
118 * Returns whether two timestamps are equal.
119 * @param dataTimestamp The second timestamp to compare
120 * @return True, if so
121 */
122 bool operator==(const DataTimestamp& dataTimestamp) const;
123
124 /**
125 * Returns whether this timestamp is less than another timestamp.
126 * @param dataTimestamp The second timestamp to compare
127 * @return True, if so
128 */
129 bool operator<(const DataTimestamp& dataTimestamp) const;
130
131 /**
132 * Returns whether this timestamp is less than or equal to another timestamp.
133 * @param dataTimestamp The second timestamp to compare
134 * @return True, if so
135 */
136 bool operator<=(const DataTimestamp& dataTimestamp) const;
137
138 /**
139 * Returns whether this timestamp is greater than another timestamp.
140 * @param dataTimestamp The second timestamp to compare
141 * @return True, if so
142 */
143 bool operator>(const DataTimestamp& dataTimestamp) const;
144
145 /**
146 * Returns whether this timestamp is greater than or equal to another timestamp.
147 * @param dataTimestamp The second timestamp to compare
148 * @return True, if so
149 */
150 bool operator>=(const DataTimestamp& dataTimestamp) const;
151
152 /**
153 * Reads a data timestamp from an input bitstream.
154 * @param inputBitstream The input bitstream from which the timestamp will be read
155 * @param dataTimestamp The resulting data timestamp
156 * @return True, if succeeded
157 */
158 static bool read(InputBitstream& inputBitstream, DataTimestamp& dataTimestamp);
159
160 /**
161 * Writes a data timestamp to an output bitstream.
162 * @param outputBitstream The output bitstream to which the timestamp will be written
163 * @param dataTimestamp The data timestamp to write
164 * @return True, if succeeded
165 */
166 static bool write(OutputBitstream& outputBitstream, const DataTimestamp& dataTimestamp);
167
168 protected:
169
170 /**
171 * The union holding the value.
172 */
173 union
174 {
175 /// The double value.
177 /// The int64 value.
178 int64_t intValue_;
179 } value_;
180
181 /// The value type.
182 ValueType valueType_ = VT_INVALID;
183};
184
185inline DataTimestamp::DataTimestamp(const double value) :
186 valueType_(VT_DOUBLE)
187{
188 value_.doubleValue_ = value;
189}
190
191inline DataTimestamp::DataTimestamp(const int64_t value) :
192 valueType_(VT_INT64)
193{
194 value_.intValue_ = value;
195}
196
197inline bool DataTimestamp::isDouble() const
198{
199 return valueType_ == VT_DOUBLE;
200}
201
202inline bool DataTimestamp::isInt() const
203{
204 return valueType_ == VT_INT64;
205}
206
207inline double DataTimestamp::asDouble() const
208{
209 ocean_assert(isValid());
210 ocean_assert(valueType_ == VT_DOUBLE);
211
212 return value_.doubleValue_;
213}
214
215inline int64_t DataTimestamp::asInt() const
216{
217 ocean_assert(isValid());
218 ocean_assert(valueType_ == VT_INT64);
219
220 return value_.intValue_;
221}
222
223inline double DataTimestamp::forceDouble() const
224{
225 ocean_assert(isValid());
226
227 if (valueType_ == VT_DOUBLE)
228 {
229 return asDouble();
230 }
231 else
232 {
233 ocean_assert(valueType_ == VT_INT64);
234
235 return double(asInt());
236 }
237}
238
239inline bool DataTimestamp::isValid() const
240{
241 return valueType_ != VT_INVALID;
242}
243
244inline DataTimestamp DataTimestamp::operator+(const DataTimestamp& dataTimestamp) const
245{
246 ocean_assert(isValid() && dataTimestamp.isValid());
247
248 if (valueType_ != dataTimestamp.valueType_)
249 {
250 ocean_assert(false && "Value types do not match");
251 return DataTimestamp();
252 }
253
254 if (valueType_ == VT_DOUBLE)
255 {
256 return DataTimestamp(value_.doubleValue_ + dataTimestamp.value_.doubleValue_);
257 }
258 else
259 {
260 ocean_assert(valueType_ == VT_INT64);
261 return DataTimestamp(value_.intValue_ + dataTimestamp.value_.intValue_);
262 }
263}
264
266{
267 ocean_assert(isValid() && dataTimestamp.isValid());
268
269 if (valueType_ != dataTimestamp.valueType_)
270 {
271 ocean_assert(false && "Value types do not match");
272 return *this;
273 }
274
275 if (valueType_ == VT_DOUBLE)
276 {
277 value_.doubleValue_ += dataTimestamp.value_.doubleValue_;
278 }
279 else
280 {
281 ocean_assert(valueType_ == VT_INT64);
282 value_.intValue_ += dataTimestamp.value_.intValue_;
283 }
284
285 return *this;
286}
287
288}
289
290}
291
292}
293
294#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:29
int64_t asInt() const
Returns the int64 value of this timestamp.
Definition DataTimestamp.h:215
double doubleValue_
The double value.
Definition DataTimestamp.h:176
double forceDouble() const
Returns the timestamp as a double value regardless of the actual value type.
Definition DataTimestamp.h:223
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:207
ValueType valueType_
The value type.
Definition DataTimestamp.h:182
bool isDouble() const
Returns whether this timestamp holds a double value.
Definition DataTimestamp.h:197
bool isInt() const
Returns whether this timestamp holds an int64 value.
Definition DataTimestamp.h:202
bool isValid() const
Returns whether this timestamp is valid.
Definition DataTimestamp.h:239
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:244
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:265
bool operator<(const DataTimestamp &dataTimestamp) const
Returns whether this timestamp is less than another timestamp.
int64_t intValue_
The int64 value.
Definition DataTimestamp.h:178
ValueType
Definition of the value types.
Definition DataTimestamp.h:36
@ VT_DOUBLE
Double value type.
Definition DataTimestamp.h:40
@ VT_INT64
Int64 value type.
Definition DataTimestamp.h:42
@ VT_INVALID
Invalid value type.
Definition DataTimestamp.h:38
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:418