| 1 | #cxx . . $ `<file>/mlspp/include/mls/common.h`/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 stdcxx . . $ std/::literalscxx . . $ std/literals/::string_literals; |
| 12 | |
| 13 | // Expose the bytes library globally |
| 14 | #include <bytes/bytes.h>cxx . . $ `<file>/mlspp/lib/bytes/include/bytes/bytes.h`/ |
| 15 | using namespace mlsppcxx . . $ mlspp/::bytes_ns; |
| 16 | |
| 17 | // Expose the compatibility library globally |
| 18 | #include <tls/compat.h>cxx . . $ `<file>/mlspp/lib/bytes/include/tls/compat.h`/ |
| 19 | namespace var = mlsppcxx . . $ mlspp/::tlscxx . . $ mlspp/tls/::var; |
| 20 | namespace opt = mlsppcxx . . $ mlspp/::tlscxx . . $ mlspp/tls/::opt; |
| 21 | |
| 22 | namespace mlsppcxx . . $ 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 | operatorcxx . . $ mlspp/`operator==`(bd3c88d6bafb6558).==(const var::variantcxx . . $ std/variant#<Ts...>& v, const T& t) |
| 34 | { |
| 35 | return var::visitcxx . . $ std/visit(9828ab07ec69b3d0).( |
| 36 | [&](const auto& arg) { |
| 37 | using Ucxx . . $ mlspp/`operator==`(bd3c88d6bafb6558).$anonymous_type_0#`operator()`(4d19d6bcfcba9eda).U# = stdcxx . . $ std/::decay_t<decltype(arg)>; |
| 38 | if constexpr (stdcxx . . $ std/::is_same_v<Ucxx . . $ mlspp/`operator==`(bd3c88d6bafb6558).$anonymous_type_0#`operator()`(4d19d6bcfcba9eda).U#, T>) { |
| 39 | return arg ==cxx . . $ mlspp/`operator==`(bd3c88d6bafb6558). t; |
| 40 | } else { |
| 41 | return false; |
| 42 | } |
| 43 | }, |
| 44 | v); |
| 45 | } |
| 46 | |
| 47 | template<typename T, typename... Ts> |
| 48 | bool |
| 49 | operatorcxx . . $ mlspp/`operator==`(e3e2837c75589e75).==(const T& t, const var::variantcxx . . $ std/variant#<Ts...>& v) |
| 50 | { |
| 51 | return v ==cxx . . $ mlspp/`operator==`(bd3c88d6bafb6558). t; |
| 52 | } |
| 53 | |
| 54 | template<typename T, typename... Ts> |
| 55 | bool |
| 56 | operatorcxx . . $ mlspp/`operator!=`(bd3c88d6bafb6558).!=(const var::variantcxx . . $ std/variant#<Ts...>& v, const T& t) |
| 57 | { |
| 58 | return !(v ==cxx . . $ mlspp/`operator==`(bd3c88d6bafb6558). t); |
| 59 | } |
| 60 | |
| 61 | template<typename T, typename... Ts> |
| 62 | bool |
| 63 | operatorcxx . . $ mlspp/`operator!=`(e3e2837c75589e75).!=(const T& t, const var::variantcxx . . $ std/variant#<Ts...>& v) |
| 64 | { |
| 65 | return !(v ==cxx . . $ mlspp/`operator==`(bd3c88d6bafb6558). t); |
| 66 | } |
| 67 | |
| 68 | using epoch_tcxx . . $ mlspp/epoch_t# = uint64_tcxx . . $ uint64_t#; |
| 69 | |
| 70 | /// |
| 71 | /// Get the current system clock time in the format MLS expects |
| 72 | /// |
| 73 | |
| 74 | uint64_tcxx . . $ uint64_t# |
| 75 | seconds_since_epochcxx . . $ mlspp/seconds_since_epoch(f708dce9a8856c70).(); |
| 76 | |
| 77 | /// |
| 78 | /// Easy construction of overloaded lambdas |
| 79 | /// |
| 80 | |
| 81 | template<class... Ts> |
| 82 | struct overloadedcxx . . $ mlspp/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 dummycxx . . $ mlspp/overloaded#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> overloadedcxx . . $ mlspp/`<deduction guide for overloaded>`(89565262668af623).(Ts...) -> overloadedcxx . . $ mlspp/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 stdcxx . . $ std/::enable_if<T::_tls_serializable, bool>::type |
| 108 | operatorcxx . . $ mlspp/`operator==`(5401d31d8ac0aee4).==(const T& lhs, const T& rhs) |
| 109 | { |
| 110 | return lhs._tls_fields_w() ==cxx . . $ mlspp/`operator==`(5401d31d8ac0aee4). rhs._tls_fields_w(); |
| 111 | } |
| 112 | |
| 113 | template<typename T> |
| 114 | inline typename stdcxx . . $ std/::enable_if<T::_tls_serializable, bool>::type |
| 115 | operatorcxx . . $ mlspp/`operator!=`(5401d31d8ac0aee4).!=(const T& lhs, const T& rhs) |
| 116 | { |
| 117 | return lhs._tls_fields_w() !=cxx . . $ mlspp/`operator==`(bd3c88d6bafb6558). 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 NotImplementedErrorcxx . . $ mlspp/NotImplementedError# : public stdcxx . . $ std/::exception |
| 128 | { |
| 129 | public: |
| 130 | using parentcxx . . $ mlspp/NotImplementedError#parent# = stdcxx . . $ std/::exception; |
| 131 | using parentcxx . . $ mlspp/NotImplementedError#parent#::parentcxx . . $ mlspp/NotImplementedError#NotImplementedError(84ea931682164dc4).; |
| 132 | }; |
| 133 | |
| 134 | class ProtocolErrorcxx . . $ mlspp/ProtocolError# : public stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error# |
| 135 | { |
| 136 | public: |
| 137 | using parentcxx . . $ mlspp/ProtocolError#parent# = stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#; |
| 138 | using parentcxx . . $ mlspp/ProtocolError#parent#::parentcxx . . $ mlspp/ProtocolError#ProtocolError(85c52e162fed56f9).; |
| 139 | }; |
| 140 | |
| 141 | class IncompatibleNodesErrorcxx . . $ mlspp/IncompatibleNodesError# : public stdcxx . . $ std/::invalid_argumentcxx . . $ std/invalid_argument# |
| 142 | { |
| 143 | public: |
| 144 | using parentcxx . . $ mlspp/IncompatibleNodesError#parent# = stdcxx . . $ std/::invalid_argumentcxx . . $ std/invalid_argument#; |
| 145 | using parentcxx . . $ mlspp/IncompatibleNodesError#parent#::parentcxx . . $ std/invalid_argument#invalid_argument(e33ff35ad90e52a6).; |
| 146 | }; |
| 147 | |
| 148 | class InvalidParameterErrorcxx . . $ mlspp/InvalidParameterError# : public stdcxx . . $ std/::invalid_argumentcxx . . $ std/invalid_argument# |
| 149 | { |
| 150 | public: |
| 151 | using parentcxx . . $ mlspp/InvalidParameterError#parent# = stdcxx . . $ std/::invalid_argumentcxx . . $ std/invalid_argument#; |
| 152 | using parentcxx . . $ mlspp/InvalidParameterError#parent#::parentcxx . . $ mlspp/InvalidParameterError#InvalidParameterError(e33ff35ad90e52a6).; |
| 153 | }; |
| 154 | |
| 155 | class InvalidPathErrorcxx . . $ mlspp/InvalidPathError# : public stdcxx . . $ std/::invalid_argumentcxx . . $ std/invalid_argument# |
| 156 | { |
| 157 | public: |
| 158 | using parentcxx . . $ mlspp/InvalidPathError#parent# = stdcxx . . $ std/::invalid_argumentcxx . . $ std/invalid_argument#; |
| 159 | using parentcxx . . $ mlspp/InvalidPathError#parent#::parentcxx . . $ mlspp/InvalidPathError#InvalidPathError(9e1a5333fea6773e).; |
| 160 | }; |
| 161 | |
| 162 | class InvalidIndexErrorcxx . . $ mlspp/InvalidIndexError# : public stdcxx . . $ std/::invalid_argumentcxx . . $ std/invalid_argument# |
| 163 | { |
| 164 | public: |
| 165 | using parentcxx . . $ mlspp/InvalidIndexError#parent# = stdcxx . . $ std/::invalid_argumentcxx . . $ std/invalid_argument#; |
| 166 | using parentcxx . . $ mlspp/InvalidIndexError#parent#::parentcxx . . $ mlspp/InvalidIndexError#InvalidIndexError(e33ff35ad90e52a6).; |
| 167 | }; |
| 168 | |
| 169 | class InvalidMessageTypeErrorcxx . . $ mlspp/InvalidMessageTypeError# : public stdcxx . . $ std/::invalid_argumentcxx . . $ std/invalid_argument# |
| 170 | { |
| 171 | public: |
| 172 | using parentcxx . . $ mlspp/InvalidMessageTypeError#parent# = stdcxx . . $ std/::invalid_argumentcxx . . $ std/invalid_argument#; |
| 173 | using parentcxx . . $ mlspp/InvalidMessageTypeError#parent#::parentcxx . . $ mlspp/InvalidMessageTypeError#InvalidMessageTypeError(e33ff35ad90e52a6).; |
| 174 | }; |
| 175 | |
| 176 | class MissingNodeErrorcxx . . $ mlspp/MissingNodeError# : public stdcxx . . $ std/::out_of_rangecxx . . $ std/out_of_range# |
| 177 | { |
| 178 | public: |
| 179 | using parentcxx . . $ mlspp/MissingNodeError#parent# = stdcxx . . $ std/::out_of_rangecxx . . $ std/out_of_range#; |
| 180 | using parentcxx . . $ mlspp/MissingNodeError#parent#::parentcxx . . $ mlspp/MissingNodeError#parent#; |
| 181 | }; |
| 182 | |
| 183 | class MissingStateErrorcxx . . $ mlspp/MissingStateError# : public stdcxx . . $ std/::out_of_rangecxx . . $ std/out_of_range# |
| 184 | { |
| 185 | public: |
| 186 | using parentcxx . . $ mlspp/MissingStateError#parent# = stdcxx . . $ std/::out_of_rangecxx . . $ std/out_of_range#; |
| 187 | using parentcxx . . $ mlspp/MissingStateError#parent#::parentcxx . . $ mlspp/MissingStateError#MissingStateError(85c52e162fed56f9).; |
| 188 | }; |
| 189 | |
| 190 | // A slightly more elegant way to silence -Werror=unused-variable |
| 191 | template<typename T> |
| 192 | void |
| 193 | silence_unusedcxx . . $ mlspp/silence_unused(77d9f17553126ecb).(const T& val) |
| 194 | { |
| 195 | (void)val; |
| 196 | } |
| 197 | |
| 198 | namespace stdxcxx . . $ mlspp/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 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<Value> |
| 215 | transformcxx . . $ mlspp/stdx/transform(895a059819fd6266).(const Container& c, const UnaryOperation& op) |
| 216 | { |
| 217 | auto out = stdcxx . . $ std/::vectorcxx . . $ std/vector#<Value>{}; |
| 218 | auto ins = stdcxx . . $ std/::insertercxx . . $ std/inserter(cc902069f641a146).(out, out.begin()); |
| 219 | stdcxx . . $ std/::transformcxx . . $ std/transform(da30451c4a040264).(c.begin(), c.end(), ins, op); |
| 220 | return out; |
| 221 | } |
| 222 | |
| 223 | template<typename Container, typename UnaryPredicate> |
| 224 | bool |
| 225 | any_ofcxx . . $ mlspp/stdx/any_of(acb94948942028a1).(const Container& c, const UnaryPredicate& pred) |
| 226 | { |
| 227 | return stdcxx . . $ std/::any_ofcxx . . $ std/any_of(4baa325cfe498248).(c.begin(), c.end(), pred); |
| 228 | } |
| 229 | |
| 230 | template<typename Container, typename UnaryPredicate> |
| 231 | bool |
| 232 | all_ofcxx . . $ mlspp/stdx/all_of(acb94948942028a1).(const Container& c, const UnaryPredicate& pred) |
| 233 | { |
| 234 | return stdcxx . . $ std/::all_ofcxx . . $ std/all_of(4baa325cfe498248).(c.begin(), c.end(), pred); |
| 235 | } |
| 236 | |
| 237 | template<typename Container, typename UnaryPredicate> |
| 238 | auto |
| 239 | count_ifcxx . . $ mlspp/stdx/count_if(91f28ad055782622).(const Container& c, const UnaryPredicate& pred) |
| 240 | { |
| 241 | return stdcxx . . $ std/::count_ifcxx . . $ std/count_if(1fd41afbdb0d1217).(c.begin(), c.end(), pred); |
| 242 | } |
| 243 | |
| 244 | template<typename Container, typename Value> |
| 245 | bool |
| 246 | containscxx . . $ mlspp/stdx/contains(acb94948942028a1).(const Container& c, const Value& val) |
| 247 | { |
| 248 | return stdcxx . . $ std/::findcxx . . $ std/find(2814d77e25a3b3d7).(c.begin(), c.end(), val) !=cxx . . $ mlspp/`operator==`(e3e2837c75589e75). c.end(); |
| 249 | } |
| 250 | |
| 251 | template<typename Container, typename UnaryPredicate> |
| 252 | auto |
| 253 | find_ifcxx . . $ mlspp/stdx/find_if(8bc5600e82dc1b7c).(Container& c, const UnaryPredicate& pred) |
| 254 | { |
| 255 | return stdcxx . . $ std/::find_ifcxx . . $ std/find_if(f0a0a4f94ee5cb00).(c.begin(), c.end(), pred); |
| 256 | } |
| 257 | |
| 258 | template<typename Container, typename UnaryPredicate> |
| 259 | auto |
| 260 | find_ifcxx . . $ mlspp/stdx/find_if(91f28ad055782622).(const Container& c, const UnaryPredicate& pred) |
| 261 | { |
| 262 | return stdcxx . . $ std/::find_ifcxx . . $ std/find_if(809f9eabc7de2822).(c.begin(), c.end(), pred); |
| 263 | } |
| 264 | |
| 265 | template<typename Container, typename Value> |
| 266 | auto |
| 267 | upper_boundcxx . . $ mlspp/stdx/upper_bound(91f28ad055782622).(const Container& c, const Value& val) |
| 268 | { |
| 269 | return stdcxx . . $ std/::upper_boundcxx . . $ std/upper_bound(a92da12e15934614).(c.begin(), c.end(), val); |
| 270 | } |
| 271 | |
| 272 | } // namespace stdx |
| 273 | |
| 274 | } // namespace mlspp |