Main MRPT website > C++ reference for MRPT 1.9.9
Classes
[mrpt-typemeta]

Detailed Description

Metaprogramming header-only library to obtain constexpr textual string representations of enum types and type names, including smart pointers and complex STL compound types.

Back to list of all libraries | See all modules

Library <tt>mrpt-typemeta</tt>

[New in MRPT 2.0.0]

This library is part of MRPT but has no dependencies, so it can be installed in Debian-based systems with:

    sudo apt install libmrpt-typemeta-dev

Example #1: compile-time type/struct/class names to strings

Use mrpt::typemeta::TTypeName to extract a constexpr string with a compiler-independent representation of arbitrarily-complex types and STL containers. Note that creating objects from a run-time string representation of its type is handled in a different library ([mrpt-serialization]).

See: typemeta_TTypeName/test.cpp

#include <iostream>
#include <memory> // shared_ptr
// Declare custom user types:
struct MyFooClass
{
using Ptr = std::shared_ptr<MyFooClass>;
};
namespace MyNS
{
struct MyBarClass
{
};
struct MyBarClass2
{
};
}
{
using namespace std;
using namespace mrpt::typemeta;
// Evaluation of type names as constexpr strings:
constexpr auto s1 = TTypeName<int32_t>::get();
cout << s1 << endl;
cout << TTypeName<set<vector<double>>>::get() << endl;
// Evaluation of user-defined types:
cout << TTypeName<MyFooClass>::get() << endl;
cout << TTypeName<MyFooClass::Ptr>::get() << endl;
cout << TTypeName<MyNS::MyBarClass>::get() << endl;
cout << TTypeName<MyNS::MyBarClass2>::get() << endl;
// STL typenames as strings:
cout << TTypeName<double>::get() << endl;
cout << TTypeName<vector<double>>::get() << endl;
cout << TTypeName<array<int32_t, 5>>::get() << endl;
cout << TTypeName<set<double>>::get() << endl;
cout << TTypeName<pair<int32_t, pair<int32_t, int32_t>>>::get() << endl;
cout << TTypeName<map<double, set<int32_t>>>::get() << endl;
cout << TTypeName<set<
multimap<double, pair<MyFooClass, MyNS::MyBarClass2>>>>::get()
<< endl;
}

Output:

int32_t
std::set<std::vector<double>>
MyFooClass
std::shared_ptr<MyFooClass>
MyNS::MyBarClass
MyNS::MyBarClass2
double
std::vector<double>
std::array<int32_t,5>
std::set<double>
std::pair<int32_t,std::pair<int32_t,int32_t>>
std::map<double,std::set<int32_t>>
std::set<std::multimap<double,std::pair<MyFooClass,MyNS::MyBarClass2>>>

Example #2: compile-time constexpr strings manipulation

See: typemeta_StaticString/test.cpp

#include <iostream>
{
using namespace std;
using namespace mrpt::typemeta;
constexpr string_literal<3> s = "foo";
cout << "string_literal<3>=" << s << endl;
constexpr auto a = literal("foo");
constexpr auto b = literal("bar");
// In GCC7 this can be "constexpr", but it fails in MSVC 2017 (!)
auto ab = a + b;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "a+b=" << ab << endl;
static_assert(ab.size() == 6, "***");
cout << "(a+b).size()=" << ab.size() << endl;
cout << "(a+b)[0]=" << ab[0] << endl;
cout << "(a+b)[5]=" << ab[5] << endl;
}

Output:

string_literal<3>=foo
a=foo
b=bar
a+b=foobar
(a+b).size()=6
(a+b)[0]=f
(a+b)[5]=r

Example #3: compile-time numbers to strings

See: typemeta_StaticString/test.cpp

#include <iostream>
{
using namespace std;
using namespace mrpt::typemeta;
constexpr auto n42 = num_to_string<42>::value;
constexpr auto n9999 = num_to_string<9999>::value;
cout << "42 as string=" << n42 << endl;
cout << "9999 as string=" << n9999 << endl;
}

Output:

42 as string=42
9999 as string=9999

Example #4: enum values to/from strings

See: typemeta_TEnumType/test.cpp

