Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
format-obj.hpp
Go to the documentation of this file.
1/*
2 FORMAT-OBJ.hpp - simple means to display an object
3
4 Copyright (C)
5 2016, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12*/
13
14
37#ifndef LIB_FORMAT_OBJ_H
38#define LIB_FORMAT_OBJ_H
39
40#include "lib/symbol.hpp"
41#include "lib/meta/trait.hpp"
42
43#include <boost/lexical_cast.hpp>
44
45
46namespace std { // forward declaration to avoid including <iostream>
47
48 template<typename C>
50
51 template<typename C, class _TRAITS>
52 class basic_ostream;
53
54 using ostream = basic_ostream<char, char_traits<char>>;
55}
56
57
58namespace lib {
59 class Literal;
60
61namespace meta {
62
63 std::string demangleCxx (lib::Literal rawName);
67
68}}// namespace lib::meta
69
70
71
72
73
74
75namespace util {
76
77 std::string showDouble (double) noexcept;
78 std::string showFloat (float) noexcept;
79 std::string showSize (size_t) noexcept;
80 std::string showAdr (void const* addr) noexcept;
81
83 std::ostream& showAdr (std::ostream&, void const* addr);
84
86 std::string showHash (size_t hash, uint showBytes=8) noexcept;
87
88 inline std::string
89 showHashLSB (size_t hash) noexcept
90 {
91 return showHash(hash,1);
92 }
93
94 std::string showDecimal (double) noexcept;
95 std::string showDecimal (float) noexcept;
96 std::string showDecimal (f128) noexcept;
97 std::string showComplete (double)noexcept;
98 std::string showComplete (float) noexcept;
99 std::string showComplete (f128) noexcept;
100
101
102
111
112
113
114 /* === generalise the failsafe string conversion === */
115
117 template<typename X>
118 struct StringConv<X, enable_LexicalConversion<X>>
119 {
120 static std::string
121 invoke (X const& val) noexcept
122 try { return boost::lexical_cast<std::string> (val); }
123 catch(...) { return FAILURE_INDICATOR; }
124 };
125
126 template<typename SP>
127 struct StringConv<SP, show_SmartPointer<SP>>
128 {
129 static std::string
130 invoke (SP const& smP) noexcept
131 try { return showSmartPtr (smP, lib::meta::typeSymbol(smP)); }
132 catch(...) { return FAILURE_INDICATOR; }
133 };
134
141 template<>
142 struct StringConv<double>
143 {
144 static std::string
145 invoke (double val) noexcept
146 {
147 return util::showDouble (val);
148 }
149 };
150 template<>
151 struct StringConv<float>
152 {
153 static std::string
154 invoke (float val) noexcept
155 {
156 return util::showFloat (val);
157 }
158 };
159 template<>
160 struct StringConv<bool>
161 {
162 static std::string
163 invoke (bool val) noexcept
164 {
165 return util::showBool (val);
166 }
167 };
168
169
170
171
189 template<typename TY>
190 inline std::string
191 toString (TY const& val) noexcept
192 {
193 using PlainVal = lib::meta::Strip<TY>::TypeReferred;
194 return StringConv<PlainVal>::invoke (val);
195 }
196
197
203 template<typename TY>
204 inline std::string
205 typedString (TY const& val) noexcept
206 try {
207 std::string repr = StringConv<TY>::invoke (val);
208 return 0 == repr.rfind("«", 0)? repr
209 : "«"+typeStr(val)+"»|"+repr;
210 }
211 catch(...)
212 { return FAILURE_INDICATOR; }
213
214
215
216} // namespace util
217#endif /*LIB_FORMAT_OBJ_H*/
Inline string literal.
Definition symbol.hpp:78
#define hash
unsigned int uint
Definition integral.hpp:29
long double f128
Definition integral.hpp:36
string sanitisedFullTypeName(lib::Literal rawName)
build a sanitised ID from full type name
remove_reference_t< TypeUnconst > TypeReferred
Definition trait.hpp:257
string humanReadableTypeID(Literal rawType)
pretty-print an internal C++ type representation
string primaryTypeComponent(Literal rawType)
extract core name component from a raw type spec
enable_if_c< Cond::value, T >::type enable_if
SFINAE helper to control the visibility of specialisations and overloads.
Definition meta/util.hpp:87
std::string typeSymbol(TY const *obj=nullptr)
simple expressive symbol to designate a type
Implementation namespace for support and library code.
STL namespace.
basic_ostream< char, char_traits< char > > ostream
lib::meta::enable_if< lib::meta::is_smart_ptr< typename lib::meta::Strip< SP >::TypeReferred > > show_SmartPointer
lib::meta::enable_if< lib::meta::use_LexicalConversion< X > > enable_LexicalConversion
toggle to prefer specialisation with direct lexical conversion
string showComplete(double val) noexcept
show enough decimal digits to represent every distinct value
string showDouble(double val) noexcept
pretty-print a double in (rounded) fixed-point format
std::string showBool(bool yes) noexcept
human readable display of boolean values
string showHash(size_t hash, uint showBytes) noexcept
renders the size_t in hex, optionally only trailing bytes
std::string typedString(TY const &val) noexcept
indicate type and possibly a (custom) conversion to string
ostream & showAdr(ostream &stream, void const *addr)
preconfigured format for pretty-printing of addresses
string showSize(size_t val) noexcept
string showDecimal(double val) noexcept
show maximum reproducible decimal representation
string showFloat(float val) noexcept
std::string toString(TY const &val) noexcept
get some string representation of any object, reliably.
std::string showSmartPtr(SP const &smPtr, std::string label="smP")
static std::string invoke(SP const &smP) noexcept
static std::string invoke(X const &val) noexcept
static std::string invoke(bool val) noexcept
static std::string invoke(double val) noexcept
static std::string invoke(float val) noexcept
failsafe invocation of custom string conversion.
static std::string invoke(X const &x) noexcept
Marker types to indicate a literal string and a Symbol.
Helpers for type detection, type rewriting and metaprogramming.