MRPT  2.0.4
TEnumType.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <map>
12 #include <stdexcept>
13 #include <string>
14 
15 namespace mrpt
16 {
17 namespace typemeta
18 {
19 namespace internal
20 {
21 template <typename KEY, typename VALUE>
22 struct bimap
23 {
24  std::map<KEY, VALUE> m_k2v;
25  std::map<VALUE, KEY> m_v2k;
26 
27  using const_iterator = typename std::map<KEY, VALUE>::const_iterator;
28  const_iterator begin() const { return m_k2v.begin(); }
29  const_iterator end() const { return m_k2v.end(); }
30  bool direct(const KEY& k, VALUE& out_v) const
31  {
32  auto i = m_k2v.find(k);
33  if (i == m_k2v.end()) return false;
34  out_v = i->second;
35  return true;
36  }
37  bool inverse(const VALUE& v, KEY& out_k) const
38  {
39  auto i = m_v2k.find(v);
40  if (i == m_v2k.end()) return false;
41  out_k = i->second;
42  return true;
43  }
44  void insert(const KEY& k, const VALUE& v)
45  {
46  m_k2v[k] = v;
47  m_v2k[v] = k;
48  }
49 }; // end bimap
50 } // namespace internal
51 
52 /** Only specializations of this class are defined for each enum type of
53  * interest
54  * \sa TEnumType \ingroup mrpt_typemeta_grp
55  */
56 template <typename ENUMTYPE>
58 {
59  static void fill(internal::bimap<ENUMTYPE, std::string>& m_map);
60 };
61 
62 #define MRPT_ENUM_TYPE_BEGIN(_ENUM_TYPE_WITH_NS) \
63  namespace mrpt \
64  { \
65  namespace typemeta \
66  { \
67  template <> \
68  struct TEnumTypeFiller<_ENUM_TYPE_WITH_NS> \
69  { \
70  static void fill( \
71  mrpt::typemeta::internal::bimap<_ENUM_TYPE_WITH_NS, std::string>& \
72  m_map) \
73  {
74 #define MRPT_ENUM_TYPE_BEGIN_NAMESPACE(_NAMESPACE, _ENUM_TYPE_WITH_NS) \
75  MRPT_ENUM_TYPE_BEGIN(_ENUM_TYPE_WITH_NS) \
76  using namespace _NAMESPACE;
77 
78 #define MRPT_ENUM_TYPE_END() \
79  } \
80  } \
81  ; \
82  } \
83  }
84 
85 /** For use in specializations of TEnumTypeFiller */
86 #define MRPT_FILL_ENUM(_X) m_map.insert(_X, #_X)
87 #define MRPT_FILL_ENUM_CUSTOM_NAME(_X, _NAME) m_map.insert(_X, _NAME)
88 #define MRPT_FILL_ENUM_MEMBER(_CLASS, _VALUE) \
89  m_map.insert(_CLASS::_VALUE, #_VALUE)
90 
91 /** A helper class that can convert an enum value into its textual
92  * representation, and viceversa. \ingroup mrpt_typemeta_grp */
93 template <typename ENUMTYPE>
94 struct TEnumType
95 {
96 #define _MRPT_AUXTOSTR(__AA) #__AA
97 
98  /** Gives the numerical name for a given enum text name \exception
99  * std::exception on unknown enum name */
100  static ENUMTYPE name2value(const std::string& name)
101  {
102  ENUMTYPE val;
103  if (!getBimap().inverse(name, val))
104  {
105  throw std::runtime_error(
106  std::string("TEnumType<" _MRPT_AUXTOSTR(
107  TEnumType) ">::name2value(): Unknown name: ") +
108  name);
109  }
110  return val;
111  }
112 
113  /** Gives the textual name for a given enum value \exception std::exception
114  * on unknown enum value name */
115  static std::string value2name(const ENUMTYPE val)
116  {
117  std::string s;
118  if (!getBimap().direct(val, s))
119  {
120  throw std::runtime_error(
121  std::string("TEnumType<" _MRPT_AUXTOSTR(
122  TEnumType) ">::value2name(): Unknown value: ") +
123  std::to_string(static_cast<int>(val)));
124  }
125  return s;
126  }
127 
128  /** Singleton access */
130  {
132  if (data.m_k2v.empty()) TEnumTypeFiller<ENUMTYPE>::fill(data);
133  return data;
134  }
135 #undef _MRPT_AUXTOSTR
136 };
137 
138 } // namespace typemeta
139 } // namespace mrpt
Only specializations of this class are defined for each enum type of interest.
Definition: TEnumType.h:57
#define _MRPT_AUXTOSTR(__AA)
Definition: TEnumType.h:96
std::string to_string(T v)
Just like std::to_string(), but with an overloaded version for std::string arguments.
Definition: format.h:36
static void fill(internal::bimap< ENUMTYPE, std::string > &m_map)
static ENUMTYPE name2value(const std::string &name)
Gives the numerical name for a given enum text name.
Definition: TEnumType.h:100
std::map< VALUE, KEY > m_v2k
Definition: TEnumType.h:25
bool direct(const KEY &k, VALUE &out_v) const
Definition: TEnumType.h:30
static std::string value2name(const ENUMTYPE val)
Gives the textual name for a given enum value.
Definition: TEnumType.h:115
const_iterator end() const
Definition: TEnumType.h:29
const_iterator begin() const
Definition: TEnumType.h:28
typename std::map< KEY, VALUE >::const_iterator const_iterator
Definition: TEnumType.h:27
std::map< KEY, VALUE > m_k2v
Definition: TEnumType.h:24
A helper class that can convert an enum value into its textual representation, and viceversa...
int val
Definition: mrpt_jpeglib.h:957
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
bool inverse(const VALUE &v, KEY &out_k) const
Definition: TEnumType.h:37
void insert(const KEY &k, const VALUE &v)
Definition: TEnumType.h:44
static internal::bimap< ENUMTYPE, std::string > & getBimap()
Singleton access.
Definition: TEnumType.h:129
static struct FontData data
Definition: gltext.cpp:144



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020