OpenMS
Loading...
Searching...
No Matches
EnumHelpers.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: Chris Bielow $
6// $Authors: Chris Bielow $
7// --------------------------------------------------------------------------
8
9#pragma once
10
13
14#include <algorithm>
15#include <string>
16#include <vector>
17
18namespace OpenMS
19{
20 namespace Helpers
21 {
24 template <class ContainerType>
25 Size indexOf(const ContainerType& cont, const typename ContainerType::value_type& val)
26 {
27 auto it = std::find(cont.begin(), cont.end(), val);
28 if (it == cont.end())
29 {
30 throw Exception::ElementNotFound(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, val);
31 }
32 return std::distance(cont.begin(), it);
33 }
34
44 template <class EnumType>
45 std::vector<std::string> enumNames(const std::string* names, EnumType size_value)
46 {
47 std::vector<std::string> result;
48 result.reserve(static_cast<size_t>(size_value));
49 for (size_t i = 0; i < static_cast<size_t>(size_value); ++i)
50 {
51 result.push_back(names[i]);
52 }
53 return result;
54 }
55
70 template <class EnumType>
71 const std::string& enumToName(const std::string* names, EnumType value, EnumType size_value, const char* size_name, const char* function)
72 {
73 if (value == size_value)
74 {
75 throw Exception::InvalidValue(__FILE__, __LINE__, function, "Value not allowed", size_name);
76 }
77 return names[static_cast<size_t>(value)];
78 }
79
94 template <class EnumType>
95 EnumType enumFromName(const std::string* names, EnumType size_value, const std::string& name, const char* message, const char* function)
96 {
97 const std::string* first = names;
98 const std::string* last = names + static_cast<size_t>(size_value);
99 const auto it = std::find(first, last, name);
100 if (it == last)
101 {
102 throw Exception::InvalidValue(__FILE__, __LINE__, function, message, name);
103 }
104 return static_cast<EnumType>(it - first);
105 }
106
107 }
108}
109
110
Element could not be found exception.
Definition Exception.h:657
Invalid value exception.
Definition Exception.h:306
OpenMS looks in the< code > OPENMS_THERMO_MANAGED_DIR</code > environment variable first
Definition common-cmake-parameters.doxygen:179
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
const std::string & enumToName(const std::string *names, EnumType value, EnumType size_value, const char *size_name, const char *function)
Converts an enum value to its entry in names.
Definition EnumHelpers.h:71
EnumType enumFromName(const std::string *names, EnumType size_value, const std::string &name, const char *message, const char *function)
Converts a name to the enum value at its index in names.
Definition EnumHelpers.h:95
Size indexOf(const ContainerType &cont, const typename ContainerType::value_type &val)
Definition EnumHelpers.h:25
std::vector< std::string > enumNames(const std::string *names, EnumType size_value)
Returns the names of all enum values, i.e. the first size_value entries of names.
Definition EnumHelpers.h:45
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19