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) Lumiera.org
5  2016, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
46 #ifndef LIB_FORMAT_OBJ_H
47 #define LIB_FORMAT_OBJ_H
48 
49 #include "lib/symbol.hpp"
50 #include "lib/meta/trait.hpp"
51 
52 #include <boost/lexical_cast.hpp>
53 
54 
55 namespace std { // forward declaration to avoid including <iostream>
56 
57  template<typename C>
58  struct char_traits;
59 
60  template<typename C, class _TRAITS>
61  class basic_ostream;
62 
63  using ostream = basic_ostream<char, char_traits<char>>;
64 }
65 
66 
67 namespace lib {
68  class Literal;
69 
70 namespace meta {
71 
72  std::string demangleCxx (lib::Literal rawName);
73  std::string humanReadableTypeID (lib::Literal);
74  std::string primaryTypeComponent (lib::Literal);
75  std::string sanitisedFullTypeName(lib::Literal);
76 
77 }}// namespace lib::meta
78 
79 
80 
81 
82 
83 
84 namespace util {
85 
86  std::string showDouble (double) noexcept;
87  std::string showFloat (float) noexcept;
88  std::string showSize (size_t) noexcept;
89  std::string showAddr (void const* addr) noexcept;
90 
92  std::ostream& showAddr (std::ostream&, void const* addr);
93 
95  std::string showHash (size_t hash, uint showBytes=8) noexcept;
96 
97  inline std::string
98  showHashLSB (size_t hash) noexcept
99  {
100  return showHash(hash,1);
101  }
102 
103  std::string showDecimal (double) noexcept;
104  std::string showDecimal (float) noexcept;
105  std::string showDecimal (f128) noexcept;
106  std::string showComplete (double)noexcept;
107  std::string showComplete (float) noexcept;
108  std::string showComplete (f128) noexcept;
109 
110 
111 
112  namespace {
114  template<typename X>
116 
117  template<typename SP>
119  }
120 
121 
122 
123  /* === generalise the failsafe string conversion === */
124 
126  template<typename X>
128  {
129  static std::string
130  invoke (X const& val) noexcept
131  try { return boost::lexical_cast<std::string> (val); }
132  catch(...) { return FAILURE_INDICATOR; }
133  };
134 
135  template<typename SP>
136  struct StringConv<SP, show_SmartPointer<SP>>
137  {
138  static std::string
139  invoke (SP const& smP) noexcept
140  try { return showSmartPtr (smP, lib::meta::typeSymbol(smP)); }
141  catch(...) { return FAILURE_INDICATOR; }
142  };
143 
150  template<>
151  struct StringConv<double>
152  {
153  static std::string
154  invoke (double val) noexcept
155  {
156  return util::showDouble (val);
157  }
158  };
159  template<>
160  struct StringConv<float>
161  {
162  static std::string
163  invoke (float val) noexcept
164  {
165  return util::showFloat (val);
166  }
167  };
168  template<>
169  struct StringConv<bool>
170  {
171  static std::string
172  invoke (bool val) noexcept
173  {
174  return util::showBool (val);
175  }
176  };
177 
178 
179 
180 
198  template<typename TY>
199  inline std::string
200  toString (TY const& val) noexcept
201  {
202  using PlainVal = typename lib::meta::Strip<TY>::TypeReferred;
203  return StringConv<PlainVal>::invoke (val);
204  }
205 
206 
212  template<typename TY>
213  inline std::string
214  typedString (TY const& val) noexcept
215  try {
216  std::string repr = StringConv<TY>::invoke (val);
217  return 0 == repr.rfind("«", 0)? repr
218  : "«"+typeStr(val)+"»|"+repr;
219  }
220  catch(...)
221  { return FAILURE_INDICATOR; }
222 
223 
224 
225 } // namespace util
226 #endif /*LIB_FORMAT_OBJ_H*/
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:85
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:362
std::string showBool(bool yes) noexcept
human readable display of boolean values
Definition: meta/util.hpp:444
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:92
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:200
Helpers for type detection, type rewriting and metaprogramming.
string typeSymbol()
Short readable type identifier, not necessarily unique or complete.
Definition: genfunc.hpp:87
std::string typedString(TY const &val) noexcept
indicate type and possibly a (custom) conversion to string
Definition: format-obj.hpp:214
failsafe invocation of custom string conversion.
Definition: meta/util.hpp:399
lib::meta::enable_if< lib::meta::use_LexicalConversion< X > > enable_LexicalConversion
toggle to prefer specialisation with direct lexical conversion
Definition: format-obj.hpp:115