diff --git a/src/lib/format-obj.cpp b/src/lib/format-obj.cpp index 1b4068244..84651a4ea 100644 --- a/src/lib/format-obj.cpp +++ b/src/lib/format-obj.cpp @@ -54,12 +54,16 @@ #include #include #include +#include //using util::_Fmt; using util::removePrefix; using util::removeSuffix; using std::string; +using std::regex; +using std::regex_replace; + namespace { // hard-wired configuration for debugging output.... @@ -152,12 +156,58 @@ apologies for that." + + /** \par implementation notes + * - we do not strip type adornments like `const`, `&` or `*`, + * however, the typical usage from within util::typeStr() + * is arranged in a way to absorb these adornments by the way + * the template signatures are defined + * - we _do_ simplify the type display and strip some obnoxious + * namespace prefixes with the help of `std::regex_replace` + * - we perform those simplifying rewrites _in place_ thus _overwriting_ + * the result string. This exploits the fact that the replacements are + * _always shorter_ than what is being replaced (**beware**). + * - standard regular expressions can be [assumed][stdRegEx-threadsafe] + * to be threadsafe. Thus, we're able to build an embedded shared + * static variable _on demand_ and use the performance optimisation + * offered by the standard library + * - performance wise we'll assume the transformation happens within + * the cache, so it doesn't make much of a difference if we scan + * the same comparatively short string multiple times + * + * [stdRegEx-threadsafe]: http://stackoverflow.com/questions/15752910/is-stdregex-thread-safe + */ string humanReadableTypeID (Literal rawType) { + static regex commonPrefixes {"std::" + "|lib::meta::" + "|lib::time::" + "|lib::test::" + "|lib::diff::" + "|lib::" + "|util::" + "|proc::" + "|proc::asset::" + "|proc::mobject::" + "|proc::mobject::session::" + "|proc::play::" + "|gui::model" + "|gui::ctrl" + , regex::ECMAScript | regex::optimize}; + + static regex stdAllocator {"(\\w+<(\\w+)), allocator<\\2>\\s*" + , regex::ECMAScript | regex::optimize}; + + string typeName = demangleCxx (rawType); - removePrefix (typeName, "const "); - removeSuffix (typeName, " const*"); + auto pos = typeName.begin(); + auto end = typeName.end(); + + end = regex_replace(pos, pos, end, commonPrefixes, ""); + end = regex_replace(pos, pos, end, stdAllocator, "$1"); + + typeName.resize(end - typeName.begin()); return typeName; } diff --git a/tests/library/meta/meta-utils-test.cpp b/tests/library/meta/meta-utils-test.cpp index 8cadb5699..88aa359e1 100644 --- a/tests/library/meta/meta-utils-test.cpp +++ b/tests/library/meta/meta-utils-test.cpp @@ -26,6 +26,9 @@ #include "lib/meta/typelist.hpp" #include +#include +using std::cout; +using std::endl;/////////////////////////TODO namespace lib { @@ -51,6 +54,7 @@ namespace test { run (Arg) { verify_basicAssumptions(); + verify_genericTypeDisplay(); detect_stringConversion(); detect_typeList(); @@ -78,6 +82,26 @@ namespace test { + void + verify_genericTypeDisplay() + { + cout << typeStr() <("just a char") << endl; - cout << showSizeof(murpf) << endl; - cout << showSizeof(rmpf1) << endl; - cout << showSizeof(rmpf2) << endl; - cout << showSizeof() << endl; - cout << showSizeof(size_t(24), - string{"everything"}) << endl; + cout << showSizeof("just a char") << endl; + cout << showSizeof(murpf) << endl; + cout << showSizeof(rmpf1) << endl; + cout << showSizeof(rmpf2) << endl; + cout << showSizeof() << endl; + cout << showSizeof(size_t(42), + string{"Universe"}) << endl; Wrmpf1 *p1 = &rmpf1; Wrmpf1 *p2 = 0; cout << showSizeof(p1) << endl; cout << showSizeof(p2) << endl; + + Wrmpf1 & r = rmpf1; + Wrmpf1 const& cr = r; + Wrmpf1 const* cp = &r; + + cout << showSizeof(r) << endl; + cout << showSizeof(cr) << endl; + cout << showSizeof(cp) << endl; }