Browse code

Add str.hpp

Robert Cranston authored on 16/05/2020 11:47:34
Showing 2 changed files

... ...
@@ -4,6 +4,14 @@ A C++11 helper for [OpenGL][] >=2.0 [shaders][Shader].
4 4
 
5 5
 [OpenGL]: https://www.opengl.org
6 6
 [Shader]: https://www.khronos.org/opengl/wiki/Shader
7
+[`std::runtime_error`]: https://en.cppreference.com/w/cpp/error/runtime_error
8
+[`what()`]: https://en.cppreference.com/w/cpp/error/exception/what
9
+
10
+## Dependencies
11
+
12
+[`str.hpp`][] is bundled.
13
+
14
+[`str.hpp`]: https://git.rcrnstn.net/rcrnstn/str.hpp
7 15
 
8 16
 ## License
9 17
 
10 18
new file mode 100644
... ...
@@ -0,0 +1,39 @@
1
+#ifndef STR_HPP
2
+#define STR_HPP
3
+
4
+
5
+#include <sstream>
6
+
7
+
8
+#define STR(VALUES) \
9
+    ((std::ostringstream &)(std::ostringstream() << VALUES)).str()
10
+
11
+#define STR_JOIN(JOIN, IT, ITERABLE) \
12
+    [&] { \
13
+        auto const & iterable = ITERABLE; \
14
+        auto const & join = JOIN; \
15
+        std::ostringstream oss; \
16
+        auto first = true; \
17
+        for (auto & it : iterable) { \
18
+            if (!first) \
19
+                oss << join; \
20
+            oss << IT; \
21
+            first = false; \
22
+        } \
23
+        return oss.str(); \
24
+    }()
25
+
26
+#define STR_CASE(CASE) \
27
+    case CASE: return #CASE;
28
+
29
+#define STR_COND(VALUE, CASE) \
30
+    (VALUE) == (CASE) ? #CASE :
31
+
32
+#define STR_INGIFY1(X) #X
33
+#define STR_INGIFY2(X) STR_INGIFY1(X)
34
+
35
+#define STR_HERE(VALUES) \
36
+    STR(__FILE__ << ":" << STR_INGIFY2(__LINE__) ": " << VALUES)
37
+
38
+
39
+#endif // STR_HPP