Main MRPT website > C++ reference for 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 
26  bool direct(const KEY& k, VALUE& out_v) const
27  {
28  auto i = m_k2v.find(k);
29  if (i == m_k2v.end()) return false;
30  out_v = i->second;
31  return true;
32  }
33  bool inverse(const VALUE& v, KEY& out_k) const
34  {
35  auto i = m_v2k.find(v);
36  if (i == m_v2k.end()) return false;
37  out_k = i->second;
38  return true;
39  }
40  void insert(const KEY& k, const VALUE& v)
41  {
42  m_k2v[k] = v;
43  m_v2k[v] = k;
44  }
45 }; // end bimap
46 } // NS internal
47 
48 /** Only specializations of this class are defined for each enum type of
49  * interest
50  * \sa TEnumType \ingroup mrpt_io_grp
51  */
52 template <typename ENUMTYPE>
54 {
55  static void fill(internal::bimap<ENUMTYPE, std::string>& m_map);
56 };
57 
58 #define MRPT_ENUM_TYPE_BEGIN(_ENUM_TYPE_WITH_NS) \
59  namespace mrpt \
60  { \
61  namespace typemeta \
62  { \
63  template <> \
64  struct TEnumTypeFiller<_ENUM_TYPE_WITH_NS> \
65  { \
66  static void fill( \
67  mrpt::typemeta::internal::bimap<_ENUM_TYPE_WITH_NS, std::string>& \
68  m_map) \
69  {
70 #define MRPT_ENUM_TYPE_BEGIN_NAMESPACE(_NAMESPACE, _ENUM_TYPE_WITH_NS) \
71  MRPT_ENUM_TYPE_BEGIN(_ENUM_TYPE_WITH_NS) \
72  using namespace _NAMESPACE;
73 
74 #define MRPT_ENUM_TYPE_END() \
75  } \
76  } \
77  ; \
78  } \
79  }
80 
81 /** For use in specializations of TEnumTypeFiller */
82 #define MRPT_FILL_ENUM(_X) m_map.insert(_X, #_X)
83 #define MRPT_FILL_ENUM_CUSTOM_NAME(_X, _NAME) m_map.insert(_X, _NAME)
84 #define MRPT_FILL_ENUM_MEMBER(_CLASS, _VALUE) \
85  m_map.insert(_CLASS::_VALUE, #_VALUE)
86 
87 /** A helper class that can convert an enum value into its textual
88  * representation, and viceversa. \ingroup mrpt_typename_grp */
89 template <typename ENUMTYPE>
90 struct TEnumType
91 {
92 #define _MRPT_AUXTOSTR(__AA) #__AA
93 
94  /** Gives the numerical name for a given enum text name \exception
95  * std::exception on unknown enum name */
96  static ENUMTYPE name2value(const std::string& name)
97  {
98  ENUMTYPE val;
99  if (!getBimap().inverse(name, val))
100  {
101  throw std::runtime_error(
102  std::string(
103  "TEnumType<" _MRPT_AUXTOSTR(
104  TEnumType) ">::name2value(): Unknown name: ") +
105  name);
106  }
107  return val;
108  }
109 
110  /** Gives the textual name for a given enum value \exception std::exception
111  * on unknown enum value name */
112  static std::string value2name(const ENUMTYPE val)
113  {
114  std::string s;
115  if (!getBimap().direct(val, s))
116  {
117  throw std::runtime_error(
118  std::string(
119  "TEnumType<" _MRPT_AUXTOSTR(
120  TEnumType) ">::value2name(): Unknown value: ") +
121  std::to_string(static_cast<int>(val)));
122  }
123  return s;
124  }
125 
126  /** Singleton access */
128  {
130  if (data.m_k2v.empty()) TEnumTypeFiller<ENUMTYPE>::fill(data);
131  return data;
132  }
133 #undef _MRPT_AUXTOSTR
134 };
135 
136 } // End of namespace
137 } // end of namespace
s
GLdouble s
Definition: glext.h:3676
mrpt::to_string
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:27
mrpt::typemeta::internal::bimap::direct
bool direct(const KEY &k, VALUE &out_v) const
Definition: TEnumType.h:26
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
name
GLuint const GLchar * name
Definition: glext.h:4054
v
const GLdouble * v
Definition: glext.h:3678
mrpt::typemeta::internal::bimap
Definition: TEnumType.h:21
val
int val
Definition: mrpt_jpeglib.h:955
mrpt::typemeta::TEnumTypeFiller::fill
static void fill(internal::bimap< ENUMTYPE, std::string > &m_map)
data
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3547
mrpt::typemeta::TEnumType
A helper class that can convert an enum value into its textual representation, and viceversa.
Definition: config/CConfigFileBase.h:24
mrpt::typemeta::internal::bimap::inverse
bool inverse(const VALUE &v, KEY &out_k) const
Definition: TEnumType.h:33
mrpt::typemeta::TEnumType::getBimap
static internal::bimap< ENUMTYPE, std::string > & getBimap()
Singleton access.
Definition: TEnumType.h:127
mrpt::pbmap::inverse
Eigen::Matrix< dataType, 4, 4 > inverse(Eigen::Matrix< dataType, 4, 4 > &pose)
Definition: Miscellaneous.h:82
mrpt::typemeta::internal::bimap::m_v2k
std::map< VALUE, KEY > m_v2k
Definition: TEnumType.h:24
_MRPT_AUXTOSTR
#define _MRPT_AUXTOSTR(__AA)
Definition: TEnumType.h:92
mrpt::typemeta::TEnumType::value2name
static std::string value2name(const ENUMTYPE val)
Gives the textual name for a given enum value.
Definition: TEnumType.h:112
mrpt::typemeta::internal::bimap::insert
void insert(const KEY &k, const VALUE &v)
Definition: TEnumType.h:40
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::typemeta::TEnumTypeFiller
Only specializations of this class are defined for each enum type of interest.
Definition: TEnumType.h:53
mrpt::typemeta::TEnumType::name2value
static ENUMTYPE name2value(const std::string &name)
Gives the numerical name for a given enum text name.
Definition: TEnumType.h:96
mrpt::typemeta::internal::bimap::m_k2v
std::map< KEY, VALUE > m_k2v
Definition: TEnumType.h:23



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST