Main MRPT website > C++ reference for MRPT 1.5.6
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 <mrpt/utils/mrpt_stdint.h> // compiler-independent version of "stdint.h"
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 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
unsigned __int16 uint16_t
Definition: rptypes.h:46
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:47
signed char int8_t
Definition: rptypes.h:42
unsigned char uint8_t
Definition: rptypes.h:43
MRPT_DECLARE_TTYPENAME(mrpt::graphs::detail::TMRSlamNodeAnnotations)
__int16 int16_t
Definition: rptypes.h:45
__int64 int64_t
Definition: rptypes.h:51
GLsizei const GLchar ** string
Definition: glext.h:3919
__int32 int32_t
Definition: rptypes.h:48
unsigned __int64 uint64_t
Definition: rptypes.h:52
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
unsigned __int32 uint32_t
Definition: rptypes.h:49



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019