Lumiera  0.pre.03
»edityourfreedom«
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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);
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 
94 
95  namespace {
97  template<typename X>
98  using enable_LexicalConversion = lib::meta::enable_if< lib::meta::use_LexicalConversion<X>>;
99 
100  template<typename SP>
102  }
103 
104 
105 
106  /* === generalise the failsafe string conversion === */
107 
109  template<typename X>
110  struct StringConv<X, enable_LexicalConversion<X>>
111  {
112  static std::string
113  invoke (X const& val) noexcept
114  try { return boost::lexical_cast<std::string> (val); }
115  catch(...) { return FAILURE_INDICATOR; }
116  };
117 
118  template<typename SP>
119  struct StringConv<SP, show_SmartPointer<SP>>
120  {
121  static std::string
122  invoke (SP const& smP) noexcept
123  try { return showSmartPtr (smP, lib::meta::typeSymbol(smP)); }
124  catch(...) { return FAILURE_INDICATOR; }
125  };
126 
133  template<>
134  struct StringConv<double>
135  {
136  static std::string
137  invoke (double val) noexcept
138  {
139  return util::showDouble (val);
140  }
141  };
142  template<>
143  struct StringConv<float>
144  {
145  static std::string
146  invoke (float val) noexcept
147  {
148  return util::showFloat (val);
149  }
150  };
151  template<>
152  struct StringConv<bool>
153  {
154  static std::string
155  invoke (bool val) noexcept
156  {
157  return util::showBool (val);
158  }
159  };
160 
161 
162 
163 
172  template<typename TY>
173  inline std::string
174  toString (TY const& val) noexcept
175  {
176  return StringConv<TY>::invoke (val);
177  }
178 
179 
185  template<typename TY>
186  inline std::string
187  typedString (TY const& val) noexcept
188  try {
189  std::string repr = StringConv<TY>::invoke (val);
190  return 0 == repr.rfind("«", 0)? repr
191  : "«"+typeStr(val)+"»|"+repr;
192  }
193  catch(...)
194  { return FAILURE_INDICATOR; }
195 
196 
197 
198 } // namespace util
199 #endif /*LIB_FORMAT_OBJ_H*/
std::string typeSymbol(TY const *obj=nullptr)
simple expressive symbol to designate a type
Definition: meta/util.hpp:304
typename enable_if_c< Cond::value, T >::type enable_if
SFINAE helper to control the visibility of specialisations and overloads.
Definition: meta/util.hpp:91
std::string toString(TY const &val) noexcept
get some string representation of any object, reliably.
Definition: format-obj.hpp:174
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:75
STL namespace.
string demangleCxx(Literal rawName)
Fallback type-ID:
Definition: format-obj.cpp:157
ostream & showAddr(ostream &stream, void const *addr)
preconfigured format for pretty-printing of addresses
Definition: format-obj.cpp:376
std::string typeStr(TY const *obj=nullptr) noexcept
failsafe human readable type display
Definition: meta/util.hpp:272
std::string showBool(bool yes) noexcept
human readable display of boolean values
Definition: meta/util.hpp:404
Implementation namespace for support and library code.
string sanitisedFullTypeName(lib::Literal rawName)
build a sanitised ID from full type name
Definition: format-obj.cpp:297
string showFloat(float val) noexcept
Definition: format-obj.cpp:352
std::string showSmartPtr(SP const &smPtr, std::string label="smP")
Definition: meta/util.hpp:392
Marker types to indicate a literal string and a Symbol.
string showSize(size_t val) noexcept
Definition: format-obj.cpp:364
static std::string invoke(SP const &smP) noexcept
Definition: format-obj.hpp:122
std::string typedString(TY const &val) noexcept
indicate type and possibly a (custom) conversion to string
Definition: format-obj.hpp:187
static std::string invoke(X const &x) noexcept
Definition: meta/util.hpp:346
Helpers for type detection, type rewriting and metaprogramming.
string primaryTypeComponent(Literal rawType)
extract core name component from a raw type spec
Definition: format-obj.cpp:259
static std::string invoke(X const &val) noexcept
Definition: format-obj.hpp:113
string humanReadableTypeID(Literal rawType)
pretty-print an internal C++ type representation
Definition: format-obj.cpp:188
static std::string invoke(float val) noexcept
Definition: format-obj.hpp:146
static std::string invoke(double val) noexcept
Definition: format-obj.hpp:137
const string FAILURE_INDICATOR
Definition: meta/util.hpp:243
failsafe invocation of custom string conversion.
Definition: meta/util.hpp:343
static std::string invoke(bool val) noexcept
Definition: format-obj.hpp:155
string showDouble(double val) noexcept
pretty-print a double in fixed-point format
Definition: format-obj.cpp:340