Browse code

Add implementation

Robert Cranston authored on 18/05/2020 13:08:47
Showing 1 changed files
... ...
@@ -0,0 +1,120 @@
1
+#ifndef STR_HPP_
2
+#define STR_HPP_
3
+
4
+
5
+#include <cerrno>
6
+#include <exception>
7
+#include <functional>
8
+#include <initializer_list>
9
+#include <sstream>
10
+#include <stdexcept>
11
+#include <string>
12
+#include <system_error>
13
+
14
+
15
+#define STR(VALUE) \
16
+    (((std::ostringstream &)(std::ostringstream{} << VALUE)).str())
17
+
18
+#define STR_FUNC(VALUE) \
19
+    ([=]() { return STR(VALUE); })
20
+
21
+#define STR_JOIN(JOIN, ITERATOR, VALUE, ...) \
22
+    ([&]() \
23
+    { \
24
+        auto const & str_join_  = JOIN; \
25
+        auto         str_oss_   = std::ostringstream{}; \
26
+        auto         str_first_ = true; \
27
+        for (auto const & ITERATOR : __VA_ARGS__) \
28
+        { \
29
+            auto str_value_ = STR(VALUE); \
30
+            if (str_value_.empty()) \
31
+                continue; \
32
+            if (!str_first_) \
33
+                str_oss_ << str_join_; \
34
+            str_first_ = false; \
35
+            str_oss_ << str_value_; \
36
+        } \
37
+        return str_oss_.str(); \
38
+    }())
39
+
40
+#define STR_JOIN_INIT(JOIN, ITERATOR, VALUE, TYPE, ...) \
41
+    STR_JOIN(JOIN, ITERATOR, VALUE, std::initializer_list<TYPE>__VA_ARGS__)
42
+
43
+#define STR_JOIN_INITSTR(JOIN, ITERATOR, VALUE, ...) \
44
+    STR_JOIN_INIT(JOIN, ITERATOR, VALUE, std::string, __VA_ARGS__)
45
+
46
+
47
+#define STR_CASE(CASE) \
48
+    case (CASE): return (#CASE);
49
+
50
+#define STR_COND(VALUE, CASE) \
51
+    ((VALUE) == (CASE)) ? (#CASE) :
52
+
53
+#define STR_INIT(CASE) \
54
+    {(CASE), (#CASE)},
55
+
56
+#define STR_ARGS(ARG) \
57
+    ARG, #ARG
58
+
59
+#define STR_ARGS_PREFIX(PREFIX, ARG) \
60
+    PREFIX##ARG, #ARG
61
+
62
+
63
+#define STR_HERE(VALUE) \
64
+    STR(__FILE__ << ":" << __LINE__ << ":" << __func__ << ": " << VALUE)
65
+
66
+
67
+#ifndef STR_EXCEPTION
68
+    #define STR_EXCEPTION std::runtime_error
69
+#endif
70
+
71
+#define STR_THROW(VALUE) \
72
+    (throw STR_EXCEPTION{STR(VALUE)})
73
+
74
+#define STR_THROW_ERRNO() \
75
+    STR_THROW(std::generic_category().message(errno) << ".")
76
+
77
+#define STR_RETHROW(VALUE) \
78
+    try \
79
+    { \
80
+        throw; \
81
+    } \
82
+    catch (std::exception const & str_exception_) \
83
+    { \
84
+        STR_THROW(VALUE << str_exception_.what()); \
85
+    }
86
+
87
+#define STR_NESTED_THROW(VALUE) \
88
+    (std::throw_with_nested(STR_EXCEPTION{STR(VALUE)}))
89
+
90
+#define STR_NESTED_THROW_ERRNO() \
91
+    STR_NESTED_THROW(std::generic_category().message(errno) << ".")
92
+
93
+#define STR_NESTED_WHAT(JOIN, EXCEPTION) \
94
+    ([&]() \
95
+    { \
96
+        auto const & str_join_   = JOIN; \
97
+        auto         str_oss_    = std::ostringstream{}; \
98
+        auto         str_first_  = true; \
99
+        auto str_what_ = std::function<void(std::exception const &)>{}; \
100
+        str_what_ = [&](std::exception const & str_exception_) \
101
+        { \
102
+            if (!str_first_) \
103
+                str_oss_ << str_join_; \
104
+            str_first_ = false; \
105
+            str_oss_ << str_exception_.what(); \
106
+            try \
107
+            { \
108
+                std::rethrow_if_nested(str_exception_); \
109
+            } \
110
+            catch (std::exception const & str_exception_nested_) \
111
+            { \
112
+                str_what_(str_exception_nested_); \
113
+            } \
114
+        }; \
115
+        str_what_(EXCEPTION); \
116
+        return str_oss_.str(); \
117
+    }())
118
+
119
+
120
+#endif // STR_HPP_
Browse code

Add project

Robert Cranston authored on 23/02/2021 01:12:10
Showing 1 changed files
1 1
new file mode 100644