CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
EnumCast.h
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2010 by Federico Marulli *
3  * federico.marulli3@unibo.it *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public *
16  * License along with this program; if not, write to the Free *
17  * Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ********************************************************************/
20 
35 #ifndef __ENUMCAST__
36 #define __ENUMCAST__
37 
38 namespace cbl {
39 
46  template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
47  T castFromValue (const int i) { return static_cast<T>(i); }
48 
58  template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
59  T castFromName (const std::string name, const std::vector<std::string> list) {
60  std::vector<std::string>::const_iterator it = std::find(list.begin(), list.end(), name);
61 
62  if (it!=list.end())
63  return castFromValue<T>(int(it-list.begin()));
64 
65  else {
66  std::cerr << par::col_red << std::endl <<"*** Error in castFromName of EnumCast.h! The name does not correspond to any element in list ***" << std::endl << par::col_default << std::endl;
67  exit(1);
68  }
69 
70  }
71 
78  template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
79  std::vector<T> castFromValues (const std::vector<int> ii)
80  {
81  std::vector<T> en(ii.size());
82  for (size_t i=0; i<ii.size(); i++)
83  en[i] = castFromValue<T>(ii[i]);
84  return en;
85  }
86 
96  template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
97  std::vector<T> castFromNames (const std::vector<std::string> names, const std::vector<std::string> list)
98  {
99  std::vector<T> en(names.size());
100 
101  for (size_t i=0; i<names.size(); i++)
102  en[i] = castFromName<T>(names[i], list);
103 
104  return en;
105  }
106 
107 }
108 
109 #endif
static const std::string col_red
red colour (used when printing something on the screen)
Definition: Constants.h:300
static const std::string col_default
default colour (used when printing something on the screen)
Definition: Constants.h:297
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
std::vector< T > castFromNames(const std::vector< std::string > names, const std::vector< std::string > list)
cast an object of type enum class from names
Definition: EnumCast.h:97
T castFromName(const std::string name, const std::vector< std::string > list)
cast an object of type enum class from its name
Definition: EnumCast.h:59
T castFromValue(const int i)
cast an object of type enum class from its index
Definition: EnumCast.h:47
std::vector< T > castFromValues(const std::vector< int > ii)
cast objects of type enum class from indeces
Definition: EnumCast.h:79