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



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