toString(#985): new minimal string-conversion facility

now placed into the very basic header lib/meta/util.hpp
This commit is contained in:
Fischlurch 2016-01-05 23:52:48 +01:00
parent 0c4495a451
commit 5be35a407f
2 changed files with 31 additions and 19 deletions

View file

@ -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<typename X, typename COND =void>
struct CustomStringConv
{
static string invoke (X const& x) { return "«"+typeStr(x)+"»"; }
};
template<typename X>
struct CustomStringConv<X, enable_if<can_convertToString<X>> >
{
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<typename X>
inline string

View file

@ -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<typename X, typename COND =void>
struct CustomStringConv
{
static std::string invoke (X const& x) { return "«"+typeStr(x)+"»"; }
};
template<typename X>
struct CustomStringConv<X, enable_if<can_convertToString<X>> >
{
static std::string
invoke (X const& val)
try { return std::string(val); }
catch(...) { return ""; }
};
}} // namespace lib::meta
#endif