diff --git a/src/common/advice/index.hpp b/src/common/advice/index.hpp index de9360c5a..63abbfa5e 100644 --- a/src/common/advice/index.hpp +++ b/src/common/advice/index.hpp @@ -91,7 +91,7 @@ #include "lib/symbol.hpp" #include "include/logging.h" #include "lib/iter-adapter-stl.hpp" -#include "lib/format-util.hpp" +#include "lib/format-obj.hpp" #include "lib/util-foreach.hpp" #include "lib/util.hpp" #include "common/advice/binding.hpp" @@ -109,7 +109,7 @@ namespace advice { using std::unordered_map; using lib::iter_stl::eachVal; using lib::iter_stl::eachElm; - using util::str; + using util::toString; using util::for_each; using util::contains; using util::unConst; @@ -241,7 +241,7 @@ namespace advice { operator string() const ///< debugging helper: show Cluster contents { - string dump{"elmList("+str(elms_.size())+")\n"}; + string dump{"elmList("+toString(elms_.size())+")\n"}; for (auto const& entry : elms_) dump += "E...:"+entry+"\n"; return dump; diff --git a/src/lib/diff/gen-node.cpp b/src/lib/diff/gen-node.cpp index ab99a8bc5..6da07156f 100644 --- a/src/lib/diff/gen-node.cpp +++ b/src/lib/diff/gen-node.cpp @@ -42,7 +42,6 @@ #include "lib/error.hpp" -#include "lib/format-util.hpp" #include "lib/diff/diff-language.hpp" #include "lib/diff/gen-node.hpp" #include "lib/util-quant.hpp" diff --git a/src/lib/diff/record.hpp b/src/lib/diff/record.hpp index 3484da5ba..764ce2cd1 100644 --- a/src/lib/diff/record.hpp +++ b/src/lib/diff/record.hpp @@ -90,6 +90,7 @@ #include "lib/iter-adapter.hpp" #include "lib/iter-adapter-stl.hpp" #include "lib/itertools.hpp" +#include "lib/format-util.hpp" #include "lib/util.hpp" #include "lib/diff/record-content-mutator.hpp" @@ -724,9 +725,6 @@ namespace diff{ template Record::operator std::string() const { -#ifndef LIB_FORMAT_UTIL_H - return "Record(...)"; -#else using util::join; using lib::transformIterator; @@ -736,7 +734,6 @@ namespace diff{ + (isnil(this->scope())? "" : "|{"+join (this->scope())+"}") + ")" ; -#endif } diff --git a/src/lib/format-string.cpp b/src/lib/format-string.cpp index 30f2481f0..769849b5e 100644 --- a/src/lib/format-string.cpp +++ b/src/lib/format-string.cpp @@ -161,21 +161,21 @@ namespace util { catch (boost::io::too_many_args& argErr) { WARN (progress, "Format: excess argument '%s' of type «%s» ignored." - , cStr(str(val)) + , cStr(toString(val)) , cStr(typeStr(val))); } catch (std::exception& failure) { _clear_errorflag(); WARN (progress, "Format: Parameter '%s' causes problems: %s" - , cStr(str(val)) + , cStr(toString(val)) , failure.what()); pushFailsafeReplacement (formatter, failure.what()); } catch (...) { _clear_errorflag(); - WARN (progress, "Format: Unexpected problems accepting format parameter '%s'", cStr(str(val))); + WARN (progress, "Format: Unexpected problems accepting format parameter '%s'", cStr(toString(val))); pushFailsafeReplacement (formatter); } diff --git a/src/lib/format-string.hpp b/src/lib/format-string.hpp index bf8bf6358..24d842be0 100644 --- a/src/lib/format-string.hpp +++ b/src/lib/format-string.hpp @@ -112,7 +112,6 @@ #include #include #include -#include @@ -137,7 +136,6 @@ namespace lib { namespace util { using std::string; - using boost::enable_if; typedef unsigned char uchar; @@ -380,7 +378,7 @@ namespace util { /** some custom types explicitly provide a string representation */ template - struct _Fmt::Converter >::type> + struct _Fmt::Converter> > { static void dump (VAL const& val, Implementation& impl) @@ -400,7 +398,7 @@ namespace util { /** some basic types are directly forwarded down to the implementation; * @note this requires explicit specialisations in format-string.cpp */ template - struct _Fmt::Converter >::type> + struct _Fmt::Converter> > { static void dump (const VAL val, Implementation& impl) diff --git a/src/lib/format-util.hpp b/src/lib/format-util.hpp index b2ef70355..96e539c67 100644 --- a/src/lib/format-util.hpp +++ b/src/lib/format-util.hpp @@ -47,131 +47,18 @@ #include #include -#include #include #include -#include -#include namespace util { - using boost::enable_if; - using lib::meta::can_convertToString; - using lib::meta::can_lexical2string; using lib::meta::can_IterForEach; - using lib::Symbol; - using util::removePrefix; - using util::removeSuffix; - using util::isnil; using std::string; using std::move; - namespace { // we need to guard the string conversion - // to avoid a compiler error in case the type isn't convertible.... - - // precision for rendering of double values - const auto DIAGNOSTICS_DOUBLE_PRECISION = 8; - const auto DIAGNOSTICS_FLOAT_PRECISION = 5; - - - template - struct use_StringConversion : can_convertToString { }; - - template - struct use_LexicalConversion - { - enum { value = can_lexical2string::value - && !can_convertToString::value - }; - }; - - - /** helper: reliably get some string representation for type X */ - template - struct _InvokeFailsafe - { - static string toString (X const&) { return ""; } - }; - - template - struct _InvokeFailsafe >::type> - { - static string - toString (X const& val) - try { return string(val); } - catch(...) { return ""; } - }; - - template - struct _InvokeFailsafe >::type> - { - static string - toString (X const& val) - try { return boost::lexical_cast (val); } - catch(...) { return ""; } - }; - - /** explicit specialisation to control precision of double values. - * @note we set an explicit precision, since this is a diagnostic facility - * and we typically do not want to see all digits, but, for test code, - * we do want a predictable string representation of simple fractional - * values like `0.1` (which can not be represented as binary floats) - */ - template<> - struct _InvokeFailsafe - { - static string - toString (double const& val) - try { - std::ostringstream buffer; - buffer.precision(DIAGNOSTICS_DOUBLE_PRECISION); - buffer << val; - return buffer.str(); - } - catch(...) { return ""; } - }; - template<> - struct _InvokeFailsafe - { - static string - toString (float const& val) - try { - std::ostringstream buffer; - buffer.precision(DIAGNOSTICS_FLOAT_PRECISION); - buffer << val; - return buffer.str(); - } - catch(...) { return ""; } - }; - }//(End) guards/helpers - - - /** try to get an object converted to string. - * A custom/standard conversion to string is used, - * if applicable; otherwise, some standard types can be - * converted by a lexical_cast (based on operator<< ). - * Otherwise, either the fallback string is used, or just - * a string based on the (mangled) type. - */ - template - inline string - str ( TY const& val - , Symbol prefix="" ///< prefix to prepend in case conversion is possible - , Symbol fallback =0 /// < replacement text to show if string conversion fails - ) - { - string res = _InvokeFailsafe::toString(val); - if (!isnil (res)) - return string(prefix) + res; - else - return fallback? string(fallback) - : "«"+typeStr(val)+"»"; - } - - namespace { // helper to convert arbitrary elements toString @@ -184,7 +71,7 @@ namespace util { inline void do_stringify(CON& container, X const& elm, ELMS const& ...args) { - container += util::str(elm); + container += util::toString (elm); do_stringify (container, args...); } @@ -251,7 +138,7 @@ namespace util { }; template - struct _RangeIter >::type> + struct _RangeIter> > { IT iter; @@ -291,7 +178,7 @@ namespace util { using Coll = typename lib::meta::Strip::Type; using Val = typename Coll::value_type; - std::function toString = [] (Val const& val) { return str(val); }; + std::function toString = [] (Val const& val) { return util::toString(val); }; _RangeIter range(std::forward(coll)); auto strings = lib::transformIterator(range.iter, toString); diff --git a/src/lib/sub-id.hpp b/src/lib/sub-id.hpp index 38ec49bc3..472f7d65d 100644 --- a/src/lib/sub-id.hpp +++ b/src/lib/sub-id.hpp @@ -116,8 +116,8 @@ namespace lib { operator string() const { - using util::str; - return str (baseID_); // note: extension point + using util::toString; + return toString (baseID_); // note: extension point } }; diff --git a/src/proc/control/memento-tie.hpp b/src/proc/control/memento-tie.hpp index 0222f1ef8..f2cf2dcad 100644 --- a/src/proc/control/memento-tie.hpp +++ b/src/proc/control/memento-tie.hpp @@ -45,6 +45,7 @@ #include "lib/meta/function-closure.hpp" #include "proc/control/command-signature.hpp" #include "lib/functor-util.hpp" +#include "lib/format-obj.hpp" #include "lib/util.hpp" #include @@ -209,12 +210,8 @@ namespace control { if (!isCaptured_) return ""; - return "<" -#ifdef LIB_FORMAT_UTIL_H - + util::str(memento_, "mem: ", "·memento·") -#else - + std::string("memento") -#endif + return ""; } diff --git a/tests/core/proc/control/command-use1-test.cpp b/tests/core/proc/control/command-use1-test.cpp index ca1816e63..4e87538d1 100644 --- a/tests/core/proc/control/command-use1-test.cpp +++ b/tests/core/proc/control/command-use1-test.cpp @@ -25,7 +25,6 @@ #include "lib/test/test-helper.hpp" #include "proc/control/command-invocation.hpp" #include "proc/control/command-def.hpp" -#include "lib/format-util.hpp" #include "lib/format-cout.hpp" #include "lib/util.hpp" @@ -38,7 +37,6 @@ namespace test { using util::isSameObject; using util::contains; - using util::str; diff --git a/tests/library/format-helper-test.cpp b/tests/library/format-helper-test.cpp index b7c43824d..1f1702b47 100644 --- a/tests/library/format-helper-test.cpp +++ b/tests/library/format-helper-test.cpp @@ -23,6 +23,7 @@ #include "lib/test/run.hpp" #include "lib/format-util.hpp" +#include "lib/format-cout.hpp" #include "lib/format-string.hpp" #include "lib/iter-adapter-stl.hpp" #include "lib/error.hpp" @@ -82,8 +83,10 @@ namespace test { /***************************************************************************//** * @test verifies the proper working of some string-formatting helper functions. - * - util::str() provides a failsafe to-String conversion, preferring - * an built-in conversion, falling back to just a mangled type string. + * - util::toString() provides a failsafe to-String conversion, preferring + * an built-in conversion, falling back to just a type string. + * - util::join() combines elements from arbitrary containers or iterators + * into a string, relyint on aforementioned generic string conversion * @see format-util.hpp */ class FormatHelper_test @@ -107,15 +110,15 @@ namespace test { Reticent closeLipped; UnReticent chatterer; - cout << str (closeLipped) << endl; - cout << str (closeLipped, "he says: ", "") << endl; + cout << toString (closeLipped) << endl; + cout << toString (chatterer) << endl; - cout << str (chatterer) << endl; - cout << str (chatterer, "he says: ", "") << endl; - - cout << str (false, "the truth: ") << endl; - cout << str (12.34e55, "just a number: ") << endl; - cout << str (short(12)) << str (345L) << str ('X') << endl; + cout << toString (false) << endl; + cout << toString (12.34e55) << endl; + cout << toString (short(12)) + << toString (345L) + << toString ('X') + << endl; } diff --git a/tests/library/sub-id-test.cpp b/tests/library/sub-id-test.cpp index fc668224e..8b7ab938b 100644 --- a/tests/library/sub-id-test.cpp +++ b/tests/library/sub-id-test.cpp @@ -55,7 +55,7 @@ namespace test{ inline string - str (Colour c) ///< make the enum printable + toString (Colour c) ///< make the enum printable { static string sym("RGB"); return sym.substr(c,1);