casadi_enum.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_ENUM_HPP
27 #define CASADI_ENUM_HPP
28 
29 #include <string>
30 #include <sstream>
31 
32 #include "exception.hpp"
33 
34 #include <casadi/core/casadi_export.h>
35 
37 namespace casadi {
38 
40 template<typename T>
41 struct enum_traits {
42  static const size_t n_enum = static_cast<size_t>(T::NUMEL);
43 };
44 
46 template<typename T>
47 bool has_enum(const std::string& s) {
48  // Look for a match
49  for (size_t i = 0; i < enum_traits<T>::n_enum; ++i) {
50  if (s == to_string(static_cast<T>(i))) return true;
51  }
52  // Not found
53  return false;
54 }
55 
57 template<typename T>
58 T to_enum(const std::string& s, const std::string& s_def = "") {
59  // Default value, if empty string
60  if (s.empty() && !s_def.empty()) return to_enum<T>(s_def);
61  // Linear search over permitted values
62  for (size_t i = 0; i < enum_traits<T>::n_enum; ++i) {
63  if (s == to_string(static_cast<T>(i))) return static_cast<T>(i);
64  }
65  // Informative error message
66  std::stringstream ss;
67  ss << "No such enum: '" << s << "'. Permitted values: ";
68  for (size_t i = 0; i < enum_traits<T>::n_enum; ++i) {
69  // Separate strings
70  if (i > 0) ss << ", ";
71  // Print enum name
72  ss << "'" << to_string(static_cast<T>(i)) << "'";
73  }
74  casadi_error(ss.str());
75  return static_cast<T>(enum_traits<T>::n_enum); // never reached
76 }
77 
79 template<typename T>
80 std::vector<std::string> enum_names() {
81  std::vector<std::string> r(enum_traits<T>::n_enum);
82  for (size_t i = 0; i < enum_traits<T>::n_enum; ++i)
83  r[i] = to_string(static_cast<T>(i));
84  return r;
85 }
86 
87 } // namespace casadi
89 
90 #endif // CASADI_ENUM_HPP
The casadi namespace.