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
18class QString;
19
20namespace OpenMS
21{
22 class ParamValue;
23
33 class OPENMS_DLLAPI DataValue
34 {
35
36public:
37
39 static const DataValue EMPTY;
40
53
55 static const std::string NamesOfDataType[SIZE_OF_DATATYPE];
56
58 enum UnitType : unsigned char
59 {
62 OTHER
63 };
64
66
67
72 DataValue(DataValue&&) noexcept;
74 DataValue(const char*);
76 DataValue(const std::string&);
80 DataValue(const QString&);
88 DataValue(long double);
90 DataValue(double);
92 DataValue(float);
94 DataValue(short int);
96 DataValue(unsigned short int);
100 DataValue(unsigned);
102 DataValue(long int);
104 DataValue(unsigned long);
106 DataValue(long long);
108 DataValue(unsigned long long);
114
118
119
123 operator ParamValue() const;
124
130 operator std::string() const;
131
137 operator StringList() const;
138
144 operator IntList() const;
145
151 operator DoubleList() const;
152
160 operator long double() const;
161
169 operator double() const;
170
178 operator float() const;
179
187 operator short int() const;
188
196 operator unsigned short int() const;
197
206 operator int() const;
207
215 operator unsigned int() const;
216
224 operator long int() const;
225
233 operator unsigned long int() const;
234
242 operator long long() const;
243
251 operator unsigned long long() const;
252
260 bool toBool() const;
261
268 const char* toChar() const;
269
275 StringList toStringList() const;
276
282 IntList toIntList() const;
283
289 DoubleList toDoubleList() const;
291
294
295
296 DataValue& operator=(const DataValue&);
298 DataValue& operator=(DataValue&&) noexcept;
300 DataValue& operator=(const char*);
302 DataValue& operator=(const std::string&);
304 DataValue& operator=(const String&);
306 DataValue& operator=(const QString&);
308 DataValue& operator=(const StringList&);
310 DataValue& operator=(const IntList&);
312 DataValue& operator=(const DoubleList&);
314 DataValue& operator=(const long double);
316 DataValue& operator=(const double);
318 DataValue& operator=(const float);
320 DataValue& operator=(const short int);
322 DataValue& operator=(const unsigned short int);
324 DataValue& operator=(const int);
326 DataValue& operator=(const unsigned);
328 DataValue& operator=(const long int);
330 DataValue& operator=(const unsigned long);
332 DataValue& operator=(const long long);
334 DataValue& operator=(const unsigned long long);
336
340
341
346 String toString(bool full_precision = true) const;
347
349 QString toQString() const;
351
353 DataType valueType() const;
354
360 bool isEmpty() const;
361
364
365
367 UnitType getUnitType() const;
368
369 void setUnitType(const UnitType & u);
370
372 bool hasUnit() const;
373
375 const int32_t & getUnit() const;
376
378 void setUnit(const int32_t & unit);
379
381
383 friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream&, const DataValue&);
384
386 friend OPENMS_DLLAPI bool operator==(const DataValue&, const DataValue&);
387
389 friend OPENMS_DLLAPI bool operator<(const DataValue&, const DataValue&);
390
392 friend OPENMS_DLLAPI bool operator>(const DataValue&, const DataValue&);
393
395 friend OPENMS_DLLAPI bool operator!=(const DataValue&, const DataValue&);
396
397protected:
398
400 DataType value_type_;
401
403 UnitType unit_type_;
404
406 int32_t unit_;
407
409 union
410 {
411 SignedSize ssize_;
412 double dou_;
413 String* str_;
414 StringList* str_list_;
415 IntList* int_list_;
416 DoubleList* dou_list_;
417 } data_;
418
419private:
420
422 void clear_() noexcept;
423 };
424} // namespace OpenMS
425
426// Hash function specialization for DataValue
427namespace std
428{
439 template<>
440 struct hash<OpenMS::DataValue>
441 {
442 std::size_t operator()(const OpenMS::DataValue& dv) const noexcept
443 {
444 std::size_t seed = 0;
445
446 // Hash the value type
447 OpenMS::hash_combine(seed, OpenMS::hash_int(static_cast<int>(dv.valueType())));
448
449 // Hash the value content based on type
450 if (!dv.isEmpty())
451 {
452 switch (dv.valueType())
453 {
455 {
456 // operator== uses fabs(a - b) < 1e-6 for doubles, so we round to 6 decimal places
457 // to ensure equal values (per operator==) produce identical hashes
458 double val = static_cast<double>(dv);
459 // Round to 6 decimal places: multiply by 1e6, round, then hash as int64
460 int64_t rounded = static_cast<int64_t>(std::round(val * 1e6));
462 break;
463 }
464 default:
465 // For all other types (string, int, lists), use toString() which is consistent with operator==
467 break;
468 }
469 }
470
471 // Hash unit information if present
472 if (dv.hasUnit())
473 {
474 OpenMS::hash_combine(seed, OpenMS::hash_int(static_cast<int>(dv.getUnitType())));
475 OpenMS::hash_combine(seed, OpenMS::hash_int(dv.getUnit()));
476 }
477
478 return seed;
479 }
480 };
481} // namespace std
482
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition DataValue.h:34
static const DataValue EMPTY
Empty data value for comparisons.
Definition DataValue.h:39
UnitType
Supported types for DataValue.
Definition DataValue.h:59
@ MS_ONTOLOGY
ms.ontology MS
Definition DataValue.h:61
@ UNIT_ONTOLOGY
unit.ontology UO
Definition DataValue.h:60
DataValue(const DataValue &)
Copy constructor.
DataType
Supported types for DataValue.
Definition DataValue.h:43
@ DOUBLE_VALUE
double value
Definition DataValue.h:46
@ EMPTY_VALUE
empty
Definition DataValue.h:50
@ INT_LIST
integer list
Definition DataValue.h:48
@ STRING_VALUE
string value
Definition DataValue.h:44
@ STRING_LIST
string list
Definition DataValue.h:47
@ INT_VALUE
integer value
Definition DataValue.h:45
@ DOUBLE_LIST
double list
Definition DataValue.h:49
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:34
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:442