|
7 | 7 | #include "exceptions/malformed_url_exception.h" |
8 | 8 |
|
9 | 9 | namespace url_parser { |
| 10 | + |
| 11 | +struct explicit_bool { |
| 12 | + bool b = false; |
| 13 | + template <class T, std::enable_if_t<std::is_same_v<T, bool>, bool> = true> |
| 14 | + explicit_bool(T v) : b(v) {} |
| 15 | + explicit_bool(explicit_bool const&) noexcept = default; |
| 16 | + explicit_bool& operator=(explicit_bool const&) & noexcept = default; |
| 17 | + explicit_bool() noexcept = default; |
| 18 | + ~explicit_bool() noexcept = default; |
| 19 | + bool operator!() const { return !b; } |
| 20 | + explicit operator bool() const { return b; } |
| 21 | +}; |
| 22 | + |
| 23 | +struct explicit_int { |
| 24 | + int i = 0; |
| 25 | + template <class T, std::enable_if_t<std::is_same_v<T, int>, int> = true> |
| 26 | + explicit_int(T v) : i(v) {} |
| 27 | + explicit_int(explicit_int const&) noexcept = default; |
| 28 | + explicit_int& operator=(explicit_int const&) & noexcept = default; |
| 29 | + explicit_int() noexcept = default; |
| 30 | + ~explicit_int() noexcept = default; |
| 31 | + |
| 32 | + explicit operator int() const { return i; } |
| 33 | +}; |
10 | 34 | struct Url { |
11 | 35 | std::string protocol; |
12 | 36 | std::string host; |
13 | 37 | std::vector<std::string> pathParams; |
14 | | - std::unordered_map<std::string, std::variant<std::string, int, bool>> queries; |
| 38 | + std::unordered_map<std::string, std::variant<std::string, explicit_int, explicit_bool>> queries; |
15 | 39 |
|
16 | 40 | std::string GetProtocolAndHost() const { return protocol + "://" + host; } |
17 | 41 |
|
@@ -102,10 +126,10 @@ inline std::string FromUrl(const Url& url) { |
102 | 126 | std::string value_str; |
103 | 127 | if (std::holds_alternative<std::string>(value)) { |
104 | 128 | value_str = std::get<std::string>(value); |
105 | | - } else if (std::holds_alternative<int>(value)) { |
106 | | - value_str = std::to_string(std::get<int>(value)); |
107 | | - } else if (std::holds_alternative<bool>(value)) { |
108 | | - value_str = std::get<bool>(value) ? "true" : "false"; |
| 129 | + } else if (std::holds_alternative<explicit_int>(value)) { |
| 130 | + value_str = std::to_string(int(std::get<explicit_int>(value))); |
| 131 | + } else if (std::holds_alternative<explicit_bool>(value)) { |
| 132 | + value_str = std::get<explicit_bool>(value) ? "true" : "false"; |
109 | 133 | } |
110 | 134 | if (!query_string.empty()) { |
111 | 135 | query_string += "&"; |
|
0 commit comments