Blue Brain BioExplorer
EnumUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2024, EPFL/Blue Brain Project
3  * All rights reserved. Do not distribute without permission.
4  * Responsible Author: Jonas Karlsson <jonas.karlsson@epfl.ch>
5  *
6  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License version 3.0 as published
10  * by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #pragma once
23 
24 #include <string>
25 #include <tuple>
26 #include <vector>
27 
28 namespace core
29 {
30 /*
31  * This file contains helper functions for converting strings to and from enums.
32  * To use the helper functions define the method 'enumMap' with a specialization
33  * for your enum type.
34  */
35 
36 /* Returns a mapping from a name to an enum type. */
37 template <typename EnumT>
38 std::vector<std::pair<std::string, EnumT>> enumMap();
39 
40 /* Returns all names for given enum type 'EnumT' */
41 template <typename EnumT>
42 inline std::vector<std::string> enumNames()
43 {
44  std::vector<std::string> v;
45  for (const auto& p : enumMap<EnumT>())
46  v.push_back(p.first);
47  return v;
48 }
49 
50 /* Convert a string to an enum. */
51 template <typename EnumT>
52 inline EnumT stringToEnum(const std::string& v)
53 {
54  for (const auto& p : enumMap<EnumT>())
55  if (p.first == v)
56  return p.second;
57 
58  throw std::runtime_error("Could not match enum '" + v + "'");
59  return static_cast<EnumT>(0);
60 }
61 
62 /* Convert an enum to a string. */
63 template <typename EnumT>
64 inline std::string enumToString(const EnumT v)
65 {
66  for (const auto& p : enumMap<EnumT>())
67  if (p.second == v)
68  return p.first;
69 
70  throw std::runtime_error("Could not match enum");
71  return "Invalid";
72 }
73 } // namespace core
EnumT stringToEnum(const std::string &v)
Definition: EnumUtils.h:52
std::string enumToString(const EnumT v)
Definition: EnumUtils.h:64
std::vector< std::string > enumNames()
Definition: EnumUtils.h:42
std::vector< std::pair< std::string, GeometryQuality > > enumMap()
Definition: Types.h:405