MRPT  2.0.2
TTypeName.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 
12 #include <cstdint>
13 
14 #ifdef __APPLE__
15 #include <memory>
16 #else
17 // frwd decl for TTypeName specialization:
18 namespace std
19 {
20 template <class T>
21 class shared_ptr;
22 }
23 #endif
24 
25 namespace mrpt
26 {
27 namespace typemeta
28 {
29 /** @name Conversion of type to string at compile time
30  * IMPORTANT: See also <mrpt/typemeta/TTypeName_stl.h> for definitions for STL
31  * containers, and <mrpt/serialization/stl_serialization.h> for serialization.
32 @{ */
33 
34 /** A template to obtain the type of its argument as a string at compile time.
35  * It works with all classes derived from CObject, plus many
36  * specializations for the plain data types (bool, double, uint8_t, etc...)
37  * For example:
38  * \code
39  * cout << TTypeName<double>::get() << endl; // "double"
40  * cout << TTypeName<CPose2D>::get() << endl; // "CPose2D"
41  * \endcode
42  *
43  * Users can extend this for custom structs/classes with the macro
44  * DECLARE_CUSTOM_TTYPENAME:
45  * \code
46  * class MyClass { ... };
47  * DECLARE_CUSTOM_TTYPENAME(MyClass)
48  * cout << TTypeName<MyClass>::get() << endl; // "MyClass"
49  * \endcode
50  * or alternatively, to avoid adding out-of-class macros:
51  * \code
52  * namespace MyNS {
53  * class MyClass {
54  * DECLARE_TTYPENAME_CLASSNAME(MyNS::MyClass)
55  * };
56  * }
57  * cout << TTypeName<MyNS::MyClass>::get() << endl; // "MyNS::MyClass"
58  * \endcode
59  * The following types are NOT ALLOWED since they have platform-dependant
60  * sizes:
61  * - int, unsigned int
62  * - long, unsigned long
63  * - short, unsigned short
64  * - size_t
65  *
66  * \ingroup mrpt_typemeta_grp
67  */
68 template <typename T>
69 struct TTypeName
70 {
71  constexpr static auto get() { return T::getClassName(); }
72 };
73 
74 /** Specialization for shared_ptr<T> */
75 template <typename T>
76 struct TTypeName<std::shared_ptr<T>>
77 {
78  constexpr static auto get()
79  {
80  return literal("std::shared_ptr<") + TTypeName<T>::get() + literal(">");
81  }
82 };
83 
84 /** Identical to MRPT_DECLARE_TTYPENAME but intended for user code.
85  * MUST be placed at the GLOBAL namespace. Can be used for types declared
86  * at the global or within some namespace. Just write the full namespace path
87  * as `_TYPE` argument here.
88  * \sa TTypeName, DECLARE_TTYPENAME_CLASSNAME
89  */
90 #define DECLARE_CUSTOM_TTYPENAME(_TYPE) \
91  namespace mrpt \
92  { \
93  namespace typemeta \
94  { \
95  MRPT_DECLARE_TTYPENAME(_TYPE) \
96  } \
97  }
98 
99 /** Like DECLARE_CUSTOM_TTYPENAME(), but for use within the class declaration
100  * body. It has the advantage of not requiring macros/definitions out of the
101  * original class namespace.
102  * \sa TTypeName
103  */
104 #define DECLARE_TTYPENAME_CLASSNAME(_CLASSNAME) \
105  public: \
106  static constexpr auto getClassName() \
107  { \
108  return mrpt::typemeta::literal(#_CLASSNAME); \
109  }
110 
111 #define MRPT_DECLARE_TTYPENAME(_TYPE) \
112  template <> \
113  struct TTypeName<_TYPE> \
114  { \
115  constexpr static auto get() { return literal(#_TYPE); } \
116  };
117 /** Declares a typename to be "namespace::type"
118  * \sa MRPT_DECLARE_TTYPENAME_NO_NAMESPACE */
119 #define MRPT_DECLARE_TTYPENAME_NAMESPACE(_TYPE, __NS) \
120  template <> \
121  struct TTypeName<__NS::_TYPE> \
122  { \
123  constexpr static auto get() { return literal(#__NS "::" #_TYPE); } \
124  };
125 
126 /** Declares a typename to be "type" (without the NS prefix)
127  * \sa MRPT_DECLARE_TTYPENAME_NAMESPACE */
128 #define MRPT_DECLARE_TTYPENAME_NO_NAMESPACE(_TYPE, __NS) \
129  template <> \
130  struct TTypeName<__NS::_TYPE> \
131  { \
132  constexpr static auto get() { return literal(#_TYPE); } \
133  };
134 
135 #define MRPT_DECLARE_TTYPENAME_PTR(_TYPE) \
136  template <> \
137  struct TTypeName<_TYPE::Ptr> \
138  { \
139  static auto get() { return TTypeName<_TYPE>::get(); } \
140  };
141 
142 #define MRPT_DECLARE_TTYPENAME_PTR_NAMESPACE(_TYPE, __NS) \
143  template <> \
144  struct TTypeName<__NS::_TYPE::Ptr> \
145  { \
146  static auto get() { return TTypeName<__NS::_TYPE>::get(); } \
147  };
148 
152 MRPT_DECLARE_TTYPENAME(uint64_t)
153 MRPT_DECLARE_TTYPENAME(int64_t)
154 MRPT_DECLARE_TTYPENAME(uint32_t)
155 MRPT_DECLARE_TTYPENAME(int32_t)
156 MRPT_DECLARE_TTYPENAME(uint16_t)
157 MRPT_DECLARE_TTYPENAME(int16_t)
158 MRPT_DECLARE_TTYPENAME(uint8_t)
160 
161 /** @} */
162 
163 } // namespace typemeta
164 } // namespace mrpt
STL namespace.
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:69
constexpr auto literal(const char(&lit)[N_PLUS_1]) -> string_literal< N_PLUS_1 - 1 >
Definition: static_string.h:46
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
static constexpr auto get()
Definition: TTypeName.h:71
#define MRPT_DECLARE_TTYPENAME(_TYPE)
Definition: TTypeName.h:111



Page generated by Doxygen 1.8.14 for MRPT 2.0.2 Git: 9b4fd2465 Mon May 4 16:59:08 2020 +0200 at lun may 4 17:26:07 CEST 2020