From 7ed07ce3c5ce2bed4d203b8ad36820da074c0e60 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Mon, 4 Jan 2016 04:01:34 +0100 Subject: [PATCH] solution for a minimal toString-invocator when leaving out the lexical-cast part and when inlining the boost::enable_if solution, we can get basically at zero inclusion overhead --- research/try.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/research/try.cpp b/research/try.cpp index 1ff44da5f..b1e650772 100644 --- a/research/try.cpp +++ b/research/try.cpp @@ -44,13 +44,17 @@ typedef unsigned int uint; #include "lib/diff/gen-node.hpp" #include "lib/util.hpp" +#include "lib/meta/util.hpp" + #include //#include -#include #include +#include +#include using lib::diff::GenNode; using lib::P; +using lib::meta::can_convertToString; //using util::unConst; using std::string; @@ -69,6 +73,68 @@ newP (ARGS&&... ctorArgs) return P{new X {std::forward(ctorArgs)...}}; } +/////////////////////////////////////////planned for meta/util.hpp + template + struct enable_if_c { + typedef T type; + }; + + template + struct enable_if_c {}; + + template + using enable_if = typename enable_if_c::type; + + template + using disable_if = typename enable_if_c::type; + +/////////////////////////////////////////planned for meta/util.hpp + + +///////////////////////////////copied from format-util.hpp + template + inline string + typeStr (const TY* obj=0) + { + auto mangledType = obj? typeid(obj).name() + : typeid(TY).name(); + return string("«")+ mangledType +"»"; + } + + template + inline string + typeStr (TY const& ref) + { return typeStr(&ref); } + + + template + struct CustomStringConv + { + static string invoke (X const& x) { return typeStr(x); } + }; + + template + struct CustomStringConv> > + { + static string + invoke (X const& val) + try { return string(val); } + catch(...) { return ""; } + }; + +///////////////////////////////copied from format-util.hpp + + +template +inline string +stringz (P ptr) +{ + if (not ptr) + return "P"+typeStr(ptr.get())+"{ null }"; + else + return CustomStringConv::invoke (*ptr); +} + int main (int, char**) @@ -76,8 +142,8 @@ main (int, char**) auto psss = newP(); auto gnng = newP("Hui", "Buh"); - cout << "uiii..." << psss <