Main MRPT website > C++ reference for 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-2017, 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 #ifndef TTYPENAME_H
10 #define TTYPENAME_H
11 
12 #include <cstdint>
13 #include <string>
14 
15 namespace mrpt
16 {
17 namespace utils
18 {
19 /** @name Conversion of type to string at compile time
20  * IMPORTANT: See also the implementation of Serialization for STL containers
21 in <mrpt/utils/stl_serialization.h>
22 @{ */
23 
24 /** A template to obtain the type of its argument as a string at compile time.
25  * It works with all classes derived from CSerializable, plus many
26  * specializations for the plain data types (bool, double, uint8_t, etc...)
27  * For example:
28  * \code
29  * cout << TTypeName<double>::get() << endl; //
30  * "double"
31  * cout << TTypeName<CPose2D>::get() << endl; //
32  * "CPose2D"
33  * cout << TTypeName<mrpt::maps::COccupancyGridMap2D>::get() << endl; //
34  * "COccupancyGridMap2D"
35  * \endcode
36  *
37  * Users can extend this for custom structs/classes with the macro
38  * DECLARE_CUSTOM_TTYPENAME:
39  * \code
40  * class MyClass { ... };
41  * DECLARE_CUSTOM_TTYPENAME(MyClass)
42  * cout << TTypeName<MyClass>::get() << endl; //
43  * "MyClass"
44  * \endcode
45  *
46  * The following types are NOT ALLOWED since they have platform-dependant
47  * sizes:
48  * - int, unsigned int
49  * - long, unsigned long
50  * - short, unsigned short
51  * - size_t
52  *
53  */
54 template <typename T>
55 struct TTypeName
56 {
57  static std::string get() { return std::string(T::className); }
58 };
59 
60 /** Identical to MRPT_DECLARE_TTYPENAME but intended for user code.
61  * MUST be placed at the GLOBAL namespace.
62  */
63 #define DECLARE_CUSTOM_TTYPENAME(_TYPE) \
64  namespace mrpt \
65  { \
66  namespace utils \
67  { \
68  MRPT_DECLARE_TTYPENAME(_TYPE) \
69  } \
70  }
71 
72 #define MRPT_DECLARE_TTYPENAME(_TYPE) \
73  template <> \
74  struct TTypeName<_TYPE> \
75  { \
76  static std::string get() { return std::string(#_TYPE); } \
77  };
78 
79 #define MRPT_DECLARE_TTYPENAME_NAMESPACE(_TYPE, __NS) \
80  template <> \
81  struct TTypeName<__NS::_TYPE> \
82  { \
83  static std::string get() { return std::string(#_TYPE); } \
84  };
85 
86 #define MRPT_DECLARE_TTYPENAME_PTR(_TYPE) \
87  template <> \
88  struct TTypeName<_TYPE::Ptr> \
89  { \
90  static std::string get() { return TTypeName<_TYPE>::get(); } \
91  };
92 
93 #define MRPT_DECLARE_TTYPENAME_PTR_NAMESPACE(_TYPE, __NS) \
94  template <> \
95  struct TTypeName<__NS::_TYPE::Ptr> \
96  { \
97  static std::string get() { return TTypeName<__NS::_TYPE>::get(); } \
98  };
99 
111 
112 /** @} */
113 
114 } // End of namespace
115 } // End of namespace
116 
117 #endif
unsigned __int16 uint16_t
Definition: rptypes.h:44
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:55
signed char int8_t
Definition: rptypes.h:40
unsigned char uint8_t
Definition: rptypes.h:41
__int16 int16_t
Definition: rptypes.h:43
__int64 int64_t
Definition: rptypes.h:49
GLsizei const GLchar ** string
Definition: glext.h:4101
__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
#define MRPT_DECLARE_TTYPENAME(_TYPE)
Definition: TTypeName.h:72



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019