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>
{
using Ptr = std::shared_ptr<MyFooClass>;
};
{
struct MyBarClass
{
};
struct MyBarClass2
{
};
}
{
using namespace std;
constexpr auto s1 = TTypeName<int32_t>::get();
cout << s1 << endl;
cout << TTypeName<set<vector<double>>>::get() << endl;
cout << TTypeName<MyFooClass>::get() << endl;
cout << TTypeName<MyFooClass::Ptr>::get() << endl;
cout << TTypeName<MyNS::MyBarClass>::get() << endl;
cout << TTypeName<MyNS::MyBarClass2>::get() << endl;
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;
}
#define DECLARE_CUSTOM_TTYPENAME(_TYPE)
Identical to MRPT_DECLARE_TTYPENAME but intended for user code.
#define DECLARE_TTYPENAME_CLASSNAME(_CLASSNAME)
Like DECLARE_CUSTOM_TTYPENAME(), but for use within the class declaration body.
std::shared_ptr< MyFooClass > Ptr
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;
constexpr string_literal<3>
s =
"foo";
cout <<
"string_literal<3>=" <<
s << endl;
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;
}
GLubyte GLubyte GLubyte a
void Test_StaticString()
[example sstring]
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;
cout << "42 as string=" << n42 << endl;
cout << "9999 as string=" << n9999 << endl;
}
GLsizei const GLfloat * value
void Test_StaticNum2Str()
[example sstring]
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>
{
};
{
};
{
using namespace std;
cout << "White => " << (int)TEnumType<TestColors>::name2value("White")
<< endl;
cout << "Black => " << (int)TEnumType<TestColors>::name2value("Black")
<< endl;
cout << "Gray => " << (int)TEnumType<TestColors>::name2value("Gray")
<< endl;
cout <<
"7 <= " << TEnumType<TestColors>::value2name(
TestColors(7))
<< endl;
}
#define MRPT_ENUM_TYPE_END()
#define MRPT_FILL_ENUM_MEMBER(_CLASS, _VALUE)
#define MRPT_FILL_ENUM(_X)
For use in specializations of TEnumTypeFiller.
#define MRPT_ENUM_TYPE_BEGIN(_ENUM_TYPE_WITH_NS)
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
Output:
White => 15
Black => 0
Gray => 7
7 <= Gray