Simbody  3.7
SimTK::NiceTypeName< T > Struct Template Reference

Obtain human-readable and XML-usable names for arbitrarily-complicated C++ types. More...

Static Public Member Functions

static const char * name ()
 The default implementation of name() here returns the raw result from `typeid(T). More...
 
static const std::string & namestr ()
 The default implementation of namestr() attempts to return a nicely demangled and canonicalized type name on all platforms, using the SimTK::demangle() and SimTK::canonicalizeTypeName() methods. More...
 
static const std::string & xmlstr ()
 The default implementation of xmlstr() takes the output of namestr() and invokes SimTK::encodeTypeNameForXML() on it. More...
 

Related Functions

(Note that these are not member functions.)

std::string demangle (const char *name)
 Attempt to demangle a type name as returned by typeid.name(), with the result hopefully suitable for meaningful display to a human. More...
 
std::string canonicalizeTypeName (std::string &&demangledTypeName)
 Given a compiler-dependent demangled type name string as returned by SimTK::demangle(), attempt to form a canonicalized representation that will be the same for any compiler. More...
 
std::string canonicalizeTypeName (const std::string &demangledTypeName)
 Same, but takes an lvalue reference so has to copy the input. More...
 
std::string encodeTypeNameForXML (std::string &&canonicalizedTypeName)
 Given a canonicalized type name, produce a modified version that is better-suited to use as an XML attribute. More...
 
std::string encodeTypeNameForXML (const std::string &niceTypeName)
 Same, but takes an lvalue reference so has to copy the input. More...
 
std::string decodeXMLTypeName (std::string &&xmlTypeName)
 Given a type name that was encoded for XML by SimTK::encodeTypeNameForXML(), restore it to its canonicalized form. More...
 
std::string decodeXMLTypeName (const std::string &xmlTypeName)
 Same, but takes an lvalue reference so has to copy the input. More...
 

Detailed Description

template<class T>
struct SimTK::NiceTypeName< T >

Obtain human-readable and XML-usable names for arbitrarily-complicated C++ types.

Three methods name(), namestr(), and xmlstr() are provided giving respectively the compiler-dependent output from typeid(T).name(), a canonicalized human-readable string, and the canonicalized string with XML-forbidden angle brackets replaced by curly braces. The default implementation is usable for most types, but if you don't like the result you can specialize to provide nicer names. For example, you may prefer SimTK::Vec3 to SimTK::Vec<3,double,1>.

Warning
Don't expect usable names for types that are defined in an anonymous namespace or for function-local types. Names will still be produced but they won't be unique and won't necessarily be compiler-independent.

The output of namestr() is typically used for error messages and testing; xmlstr() is used for type tags in XML for use in deserializing.

Member Function Documentation

◆ name()

template<class T >
static const char* SimTK::NiceTypeName< T >::name ( )
inlinestatic

The default implementation of name() here returns the raw result from `typeid(T).

name()` which will be fast but may be a mangled name in some compilers (gcc and clang included).

◆ namestr()

template<class T >
static const std::string& SimTK::NiceTypeName< T >::namestr ( )
inlinestatic

The default implementation of namestr() attempts to return a nicely demangled and canonicalized type name on all platforms, using the SimTK::demangle() and SimTK::canonicalizeTypeName() methods.

This is an expensive operation but is only done once.

◆ xmlstr()

template<class T >
static const std::string& SimTK::NiceTypeName< T >::xmlstr ( )
inlinestatic

The default implementation of xmlstr() takes the output of namestr() and invokes SimTK::encodeTypeNameForXML() on it.

Friends And Related Function Documentation

◆ demangle()

template<class T >
std::string demangle ( const char *  name)
related

Attempt to demangle a type name as returned by typeid.name(), with the result hopefully suitable for meaningful display to a human.

Behavior is compiler-dependent.

◆ canonicalizeTypeName() [1/2]

template<class T >
std::string canonicalizeTypeName ( std::string &&  demangledTypeName)
related

Given a compiler-dependent demangled type name string as returned by SimTK::demangle(), attempt to form a canonicalized representation that will be the same for any compiler.

Unnecessary spaces and superfluous keywords like "class" and "struct" are removed. The namestr() method of NiceTypeName<T> uses this function to produce a human-friendly type name that is the same on any platform. The input argument is left empty.

◆ canonicalizeTypeName() [2/2]

template<class T >
std::string canonicalizeTypeName ( const std::string &  demangledTypeName)
related

Same, but takes an lvalue reference so has to copy the input.

◆ encodeTypeNameForXML() [1/2]

template<class T >
std::string encodeTypeNameForXML ( std::string &&  canonicalizedTypeName)
related

Given a canonicalized type name, produce a modified version that is better-suited to use as an XML attribute.

This means replacing the angle brackets with curly braces to avoid trouble. The input argument is left empty.

◆ encodeTypeNameForXML() [2/2]

template<class T >
std::string encodeTypeNameForXML ( const std::string &  niceTypeName)
related

Same, but takes an lvalue reference so has to copy the input.

◆ decodeXMLTypeName() [1/2]

template<class T >
std::string decodeXMLTypeName ( std::string &&  xmlTypeName)
related

Given a type name that was encoded for XML by SimTK::encodeTypeNameForXML(), restore it to its canonicalized form.

This means replacing curly braces by angle brackets. The input argument is left empty.

◆ decodeXMLTypeName() [2/2]

template<class T >
std::string decodeXMLTypeName ( const std::string &  xmlTypeName)
related

Same, but takes an lvalue reference so has to copy the input.


The documentation for this struct was generated from the following file: