| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,121 @@ |
| 1 |
+#include <str.hpp> |
|
| 2 |
+ |
|
| 3 |
+#include <iostream> |
|
| 4 |
+#include <string> |
|
| 5 |
+#include <unordered_map> |
|
| 6 |
+#include <vector> |
|
| 7 |
+ |
|
| 8 |
+ |
|
| 9 |
+auto constexpr first = 1; |
|
| 10 |
+auto constexpr second = 2; |
|
| 11 |
+auto constexpr third = 3; |
|
| 12 |
+ |
|
| 13 |
+std::string static str_case_(int value) |
|
| 14 |
+{
|
|
| 15 |
+ switch(value) |
|
| 16 |
+ {
|
|
| 17 |
+ STR_CASE(first) |
|
| 18 |
+ STR_CASE(second) |
|
| 19 |
+ STR_CASE(third) |
|
| 20 |
+ default: |
|
| 21 |
+ return "unknown"; |
|
| 22 |
+ } |
|
| 23 |
+} |
|
| 24 |
+ |
|
| 25 |
+std::string static str_cond_(int value) |
|
| 26 |
+{
|
|
| 27 |
+ return |
|
| 28 |
+ STR_COND(value, first) |
|
| 29 |
+ STR_COND(value, second) |
|
| 30 |
+ STR_COND(value, third) |
|
| 31 |
+ "unknown"; |
|
| 32 |
+} |
|
| 33 |
+ |
|
| 34 |
+std::string static str_init_(int value) |
|
| 35 |
+{
|
|
| 36 |
+ auto str = std::unordered_map<int, std::string>{
|
|
| 37 |
+ STR_INIT(first) |
|
| 38 |
+ STR_INIT(second) |
|
| 39 |
+ STR_INIT(third) |
|
| 40 |
+ }[value]; |
|
| 41 |
+ if (str.empty()) |
|
| 42 |
+ return "unknown"; |
|
| 43 |
+ return str; |
|
| 44 |
+} |
|
| 45 |
+ |
|
| 46 |
+ |
|
| 47 |
+void static func_inner_() |
|
| 48 |
+{
|
|
| 49 |
+ try |
|
| 50 |
+ {
|
|
| 51 |
+ STR_THROW( |
|
| 52 |
+ "Expected " << "this" << ", " << |
|
| 53 |
+ "got " << "that" << "." |
|
| 54 |
+ ); |
|
| 55 |
+ } |
|
| 56 |
+ catch (...) |
|
| 57 |
+ {
|
|
| 58 |
+ STR_NESTED_THROW("Failed to do " << "inner" << ":");
|
|
| 59 |
+ } |
|
| 60 |
+} |
|
| 61 |
+ |
|
| 62 |
+void static func_outer_() |
|
| 63 |
+{
|
|
| 64 |
+ try |
|
| 65 |
+ {
|
|
| 66 |
+ func_inner_(); |
|
| 67 |
+ } |
|
| 68 |
+ catch (...) |
|
| 69 |
+ {
|
|
| 70 |
+ STR_NESTED_THROW("Failed to do " << "outer" << ":");
|
|
| 71 |
+ } |
|
| 72 |
+} |
|
| 73 |
+ |
|
| 74 |
+ |
|
| 75 |
+int main() |
|
| 76 |
+{
|
|
| 77 |
+ auto const value = 0x03; |
|
| 78 |
+ auto const values = std::vector<std::string>{"first", "", "third"};
|
|
| 79 |
+ std::cout << STR( std::hex << std::showbase << value) << std::endl; |
|
| 80 |
+ std::cout << STR_FUNC(std::hex << std::showbase << value)() << std::endl; |
|
| 81 |
+ std::cout << STR_JOIN(", ", it, "\"" << it << "\"", values) << std::endl;
|
|
| 82 |
+ std::cout << STR_JOIN_INIT(", ", it, it, int, {1, 2, 3}) << std::endl;
|
|
| 83 |
+ std::cout << STR_JOIN_INITSTR(", ", it, it, {
|
|
| 84 |
+ std::string{"first"},
|
|
| 85 |
+ "", |
|
| 86 |
+ "third", |
|
| 87 |
+ }) << std::endl; |
|
| 88 |
+ |
|
| 89 |
+ std::cout << std::endl; |
|
| 90 |
+ |
|
| 91 |
+ std::cout << str_case_(1) << std::endl; // Uses `STR_CASE`. |
|
| 92 |
+ std::cout << str_cond_(0) << std::endl; // Uses `STR_COND`. |
|
| 93 |
+ std::cout << str_init_(3) << std::endl; // Uses `STR_INIT`. |
|
| 94 |
+ |
|
| 95 |
+ std::cout << std::endl; |
|
| 96 |
+ |
|
| 97 |
+ std::cout << STR_HERE("Hello from " << "here") << std::endl;
|
|
| 98 |
+ |
|
| 99 |
+ std::cout << std::endl; |
|
| 100 |
+ |
|
| 101 |
+ try |
|
| 102 |
+ {
|
|
| 103 |
+ STR_THROW_ERRNO(); |
|
| 104 |
+ } |
|
| 105 |
+ catch (std::exception const & exception) |
|
| 106 |
+ {
|
|
| 107 |
+ std::cout << exception.what() << std::endl; |
|
| 108 |
+ } |
|
| 109 |
+ |
|
| 110 |
+ try |
|
| 111 |
+ {
|
|
| 112 |
+ func_outer_(); |
|
| 113 |
+ } |
|
| 114 |
+ catch (std::exception const & exception) |
|
| 115 |
+ {
|
|
| 116 |
+ // TODO(rcrnstn): There may be a bug in `clang-tidy` 13, check if |
|
| 117 |
+ // `STR_NESTED_WHAT` passes with a newer version. |
|
| 118 |
+ // NOLINTNEXTLINE |
|
| 119 |
+ std::cout << STR_NESTED_WHAT("\n", exception) << std::endl;
|
|
| 120 |
+ } |
|
| 121 |
+} |