type-display(#985): implement better simplification scheme

use a regexp to scan for some known obnoxious prefixes
This commit is contained in:
Fischlurch 2016-01-10 00:31:13 +01:00
parent 88120eba1a
commit 08c3d5d4c5
4 changed files with 92 additions and 10 deletions

View file

@ -54,12 +54,16 @@
#include <iomanip>
#include <sstream>
#include <string>
#include <regex>
//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;
}

View file

@ -26,6 +26,9 @@
#include "lib/meta/typelist.hpp"
#include <string>
#include <iostream>
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<SubString>() <<endl;
struct Lunatic
: Test
{
virtual void run (Arg) {}
}
lunatic;
cout << typeStr(lunatic) << endl;
cout << typeStr(&lunatic) << endl;
cout << typeStr((Test &)lunatic) << endl;
cout << typeStr((Test *) &lunatic) << endl;
cout << typeStr(&Lunatic::run) << endl;
}
//-------------------------------------------------TEST-types--
class SubString : public string
{

View file

@ -61,7 +61,7 @@ namespace test{
struct Space { };
auto CHALLENGE_1 = "some::arbitrary::BullShit<oh::RLY>";
auto CHALLENGE_2 = "lib::BullShit<const std::string& (const std::vector<proc::mobject::oh::RLY* const>)>";
auto CHALLENGE_2 = "lib::Contrived<lib::meta::Barely,true>::ClusterFuck<const std::string& (const std::vector<proc::mobject::oh::RLY* const>)>";
auto CHALLENGE_3 = "std::function<special::(anonymous namespace)::Shit(lib::P<asset::Clip, std::shared_ptr<asset::Clip>>)>";
}//(end)fixture

View file

@ -94,18 +94,26 @@ namespace test{
CHECK (2 == sizeof (rmpf2));
CHECK (3 == sizeof (rmpf3));
cout << showSizeof<char>("just a char") << endl;
cout << showSizeof(murpf) << endl;
cout << showSizeof(rmpf1) << endl;
cout << showSizeof(rmpf2) << endl;
cout << showSizeof<Wrmpf3>() << endl;
cout << showSizeof(size_t(24),
string{"everything"}) << endl;
cout << showSizeof<char>("just a char") << endl;
cout << showSizeof(murpf) << endl;
cout << showSizeof(rmpf1) << endl;
cout << showSizeof(rmpf2) << endl;
cout << showSizeof<Wrmpf3>() << 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;
}