clean-up(#985): remove code superseded by this rework

now finally able to remove most of the cruft from format-util.hpp
and get rid of the infamous util::str
This commit is contained in:
Fischlurch 2016-01-08 09:14:46 +01:00
parent 615f112f5c
commit 334f542897
11 changed files with 31 additions and 152 deletions

View file

@ -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;

View file

@ -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"

View file

@ -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<typename VAL>
Record<VAL>::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
}

View file

@ -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);
}

View file

@ -112,7 +112,6 @@
#include <string>
#include <typeinfo>
#include <boost/noncopyable.hpp>
#include <boost/utility/enable_if.hpp>
@ -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<typename VAL>
struct _Fmt::Converter<VAL, typename enable_if< _shall_convert_toString<VAL> >::type>
struct _Fmt::Converter<VAL, lib::meta::enable_if<_shall_convert_toString<VAL>> >
{
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<typename VAL>
struct _Fmt::Converter<VAL, typename enable_if< _shall_format_directly<VAL> >::type>
struct _Fmt::Converter<VAL, lib::meta::enable_if<_shall_format_directly<VAL>> >
{
static void
dump (const VAL val, Implementation& impl)

View file

@ -47,131 +47,18 @@
#include <string>
#include <sstream>
#include <cstring>
#include <utility>
#include <typeinfo>
#include <boost/lexical_cast.hpp>
#include <boost/utility/enable_if.hpp>
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<typename X>
struct use_StringConversion : can_convertToString<X> { };
template<typename X>
struct use_LexicalConversion
{
enum { value = can_lexical2string<X>::value
&& !can_convertToString<X>::value
};
};
/** helper: reliably get some string representation for type X */
template<typename X, typename COND =void>
struct _InvokeFailsafe
{
static string toString (X const&) { return ""; }
};
template<typename X>
struct _InvokeFailsafe<X, typename enable_if< use_StringConversion<X> >::type>
{
static string
toString (X const& val)
try { return string(val); }
catch(...) { return ""; }
};
template<typename X>
struct _InvokeFailsafe<X, typename enable_if< use_LexicalConversion<X> >::type>
{
static string
toString (X const& val)
try { return boost::lexical_cast<string> (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<double>
{
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<float>
{
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<typename TY>
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<TY>::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<class IT>
struct _RangeIter<IT, typename enable_if< can_IterForEach<IT> >::type>
struct _RangeIter<IT, lib::meta::enable_if< can_IterForEach<IT>> >
{
IT iter;
@ -291,7 +178,7 @@ namespace util {
using Coll = typename lib::meta::Strip<CON>::Type;
using Val = typename Coll::value_type;
std::function<string(Val const&)> toString = [] (Val const& val) { return str(val); };
std::function<string(Val const&)> toString = [] (Val const& val) { return util::toString(val); };
_RangeIter<Coll> range(std::forward<CON>(coll));
auto strings = lib::transformIterator(range.iter, toString);

View file

@ -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
}
};

View file

@ -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 <boost/operators.hpp>
@ -209,12 +210,8 @@ namespace control {
if (!isCaptured_)
return "<mem:missing>";
return "<"
#ifdef LIB_FORMAT_UTIL_H
+ util::str(memento_, "mem: ", "·memento·")
#else
+ std::string("memento")
#endif
return "<mem: "
+ util::toString (memento_)
+ ">";
}

View file

@ -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;

View file

@ -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: ", "<no comment>") << endl;
cout << toString (closeLipped) << endl;
cout << toString (chatterer) << endl;
cout << str (chatterer) << endl;
cout << str (chatterer, "he says: ", "<no comment>") << 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;
}

View file

@ -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);