| 1 | #pragma once |
| 2 | |
| 3 | #include <array> |
| 4 | #include <iomanip> |
| 5 | #include <iterator> |
| 6 | #include <sstream> |
| 7 | #include <stdexcept> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | using namespace std::literals::string_literals; |
| 12 | |
| 13 | // Expose the bytes library globally |
| 14 | #include <bytes/bytes.h> |
| 15 | using namespace mlspp::bytes_ns; |
| 16 | |
| 17 | // Expose the compatibility library globally |
| 18 | #include <tls/compat.h> |
| 19 | namespace var = mlspp::tls::var; |
| 20 | namespace opt = mlspp::tls::opt; |
| 21 | |
| 22 | namespace mlspp { |
| 23 | |
| 24 | // Make variant equality work in the same way as optional equality, with |
| 25 | // automatic unwrapping. In other words |
| 26 | // |
| 27 | // v == T(x) <=> hold_alternative<T>(v) && get<T>(v) == x |
| 28 | // |
| 29 | // For consistency, we also define symmetric and negated version. In this |
| 30 | // house, we obey the symmetric law of equivalence relations! |
| 31 | template<typename T, typename... Ts> |
| 32 | bool |
| 33 | operator==(const var::variant<Ts...>& v, const T& t) |
| 34 | { |
| 35 | return var::visit( |
| 36 | [&](const auto& arg) { |
| 37 | using U = std::decay_t<decltype(arg)>; |
| 38 | if constexpr (std::is_same_v<U, T>) { |
| 39 | return arg == t; |
| 40 | } else { |
| 41 | return false; |
| 42 | } |
| 43 | }, |
| 44 | v); |
| 45 | } |
| 46 | |
| 47 | template<typename T, typename... Ts> |
| 48 | bool |
| 49 | operator==(const T& t, const var::variant<Ts...>& v) |
| 50 | { |
| 51 | return v == t; |
| 52 | } |
| 53 | |
| 54 | template<typename T, typename... Ts> |
| 55 | bool |
| 56 | operator!=(const var::variant<Ts...>& v, const T& t) |
| 57 | { |
| 58 | return !(v == t); |
| 59 | } |
| 60 | |
| 61 | template<typename T, typename... Ts> |
| 62 | bool |
| 63 | operator!=(const T& t, const var::variant<Ts...>& v) |
| 64 | { |
| 65 | return !(v == t); |
| 66 | } |
| 67 | |
| 68 | using epoch_t = uint64_t; |
| 69 | |
| 70 | /// |
| 71 | /// Get the current system clock time in the format MLS expects |
| 72 | /// |
| 73 | |
| 74 | uint64_t |
| 75 | seconds_since_epoch(); |
| 76 | |
| 77 | /// |
| 78 | /// Easy construction of overloaded lambdas |
| 79 | /// |
| 80 | |
| 81 | template<class... Ts> |
| 82 | struct overloaded : Ts... |
| 83 | { |
| 84 | using Ts::operator()...; |
| 85 | |
| 86 | // XXX(RLB) MSVC has a bug where it incorrectly computes the size of this |
| 87 | // type. Microsoft claims they have fixed it in the latest MSVC, and GitHub |
| 88 | // claims they are running a version with the fix. But in practice, we still |
| 89 | // hit it. Including this dummy variable is a work-around. |
| 90 | // |
| 91 | // https://developercommunity.visualstudio.com/t/runtime-stack-corruption-using-stdvisit/346200 |
| 92 | int dummy = 0; |
| 93 | }; |
| 94 | |
| 95 | // clang-format off |
| 96 | // XXX(RLB): For some reason, different versions of clang-format disagree on how |
| 97 | // this should be formatted. Probably because it's new syntax with C++17? |
| 98 | // Exempting it from clang-format for now. |
| 99 | template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; |
| 100 | // clang-format on |
| 101 | |
| 102 | /// |
| 103 | /// Auto-generate equality and inequality operators for TLS-serializable things |
| 104 | /// |
| 105 | |
| 106 | template<typename T> |
| 107 | inline typename std::enable_if<T::_tls_serializable, bool>::type |
| 108 | operator==(const T& lhs, const T& rhs) |
| 109 | { |
| 110 | return lhs._tls_fields_w() == rhs._tls_fields_w(); |
| 111 | } |
| 112 | |
| 113 | template<typename T> |
| 114 | inline typename std::enable_if<T::_tls_serializable, bool>::type |
| 115 | operator!=(const T& lhs, const T& rhs) |
| 116 | { |
| 117 | return lhs._tls_fields_w() != rhs._tls_fields_w(); |
| 118 | } |
| 119 | |
| 120 | /// |
| 121 | /// Error types |
| 122 | /// |
| 123 | |
| 124 | // The `using parent = X` / `using parent::parent` construction here |
| 125 | // imports the constructors of the parent. |
| 126 | |
| 127 | class NotImplementedError : public std::exception |
| 128 | { |
| 129 | public: |
| 130 | using parent = std::exception; |
| 131 | using parent::parent; |
| 132 | }; |
| 133 | |
| 134 | class ProtocolError : public std::runtime_error |
| 135 | { |
| 136 | public: |
| 137 | using parent = std::runtime_error; |
| 138 | using parent::parent; |
| 139 | }; |
| 140 | |
| 141 | class IncompatibleNodesError : public std::invalid_argument |
| 142 | { |
| 143 | public: |
| 144 | using parent = std::invalid_argument; |
| 145 | using parent::parent; |
| 146 | }; |
| 147 | |
| 148 | class InvalidParameterError : public std::invalid_argument |
| 149 | { |
| 150 | public: |
| 151 | using parent = std::invalid_argument; |
| 152 | using parent::parent; |
| 153 | }; |
| 154 | |
| 155 | class InvalidPathError : public std::invalid_argument |
| 156 | { |
| 157 | public: |
| 158 | using parent = std::invalid_argument; |
| 159 | using parent::parent; |
| 160 | }; |
| 161 | |
| 162 | class InvalidIndexError : public std::invalid_argument |
| 163 | { |
| 164 | public: |
| 165 | using parent = std::invalid_argument; |
| 166 | using parent::parent; |
| 167 | }; |
| 168 | |
| 169 | class InvalidMessageTypeError : public std::invalid_argument |
| 170 | { |
| 171 | public: |
| 172 | using parent = std::invalid_argument; |
| 173 | using parent::parent; |
| 174 | }; |
| 175 | |
| 176 | class MissingNodeError : public std::out_of_range |
| 177 | { |
| 178 | public: |
| 179 | using parent = std::out_of_range; |
| 180 | using parent::parent; |
| 181 | }; |
| 182 | |
| 183 | class MissingStateError : public std::out_of_range |
| 184 | { |
| 185 | public: |
| 186 | using parent = std::out_of_range; |
| 187 | using parent::parent; |
| 188 | }; |
| 189 | |
| 190 | // A slightly more elegant way to silence -Werror=unused-variable |
| 191 | template<typename T> |
| 192 | void |
| 193 | silence_unused(const T& val) |
| 194 | { |
| 195 | (void)val; |
| 196 | } |
| 197 | |
| 198 | namespace stdx { |
| 199 | |
| 200 | // XXX(RLB) This method takes any container in, but always puts the resuls in |
| 201 | // std::vector. The output could be made generic with a Rust-like syntax, |
| 202 | // defining a PendingTransform object that caches the inputs, with a template |
| 203 | // `collect()` method that puts them in an output container. Which makes the |
| 204 | // calling syntax as follows: |
| 205 | // |
| 206 | // auto out = stdx::transform(in, f).collect<Container>(); |
| 207 | // |
| 208 | // (You always need the explicit specialization, even if assigning it to an |
| 209 | // explicitly typed variable, because C++ won't infer return types.) |
| 210 | // |
| 211 | // Given that the above syntax is pretty chatty, and we never need anything |
| 212 | // other than vectors here anyway, I have left this as-is. |
| 213 | template<typename Value, typename Container, typename UnaryOperation> |
| 214 | std::vector<Value> |
| 215 | transform(const Container& c, const UnaryOperation& op) |
| 216 | { |
| 217 | auto out = std::vector<Value>{}; |
| 218 | auto ins = std::inserter(out, out.begin()); |
| 219 | std::transform(c.begin(), c.end(), ins, op); |
| 220 | return out; |
| 221 | } |
| 222 | |
| 223 | template<typename Container, typename UnaryPredicate> |
| 224 | bool |
| 225 | any_of(const Container& c, const UnaryPredicate& pred) |
| 226 | { |
| 227 | return std::any_of(c.begin(), c.end(), pred); |
| 228 | } |
| 229 | |
| 230 | template<typename Container, typename UnaryPredicate> |
| 231 | bool |
| 232 | all_of(const Container& c, const UnaryPredicate& pred) |
| 233 | { |
| 234 | return std::all_of(c.begin(), c.end(), pred); |
| 235 | } |
| 236 | |
| 237 | template<typename Container, typename UnaryPredicate> |
| 238 | auto |
| 239 | count_if(const Container& c, const UnaryPredicate& pred) |
| 240 | { |
| 241 | return std::count_if(c.begin(), c.end(), pred); |
| 242 | } |
| 243 | |
| 244 | template<typename Container, typename Value> |
| 245 | bool |
| 246 | contains(const Container& c, const Value& val) |
| 247 | { |
| 248 | return std::find(c.begin(), c.end(), val) != c.end(); |
| 249 | } |
| 250 | |
| 251 | template<typename Container, typename UnaryPredicate> |
| 252 | auto |
| 253 | find_if(Container& c, const UnaryPredicate& pred) |
| 254 | { |
| 255 | return std::find_if(c.begin(), c.end(), pred); |
| 256 | } |
| 257 | |
| 258 | template<typename Container, typename UnaryPredicate> |
| 259 | auto |
| 260 | find_if(const Container& c, const UnaryPredicate& pred) |
| 261 | { |
| 262 | return std::find_if(c.begin(), c.end(), pred); |
| 263 | } |
| 264 | |
| 265 | template<typename Container, typename Value> |
| 266 | auto |
| 267 | upper_bound(const Container& c, const Value& val) |
| 268 | { |
| 269 | return std::upper_bound(c.begin(), c.end(), val); |
| 270 | } |
| 271 | |
| 272 | } // namespace stdx |
| 273 | |
| 274 | } // namespace mlspp |