Ocean
Value.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_BASE_VALUE_H
9 #define META_OCEAN_BASE_VALUE_H
10 
11 #include "ocean/base/Base.h"
12 
13 #include <string>
14 
15 namespace Ocean
16 {
17 
18 /**
19  * This class implements a type independent value.
20  * @ingroup base
21  */
22 class OCEAN_BASE_EXPORT Value
23 {
24  public:
25 
26  /**
27  * Definition of different internal value type.
28  */
29  enum ValueType
30  {
31  /// Invalid value type.
33  /// Boolean value type.
35  /// 32 bit integer value.
37  /// 64 bit integer value.
39  /// Single precision floating point value.
41  /// Double precision floating point value.
43  /// String value.
45  /// Buffer value.
46  VT_BUFFER
47  };
48 
49  protected:
50 
51  /**
52  * Definition of a struct combining memory pointer and size.
53  */
54  struct BufferStruct
55  {
56  /// Pointer of the buffer.
57  void* buffer_;
58 
59  /// The size of the buffer, in bytes.
60  size_t size_;
61  };
62 
63  public:
64 
65  /**
66  * Creates an empty value.
67  */
68  inline Value() noexcept;
69 
70  /**
71  * Move constructor.
72  * @param value the value object to move
73  */
74  Value(Value&& value);
75 
76  /**
77  * Copies a value object.
78  * @param value The value object to copy
79  */
80  Value(const Value& value);
81 
82  /**
83  * Creates a new value object with an boolean value as internal data type.
84  * @param value Internal value
85  */
86  explicit Value(const bool value);
87 
88  /**
89  * Creates a new value object with an integer value as internal data type.
90  * @param value Internal value
91  */
92  explicit Value(const int32_t value);
93 
94  /**
95  * Creates a new value object with an 64 bit integer value as internal data type.
96  * @param value Internal value
97  */
98  explicit Value(const int64_t value);
99 
100  /**
101  * Creates a new value object with a floating point value as internal data type.
102  * @param value Internal value
103  */
104  explicit Value(const float value);
105 
106  /**
107  * Creates a new value object with a double precision floating point value as internal data type.
108  * @param value Internal value
109  */
110  explicit Value(const double value);
111 
112  /**
113  * Creates a new value object with a string value as internal data type.
114  * @param value Internal value
115  */
116  explicit Value(const char* value);
117 
118  /**
119  * Creates a new value object with a string value as internal data type.
120  * @param value Internal value
121  */
122  explicit Value(const std::string& value);
123 
124  /**
125  * Creates a new value object with an arbitrary buffer as internal data type.
126  * @param value Internal value, can be nullptr if size == 0
127  * @param size The size of the buffer in bytes, with range [0, infinity)
128  */
129  explicit Value(const void* value, const size_t size);
130 
131  /**
132  * Destructs a value object.
133  */
134  ~Value();
135 
136  /**
137  * Returns the internal data type of this value object.
138  * @return Internal data type.
139  */
140  inline ValueType type() const;
141 
142  /**
143  * Returns whether the internal data type is equivalent to a given one.
144  * @param valueType Internal data type to check
145  * @return True, if so
146  */
147  inline bool isType(const ValueType valueType) const;
148 
149  /**
150  * Returns whether this object holds an boolean value as internal data.
151  * @return True, if so
152  */
153  inline bool isBool() const;
154 
155  /**
156  * Returns whether this object holds an integer value as internal data.
157  * @return True, if so
158  */
159  inline bool isInt() const;
160 
161  /**
162  * Returns whether this object holds a 64 bit integer value as internal data.
163  * @return True, if so
164  */
165  inline bool isInt64() const;
166 
167  /**
168  * Returns whether this object holds a float value as internal data.
169  * @return True, if so
170  */
171  inline bool isFloat() const;
172 
173  /**
174  * Returns whether this object holds a 64 bit float value as internal data.
175  * @param allowIntAndFloat True, to allow int, int64, float and float64 values; False, to force float64 value only
176  * @return True, if so
177  */
178  inline bool isFloat64(const bool allowIntAndFloat = false) const;
179 
180  /**
181  * Returns whether this object holds a string value as internal data.
182  * @return True, if so
183  */
184  inline bool isString() const;
185 
186  /**
187  * Returns whether this object holds a buffer value as internal data.
188  * @return True, if so
189  */
190  inline bool isBuffer() const;
191 
192  /**
193  * Returns whether this object does not holds any internal data.
194  * @return True, if so
195  */
196  inline bool isNull() const;
197 
198  /**
199  * Returns the internal value as a boolean.
200  * Beware: Check the internal type of this value object before!
201  * @return Internal data as boolean
202  */
203  bool boolValue() const;
204 
205  /**
206  * Returns the internal value as integer.
207  * Beware: Check the internal type of this value object before!
208  * @return Internal data as integer
209  */
210  int32_t intValue() const;
211 
212  /**
213  * Returns the internal value as 64 bit integer.
214  * Beware: Check the internal type of this value object before!
215  * @return Internal data as 64 bit integer
216  */
217  int64_t int64Value() const;
218 
219  /**
220  * Returns the internal value as float.
221  * Beware: Check the internal type of this value object before!
222  * @return Internal data as float
223  */
224  float floatValue() const;
225 
226  /**
227  * Returns the internal value as 64 bit float.
228  * Beware: Check the internal type of this value object before!
229  * @param allowIntAndFloat True, to allow int, int64, float and float64 values; False, to force float64 value only
230  * @return Internal data as 64 bit float
231  */
232  double float64Value(const bool allowIntAndFloat = false) const;
233 
234  /**
235  * Returns the internal value as string.
236  * Beware: Check the internal type of this value object before!
237  * @return Internal data as string
238  */
239  std::string stringValue() const;
240 
241  /**
242  * Returns the internal value as buffer.
243  * Beware: Check the internal type of this value object before!
244  * @param size The size of the buffer in bytes, with range [0, infinity)
245  * @return The pointer to the buffer value
246  */
247  const void* bufferValue(size_t& size) const;
248 
249  /**
250  * Returns the internal value as a readable string not considering the type of the value.
251  * Thus, this function can be called for any value type.
252  * @param floatingPointPrecision The number of decimal places for floating point numbers, with range [1, infinity)
253  * @return The readable string of this value
254  */
255  std::string readableString(const unsigned int floatingPointPrecision = 2u) const;
256 
257  /**
258  * Move operator.
259  * @param value The value object to be moved
260  * @return Reference to this object
261  */
262  Value& operator=(Value&& value);
263 
264  /**
265  * Assigns a value object to this one.
266  * @param value The value object to assign
267  * @return Reference to this object
268  */
269  Value& operator=(const Value& value);
270 
271  /**
272  * Returns whether two value objects are identical (having same type and holding the same value).
273  * @param value The second value to compare
274  * @return True, if so
275  */
276  bool operator==(const Value& value) const;
277 
278  /**
279  * Returns whether two value objects are not identical (not having same type or not holding the same value).
280  * @param value The second value to compare
281  * @return True, if so
282  */
283  inline bool operator!=(const Value& value) const;
284 
285  /**
286  * Returns whether this object holds any internal data.
287  * @return True, if so
288  */
289  explicit inline operator bool() const;
290 
291  /**
292  * Writes a Value object to a buffer.
293  * @param value The value to write to the buffer, must be valid
294  * @param buffer The buffer to which the value will be written, will be resized as needed
295  * @param offsetInBuffer The offset within the buffer at which position the value information will be written, with range [0, infinity)
296  * @return True, if succeeded
297  * @see readFromBuffer().
298  */
299  static bool writeToBuffer(const Value& value, std::vector<uint8_t>& buffer, const size_t offsetInBuffer = 0);
300 
301  /**
302  * Reads a Value object from a buffer.
303  * @param buffer The pointer to the input buffer, must be valid
304  * @param size The size of the input buffer in bytes, with range [1, infinity)
305  * @return The resulting Value object, invalid if the input was invalid
306  * @see writeToBuffer().
307  */
308  static Value readFromBuffer(const void* buffer, const size_t size);
309 
310  protected:
311 
312  /**
313  * Releases the value data.
314  */
315  void release();
316 
317  protected:
318 
319  /// Internal value type.
320  ValueType valueType_ = VT_INVALID;
321 
322  /**
323  * Definition of a union holding all possible values.
324  */
326  {
327  /// Possible boolean value.
329 
330  /// Possible int value.
331  int32_t valueInt_;
332 
333  /// Possible int 64 value.
334  int64_t valueInt64_;
335 
336  /// Possible floating point value.
337  float valueFloat_;
338 
339  /// Possible double precision floating point value.
341 
342  /// Possible buffer value.
344  } valueUnion_;
345 };
346 
347 inline Value::Value() noexcept :
348  valueType_(VT_INVALID)
349 {
350  // nothing to do here
351 }
352 
354 {
355  return valueType_;
356 }
357 
358 inline bool Value::isType(const ValueType valueType) const
359 {
360  return valueType_ == valueType;
361 }
362 
363 inline bool Value::isBool() const
364 {
365  return valueType_ == VT_BOOL;
366 }
367 
368 inline bool Value::isInt() const
369 {
370  return valueType_ == VT_INT_32;
371 }
372 
373 inline bool Value::isInt64() const
374 {
375  return valueType_ == VT_INT_64;
376 }
377 
378 inline bool Value::isFloat() const
379 {
380  return valueType_ == VT_FLOAT_32;
381 }
382 
383 inline bool Value::isFloat64(const bool allowIntAndFloat) const
384 {
385  if (allowIntAndFloat)
386  {
388  }
389  else
390  {
391  return valueType_ == VT_FLOAT_64;
392  }
393 }
394 
395 inline bool Value::isString() const
396 {
397  return valueType_ == VT_STRING;
398 }
399 
400 inline bool Value::isBuffer() const
401 {
402  return valueType_ == VT_BUFFER;
403 }
404 
405 inline bool Value::isNull() const
406 {
407  return valueType_ == VT_INVALID;
408 }
409 
410 inline bool Value::operator!=(const Value& value) const
411 {
412  return !(*this == value);
413 }
414 
415 inline Value::operator bool() const
416 {
417  return valueType_ != VT_INVALID;
418 }
419 
420 }
421 
422 #endif // META_OCEAN_BASE_VALUE_H
This class implements a type independent value.
Definition: Value.h:23
bool isString() const
Returns whether this object holds a string value as internal data.
Definition: Value.h:395
ValueType type() const
Returns the internal data type of this value object.
Definition: Value.h:353
bool isFloat64(const bool allowIntAndFloat=false) const
Returns whether this object holds a 64 bit float value as internal data.
Definition: Value.h:383
bool isType(const ValueType valueType) const
Returns whether the internal data type is equivalent to a given one.
Definition: Value.h:358
bool isBuffer() const
Returns whether this object holds a buffer value as internal data.
Definition: Value.h:400
ValueType
Definition of different internal value type.
Definition: Value.h:30
@ VT_INT_64
64 bit integer value.
Definition: Value.h:38
@ VT_FLOAT_32
Single precision floating point value.
Definition: Value.h:40
@ VT_STRING
String value.
Definition: Value.h:44
@ VT_INT_32
32 bit integer value.
Definition: Value.h:36
@ VT_INVALID
Invalid value type.
Definition: Value.h:32
@ VT_BUFFER
Buffer value.
Definition: Value.h:46
@ VT_BOOL
Boolean value type.
Definition: Value.h:34
@ VT_FLOAT_64
Double precision floating point value.
Definition: Value.h:42
ValueType valueType_
Internal value type.
Definition: Value.h:320
bool isFloat() const
Returns whether this object holds a float value as internal data.
Definition: Value.h:378
bool isInt() const
Returns whether this object holds an integer value as internal data.
Definition: Value.h:368
bool operator!=(const Value &value) const
Returns whether two value objects are not identical (not having same type or not holding the same val...
Definition: Value.h:410
bool isNull() const
Returns whether this object does not holds any internal data.
Definition: Value.h:405
Value() noexcept
Creates an empty value.
Definition: Value.h:347
bool isBool() const
Returns whether this object holds an boolean value as internal data.
Definition: Value.h:363
bool isInt64() const
Returns whether this object holds a 64 bit integer value as internal data.
Definition: Value.h:373
void release(T *object)
This functions allows to release a DirectShow object if it does exist.
Definition: DSObject.h:266
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15
Definition of a struct combining memory pointer and size.
Definition: Value.h:55
size_t size_
The size of the buffer, in bytes.
Definition: Value.h:60
void * buffer_
Pointer of the buffer.
Definition: Value.h:57
Definition of a union holding all possible values.
Definition: Value.h:326
double valueFloat64_
Possible double precision floating point value.
Definition: Value.h:340
int32_t valueInt_
Possible int value.
Definition: Value.h:331
bool valueBool_
Possible boolean value.
Definition: Value.h:328
BufferStruct bufferStruct_
Possible buffer value.
Definition: Value.h:343
float valueFloat_
Possible floating point value.
Definition: Value.h:337
int64_t valueInt64_
Possible int 64 value.
Definition: Value.h:334