OpenMS
Loading...
Searching...
No Matches
DataValue.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Timo Sachsenberg$
6// $Authors: Marc Sturm $
7// --------------------------------------------------------------------------
8
9#pragma once
10
13
16#include <OpenMS/OpenMSConfig.h>
17
18namespace OpenMS
19{
20 class ParamValue;
21
31 class OPENMS_DLLAPI DataValue
32 {
33
34public:
35
37 static const DataValue EMPTY;
38
51
53 static const std::string NamesOfDataType[SIZE_OF_DATATYPE];
54
56 enum UnitType : unsigned char
57 {
60 OTHER
61 };
62
64
65
70 DataValue(DataValue&&) noexcept;
72 DataValue(const char*);
74 DataValue(const std::string&);
84 DataValue(long double);
86 DataValue(double);
88 DataValue(float);
90 DataValue(short int);
92 DataValue(unsigned short int);
96 DataValue(unsigned);
98 DataValue(long int);
100 DataValue(unsigned long);
102 DataValue(long long);
104 DataValue(unsigned long long);
110
114
115
119 operator ParamValue() const;
120
126 operator std::string() const;
127
133 operator StringList() const;
134
140 operator IntList() const;
141
147 operator DoubleList() const;
148
156 operator long double() const;
157
165 operator double() const;
166
174 operator float() const;
175
183 operator short int() const;
184
192 operator unsigned short int() const;
193
202 operator int() const;
203
211 operator unsigned int() const;
212
220 operator long int() const;
221
229 operator unsigned long int() const;
230
238 operator long long() const;
239
247 operator unsigned long long() const;
248
256 bool toBool() const;
257
264 const char* toChar() const;
265
271 StringList toStringList() const;
272
278 IntList toIntList() const;
279
285 DoubleList toDoubleList() const;
287
290
291
292 DataValue& operator=(const DataValue&);
294 DataValue& operator=(DataValue&&) noexcept;
296 DataValue& operator=(const char*);
298 DataValue& operator=(const std::string&);
300 DataValue& operator=(const String&);
302 DataValue& operator=(const StringList&);
304 DataValue& operator=(const IntList&);
306 DataValue& operator=(const DoubleList&);
308 DataValue& operator=(const long double);
310 DataValue& operator=(const double);
312 DataValue& operator=(const float);
314 DataValue& operator=(const short int);
316 DataValue& operator=(const unsigned short int);
318 DataValue& operator=(const int);
320 DataValue& operator=(const unsigned);
322 DataValue& operator=(const long int);
324 DataValue& operator=(const unsigned long);
326 DataValue& operator=(const long long);
328 DataValue& operator=(const unsigned long long);
330
334
335
340 String toString(bool full_precision = true) const;
341
343
345 DataType valueType() const;
346
352 bool isEmpty() const;
353
356
357
359 UnitType getUnitType() const;
360
361 void setUnitType(const UnitType & u);
362
364 bool hasUnit() const;
365
367 const int32_t & getUnit() const;
368
370 void setUnit(const int32_t & unit);
371
373
375 friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream&, const DataValue&);
376
378 friend OPENMS_DLLAPI bool operator==(const DataValue&, const DataValue&);
379
381 friend OPENMS_DLLAPI bool operator<(const DataValue&, const DataValue&);
382
384 friend OPENMS_DLLAPI bool operator>(const DataValue&, const DataValue&);
385
387 friend OPENMS_DLLAPI bool operator!=(const DataValue&, const DataValue&);
388
389protected:
390
392 DataType value_type_;
393
395 UnitType unit_type_;
396
398 int32_t unit_;
399
401 union
402 {
403 SignedSize ssize_;
404 double dou_;
405 String* str_;
406 StringList* str_list_;
407 IntList* int_list_;
408 DoubleList* dou_list_;
409 } data_;
410
411private:
412
414 void clear_() noexcept;
415 };
416} // namespace OpenMS
417
418// Hash function specialization for DataValue
419namespace std
420{
431 template<>
432 struct hash<OpenMS::DataValue>
433 {
434 std::size_t operator()(const OpenMS::DataValue& dv) const noexcept
435 {
436 std::size_t seed = 0;
437
438 // Hash the value type
439 OpenMS::hash_combine(seed, OpenMS::hash_int(static_cast<int>(dv.valueType())));
440
441 // Hash the value content based on type
442 if (!dv.isEmpty())
443 {
444 switch (dv.valueType())
445 {
447 {
448 // operator== uses fabs(a - b) < 1e-6 for doubles, so we round to 6 decimal places
449 // to ensure equal values (per operator==) produce identical hashes
450 double val = static_cast<double>(dv);
451 // Round to 6 decimal places: multiply by 1e6, round, then hash as int64
452 int64_t rounded = static_cast<int64_t>(std::round(val * 1e6));
454 break;
455 }
456 default:
457 // For all other types (string, int, lists), use toString() which is consistent with operator==
459 break;
460 }
461 }
462
463 // Hash unit information if present
464 if (dv.hasUnit())
465 {
466 OpenMS::hash_combine(seed, OpenMS::hash_int(static_cast<int>(dv.getUnitType())));
467 OpenMS::hash_combine(seed, OpenMS::hash_int(dv.getUnit()));
468 }
469
470 return seed;
471 }
472 };
473} // namespace std
474
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition DataValue.h:32
static const DataValue EMPTY
Empty data value for comparisons.
Definition DataValue.h:37
UnitType
Supported types for DataValue.
Definition DataValue.h:57
@ MS_ONTOLOGY
ms.ontology MS
Definition DataValue.h:59
@ UNIT_ONTOLOGY
unit.ontology UO
Definition DataValue.h:58
DataValue(const DataValue &)
Copy constructor.
DataType
Supported types for DataValue.
Definition DataValue.h:41
@ DOUBLE_VALUE
double value
Definition DataValue.h:44
@ EMPTY_VALUE
empty
Definition DataValue.h:48
@ INT_LIST
integer list
Definition DataValue.h:46
@ STRING_VALUE
string value
Definition DataValue.h:42
@ STRING_LIST
string list
Definition DataValue.h:45
@ INT_VALUE
integer value
Definition DataValue.h:43
@ DOUBLE_LIST
double list
Definition DataValue.h:47
void clear_() noexcept
Clears the current state of the DataValue and release every used memory.
DataValue(DataValue &&) noexcept
Move constructor.
DataValue()
Default constructor.
Class to hold strings, numeric values, vectors of strings and vectors of numeric values using the stl...
Definition ParamValue.h:31
A more convenient string class.
Definition String.h:32
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition Types.h:104
std::vector< Int > IntList
Vector of signed integers.
Definition ListUtils.h:29
std::vector< String > StringList
Vector of String.
Definition ListUtils.h:44
std::vector< double > DoubleList
Vector of double precision real types.
Definition ListUtils.h:36
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::size_t hash_int(T value) noexcept
Hash for an integer type.
Definition HashUtils.h:107
void hash_combine(std::size_t &seed, std::size_t value) noexcept
Combine a hash value with additional data using golden ratio mixing.
Definition HashUtils.h:87
std::size_t fnv1a_hash_string(const std::string &s) noexcept
FNV-1a hash for a string.
Definition HashUtils.h:70
STL namespace.
std::size_t operator()(const OpenMS::DataValue &dv) const noexcept
Definition DataValue.h:434