MRPT  1.9.9
TEnumType.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <map>
12 #include <stdexcept>
13 
14 namespace mrpt
15 {
16 namespace typemeta
17 {
18 namespace internal
19 {
20 template <typename KEY, typename VALUE>
21 struct bimap
22 {
23  std::map<KEY, VALUE> m_k2v;
24  std::map<VALUE, KEY> m_v2k;
25 
27  const_iterator begin() const { return m_k2v.begin(); }
28  const_iterator end() const { return m_k2v.end(); }
29 
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_io_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_typename_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
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:24
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:28
const_iterator begin() const
Definition: TEnumType.h:27
GLdouble s
Definition: glext.h:3676
typename std::map< KEY, VALUE >::const_iterator const_iterator
Definition: TEnumType.h:26
std::map< KEY, VALUE > m_k2v
Definition: TEnumType.h:23
A helper class that can convert an enum value into its textual representation, and viceversa...
Eigen::Matrix< dataType, 4, 4 > inverse(Eigen::Matrix< dataType, 4, 4 > &pose)
Definition: Miscellaneous.h:80
int val
Definition: mrpt_jpeglib.h:955
GLsizei const GLchar ** string
Definition: glext.h:4101
const GLdouble * v
Definition: glext.h:3678
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLuint const GLchar * name
Definition: glext.h:4054
std::string std::string to_string(T v)
Just like std::to_string(), but with an overloaded version for std::string arguments.
Definition: format.h:30
bool inverse(const VALUE &v, KEY &out_k) const
Definition: TEnumType.h:37
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3546
const Scalar * const_iterator
Definition: eigen_plugins.h:27
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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020