OpenMS
Loading...
Searching...
No Matches
ParamValue.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// $Authors: Ruben Grünberg $
6// --------------------------------------------------------------------------
7
8#pragma once
9
10#include <OpenMS/OpenMSConfig.h>
12
13#include <cstddef> // for ptrdiff_t
14#include <functional>
15#include <string>
16#include <vector>
17
18
19namespace OpenMS
20{
30 class OPENMS_DLLAPI ParamValue
31 {
32
33public:
34
36 static const ParamValue EMPTY;
37
49
51
52
59 ParamValue(const char*);
61 ParamValue(const std::string&);
63 ParamValue(const std::vector<std::string>&);
65 ParamValue(const std::vector<int>&);
67 ParamValue(const std::vector<double>&);
69 ParamValue(long double);
71 ParamValue(double);
73 ParamValue(float);
75 ParamValue(short int);
77 ParamValue(unsigned short int);
81 ParamValue(unsigned);
83 ParamValue(long int);
85 ParamValue(unsigned long);
87 ParamValue(long long);
89 ParamValue(unsigned long long);
93
97
98
104 operator std::string() const;
105
111 operator std::vector<std::string>() const;
112
118 operator std::vector<int>() const;
119
125 operator std::vector<double>() const;
126
134 operator long double() const;
135
143 operator double() const;
144
152 operator float() const;
153
161 operator short int() const;
162
170 operator unsigned short int() const;
171
180 operator int() const;
181
189 operator unsigned int() const;
190
198 operator long int() const;
199
207 operator unsigned long int() const;
208
216 operator long long() const;
217
225 operator unsigned long long() const;
226
234 bool toBool() const;
235
242 const char* toChar() const;
243
250 std::string toString(bool full_precision = true) const;
251
257 std::vector<std::string> toStringVector() const;
258
264 std::vector<int> toIntVector() const;
265
271 std::vector<double> toDoubleVector() const;
273
276
277
278 ParamValue& operator=(const ParamValue&);
280 ParamValue& operator=(ParamValue&&) noexcept;
282 ParamValue& operator=(const char*);
284 ParamValue& operator=(const std::string&);
286 ParamValue& operator=(const std::vector<std::string>&);
288 ParamValue& operator=(const std::vector<int>&);
290 ParamValue& operator=(const std::vector<double>&);
292 ParamValue& operator=(const long double);
294 ParamValue& operator=(const double);
296 ParamValue& operator=(const float);
298 ParamValue& operator=(const short int);
300 ParamValue& operator=(const unsigned short int);
302 ParamValue& operator=(const int);
304 ParamValue& operator=(const unsigned);
306 ParamValue& operator=(const long int);
308 ParamValue& operator=(const unsigned long);
310 ParamValue& operator=(const long long);
312 ParamValue& operator=(const unsigned long long);
314
316 inline ValueType valueType() const
317 {
318 return value_type_;
319 }
320
326 inline bool isEmpty() const
327 {
328 return value_type_ == EMPTY_VALUE;
329 }
330
332 friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream&, const ParamValue&);
333
335 friend OPENMS_DLLAPI bool operator==(const ParamValue&, const ParamValue&);
336
338 friend OPENMS_DLLAPI bool operator<(const ParamValue&, const ParamValue&);
339
341 friend OPENMS_DLLAPI bool operator>(const ParamValue&, const ParamValue&);
342
344 friend OPENMS_DLLAPI bool operator!=(const ParamValue&, const ParamValue&);
345
346protected:
347
350
352 union
353 {
354 std::ptrdiff_t ssize_;
355 double dou_;
356 std::string* str_;
357 std::vector<std::string>* str_list_;
358 std::vector<int>* int_list_;
359 std::vector<double>* dou_list_;
360 } data_;
361
362private:
363
365 void clear_() noexcept;
366
370 static std::string doubleToString(double value, bool full_precision = true);
371
372 };
373} // namespace OpenMS
374
375// std::hash specialization for ParamValue - must be in std namespace
376namespace std
377{
378 template<>
379 struct hash<OpenMS::ParamValue>
380 {
381 std::size_t operator()(const OpenMS::ParamValue& pv) const noexcept
382 {
383 using namespace OpenMS;
384 std::size_t seed = hash_int(static_cast<unsigned char>(pv.valueType()));
385
386 switch (pv.valueType())
387 {
388 case ParamValue::EMPTY_VALUE:
389 // No additional data to hash
390 break;
391 case ParamValue::STRING_VALUE:
392 {
393 std::string s = pv;
394 hash_combine(seed, fnv1a_hash_string(s));
395 break;
396 }
397 case ParamValue::INT_VALUE:
398 {
399 std::ptrdiff_t i = static_cast<long long>(pv);
400 hash_combine(seed, hash_int(i));
401 break;
402 }
403 case ParamValue::DOUBLE_VALUE:
404 {
405 double d = pv;
406 hash_combine(seed, hash_float(d));
407 break;
408 }
409 case ParamValue::STRING_LIST:
410 {
411 std::vector<std::string> sl = pv.toStringVector();
412 hash_combine(seed, hash_int(sl.size()));
413 for (const auto& s : sl)
414 {
415 hash_combine(seed, fnv1a_hash_string(s));
416 }
417 break;
418 }
419 case ParamValue::INT_LIST:
420 {
421 std::vector<int> il = pv.toIntVector();
422 hash_combine(seed, hash_int(il.size()));
423 for (int i : il)
424 {
425 hash_combine(seed, hash_int(i));
426 }
427 break;
428 }
429 case ParamValue::DOUBLE_LIST:
430 {
431 std::vector<double> dl = pv.toDoubleVector();
432 hash_combine(seed, hash_int(dl.size()));
433 for (double d : dl)
434 {
435 hash_combine(seed, hash_float(d));
436 }
437 break;
438 }
439 }
440 return seed;
441 }
442 };
443} // namespace std
444
Class to hold strings, numeric values, vectors of strings and vectors of numeric values using the stl...
Definition ParamValue.h:31
friend std::ostream & operator<<(std::ostream &, const ParamValue &)
output stream operator
ValueType value_type_
Type of the currently stored value.
Definition ParamValue.h:349
ParamValue()
Default constructor.
friend bool operator==(const ParamValue &, const ParamValue &)
Equality comparator.
ParamValue(const ParamValue &)
Copy constructor.
void clear_() noexcept
Clears the current state of the ParamValue and release every used memory.
friend bool operator!=(const ParamValue &, const ParamValue &)
Equality comparator.
bool isEmpty() const
Test if the value is empty.
Definition ParamValue.h:326
friend bool operator>(const ParamValue &, const ParamValue &)
Greater than comparator (for vectors we use the size)
ValueType
Supported types for ParamValue.
Definition ParamValue.h:40
@ DOUBLE_VALUE
double value
Definition ParamValue.h:43
@ INT_LIST
integer vector
Definition ParamValue.h:45
@ STRING_VALUE
string value
Definition ParamValue.h:41
@ STRING_LIST
string vector
Definition ParamValue.h:44
@ INT_VALUE
integer value
Definition ParamValue.h:42
@ DOUBLE_LIST
double vector
Definition ParamValue.h:46
friend bool operator<(const ParamValue &, const ParamValue &)
Smaller than comparator (for vectors we use the size)
ParamValue(ParamValue &&) noexcept
Move constructor.
static const ParamValue EMPTY
Empty data value for comparisons.
Definition ParamValue.h:36
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
STL namespace.
std::size_t operator()(const OpenMS::ParamValue &pv) const noexcept
Definition ParamValue.h:381