Lumiera  0.pre.03
»edit your freedom«
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 
46 namespace std { // forward declaration to avoid including <iostream>
47 
48  template<typename C>
49  struct char_traits;
50 
51  template<typename C, class _TRAITS>
52  class basic_ostream;
53 
54  using ostream = basic_ostream<char, char_traits<char>>;
55 }
56 
57 
58 namespace lib {
59  class Literal;
60 
61 namespace meta {
62 
63  std::string demangleCxx (lib::Literal rawName);
64  std::string humanReadableTypeID (lib::Literal);
65  std::string primaryTypeComponent (lib::Literal);
66  std::string sanitisedFullTypeName(lib::Literal);
67 
68 }}// namespace lib::meta
69 
70 
71 
72 
73 
74 
75 namespace 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 
103  namespace {
105  template<typename X>
107 
108  template<typename SP>
110  }
111 
112 
113 
114  /* === generalise the failsafe string conversion === */
115 
117  template<typename 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 = typename 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 This is a marker type to indicate that
Definition: symbol.hpp:76
STL namespace.
Primary class template for std::hash.
string showDouble(double val) noexcept
pretty-print a double in (rounded) fixed-point format
Definition: format-obj.cpp:360
std::string showBool(bool yes) noexcept
human readable display of boolean values
Definition: meta/util.hpp:435
Implementation namespace for support and library code.
typename enable_if_c< Cond::value, T >::type enable_if
SFINAE helper to control the visibility of specialisations and overloads.
Definition: meta/util.hpp:83
Marker types to indicate a literal string and a Symbol.
std::string toString(TY const &val) noexcept
get some string representation of any object, reliably.
Definition: format-obj.hpp:191
Helpers for type detection, type rewriting and metaprogramming.
string typeSymbol()
Short readable type identifier, not necessarily unique or complete.
Definition: genfunc.hpp:78
std::string typedString(TY const &val) noexcept
indicate type and possibly a (custom) conversion to string
Definition: format-obj.hpp:205
failsafe invocation of custom string conversion.
Definition: meta/util.hpp:390
lib::meta::enable_if< lib::meta::use_LexicalConversion< X > > enable_LexicalConversion
toggle to prefer specialisation with direct lexical conversion
Definition: format-obj.hpp:106