#include <iostream>
#include <string>
// Example declaration of "enum class"
enum class TestColors
{
Black = 0,
Gray = 7,
White = 15
};
// Example declaration of plain enum
{
};
// Example declaration of "enum class"
{
using namespace std;
using namespace mrpt::typemeta;
cout << "White => " << (int)TEnumType<TestColors>::name2value("White")
<< endl;
cout << "Black => " << (int)TEnumType<TestColors>::name2value("Black")
<< endl;
cout << "Gray => " << (int)TEnumType<TestColors>::name2value("Gray")
<< endl;
<< endl;
}

Output:

White => 15
Black => 0
Gray => 7
7 <= Gray

Classes

struct  mrpt::typemeta::TTypeName< T >
 A template to obtain the type of its argument as a string at compile time. More...
 
Test_TypeName
void Test_TypeName()
Definition: vision_stereo_rectify/test.cpp:35
Test_StaticNum2Str
void Test_StaticNum2Str()
[example sstring]
Definition: vision_stereo_rectify/test.cpp:42
MRPT_ENUM_TYPE_END
#define MRPT_ENUM_TYPE_END()
Definition: TEnumType.h:74
mrpt::typemeta
Definition: config/CConfigFileBase.h:21
s
GLdouble s
Definition: glext.h:3676
num_to_string.h
MyNS
[example-define-class]
Definition: rtti_unittest.cpp:13
MyNS::MyBarClass2
Definition: typename_unittest.cpp:25
South
@ South
Definition: enumtype_unittest.cpp:32
TestColors::White
@ White
TestColors::Black
@ Black
MyNS::MyBarClass
Definition: typename_unittest.cpp:22
TestColors
TestColors
Definition: enumtype_unittest.cpp:14
mrpt::typemeta::TTypeName
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:65
DECLARE_TTYPENAME_CLASSNAME
#define DECLARE_TTYPENAME_CLASSNAME(_CLASSNAME)
Like DECLARE_CUSTOM_TTYPENAME(), but for use within the class declaration body.
Definition: TTypeName.h:100
MRPT_ENUM_TYPE_BEGIN
#define MRPT_ENUM_TYPE_BEGIN(_ENUM_TYPE_WITH_NS)
Definition: TEnumType.h:58
Directions
Directions
Definition: enumtype_unittest.cpp:28
mrpt::typemeta::num_to_string
constexpr string representation of a number.
Definition: num_to_string.h:44
TestColors::Gray
@ Gray
East
@ East
Definition: enumtype_unittest.cpp:31
TTypeName.h
mrpt::typemeta::string_literal
Definition: static_string.h:23
b
GLubyte GLubyte b
Definition: glext.h:6279
TEnumType.h
MyFooClass::Ptr
std::shared_ptr< MyFooClass > Ptr
Definition: typename_unittest.cpp:18
mrpt::typemeta::TEnumType
A helper class that can convert an enum value into its textual representation, and viceversa.
Definition: config/CConfigFileBase.h:24
MRPT_FILL_ENUM
#define MRPT_FILL_ENUM(_X)
For use in specializations of TEnumTypeFiller.
Definition: TEnumType.h:82
Test_StaticString
void Test_StaticString()
[example sstring]
Definition: vision_stereo_rectify/test.cpp:15
North
@ North
Definition: enumtype_unittest.cpp:30
DECLARE_CUSTOM_TTYPENAME
#define DECLARE_CUSTOM_TTYPENAME(_TYPE)
Identical to MRPT_DECLARE_TTYPENAME but intended for user code.
Definition: TTypeName.h:86
MRPT_FILL_ENUM_MEMBER
#define MRPT_FILL_ENUM_MEMBER(_CLASS, _VALUE)
Definition: TEnumType.h:84
void
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
West
@ West
Definition: enumtype_unittest.cpp:33
static_string.h
MyFooClass
[example typename]
Definition: typename_unittest.cpp:16
mrpt::typemeta::literal
constexpr auto literal(const char(&lit)[N_PLUS_1]) -> string_literal< N_PLUS_1 - 1 >
Definition: static_string.h:43
Test_EnumType
void Test_EnumType()
Definition: vision_stereo_rectify/test.cpp:46
a
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
TTypeName_stl.h



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST