-
Notifications
You must be signed in to change notification settings - Fork 6
/
heal.hpp
351 lines (294 loc) · 11 KB
/
heal.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< --
// std 11 <-> 03/w-boost bridge compatiblity layer, plus a few macro utils.
// - rlyeh, zlib/libpng licensed.
#ifndef __BRIDGE_HPP__
#define __BRIDGE_HPP__
#ifdef __SSE__
# define BOOST_HAS_INT128 1
# include <xmmintrin.h>
#endif
#if (__cplusplus < 201103L && !defined(_MSC_VER)) || (defined(_MSC_VER) && (_MSC_VER < 1700)) || (defined(__GLIBCXX__) && __GLIBCXX__ < 20130322L)
# define BRIDGE_VERSION 2003
# include <boost/functional.hpp> // if old libstdc++ or msc libs are found, use boost::function
# include <boost/function.hpp> //
# include <boost/thread.hpp> // and boost::thread
# include <boost/cstdint.hpp>
# include <boost/type_traits.hpp>
# include <boost/bind.hpp>
# include <boost/bind/placeholders.hpp>
namespace std {
namespace placeholders {
//...
}
using namespace boost;
}
#else
# define BRIDGE_VERSION 2011
# include <functional> // else assume modern c++11 and use std::function<> instead
# include <mutex> // and std::mutex
# include <thread> // and std::thread
# include <cstdint>
#endif
#if BRIDGE_VERSION >= 2011
#define $cpp11 $yes
#define $cpp03 $no
#else
#define $cpp11 $no
#define $cpp03 $yes
#endif
// Thread Local Storage
#if defined(__MINGW32__) || defined(__SUNPRO_C) || defined(__xlc__) || defined(__GNUC__) || defined(__clang__) || defined(__GNUC__) // __INTEL_COMPILER on linux
// MingW, Solaris Studio C/C++, IBM XL C/C++,[3] GNU C,[4] Clang[5] and Intel C++ Compiler (Linux systems)
# define $tls(x) __thread x
#else
// Visual C++,[7] Intel C/C++ (Windows systems),[8] C++Builder, and Digital Mars C++
# define $tls(x) __declspec(thread) x
#endif
// OS utils. Here is where the fun starts... good luck
#define $quote(...) #__VA_ARGS__
#define $comment(...) $no
#define $uncomment(...) $yes
#define $yes(...) __VA_ARGS__
#define $no(...)
#define $on(v) (0 v(+1)) // usage: #if $on($msvc)
#define $is $on // usage: #if $is($debug)
#define $has(...) $clang(__has_feature(__VA_ARGS__)) $celse(__VA_ARGS__) // usage: #if $has(cxx_exceptions)
#if defined(_WIN32)
# define $windows $yes
# define $welse $no
#else
# define $windows $no
# define $welse $yes
#endif
#ifdef __APPLE__
# define $apple $yes
# define $aelse $no
#else
# define $apple $no
# define $aelse $yes
#endif
#ifdef __linux__
# define $linux $yes
# define $lelse $no
#else
# define $linux $no
# define $lelse $yes
#endif
// Compiler utils
#if ULONG_MAX == 4294967295
# define $bits64 $yes
# define $bits32 $no
#else
# define $bits64 $no
# define $bits32 $yes
#endif
#if defined(NDEBUG) || defined(_NDEBUG) || defined(RELEASE)
# define $release $yes
# define $debug $no
#else
# define $release $no
# define $debug $yes
#endif
#if defined(NDEVEL) || defined(_NDEVEL) || defined(PUBLIC)
# define $public $yes
# define $devel $no
#else
# define $public $no
# define $devel $yes
#endif
#if defined(__GNUC__) || defined(__MINGW32__)
# define $gnuc $yes
# define $gelse $no
#else
# define $gnuc $no
# define $gelse $yes
#endif
#ifdef _MSC_VER
# define $msvc $yes
# define $melse $no
#else
# define $msvc $no
# define $melse $yes
#endif
#ifdef __clang__
# define $clang $yes
# define $celse $no
#else
# define $clang $no
# define $celse $yes
#endif
#if $on($msvc) || $on($gnuc) || $on($clang)
# define $undefined_compiler $no
#else
# define $undefined_compiler $yes
#endif
#if $on($windows) || $on($linux) || $on($apple)
# define $undefined_os $no
#else
# define $undefined_os $yes
#endif
#if $on($gnuc) || $on($clang)
# define $likely(expr) (__builtin_expect(!!(expr), 1))
# define $unlikely(expr) (__builtin_expect(!!(expr), 0))
#else
# define $likely(expr) ((expr))
# define $unlikely(expr) ((expr))
#endif
// try to detect if exceptions are enabled...
#if (defined(_HAS_EXCEPTIONS) && (_HAS_EXCEPTIONS > 0)) || \
(defined(_STLP_USE_EXCEPTIONS) && (_STLP_USE_EXCEPTIONS > 0)) || \
(defined(HAVE_EXCEPTIONS)) || \
(defined(__EXCEPTIONS)) || \
(defined(_CPPUNWIND)) || \
($has(cxx_exceptions)) /*(__has_feature(cxx_exceptions))*/
# define $throw $yes
# define $telse $no
#else
# define $throw $no
# define $telse $yes
#endif
// create a $warning(...) macro
// usage: $warning("this is shown at compile time")
#if $on($msvc)
# define $warning(msg) __pragma( message( msg ) )
#elif $on($gnuc) || $on($clang)
# define $$warning$impl(msg) _Pragma(#msg)
# define $warning(msg) $$warning$impl( message( msg ) )
#else
# define $warning(msg)
#endif
// create a $warning(...) macro
// usage: $warning("this is shown at compile time")
#define $$todo$stringize$impl(X) #X
#define $$todo$stringize(X) $$todo$stringize$impl(X)
#define $todo(...) $warning( __FILE__ "(" $$todo$stringize(__LINE__)") : $todo - " #__VA_ARGS__ " - [ "__func__ " ]" )
#endif // __BRIDGE_HPP__
// -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< --
// Heal is a lightweight C++ framework to aid and debug applications.
// - rlyeh, zlib/libpng licensed // ~listening to Kalas - Monuments to Ruins
// Callstack code is based on code by Magnus Norddahl (See http://goo.gl/LM5JB)
// Mem/CPU OS code is based on code by David Robert Nadeau (See http://goo.gl/8P5Jqv)
// Distributed under Creative Commons Attribution 3.0 Unported License
// http://creativecommons.org/licenses/by/3.0/deed.en_US
#ifndef __HEALHPP__
#define __HEALHPP__
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <iostream>
#include <istream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#define HEAL_VERSION "1.0.0" // (2015/10/01) Semantic versioning adherence and clean ups
/* public API */
namespace heal {
typedef std::function< int( const std::string &in ) > heal_callback_in;
extern std::vector< heal_callback_in > warns;
extern std::vector< heal_callback_in > fails;
void warn( const std::string &error );
void fail( const std::string &error );
void add_worker( heal_callback_in fn );
void die( const std::string &reason, int errorcode = -1 );
void die( int errorcode = -1, const std::string &reason = std::string() );
void breakpoint();
bool debugger( const std::string &reason = std::string() );
void alert();
void alert( const char *t, const std::string &title = std::string() );
void alert( const std::string &t, const std::string &title = std::string() );
void alert( const std::istream &is, const std::string &title = std::string() );
void alert( const size_t &t, const std::string &title = std::string() );
void alert( const double &t, const std::string &title = std::string() );
void alert( const float &t, const std::string &title = std::string() );
void alert( const int &t, const std::string &title = std::string() );
void alert( const char &t, const std::string &title = std::string() );
void alert( const bool &t, const std::string &title = std::string() );
void errorbox( const std::string &body = std::string(), const std::string &title = std::string() );
std::string prompt( const std::string ¤t_value = std::string(), const std::string &title = std::string(), const std::string &caption = std::string() );
bool is_debug();
bool is_release();
bool is_asserting();
bool is_devel();
bool is_public();
#ifndef HEAL_MAX_TRACES
#define HEAL_MAX_TRACES 128
#endif
struct callstack /* : public std::vector<const void*> */ {
enum { max_frames = HEAL_MAX_TRACES };
std::vector<void *> frames;
callstack( bool autosave = false );
size_t space() const;
void save( unsigned frames_to_skip = 0 );
std::vector<std::string> unwind( unsigned from = 0, unsigned to = ~0 ) const;
std::vector<std::string> str( const char *format12 = "#\1 \2\n", size_t skip_begin = 0 ) const;
std::string flat( const char *format12 = "#\1 \2\n", size_t skip_begin = 0 ) const;
};
template<typename T>
static inline
std::string lookup( T *ptr ) {
callstack cs;
cs.frames.push_back( (void *)ptr );
std::vector<std::string> stacktrace = cs.unwind();
return stacktrace.size() ? stacktrace[0] : std::string("????");
}
std::string demangle( const std::string &mangled );
std::vector<std::string> stacktrace( const char *format12 = "#\1 \2\n", size_t skip_initial = 0 );
std::string stackstring( const char *format12 = "#\1 \2\n", size_t skip_initial = 0 );
std::string hexdump( const void *data, size_t num_bytes, const void *self = 0 );
template<typename T> inline std::string hexdump( const T& obj ) {
return hexdump( obj.data(), obj.size() * sizeof(*obj.begin()), &obj );
}
$cpp11(
template<> inline std::string hexdump( const std::nullptr_t &obj ) {
return hexdump( 0,0,0 );
}
)
template<> inline std::string hexdump( const char &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const short &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const long &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const long long &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const unsigned char &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const unsigned short &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const unsigned long &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const unsigned long long &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const float &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const double &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const long double &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}/*
template<size_t N> inline std::string hexdump( const char (&obj)[N] ) {
return hexdump( &obj, sizeof(char) * N, &obj );
}*/
template<typename T> inline std::string hexdump( const T* obj ) {
if( !obj ) return hexdump(0,0,0);
return hexdump( *obj );
}
template<> inline std::string hexdump( const char *obj ) {
if( !obj ) return hexdump(0,0,0);
return hexdump( std::string(obj) );
}
std::string timestamp();
}
#endif // __HEALHPP__