Main MRPT website > C++ reference for MRPT 1.4.0
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-2016, 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 <mrpt/utils/mrpt_stdint.h> // compiler-independent version of "stdint.h"
13#include <string>
14
15namespace 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 in <mrpt/utils/stl_serialization.h>
21 @{ */
22
23 /** A template to obtain the type of its argument as a string at compile time.
24 * It works with all classes derived from CSerializable, plus many specializations for the plain data types (bool, double, uint8_t, etc...)
25 * For example:
26 * \code
27 * cout << TTypeName<double>::get() << endl; // "double"
28 * cout << TTypeName<CPose2D>::get() << endl; // "CPose2D"
29 * cout << TTypeName<mrpt::maps::COccupancyGridMap2D>::get() << endl; // "COccupancyGridMap2D"
30 * \endcode
31 *
32 * Users can extend this for custom structs/classes with the macro DECLARE_CUSTOM_TTYPENAME:
33 * \code
34 * class MyClass { ... };
35 * DECLARE_CUSTOM_TTYPENAME(MyClass)
36 * cout << TTypeName<MyClass>::get() << endl; // "MyClass"
37 * \endcode
38 *
39 * The following types are NOT ALLOWED since they have platform-dependant sizes:
40 * - int, unsigned int
41 * - long, unsigned long
42 * - short, unsigned short
43 * - size_t
44 *
45 */
46 template<typename T>
47 struct TTypeName
48 {
49 static std::string get() {
50 return std::string( T::classinfo->className );
51 }
52 };
53
54 /** Identical to MRPT_DECLARE_TTYPENAME but intended for user code.
55 * MUST be placed at the GLOBAL namespace.
56 */
57 #define DECLARE_CUSTOM_TTYPENAME(_TYPE) \
58 namespace mrpt { namespace utils { MRPT_DECLARE_TTYPENAME(_TYPE) } }
59
60 #define MRPT_DECLARE_TTYPENAME(_TYPE) \
61 template<> struct TTypeName <_TYPE > { \
62 static std::string get() { return std::string(#_TYPE); } };
63
64 #define MRPT_DECLARE_TTYPENAME_NAMESPACE(_TYPE,__NS) \
65 template<> struct TTypeName <__NS :: _TYPE > { \
66 static std::string get() { return std::string(#_TYPE); } };
67
68 #define MRPT_DECLARE_TTYPENAME_PTR(_TYPE) \
69 template<> struct TTypeName <_TYPE##Ptr> { \
70 static std::string get() { return TTypeName<_TYPE>::get(); } };
71
72 #define MRPT_DECLARE_TTYPENAME_PTR_NAMESPACE(_TYPE,__NS) \
73 template<> struct TTypeName <__NS :: _TYPE##Ptr> { \
74 static std::string get() { return TTypeName<__NS :: _TYPE>::get(); } };
75
87
88 /** @} */
89
90 } // End of namespace
91} // End of namespace
92
93#endif
#define MRPT_DECLARE_TTYPENAME(_TYPE)
Definition TTypeName.h:60
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
unsigned long uint32_t
Definition pstdint.h:216
unsigned int uint16_t
Definition pstdint.h:170
signed long int32_t
Definition pstdint.h:247
signed int int16_t
Definition pstdint.h:194
unsigned char uint8_t
Definition pstdint.h:143
signed char int8_t
Definition pstdint.h:158
A template to obtain the type of its argument as a string at compile time.
Definition TTypeName.h:48
static std::string get()
Definition TTypeName.h:49



Page generated by Doxygen 1.9.7 for MRPT 1.4.0 SVN: at Tue Jun 13 14:27:49 UTC 2023