From 5be35a407fdb363d95ef9bbfe0e87c35c1de7777 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Tue, 5 Jan 2016 23:52:48 +0100 Subject: [PATCH] toString(#985): new minimal string-conversion facility now placed into the very basic header lib/meta/util.hpp --- research/try.cpp | 21 ++------------------- src/lib/meta/util.hpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/research/try.cpp b/research/try.cpp index 78be072e2..643d3a728 100644 --- a/research/try.cpp +++ b/research/try.cpp @@ -58,7 +58,7 @@ ** between values, references and pointers, which also means, we do ** not want to indicate pointers explicitly (just signal NULL, when ** encountered). The situation is slightly different for the `ostream` - ** inserter; in a moder GUI application, there isn't much use for + ** inserter; in a modern GUI application, there isn't much use for ** STDOUT and STDERR, beyond error messages and unit testing. ** Thus, we can strive at building a more convenient flavour ** here, which does indeed even shows the address of pointers. @@ -83,6 +83,7 @@ using lib::P; using lib::meta::enable_if; using lib::meta::typeStr; using lib::meta::can_convertToString; +using lib::meta::CustomStringConv; using std::string; using std::cout; @@ -94,24 +95,6 @@ using std::endl; -///////////////////////////////planned minimal conversion, maybe in meta/util.hpp ? - 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 "↯"; } - }; -///////////////////////////////planned minimal conversion, maybe in meta/util.hpp ? - - ///////////////////////////////shall go into the implementation of lib::P template inline string diff --git a/src/lib/meta/util.hpp b/src/lib/meta/util.hpp index 2ac910290..b519cff39 100644 --- a/src/lib/meta/util.hpp +++ b/src/lib/meta/util.hpp @@ -220,5 +220,34 @@ namespace meta { + /** failsafe invocation of custom string conversion. + * @return string to represent the object, by default a [type display](\ref typeStr) + * @remarks this is a lightweight solution to at least _get any human readable string + * representation for pretty much every language object._ This minimal solution + * is defined here, to allow for built-in diagnostics for custom types without + * the danger of creating much header inclusion and code size bloat. A more + * elaborate, [extended solution](lib::toString), including _lexical conversions + * for numbers,_ is defined in format-obj.hpp + * @note any exceptions during string conversion are caught and silently ignored; + * the returned string indicates "↯" in this case. + */ + template + struct CustomStringConv + { + static std::string invoke (X const& x) { return "«"+typeStr(x)+"»"; } + }; + + template + struct CustomStringConv> > + { + static std::string + invoke (X const& val) + try { return std::string(val); } + catch(...) { return "↯"; } + }; + + + + }} // namespace lib::meta #endif