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 * Returns whether two timestamps are equal.
103 * @param dataTimestamp The second timestamp to compare
104 * @return True, if so
105 */
106 bool operator==(const DataTimestamp& dataTimestamp) const;
107
108 /**
109 * Returns whether this timestamp is less than another timestamp.
110 * @param dataTimestamp The second timestamp to compare
111 * @return True, if so
112 */
113 bool operator<(const DataTimestamp& dataTimestamp) const;
114
115 /**
116 * Returns whether this timestamp is less than or equal to another timestamp.
117 * @param dataTimestamp The second timestamp to compare
118 * @return True, if so
119 */
120 bool operator<=(const DataTimestamp& dataTimestamp) const;
121
122 /**
123 * Returns whether this timestamp is greater than another timestamp.
124 * @param dataTimestamp The second timestamp to compare
125 * @return True, if so
126 */
127 bool operator>(const DataTimestamp& dataTimestamp) const;
128
129 /**
130 * Returns whether this timestamp is greater than or equal to another timestamp.
131 * @param dataTimestamp The second timestamp to compare
132 * @return True, if so
133 */
134 bool operator>=(const DataTimestamp& dataTimestamp) const;
135
136 /**
137 * Reads a data timestamp from an input bitstream.
138 * @param inputBitstream The input bitstream from which the timestamp will be read
139 * @param dataTimestamp The resulting data timestamp
140 * @return True, if succeeded
141 */
142 static bool read(InputBitstream& inputBitstream, DataTimestamp& dataTimestamp);
143
144 /**
145 * Writes a data timestamp to an output bitstream.
146 * @param outputBitstream The output bitstream to which the timestamp will be written
147 * @param dataTimestamp The data timestamp to write
148 * @return True, if succeeded
149 */
150 static bool write(OutputBitstream& outputBitstream, const DataTimestamp& dataTimestamp);
151
152 protected:
153
154 /**
155 * The union holding the value.
156 */
157 union
158 {
159 /// The double value.
161 /// The int64 value.
162 int64_t intValue_;
163 } value_;
164
165 /// The value type.
166 ValueType valueType_ = VT_INVALID;
167};
168
169inline DataTimestamp::DataTimestamp(const double value) :
170 valueType_(VT_DOUBLE)
171{
172 value_.doubleValue_ = value;
173}
174
175inline DataTimestamp::DataTimestamp(const int64_t value) :
176 valueType_(VT_INT64)
177{
178 value_.intValue_ = value;
179}
180
181inline bool DataTimestamp::isDouble() const
182{
183 return valueType_ == VT_DOUBLE;
184}
185
186inline bool DataTimestamp::isInt() const
187{
188 return valueType_ == VT_INT64;
189}
190
191inline double DataTimestamp::asDouble() const
192{
193 ocean_assert(isValid());
194 ocean_assert(valueType_ == VT_DOUBLE);
195
196 return value_.doubleValue_;
197}
198
199inline int64_t DataTimestamp::asInt() const
200{
201 ocean_assert(isValid());
202 ocean_assert(valueType_ == VT_INT64);
203
204 return value_.intValue_;
205}
206
207inline double DataTimestamp::forceDouble() const
208{
209 ocean_assert(isValid());
210
211 if (valueType_ == VT_DOUBLE)
212 {
213 return asDouble();
214 }
215 else
216 {
217 ocean_assert(valueType_ == VT_INT64);
218
219 return double(asInt());
220 }
221}
222
223inline bool DataTimestamp::isValid() const
224{
225 return valueType_ != VT_INVALID;
226}
227
228}
229
230}
231
232}
233
234#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:199
double doubleValue_
The double value.
Definition DataTimestamp.h:160
double forceDouble() const
Returns the timestamp as a double value regardless of the actual value type.
Definition DataTimestamp.h:207
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:191
ValueType valueType_
The value type.
Definition DataTimestamp.h:166
bool isDouble() const
Returns whether this timestamp holds a double value.
Definition DataTimestamp.h:181
bool isInt() const
Returns whether this timestamp holds an int64 value.
Definition DataTimestamp.h:186
bool isValid() const
Returns whether this timestamp is valid.
Definition DataTimestamp.h:223
bool operator==(const DataTimestamp &dataTimestamp) const
Returns whether two timestamps are equal.
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.
bool operator<(const DataTimestamp &dataTimestamp) const
Returns whether this timestamp is less than another timestamp.
int64_t intValue_
The int64 value.
Definition DataTimestamp.h:162
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