From d09a5846d4ee6aa2e52c69bded3214192f7e305c Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Mon, 4 Jan 2016 22:21:09 +0100 Subject: [PATCH] basically a working solution for toString in ostream ...and learned a lot about the new type_traits on the way. As it seems, it is not possible to get a clean error message when passing an "object" with no custom string conversion; instead, some overload for an rvalue-ostream kicks in. probably I'll go for shoing a type string in these cases --- research/try.cpp | 94 ++++++++++++++++++- src/proc/asset.hpp | 5 +- src/proc/control/command-closure.hpp | 2 +- tests/core/proc/asset/asset-diagnostics.hpp | 2 +- tests/core/proc/control/command-use1-test.cpp | 2 +- .../proc/mobject/builder/buildertooltest.cpp | 2 +- tests/core/proc/mobject/mobject-ref-test.cpp | 2 +- 7 files changed, 101 insertions(+), 8 deletions(-) diff --git a/research/try.cpp b/research/try.cpp index 62cd11435..510973c52 100644 --- a/research/try.cpp +++ b/research/try.cpp @@ -45,9 +45,11 @@ typedef unsigned int uint; //#include "lib/util.hpp" #include "lib/meta/util.hpp" +#include "lib/meta/trait.hpp" #include //#include +#include #include #include //#include @@ -136,14 +138,102 @@ stringz (P ptr) } + +/////////////////////////////////////////reworked traits + namespace { + + using lib::meta::Strip; + using std::__or_; + using std::__and_; + using std::__not_; + + template + struct is_basically + : std::is_same ::TypePlain + ,typename Strip::TypePlain> + { }; + + template + struct can_lexical2string + : __or_< std::is_arithmetic + , is_basically + , is_basically::type, char> + > + { }; + } +/////////////////////////////////////////reworked traits + + + +/////////////////////////////////////////planned new ostream inclusion + namespace { + + template + struct use_StringConversion + { + enum { value = can_convertToString::value + && !can_lexical2string::value + }; + }; + + template + struct use_ObjectTypeIndicator + : __and_<__not_> + ,__not_> + ,std::is_object + > + { }; + } + + template>> + std::ostream& + operator<< (std::ostream& os, X const& obj) + { + return os << CustomStringConv::invoke (obj); + } +// +// template>> +// std::ostream& +// operator<< (std::ostream& os, X const& obj) +// { +// return os << CustomStringConv::invoke (obj); +// } + + template + std::ostream& + operator<< (std::ostream& os, P const& ptr) + { + return os << stringz (ptr); + } + +/////////////////////////////////////////planned new ostream inclusion + + int main (int, char**) { auto psss = newP(); auto gnng = newP("Hui", "Buh"); - cout << "uiii..." << stringz(psss) <; + using BasicallyChar = is_basically::type, char>; + + SHOW_CHECK (BasicallyChar); + SHOW_CHECK (BasicallyString); + SHOW_CHECK (std::is_arithmetic); + SHOW_CHECK (can_lexical2string); + SHOW_CHECK (can_convertToString); + SHOW_CHECK (use_StringConversion); + + cout << "mauu..." << psss < ID::INVALID = ID(0); - /** convenient for debugging */ + /** convenient for debugging + * @deprecated to be obsoleted by automatically using + * custom string conversion in ostreams + */ inline string str (PcAsset const& a) { if (a) diff --git a/src/proc/control/command-closure.hpp b/src/proc/control/command-closure.hpp index 11ea1fc68..c2dbb1b3d 100644 --- a/src/proc/control/command-closure.hpp +++ b/src/proc/control/command-closure.hpp @@ -184,7 +184,7 @@ namespace control { ostream& dump (ostream& output) const { - return BASE::dump (output << util::str (element()) << ','); + return BASE::dump (output << util::str (element()) << ','); //////////////////TICKET #985 : make str obsolete! } friend bool diff --git a/tests/core/proc/asset/asset-diagnostics.hpp b/tests/core/proc/asset/asset-diagnostics.hpp index c3b009145..69dfd0024 100644 --- a/tests/core/proc/asset/asset-diagnostics.hpp +++ b/tests/core/proc/asset/asset-diagnostics.hpp @@ -61,7 +61,7 @@ namespace asset { else { _Fmt fmt("%s %|50T.| id=%s adr=%p smart-ptr=%p use-count=%u"); - cout << fmt % str(aa) % aa->getID() % aa.get() % &aa % (aa.use_count() - 1) << "\n"; + cout << fmt % str(aa) % aa->getID() % aa.get() % &aa % (aa.use_count() - 1) << "\n"; ////////////TICKET #985 : make str obsolete } } diff --git a/tests/core/proc/control/command-use1-test.cpp b/tests/core/proc/control/command-use1-test.cpp index 827e49481..7e6d5b42f 100644 --- a/tests/core/proc/control/command-use1-test.cpp +++ b/tests/core/proc/control/command-use1-test.cpp @@ -122,7 +122,7 @@ namespace test { CHECK (com); CHECK (com == Command::get("test.command1.2")); CHECK (contains (str(com), "test.command1.2")); - CHECK (contains (str(com), "{def}")); + CHECK (contains (str(com), "{def}")); //////////////////TICKET #985 : make str obsolete! CHECK (!com.canExec()); VERIFY_ERROR (UNBOUND_ARGUMENTS, com() ); CHECK ( 0 == command1::check_); diff --git a/tests/core/proc/mobject/builder/buildertooltest.cpp b/tests/core/proc/mobject/builder/buildertooltest.cpp index c550f7b59..b4938cb5b 100644 --- a/tests/core/proc/mobject/builder/buildertooltest.cpp +++ b/tests/core/proc/mobject/builder/buildertooltest.cpp @@ -73,7 +73,7 @@ namespace test { void treat (Clip& c) { Placement& pC = getPlacement(); - cout << "Clip on media : "<< str(pC->getMedia()) <<"\n"; + cout << "Clip on media : "<< str(pC->getMedia()) <<"\n"; //////////////////TICKET #985 : make str obsolete! CHECK (pC->operator==(c)); log_ = string (pC); } diff --git a/tests/core/proc/mobject/mobject-ref-test.cpp b/tests/core/proc/mobject/mobject-ref-test.cpp index 78b735d07..fc1eb9224 100644 --- a/tests/core/proc/mobject/mobject-ref-test.cpp +++ b/tests/core/proc/mobject/mobject-ref-test.cpp @@ -199,7 +199,7 @@ namespace test { // access MObject (Clip API) // cout << rMO->operator string() << endl; /////////////////////TICKET #428 PMedia media = rMO->getMedia(); - cout << str(media) << endl; /////////////////////TICKET #520 + cout << str(media) << endl; /////////////////////TICKET #520 ///////TICKET #985 : make str obsolete! Duration mediaLength = media->getLength(); CHECK (!isnil (mediaLength)); CHECK (rMO->isValid());