change formatting helper to exploit RTTI (!)
our formatting helper for diagnostics output, which is primarily used in the unit-tests, first tries to invoke a custom string conversion. If that is not possible, it falls back to printing the demangled type name of the object in question. With just a minor change we're able to evaluate RTTI here and print the actual type name, instead of the static supertype the compiler sees on invocation. We just rely on the typeid(obj) built-in function. The only catch is we have to strip the " const*" suffix (and no, it is not possible to do that on metaprogramming level, due to the special situation where we have a void*) This also prompted me to write some util functions for this often encountered task to check / remove a prefix or suffix Hopefully I've got those functions correct and safe....
This commit is contained in:
parent
2e4d74747e
commit
fc5b59a848
2 changed files with 39 additions and 2 deletions
|
|
@ -65,6 +65,8 @@ namespace util {
|
|||
using lib::meta::can_lexical2string;
|
||||
using lib::meta::can_IterForEach;
|
||||
using lib::Symbol;
|
||||
using util::removePrefix;
|
||||
using util::removeSuffix;
|
||||
using util::isnil;
|
||||
using std::string;
|
||||
using std::move;
|
||||
|
|
@ -153,9 +155,14 @@ namespace util {
|
|||
/** @return a string denoting the type. */
|
||||
template<typename TY>
|
||||
inline string
|
||||
tyStr (const TY* =0)
|
||||
tyStr (const TY* obj=0)
|
||||
{
|
||||
return "«"+ lib::test::demangleCxx (typeid(TY).name())+"»";
|
||||
auto mangledType = obj? typeid(obj).name()
|
||||
: typeid(TY).name();
|
||||
string typeName = lib::test::demangleCxx (mangledType);
|
||||
removePrefix (typeName, "const ");
|
||||
removeSuffix (typeName, " const*");
|
||||
return "«"+ typeName +"»";
|
||||
}
|
||||
|
||||
template<typename TY>
|
||||
|
|
|
|||
|
|
@ -155,6 +155,36 @@ namespace util {
|
|||
return 0 == str.rfind(prefix, 0);
|
||||
}
|
||||
|
||||
/** check if string ends with the given suffix */
|
||||
inline bool
|
||||
endsWith (string const& str, string const& suffix)
|
||||
{
|
||||
size_t l = suffix.length();
|
||||
if (l > str.length()) return false;
|
||||
size_t pos = str.length() - l;
|
||||
return pos == str.find(suffix, pos);
|
||||
}
|
||||
|
||||
inline bool
|
||||
endsWith (string const& str, const char* suffix)
|
||||
{
|
||||
return endsWith (str, string(suffix));
|
||||
}
|
||||
|
||||
inline void
|
||||
removePrefix (string& str, string const& prefix)
|
||||
{
|
||||
if (not startsWith (str,prefix)) return;
|
||||
str = str.substr (prefix.length());
|
||||
}
|
||||
|
||||
inline void
|
||||
removeSuffix (string& str, string const& suffix)
|
||||
{
|
||||
if (not endsWith (str,suffix)) return;
|
||||
str.resize(str.length() - suffix.length());
|
||||
}
|
||||
|
||||
|
||||
/** shortcut for containment test on a map */
|
||||
template <typename MAP>
|
||||
|
|
|
|||
Loading…
Reference in a new issue