[e679283] | 1 | /*
|
---|
| 2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #ifndef LIBCPP_FUNCTIONAL
|
---|
| 30 | #define LIBCPP_FUNCTIONAL
|
---|
| 31 |
|
---|
[22ba300] | 32 | #include <limits>
|
---|
[9c00022] | 33 | #include <memory>
|
---|
| 34 | #include <typeinfo>
|
---|
[e679283] | 35 | #include <type_traits>
|
---|
[22ba300] | 36 | #include <utility>
|
---|
[9f77d98] | 37 |
|
---|
[e679283] | 38 | namespace std
|
---|
| 39 | {
|
---|
| 40 | namespace aux
|
---|
| 41 | {
|
---|
| 42 | /**
|
---|
| 43 | * 20.9.2, requirements:
|
---|
| 44 | */
|
---|
| 45 | template<class R, class T, class T1, class... Ts>
|
---|
[22ba300] | 46 | decltype(auto) invoke(R T::* f, T1&& t1, Ts&&... args)
|
---|
[e679283] | 47 | {
|
---|
| 48 | if constexpr (is_member_function_pointer_v<decltype(f)>)
|
---|
| 49 | {
|
---|
| 50 | if constexpr (is_base_of_v<T, remove_reference_t<T1>>)
|
---|
| 51 | // (1.1)
|
---|
[22ba300] | 52 | return (t1.*f)(forward<Ts>(args)...);
|
---|
[e679283] | 53 | else
|
---|
| 54 | // (1.2)
|
---|
[22ba300] | 55 | return ((*t1).*f)(forward<Ts>(args)...);
|
---|
[e679283] | 56 | }
|
---|
[22ba300] | 57 | else if constexpr (is_member_object_pointer_v<decltype(f)> && sizeof...(args) == 0)
|
---|
[e679283] | 58 | {
|
---|
| 59 | /**
|
---|
| 60 | * Note: Standard requires to N be equal to 1, but we take t1 directly
|
---|
| 61 | * so we need sizeof...(args) to be 0.
|
---|
| 62 | */
|
---|
| 63 | if constexpr (is_base_of_v<T, remove_reference_t<T1>>)
|
---|
| 64 | // (1.3)
|
---|
| 65 | return t1.*f;
|
---|
| 66 | else
|
---|
| 67 | // (1.4)
|
---|
| 68 | return (*t1).*f;
|
---|
| 69 | }
|
---|
[22ba300] | 70 |
|
---|
| 71 | /**
|
---|
| 72 | * Note: If this condition holds this will not be reachable,
|
---|
| 73 | * but a new addition to the standard (17.7 point (8.1))
|
---|
| 74 | * prohibits us from simply using false as the condition here,
|
---|
| 75 | * so we use this because we know it is false here.
|
---|
| 76 | */
|
---|
| 77 | static_assert(is_member_function_pointer_v<decltype(f)>, "invalid invoke");
|
---|
[e679283] | 78 | }
|
---|
| 79 |
|
---|
| 80 | template<class F, class... Args>
|
---|
| 81 | decltype(auto) invoke(F&& f, Args&&... args)
|
---|
| 82 | {
|
---|
| 83 | // (1.5)
|
---|
| 84 | return f(forward<Args>(args)...);
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | * 20.9.3, invoke:
|
---|
| 90 | */
|
---|
| 91 |
|
---|
| 92 | template<class F, class... Args>
|
---|
| 93 | result_of_t<F&&(Args&&...)> invoke(F&& f, Args&&... args)
|
---|
| 94 | {
|
---|
| 95 | return aux::invoke(forward<F>(f)(forward<Args>(args)...));
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | * 20.9.4, reference_wrapper:
|
---|
| 100 | */
|
---|
| 101 |
|
---|
| 102 | template<class T>
|
---|
[65dde99] | 103 | class reference_wrapper
|
---|
| 104 | {
|
---|
| 105 | public:
|
---|
| 106 | using type = T;
|
---|
| 107 | // TODO: conditional typedefs
|
---|
| 108 |
|
---|
| 109 | reference_wrapper(type& val) noexcept
|
---|
| 110 | : data_{&val}
|
---|
| 111 | { /* DUMMY BODY */ }
|
---|
| 112 |
|
---|
| 113 | reference_wrapper(type&&) = delete;
|
---|
| 114 |
|
---|
| 115 | reference_wrapper(const reference_wrapper& other) noexcept
|
---|
| 116 | : data_{other.data_}
|
---|
| 117 | { /* DUMMY BODY */ }
|
---|
| 118 |
|
---|
| 119 | reference_wrapper& operator=(const reference_wrapper& other) noexcept
|
---|
| 120 | {
|
---|
| 121 | data_ = other.data_;
|
---|
| 122 |
|
---|
| 123 | return *this;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | operator type&() const noexcept
|
---|
| 127 | {
|
---|
| 128 | return *data_;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | type& get() const noexcept
|
---|
| 132 | {
|
---|
| 133 | return *data_;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | template<class... Args>
|
---|
| 137 | result_of_t<type&(Args&&...)> operator()(Args&&... args) const
|
---|
| 138 | {
|
---|
| 139 | return invoke(*data_, std::forward<Args>(args)...);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | private:
|
---|
| 143 | type* data_;
|
---|
| 144 | };
|
---|
[e679283] | 145 |
|
---|
| 146 | template<class T>
|
---|
[65dde99] | 147 | reference_wrapper<T> ref(T& t) noexcept
|
---|
| 148 | {
|
---|
| 149 | return reference_wrapper<T>{t};
|
---|
| 150 | }
|
---|
[e679283] | 151 |
|
---|
| 152 | template<class T>
|
---|
[65dde99] | 153 | reference_wrapper<const T> cref(const T& t) noexcept
|
---|
| 154 | {
|
---|
| 155 | return reference_wrapper<const T>{t};
|
---|
| 156 | }
|
---|
[e679283] | 157 |
|
---|
| 158 | template<class T>
|
---|
| 159 | void ref(const T&&) = delete;
|
---|
| 160 |
|
---|
| 161 | template<class T>
|
---|
| 162 | void cref(const T&&) = delete;
|
---|
| 163 |
|
---|
| 164 | template<class T>
|
---|
[65dde99] | 165 | reference_wrapper<T> ref(reference_wrapper<T> t) noexcept
|
---|
| 166 | {
|
---|
| 167 | return ref(t.get());
|
---|
| 168 | }
|
---|
[e679283] | 169 |
|
---|
| 170 | template<class T>
|
---|
[65dde99] | 171 | reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept
|
---|
| 172 | {
|
---|
| 173 | return cref(t.get());
|
---|
| 174 | }
|
---|
[e679283] | 175 |
|
---|
| 176 | /**
|
---|
| 177 | * 20.9.5, arithmetic operations:
|
---|
| 178 | */
|
---|
| 179 |
|
---|
| 180 | template<class T = void>
|
---|
| 181 | struct plus
|
---|
| 182 | {
|
---|
| 183 | constexpr T operator()(const T& lhs, const T& rhs) const
|
---|
| 184 | {
|
---|
| 185 | return lhs + rhs;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | using first_argument_type = T;
|
---|
| 189 | using second_argument_type = T;
|
---|
| 190 | using result_type = T;
|
---|
| 191 | };
|
---|
| 192 |
|
---|
| 193 | template<class T = void>
|
---|
| 194 | struct minus
|
---|
| 195 | {
|
---|
| 196 | constexpr T operator()(const T& lhs, const T& rhs) const
|
---|
| 197 | {
|
---|
| 198 | return lhs - rhs;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | using first_argument_type = T;
|
---|
| 202 | using second_argument_type = T;
|
---|
| 203 | using result_type = T;
|
---|
| 204 | };
|
---|
| 205 |
|
---|
| 206 | template<class T = void>
|
---|
| 207 | struct multiplies
|
---|
| 208 | {
|
---|
| 209 | constexpr T operator()(const T& lhs, const T& rhs) const
|
---|
| 210 | {
|
---|
| 211 | return lhs * rhs;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | using first_argument_type = T;
|
---|
| 215 | using second_argument_type = T;
|
---|
| 216 | using result_type = T;
|
---|
| 217 | };
|
---|
| 218 |
|
---|
| 219 | template<class T = void>
|
---|
| 220 | struct divides
|
---|
| 221 | {
|
---|
| 222 | constexpr T operator()(const T& lhs, const T& rhs) const
|
---|
| 223 | {
|
---|
| 224 | return lhs / rhs;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | using first_argument_type = T;
|
---|
| 228 | using second_argument_type = T;
|
---|
| 229 | using result_type = T;
|
---|
| 230 | };
|
---|
| 231 |
|
---|
| 232 | template<class T = void>
|
---|
| 233 | struct modulus
|
---|
| 234 | {
|
---|
| 235 | constexpr T operator()(const T& lhs, const T& rhs) const
|
---|
| 236 | {
|
---|
| 237 | return lhs % rhs;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | using first_argument_type = T;
|
---|
| 241 | using second_argument_type = T;
|
---|
| 242 | using result_type = T;
|
---|
| 243 | };
|
---|
| 244 |
|
---|
| 245 | template<class T = void>
|
---|
| 246 | struct negate
|
---|
| 247 | {
|
---|
| 248 | constexpr T operator()(const T& x) const
|
---|
| 249 | {
|
---|
| 250 | return -x;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | using argument_type = T;
|
---|
| 254 | using result_type = T;
|
---|
| 255 | };
|
---|
| 256 |
|
---|
| 257 | namespace aux
|
---|
| 258 | {
|
---|
| 259 | /**
|
---|
| 260 | * Used by some functions like std::set::find to determine
|
---|
| 261 | * whether a functor is transparent.
|
---|
| 262 | */
|
---|
| 263 | struct transparent_t;
|
---|
[8830faa] | 264 |
|
---|
| 265 | template<class T, class = void>
|
---|
| 266 | struct is_transparent: false_type
|
---|
| 267 | { /* DUMMY BODY */ };
|
---|
| 268 |
|
---|
| 269 | template<class T>
|
---|
| 270 | struct is_transparent<T, void_t<typename T::is_transparent>>
|
---|
| 271 | : true_type
|
---|
| 272 | { /* DUMMY BODY */ };
|
---|
| 273 |
|
---|
| 274 | template<class T>
|
---|
| 275 | inline constexpr bool is_transparent_v = is_transparent<T>::value;
|
---|
[e679283] | 276 | }
|
---|
| 277 |
|
---|
| 278 | template<>
|
---|
| 279 | struct plus<void>
|
---|
| 280 | {
|
---|
| 281 | template<class T, class U>
|
---|
[22ba300] | 282 | constexpr auto operator()(T&& lhs, U&& rhs) const
|
---|
[e679283] | 283 | -> decltype(forward<T>(lhs) + forward<U>(rhs))
|
---|
| 284 | {
|
---|
| 285 | return forward<T>(lhs) + forward<T>(rhs);
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | using is_transparent = aux::transparent_t;
|
---|
| 289 | };
|
---|
| 290 |
|
---|
| 291 | template<>
|
---|
| 292 | struct minus<void>
|
---|
| 293 | {
|
---|
| 294 | template<class T, class U>
|
---|
[22ba300] | 295 | constexpr auto operator()(T&& lhs, U&& rhs) const
|
---|
[e679283] | 296 | -> decltype(forward<T>(lhs) - forward<U>(rhs))
|
---|
| 297 | {
|
---|
| 298 | return forward<T>(lhs) - forward<T>(rhs);
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | using is_transparent = aux::transparent_t;
|
---|
| 302 | };
|
---|
| 303 |
|
---|
| 304 | template<>
|
---|
| 305 | struct multiplies<void>
|
---|
| 306 | {
|
---|
| 307 | template<class T, class U>
|
---|
[22ba300] | 308 | constexpr auto operator()(T&& lhs, U&& rhs) const
|
---|
[e679283] | 309 | -> decltype(forward<T>(lhs) * forward<U>(rhs))
|
---|
| 310 | {
|
---|
| 311 | return forward<T>(lhs) * forward<T>(rhs);
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | using is_transparent = aux::transparent_t;
|
---|
| 315 | };
|
---|
| 316 |
|
---|
| 317 | template<>
|
---|
| 318 | struct divides<void>
|
---|
| 319 | {
|
---|
| 320 | template<class T, class U>
|
---|
[22ba300] | 321 | constexpr auto operator()(T&& lhs, U&& rhs) const
|
---|
[e679283] | 322 | -> decltype(forward<T>(lhs) / forward<U>(rhs))
|
---|
| 323 | {
|
---|
| 324 | return forward<T>(lhs) / forward<T>(rhs);
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | using is_transparent = aux::transparent_t;
|
---|
| 328 | };
|
---|
| 329 |
|
---|
| 330 | template<>
|
---|
| 331 | struct modulus<void>
|
---|
| 332 | {
|
---|
| 333 | template<class T, class U>
|
---|
[22ba300] | 334 | constexpr auto operator()(T&& lhs, U&& rhs) const
|
---|
[e679283] | 335 | -> decltype(forward<T>(lhs) % forward<U>(rhs))
|
---|
| 336 | {
|
---|
| 337 | return forward<T>(lhs) % forward<T>(rhs);
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | using is_transparent = aux::transparent_t;
|
---|
| 341 | };
|
---|
| 342 |
|
---|
| 343 | template<>
|
---|
| 344 | struct negate<void>
|
---|
| 345 | {
|
---|
| 346 | template<class T>
|
---|
[22ba300] | 347 | constexpr auto operator()(T&& x) const
|
---|
[e679283] | 348 | -> decltype(-forward<T>(x))
|
---|
| 349 | {
|
---|
| 350 | return -forward<T>(x);
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | using is_transparent = aux::transparent_t;
|
---|
| 354 | };
|
---|
| 355 |
|
---|
| 356 | /**
|
---|
| 357 | * 20.9.6, comparisons:
|
---|
| 358 | */
|
---|
| 359 |
|
---|
| 360 | template<class T = void>
|
---|
[22ba300] | 361 | struct equal_to
|
---|
[e679283] | 362 | {
|
---|
| 363 | constexpr bool operator()(const T& lhs, const T& rhs) const
|
---|
| 364 | {
|
---|
| 365 | return lhs == rhs;
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | using first_argument_type = T;
|
---|
| 369 | using second_argument_type = T;
|
---|
| 370 | using result_type = bool;
|
---|
| 371 | };
|
---|
| 372 |
|
---|
| 373 | template<class T = void>
|
---|
| 374 | struct not_equal_to
|
---|
| 375 | {
|
---|
| 376 | constexpr bool operator()(const T& lhs, const T& rhs) const
|
---|
| 377 | {
|
---|
| 378 | return lhs != rhs;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | using first_argument_type = T;
|
---|
| 382 | using second_argument_type = T;
|
---|
| 383 | using result_type = bool;
|
---|
| 384 | };
|
---|
| 385 |
|
---|
| 386 | template<class T = void>
|
---|
| 387 | struct greater
|
---|
| 388 | {
|
---|
| 389 | constexpr bool operator()(const T& lhs, const T& rhs) const
|
---|
| 390 | {
|
---|
| 391 | return lhs > rhs;
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | using first_argument_type = T;
|
---|
| 395 | using second_argument_type = T;
|
---|
| 396 | using result_type = bool;
|
---|
| 397 | };
|
---|
| 398 |
|
---|
| 399 | template<class T = void>
|
---|
| 400 | struct less
|
---|
| 401 | {
|
---|
| 402 | constexpr bool operator()(const T& lhs, const T& rhs) const
|
---|
| 403 | {
|
---|
| 404 | return lhs < rhs;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | using first_argument_type = T;
|
---|
| 408 | using second_argument_type = T;
|
---|
| 409 | using result_type = bool;
|
---|
| 410 | };
|
---|
| 411 |
|
---|
| 412 | template<class T = void>
|
---|
| 413 | struct greater_equal
|
---|
| 414 | {
|
---|
| 415 | constexpr bool operator()(const T& lhs, const T& rhs) const
|
---|
| 416 | {
|
---|
| 417 | return lhs >= rhs;
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | using first_argument_type = T;
|
---|
| 421 | using second_argument_type = T;
|
---|
| 422 | using result_type = bool;
|
---|
| 423 | };
|
---|
| 424 |
|
---|
| 425 | template<class T = void>
|
---|
| 426 | struct less_equal
|
---|
| 427 | {
|
---|
| 428 | constexpr bool operator()(const T& lhs, const T& rhs) const
|
---|
| 429 | {
|
---|
| 430 | return lhs <= rhs;
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | using first_argument_type = T;
|
---|
| 434 | using second_argument_type = T;
|
---|
| 435 | using result_type = bool;
|
---|
| 436 | };
|
---|
| 437 |
|
---|
| 438 | template<>
|
---|
| 439 | struct equal_to<void>
|
---|
| 440 | {
|
---|
| 441 | template<class T, class U>
|
---|
[22ba300] | 442 | constexpr auto operator()(T&& lhs, U&& rhs) const
|
---|
[e679283] | 443 | -> decltype(forward<T>(lhs) == forward<U>(rhs))
|
---|
| 444 | {
|
---|
| 445 | return forward<T>(lhs) == forward<U>(rhs);
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 | using is_transparent = aux::transparent_t;
|
---|
| 449 | };
|
---|
| 450 |
|
---|
| 451 | template<>
|
---|
| 452 | struct not_equal_to<void>
|
---|
| 453 | {
|
---|
| 454 | template<class T, class U>
|
---|
[22ba300] | 455 | constexpr auto operator()(T&& lhs, U&& rhs) const
|
---|
[e679283] | 456 | -> decltype(forward<T>(lhs) != forward<U>(rhs))
|
---|
| 457 | {
|
---|
| 458 | return forward<T>(lhs) != forward<U>(rhs);
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | using is_transparent = aux::transparent_t;
|
---|
| 462 | };
|
---|
| 463 |
|
---|
| 464 | template<>
|
---|
| 465 | struct greater<void>
|
---|
| 466 | {
|
---|
| 467 | template<class T, class U>
|
---|
[22ba300] | 468 | constexpr auto operator()(T&& lhs, U&& rhs) const
|
---|
[e679283] | 469 | -> decltype(forward<T>(lhs) > forward<U>(rhs))
|
---|
| 470 | {
|
---|
| 471 | return forward<T>(lhs) > forward<U>(rhs);
|
---|
| 472 | }
|
---|
| 473 |
|
---|
| 474 | using is_transparent = aux::transparent_t;
|
---|
| 475 | };
|
---|
| 476 |
|
---|
| 477 | template<>
|
---|
| 478 | struct less<void>
|
---|
| 479 | {
|
---|
| 480 | template<class T, class U>
|
---|
[22ba300] | 481 | constexpr auto operator()(T&& lhs, U&& rhs) const
|
---|
[e679283] | 482 | -> decltype(forward<T>(lhs) < forward<U>(rhs))
|
---|
| 483 | {
|
---|
| 484 | return forward<T>(lhs) < forward<U>(rhs);
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | using is_transparent = aux::transparent_t;
|
---|
| 488 | };
|
---|
| 489 |
|
---|
| 490 | template<>
|
---|
| 491 | struct greater_equal<void>
|
---|
| 492 | {
|
---|
| 493 | template<class T, class U>
|
---|
[22ba300] | 494 | constexpr auto operator()(T&& lhs, U&& rhs) const
|
---|
[e679283] | 495 | -> decltype(forward<T>(lhs) >= forward<U>(rhs))
|
---|
| 496 | {
|
---|
| 497 | return forward<T>(lhs) >= forward<U>(rhs);
|
---|
| 498 | }
|
---|
| 499 |
|
---|
| 500 | using is_transparent = aux::transparent_t;
|
---|
| 501 | };
|
---|
| 502 |
|
---|
| 503 | template<>
|
---|
| 504 | struct less_equal<void>
|
---|
| 505 | {
|
---|
| 506 | template<class T, class U>
|
---|
[22ba300] | 507 | constexpr auto operator()(T&& lhs, U&& rhs) const
|
---|
[e679283] | 508 | -> decltype(forward<T>(lhs) <= forward<U>(rhs))
|
---|
| 509 | {
|
---|
| 510 | return forward<T>(lhs) <= forward<U>(rhs);
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | using is_transparent = aux::transparent_t;
|
---|
| 514 | };
|
---|
| 515 |
|
---|
| 516 | /**
|
---|
| 517 | * 20.9.7, logical operations:
|
---|
| 518 | */
|
---|
| 519 |
|
---|
| 520 | template<class T = void>
|
---|
| 521 | struct logical_and
|
---|
| 522 | {
|
---|
| 523 | constexpr bool operator()(const T& lhs, const T& rhs) const
|
---|
| 524 | {
|
---|
| 525 | return lhs && rhs;
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | using first_argument_type = T;
|
---|
| 529 | using second_argument_type = T;
|
---|
| 530 | using result_type = bool;
|
---|
| 531 | };
|
---|
| 532 |
|
---|
| 533 | template<class T = void>
|
---|
| 534 | struct logical_or
|
---|
| 535 | {
|
---|
| 536 | constexpr bool operator()(const T& lhs, const T& rhs) const
|
---|
| 537 | {
|
---|
| 538 | return lhs || rhs;
|
---|
| 539 | }
|
---|
| 540 |
|
---|
| 541 | using first_argument_type = T;
|
---|
| 542 | using second_argument_type = T;
|
---|
| 543 | using result_type = bool;
|
---|
| 544 | };
|
---|
| 545 |
|
---|
| 546 | template<class T = void>
|
---|
| 547 | struct logical_not
|
---|
| 548 | {
|
---|
| 549 | constexpr bool operator()(const T& x) const
|
---|
| 550 | {
|
---|
| 551 | return !x;
|
---|
| 552 | }
|
---|
| 553 |
|
---|
| 554 | using argument_type = T;
|
---|
| 555 | using result_type = bool;
|
---|
| 556 | };
|
---|
| 557 |
|
---|
| 558 | template<>
|
---|
| 559 | struct logical_and<void>
|
---|
| 560 | {
|
---|
| 561 | template<class T, class U>
|
---|
[22ba300] | 562 | constexpr auto operator()(T&& lhs, U&& rhs) const
|
---|
[e679283] | 563 | -> decltype(forward<T>(lhs) && forward<U>(rhs))
|
---|
| 564 | {
|
---|
| 565 | return forward<T>(lhs) && forward<U>(rhs);
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 | using is_transparent = aux::transparent_t;
|
---|
| 569 | };
|
---|
| 570 |
|
---|
| 571 | template<>
|
---|
| 572 | struct logical_or<void>
|
---|
| 573 | {
|
---|
| 574 | template<class T, class U>
|
---|
[22ba300] | 575 | constexpr auto operator()(T&& lhs, U&& rhs) const
|
---|
[e679283] | 576 | -> decltype(forward<T>(lhs) || forward<U>(rhs))
|
---|
| 577 | {
|
---|
| 578 | return forward<T>(lhs) || forward<U>(rhs);
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 | using is_transparent = aux::transparent_t;
|
---|
| 582 | };
|
---|
| 583 |
|
---|
| 584 | template<>
|
---|
| 585 | struct logical_not<void>
|
---|
| 586 | {
|
---|
| 587 | template<class T>
|
---|
[22ba300] | 588 | constexpr auto operator()(T&& x) const
|
---|
[e679283] | 589 | -> decltype(!forward<T>(x))
|
---|
| 590 | {
|
---|
[22ba300] | 591 | return !forward<T>(x);
|
---|
[e679283] | 592 | }
|
---|
| 593 |
|
---|
| 594 | using is_transparent = aux::transparent_t;
|
---|
| 595 | };
|
---|
| 596 |
|
---|
| 597 | /**
|
---|
| 598 | * 20.9.8, bitwise operations:
|
---|
| 599 | */
|
---|
| 600 |
|
---|
| 601 | template<class T = void>
|
---|
| 602 | struct bit_and
|
---|
| 603 | {
|
---|
| 604 | constexpr T operator()(const T& lhs, const T& rhs) const
|
---|
| 605 | {
|
---|
| 606 | return lhs & rhs;
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | using first_argument_type = T;
|
---|
| 610 | using second_argument_type = T;
|
---|
| 611 | using result_type = T;
|
---|
| 612 | };
|
---|
| 613 |
|
---|
| 614 | template<class T = void>
|
---|
| 615 | struct bit_or
|
---|
| 616 | {
|
---|
| 617 | constexpr T operator()(const T& lhs, const T& rhs) const
|
---|
| 618 | {
|
---|
| 619 | return lhs | rhs;
|
---|
| 620 | }
|
---|
| 621 |
|
---|
| 622 | using first_argument_type = T;
|
---|
| 623 | using second_argument_type = T;
|
---|
| 624 | using result_type = T;
|
---|
| 625 | };
|
---|
| 626 |
|
---|
| 627 | template<class T = void>
|
---|
| 628 | struct bit_xor
|
---|
| 629 | {
|
---|
| 630 | constexpr T operator()(const T& lhs, const T& rhs) const
|
---|
| 631 | {
|
---|
| 632 | return lhs ^ rhs;
|
---|
| 633 | }
|
---|
| 634 |
|
---|
| 635 | using first_argument_type = T;
|
---|
| 636 | using second_argument_type = T;
|
---|
| 637 | using result_type = T;
|
---|
| 638 | };
|
---|
| 639 |
|
---|
| 640 | template<class T = void>
|
---|
| 641 | struct bit_not
|
---|
| 642 | {
|
---|
| 643 | constexpr bool operator()(const T& x) const
|
---|
| 644 | {
|
---|
| 645 | return ~x;
|
---|
| 646 | }
|
---|
| 647 |
|
---|
| 648 | using argument_type = T;
|
---|
| 649 | using result_type = T;
|
---|
| 650 | };
|
---|
| 651 |
|
---|
| 652 | template<>
|
---|
| 653 | struct bit_and<void>
|
---|
| 654 | {
|
---|
| 655 | template<class T, class U>
|
---|
[22ba300] | 656 | constexpr auto operator()(T&& lhs, U&& rhs) const
|
---|
[e679283] | 657 | -> decltype(forward<T>(lhs) & forward<U>(rhs))
|
---|
| 658 | {
|
---|
| 659 | return forward<T>(lhs) & forward<U>(rhs);
|
---|
| 660 | }
|
---|
| 661 |
|
---|
| 662 | using is_transparent = aux::transparent_t;
|
---|
| 663 | };
|
---|
| 664 |
|
---|
| 665 | template<>
|
---|
| 666 | struct bit_or<void>
|
---|
| 667 | {
|
---|
| 668 | template<class T, class U>
|
---|
[22ba300] | 669 | constexpr auto operator()(T&& lhs, U&& rhs) const
|
---|
[e679283] | 670 | -> decltype(forward<T>(lhs) | forward<U>(rhs))
|
---|
| 671 | {
|
---|
| 672 | return forward<T>(lhs) | forward<U>(rhs);
|
---|
| 673 | }
|
---|
| 674 |
|
---|
| 675 | using is_transparent = aux::transparent_t;
|
---|
| 676 | };
|
---|
| 677 |
|
---|
| 678 | template<>
|
---|
| 679 | struct bit_xor<void>
|
---|
| 680 | {
|
---|
| 681 | template<class T, class U>
|
---|
[22ba300] | 682 | constexpr auto operator()(T&& lhs, U&& rhs) const
|
---|
[e679283] | 683 | -> decltype(forward<T>(lhs) ^ forward<U>(rhs))
|
---|
| 684 | {
|
---|
| 685 | return forward<T>(lhs) ^ forward<U>(rhs);
|
---|
| 686 | }
|
---|
| 687 |
|
---|
| 688 | using is_transparent = aux::transparent_t;
|
---|
| 689 | };
|
---|
| 690 |
|
---|
| 691 | template<>
|
---|
| 692 | struct bit_not<void>
|
---|
| 693 | {
|
---|
| 694 | template<class T>
|
---|
[22ba300] | 695 | constexpr auto operator()(T&& x) const
|
---|
[e679283] | 696 | -> decltype(~forward<T>(x))
|
---|
| 697 | {
|
---|
[22ba300] | 698 | return ~forward<T>(x);
|
---|
[e679283] | 699 | }
|
---|
| 700 |
|
---|
| 701 | using is_transparent = aux::transparent_t;
|
---|
| 702 | };
|
---|
| 703 |
|
---|
| 704 | /**
|
---|
| 705 | * 20.9.9, negators:
|
---|
| 706 | */
|
---|
| 707 |
|
---|
| 708 | template<class Predicate>
|
---|
[0a414494] | 709 | class unary_negate
|
---|
| 710 | {
|
---|
| 711 | public:
|
---|
| 712 | using result_type = bool;
|
---|
| 713 | using argument_type = typename Predicate::argument_type;
|
---|
| 714 |
|
---|
| 715 | constexpr explicit unary_negate(const Predicate& pred)
|
---|
| 716 | : pred_{pred}
|
---|
| 717 | { /* DUMMY BODY */ }
|
---|
| 718 |
|
---|
| 719 | constexpr result_type operator()(const argument_type& arg)
|
---|
| 720 | {
|
---|
| 721 | return !pred_(arg);
|
---|
| 722 | }
|
---|
| 723 |
|
---|
| 724 | private:
|
---|
| 725 | Predicate pred_;
|
---|
| 726 | };
|
---|
[e679283] | 727 |
|
---|
| 728 | template<class Predicate>
|
---|
[0a414494] | 729 | constexpr unary_negate<Predicate> not1(const Predicate& pred)
|
---|
| 730 | {
|
---|
| 731 | return unary_negate<Predicate>{pred};
|
---|
| 732 | }
|
---|
[e679283] | 733 |
|
---|
| 734 | template<class Predicate>
|
---|
[0a414494] | 735 | class binary_negate
|
---|
| 736 | {
|
---|
| 737 | public:
|
---|
| 738 | using result_type = bool;
|
---|
| 739 | using first_argument_type = typename Predicate::first_argument_type;
|
---|
| 740 | using second_argument_type = typename Predicate::second_argument_type;
|
---|
| 741 |
|
---|
| 742 | constexpr explicit binary_negate(const Predicate& pred)
|
---|
| 743 | : pred_{pred}
|
---|
| 744 | { /* DUMMY BODY */ }
|
---|
| 745 |
|
---|
| 746 | constexpr result_type operator()(const first_argument_type& arg1,
|
---|
| 747 | const second_argument_type& arg2)
|
---|
| 748 | {
|
---|
| 749 | return !pred_(arg1, arg2);
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 | private:
|
---|
| 753 | Predicate pred_;
|
---|
| 754 | };
|
---|
[e679283] | 755 |
|
---|
| 756 | template<class Predicate>
|
---|
| 757 | constexpr binary_negate<Predicate> not2(const Predicate& pred);
|
---|
| 758 |
|
---|
| 759 | /**
|
---|
| 760 | * 20.9.10, bind:
|
---|
| 761 | */
|
---|
| 762 |
|
---|
| 763 | template<class T>
|
---|
| 764 | struct is_bind_expression;
|
---|
| 765 |
|
---|
| 766 | template<class T>
|
---|
| 767 | struct is_placeholder;
|
---|
| 768 |
|
---|
| 769 | // TODO: void should be /unspecified/
|
---|
| 770 | template<class F, class... Args>
|
---|
| 771 | void bind(F&& f, Args&&... args);
|
---|
| 772 |
|
---|
| 773 | template<class R, class F, class... Args>
|
---|
| 774 | void bind(F&& f, Args&&... args);
|
---|
| 775 |
|
---|
| 776 | namespace placeholders
|
---|
| 777 | {
|
---|
| 778 | /**
|
---|
| 779 | * TODO: for X from 1 to implementation defined M
|
---|
| 780 | * extern /unspecified/ _X;
|
---|
| 781 | */
|
---|
| 782 | }
|
---|
| 783 |
|
---|
| 784 | /**
|
---|
| 785 | * 20.9.11, member function adaptors:
|
---|
| 786 | */
|
---|
| 787 |
|
---|
| 788 | // TODO: void should be /unspecified/
|
---|
| 789 | template<class R, class T>
|
---|
| 790 | void mem_fn(R T::* f);
|
---|
| 791 |
|
---|
| 792 | /**
|
---|
| 793 | * 20.9.12, polymorphic function adaptors:
|
---|
| 794 | */
|
---|
| 795 |
|
---|
[9c00022] | 796 | namespace aux
|
---|
| 797 | {
|
---|
| 798 | // TODO: fix this
|
---|
| 799 | /* template<class, class T, class... Args> */
|
---|
| 800 | /* struct is_callable_impl: false_type */
|
---|
| 801 | /* { /1* DUMMY BODY *1/ }; */
|
---|
| 802 |
|
---|
| 803 | /* template<class, class R, class... Args> */
|
---|
| 804 | /* struct is_callable_impl< */
|
---|
| 805 | /* void_t<decltype(aux::invoke(declval<R(Args...)>(), declval<Args>()..., R))>, */
|
---|
| 806 | /* R, Args... */
|
---|
| 807 | /* > : true_type */
|
---|
| 808 | /* { /1* DUMMY BODY *1/ }; */
|
---|
| 809 |
|
---|
| 810 | /* template<class T> */
|
---|
| 811 | /* struct is_callable: is_callable_impl<void_t<>, T> */
|
---|
| 812 | /* { /1* DUMMY BODY *1/ }; */
|
---|
| 813 |
|
---|
| 814 | template<class Callable, class R, class... Args>
|
---|
| 815 | R invoke_callable(Callable* clbl, Args&&... args)
|
---|
| 816 | {
|
---|
| 817 | return (*clbl)(forward<Args>(args)...);
|
---|
| 818 | }
|
---|
| 819 |
|
---|
| 820 | template<class Callable>
|
---|
| 821 | void copy_callable(Callable* to, Callable* from)
|
---|
| 822 | {
|
---|
| 823 | new(to) Callable{*from};
|
---|
| 824 | }
|
---|
| 825 |
|
---|
| 826 | template<class Callable>
|
---|
| 827 | void destroy_callable(Callable* clbl)
|
---|
| 828 | {
|
---|
| 829 | if (clbl)
|
---|
| 830 | clbl->~Callable();
|
---|
| 831 | }
|
---|
| 832 | }
|
---|
| 833 |
|
---|
[e679283] | 834 | class bad_function_call;
|
---|
| 835 |
|
---|
| 836 | template<class>
|
---|
| 837 | class function; // undefined
|
---|
| 838 |
|
---|
[9c00022] | 839 | /**
|
---|
| 840 | * Note: Ideally, this implementation wouldn't
|
---|
| 841 | * copy the target if it was a pointer to
|
---|
| 842 | * a function, but for the simplicity of the
|
---|
| 843 | * implementation, we do copy even in that
|
---|
| 844 | * case for now. It would be a nice optimization
|
---|
| 845 | * if this was changed in the future.
|
---|
| 846 | */
|
---|
[e679283] | 847 | template<class R, class... Args>
|
---|
[9c00022] | 848 | class function<R(Args...)>
|
---|
| 849 | {
|
---|
| 850 | public:
|
---|
| 851 | using result_type = R;
|
---|
| 852 | // TODO: conditional typedefs
|
---|
| 853 |
|
---|
| 854 | /**
|
---|
| 855 | * 20.9.12.2.1, construct/copy/destroy:
|
---|
| 856 | */
|
---|
| 857 |
|
---|
| 858 | function() noexcept
|
---|
| 859 | : callable_{}, callable_size_{}, call_{},
|
---|
| 860 | copy_{}, dest_{}
|
---|
| 861 | { /* DUMMY BODY */ }
|
---|
| 862 |
|
---|
| 863 | function(nullptr_t) noexcept
|
---|
| 864 | : function{}
|
---|
| 865 | { /* DUMMY BODY */ }
|
---|
| 866 |
|
---|
| 867 | function(const function& other)
|
---|
| 868 | : callable_{}, callable_size_{other.callable_size_},
|
---|
| 869 | call_{other.call_}, copy_{other.copy_}, dest_{other.dest_}
|
---|
| 870 | {
|
---|
| 871 | callable_ = new uint8_t[callable_size_];
|
---|
| 872 | (*copy_)(callable_, other.callable_);
|
---|
| 873 | }
|
---|
| 874 |
|
---|
| 875 | function(function&& other)
|
---|
| 876 | : callable_{other.callable_}, callable_size_{other.callable_size_},
|
---|
| 877 | call_{other.call_}, copy_{other.copy_}, dest_{other.dest_}
|
---|
| 878 | {
|
---|
| 879 | other.callable_ = nullptr;
|
---|
| 880 | other.callable_size_ = size_t{};
|
---|
| 881 | other.call_ = nullptr;
|
---|
| 882 | other.copy_ = nullptr;
|
---|
| 883 | other.dest_ = nullptr;
|
---|
| 884 | }
|
---|
| 885 |
|
---|
| 886 | // TODO: shall not participate in overloading unless aux::is_callable<F>
|
---|
| 887 | template<class F>
|
---|
| 888 | function(F f)
|
---|
| 889 | : callable_{}, callable_size_{sizeof(F)},
|
---|
| 890 | call_{(call_t)aux::invoke_callable<F, R, Args...>},
|
---|
| 891 | copy_{(copy_t)aux::copy_callable<F>},
|
---|
| 892 | dest_{(dest_t)aux::destroy_callable<F>}
|
---|
| 893 | {
|
---|
| 894 | callable_ = new uint8_t[callable_size_];
|
---|
| 895 | (*copy_)(callable_, (uint8_t*)&f);
|
---|
| 896 | }
|
---|
| 897 |
|
---|
| 898 | /**
|
---|
| 899 | * Note: For the moment we're ignoring the allocator
|
---|
| 900 | * for simplicity of the implementation.
|
---|
| 901 | */
|
---|
| 902 |
|
---|
| 903 | template<class A>
|
---|
| 904 | function(allocator_arg_t, const A& a) noexcept
|
---|
| 905 | : function{}
|
---|
| 906 | { /* DUMMY BODY */ }
|
---|
| 907 |
|
---|
| 908 | template<class A>
|
---|
| 909 | function(allocator_arg_t, const A& a, nullptr_t) noexcept
|
---|
| 910 | : function{}
|
---|
| 911 | { /* DUMMY BODY */ }
|
---|
| 912 |
|
---|
| 913 | template<class A>
|
---|
| 914 | function(allocator_arg_t, const A& a, const function& other)
|
---|
| 915 | : function{other}
|
---|
| 916 | { /* DUMMY BODY */ }
|
---|
| 917 |
|
---|
| 918 | template<class A>
|
---|
| 919 | function(allocator_arg_t, const A& a, function&& other)
|
---|
| 920 | : function{move(other)}
|
---|
| 921 | { /* DUMMY BODY */ }
|
---|
| 922 |
|
---|
| 923 | // TODO: shall not participate in overloading unless aux::is_callable<F>
|
---|
| 924 | template<class F, class A>
|
---|
| 925 | function(allocator_arg_t, const A& a, F f)
|
---|
| 926 | : function{f}
|
---|
| 927 | { /* DUMMY BODY */ }
|
---|
| 928 |
|
---|
| 929 | function& operator=(const function& rhs)
|
---|
| 930 | {
|
---|
| 931 | function{rhs}.swap(*this);
|
---|
| 932 |
|
---|
| 933 | return *this;
|
---|
| 934 | }
|
---|
| 935 |
|
---|
| 936 | /**
|
---|
| 937 | * Note: We have to copy call_, copy_
|
---|
| 938 | * and dest_ because they can be templated
|
---|
| 939 | * by a type F we don't know.
|
---|
| 940 | */
|
---|
| 941 | function& operator=(function&& rhs)
|
---|
| 942 | {
|
---|
| 943 | clear_();
|
---|
| 944 |
|
---|
| 945 | callable_ = rhs.callable_;
|
---|
| 946 | callable_size_ = rhs.callable_size_;
|
---|
| 947 | call_ = rhs.call_;
|
---|
| 948 | copy_ = rhs.copy_;
|
---|
| 949 | dest_ = rhs.dest_;
|
---|
| 950 |
|
---|
| 951 | rhs.callable_ = nullptr;
|
---|
| 952 | rhs.callable_size_ = size_t{};
|
---|
| 953 | rhs.call_ = nullptr;
|
---|
| 954 | rhs.copy_ = nullptr;
|
---|
| 955 | rhs.dest_ = nullptr;
|
---|
| 956 |
|
---|
| 957 | return *this;
|
---|
| 958 | }
|
---|
| 959 |
|
---|
| 960 | function& operator=(nullptr_t) noexcept
|
---|
| 961 | {
|
---|
| 962 | clear_();
|
---|
| 963 |
|
---|
| 964 | return *this;
|
---|
| 965 | }
|
---|
| 966 |
|
---|
| 967 | // TODO: shall not participate in overloading unless aux::is_callable<F>
|
---|
| 968 | template<class F>
|
---|
| 969 | function& operator=(F&& f)
|
---|
| 970 | {
|
---|
| 971 | callable_size_ = sizeof(F);
|
---|
| 972 | callable_ = new uint8_t[callable_size_];
|
---|
| 973 | call_ = aux::invoke_callable<F, R, Args...>;
|
---|
| 974 | copy_ = aux::copy_callable<F>;
|
---|
| 975 | dest_ = aux::destroy_callable<F>;
|
---|
| 976 |
|
---|
| 977 | (*copy_)(callable_, (uint8_t*)&f);
|
---|
| 978 | }
|
---|
| 979 |
|
---|
| 980 | template<class F>
|
---|
| 981 | function& operator=(reference_wrapper<F> ref) noexcept
|
---|
| 982 | {
|
---|
| 983 | return (*this) = ref.get();
|
---|
| 984 | }
|
---|
| 985 |
|
---|
| 986 | ~function()
|
---|
| 987 | {
|
---|
| 988 | if (callable_)
|
---|
| 989 | {
|
---|
| 990 | (*dest_)(callable_);
|
---|
| 991 | delete[] callable_;
|
---|
| 992 | }
|
---|
| 993 | }
|
---|
| 994 |
|
---|
| 995 | /**
|
---|
| 996 | * 20.9.12.2.2, function modifiers:
|
---|
| 997 | */
|
---|
| 998 |
|
---|
| 999 | void swap(function& other) noexcept
|
---|
| 1000 | {
|
---|
| 1001 | std::swap(callable_, other.callable_);
|
---|
| 1002 | std::swap(callable_size_, other.callable_size_);
|
---|
| 1003 | std::swap(call_, other.call_);
|
---|
| 1004 | std::swap(copy_, other.copy_);
|
---|
| 1005 | std::swap(dest_, other.dest_);
|
---|
| 1006 | }
|
---|
| 1007 |
|
---|
| 1008 | template<class F, class A>
|
---|
| 1009 | void assign(F&& f, const A& a)
|
---|
| 1010 | {
|
---|
| 1011 | function{allocator_arg, a, forward<F>(f)}.swap(*this);
|
---|
| 1012 | }
|
---|
| 1013 |
|
---|
| 1014 | /**
|
---|
| 1015 | * 20.9.12.2.3, function capacity:
|
---|
| 1016 | */
|
---|
| 1017 |
|
---|
| 1018 | explicit operator bool() const noexcept
|
---|
| 1019 | {
|
---|
| 1020 | return callable_ != nullptr;
|
---|
| 1021 | }
|
---|
| 1022 |
|
---|
| 1023 | /**
|
---|
| 1024 | * 20.9.12.2.4, function invocation:
|
---|
| 1025 | */
|
---|
| 1026 |
|
---|
| 1027 | result_type operator()(Args... args) const
|
---|
| 1028 | {
|
---|
| 1029 | // TODO: throw bad_function_call if !callable_ || !call_
|
---|
| 1030 | if constexpr (is_same_v<R, void>)
|
---|
| 1031 | (*call_)(callable_, forward<Args>(args)...);
|
---|
| 1032 | else
|
---|
| 1033 | return (*call_)(callable_, forward<Args>(args)...);
|
---|
| 1034 | }
|
---|
| 1035 |
|
---|
| 1036 | /**
|
---|
| 1037 | * 20.9.12.2.5, function target access:
|
---|
| 1038 | */
|
---|
| 1039 |
|
---|
| 1040 | const type_info& target_type() const noexcept
|
---|
| 1041 | {
|
---|
| 1042 | return typeid(*callable_);
|
---|
| 1043 | }
|
---|
| 1044 |
|
---|
| 1045 | template<class T>
|
---|
| 1046 | T* target() noexcept
|
---|
| 1047 | {
|
---|
| 1048 | if (target_type() == typeid(T))
|
---|
| 1049 | return (T*)callable_;
|
---|
| 1050 | else
|
---|
| 1051 | return nullptr;
|
---|
| 1052 | }
|
---|
| 1053 |
|
---|
| 1054 | template<class T>
|
---|
| 1055 | const T* target() const noexcept
|
---|
| 1056 | {
|
---|
| 1057 | if (target_type() == typeid(T))
|
---|
| 1058 | return (T*)callable_;
|
---|
| 1059 | else
|
---|
| 1060 | return nullptr;
|
---|
| 1061 | }
|
---|
| 1062 |
|
---|
| 1063 | private:
|
---|
| 1064 | using call_t = R(*)(uint8_t*, Args&&...);
|
---|
| 1065 | using copy_t = void (*)(uint8_t*, uint8_t*);
|
---|
| 1066 | using dest_t = void (*)(uint8_t*);
|
---|
| 1067 |
|
---|
| 1068 | uint8_t* callable_;
|
---|
| 1069 | size_t callable_size_;
|
---|
| 1070 | call_t call_;
|
---|
| 1071 | copy_t copy_;
|
---|
| 1072 | dest_t dest_;
|
---|
| 1073 |
|
---|
| 1074 | void clear_()
|
---|
| 1075 | {
|
---|
| 1076 | if (callable_)
|
---|
| 1077 | {
|
---|
| 1078 | (*dest_)(callable_);
|
---|
| 1079 | delete[] callable_;
|
---|
| 1080 | callable_ = nullptr;
|
---|
| 1081 | }
|
---|
| 1082 | }
|
---|
| 1083 | };
|
---|
| 1084 |
|
---|
| 1085 | /**
|
---|
| 1086 | * 20.9.12.2.6, null pointer comparisons:
|
---|
| 1087 | */
|
---|
[e679283] | 1088 |
|
---|
| 1089 | template<class R, class... Args>
|
---|
[9c00022] | 1090 | bool operator==(const function<R(Args...)>& f, nullptr_t) noexcept
|
---|
| 1091 | {
|
---|
| 1092 | return !f;
|
---|
| 1093 | }
|
---|
[e679283] | 1094 |
|
---|
| 1095 | template<class R, class... Args>
|
---|
[9c00022] | 1096 | bool operator==(nullptr_t, const function<R(Args...)>& f) noexcept
|
---|
| 1097 | {
|
---|
| 1098 | return !f;
|
---|
| 1099 | }
|
---|
[e679283] | 1100 |
|
---|
| 1101 | template<class R, class... Args>
|
---|
[9c00022] | 1102 | bool operator!=(const function<R(Args...)>& f, nullptr_t) noexcept
|
---|
| 1103 | {
|
---|
| 1104 | return (bool)f;
|
---|
| 1105 | }
|
---|
[e679283] | 1106 |
|
---|
| 1107 | template<class R, class... Args>
|
---|
[9c00022] | 1108 | bool operator!=(nullptr_t, const function<R(Args...)>& f) noexcept
|
---|
| 1109 | {
|
---|
| 1110 | return (bool)f;
|
---|
| 1111 | }
|
---|
[e679283] | 1112 |
|
---|
[55540fca] | 1113 | /**
|
---|
| 1114 | * 20.9.12.2.7, specialized algorithms:
|
---|
| 1115 | */
|
---|
| 1116 |
|
---|
| 1117 | template<class R, class... Args>
|
---|
| 1118 | void swap(function<R(Args...)>& f1, function<R(Args...)>& f2)
|
---|
| 1119 | {
|
---|
| 1120 | f1.swap(f2);
|
---|
| 1121 | }
|
---|
| 1122 |
|
---|
| 1123 | template<class R, class... Args, class Alloc>
|
---|
| 1124 | struct uses_allocator<function<R(Args...)>, Alloc>
|
---|
| 1125 | : true_type
|
---|
| 1126 | { /* DUMMY BODY */ };
|
---|
| 1127 |
|
---|
[e679283] | 1128 | /**
|
---|
| 1129 | * 20.9.13, hash function primary template:
|
---|
| 1130 | */
|
---|
| 1131 |
|
---|
[22ba300] | 1132 | namespace aux
|
---|
| 1133 | {
|
---|
| 1134 | template<class T>
|
---|
| 1135 | union converter
|
---|
| 1136 | {
|
---|
| 1137 | T value;
|
---|
| 1138 | uint64_t converted;
|
---|
| 1139 | };
|
---|
| 1140 |
|
---|
| 1141 | template<class T>
|
---|
| 1142 | T hash_(uint64_t x) noexcept
|
---|
| 1143 | {
|
---|
[5ae8168] | 1144 | /**
|
---|
| 1145 | * Note: std::hash is used for indexing in
|
---|
| 1146 | * unordered containers, not for cryptography.
|
---|
| 1147 | * Because of this, we decided to simply convert
|
---|
| 1148 | * the value to uin64_t, which will help us
|
---|
| 1149 | * with testing (since in order to create
|
---|
| 1150 | * a collision in a multiset or multimap
|
---|
| 1151 | * we simply need 2 values that congruent
|
---|
| 1152 | * by the size of the table.
|
---|
| 1153 | */
|
---|
| 1154 | return static_cast<T>(x);
|
---|
[22ba300] | 1155 | }
|
---|
| 1156 |
|
---|
| 1157 | template<class T>
|
---|
| 1158 | size_t hash(T x) noexcept
|
---|
| 1159 | {
|
---|
| 1160 | static_assert(is_arithmetic_v<T> || is_pointer_v<T>,
|
---|
| 1161 | "invalid type passed to aux::hash");
|
---|
| 1162 |
|
---|
| 1163 | converter<T> conv;
|
---|
| 1164 | conv.value = x;
|
---|
| 1165 |
|
---|
| 1166 | return hash_<size_t>(conv.converted);
|
---|
| 1167 | }
|
---|
| 1168 | }
|
---|
| 1169 |
|
---|
[e679283] | 1170 | template<class T>
|
---|
[9f77d98] | 1171 | struct hash
|
---|
| 1172 | { /* DUMMY BODY */ };
|
---|
[e679283] | 1173 |
|
---|
| 1174 | template<>
|
---|
[9f77d98] | 1175 | struct hash<bool>
|
---|
| 1176 | {
|
---|
| 1177 | size_t operator()(bool x) const noexcept
|
---|
| 1178 | {
|
---|
[22ba300] | 1179 | return aux::hash(x);
|
---|
[9f77d98] | 1180 | }
|
---|
| 1181 |
|
---|
| 1182 | using argument_type = bool;
|
---|
| 1183 | using result_type = size_t;
|
---|
| 1184 | };
|
---|
[e679283] | 1185 |
|
---|
| 1186 | template<>
|
---|
[9f77d98] | 1187 | struct hash<char>
|
---|
| 1188 | {
|
---|
| 1189 | size_t operator()(char x) const noexcept
|
---|
| 1190 | {
|
---|
[22ba300] | 1191 | return aux::hash(x);
|
---|
[9f77d98] | 1192 | }
|
---|
| 1193 |
|
---|
| 1194 | using argument_type = char;
|
---|
| 1195 | using result_type = size_t;
|
---|
| 1196 | };
|
---|
[e679283] | 1197 |
|
---|
| 1198 | template<>
|
---|
[9f77d98] | 1199 | struct hash<signed char>
|
---|
| 1200 | {
|
---|
| 1201 | size_t operator()(signed char x) const noexcept
|
---|
| 1202 | {
|
---|
[22ba300] | 1203 | return aux::hash(x);
|
---|
[9f77d98] | 1204 | }
|
---|
| 1205 |
|
---|
| 1206 | using argument_type = signed char;
|
---|
| 1207 | using result_type = size_t;
|
---|
| 1208 | };
|
---|
[e679283] | 1209 |
|
---|
| 1210 | template<>
|
---|
[9f77d98] | 1211 | struct hash<unsigned char>
|
---|
| 1212 | {
|
---|
| 1213 | size_t operator()(unsigned char x) const noexcept
|
---|
| 1214 | {
|
---|
[22ba300] | 1215 | return aux::hash(x);
|
---|
[9f77d98] | 1216 | }
|
---|
| 1217 |
|
---|
| 1218 | using argument_type = unsigned char;
|
---|
| 1219 | using result_type = size_t;
|
---|
| 1220 | };
|
---|
[e679283] | 1221 |
|
---|
| 1222 | template<>
|
---|
[9f77d98] | 1223 | struct hash<char16_t>
|
---|
| 1224 | {
|
---|
| 1225 | size_t operator()(char16_t x) const noexcept
|
---|
| 1226 | {
|
---|
[22ba300] | 1227 | return aux::hash(x);
|
---|
[9f77d98] | 1228 | }
|
---|
| 1229 |
|
---|
| 1230 | using argument_type = char16_t;
|
---|
| 1231 | using result_type = size_t;
|
---|
| 1232 | };
|
---|
[e679283] | 1233 |
|
---|
| 1234 | template<>
|
---|
[9f77d98] | 1235 | struct hash<char32_t>
|
---|
| 1236 | {
|
---|
| 1237 | size_t operator()(char32_t x) const noexcept
|
---|
| 1238 | {
|
---|
[22ba300] | 1239 | return aux::hash(x);
|
---|
[9f77d98] | 1240 | }
|
---|
| 1241 |
|
---|
| 1242 | using argument_type = char32_t;
|
---|
| 1243 | using result_type = size_t;
|
---|
| 1244 | };
|
---|
[e679283] | 1245 |
|
---|
| 1246 | template<>
|
---|
[9f77d98] | 1247 | struct hash<wchar_t>
|
---|
| 1248 | {
|
---|
| 1249 | size_t operator()(wchar_t x) const noexcept
|
---|
| 1250 | {
|
---|
[22ba300] | 1251 | return aux::hash(x);
|
---|
[9f77d98] | 1252 | }
|
---|
| 1253 |
|
---|
| 1254 | using argument_type = wchar_t;
|
---|
| 1255 | using result_type = size_t;
|
---|
| 1256 | };
|
---|
[e679283] | 1257 |
|
---|
| 1258 | template<>
|
---|
[9f77d98] | 1259 | struct hash<short>
|
---|
| 1260 | {
|
---|
| 1261 | size_t operator()(short x) const noexcept
|
---|
| 1262 | {
|
---|
[22ba300] | 1263 | return aux::hash(x);
|
---|
[9f77d98] | 1264 | }
|
---|
| 1265 |
|
---|
| 1266 | using argument_type = short;
|
---|
| 1267 | using result_type = size_t;
|
---|
| 1268 | };
|
---|
[e679283] | 1269 |
|
---|
| 1270 | template<>
|
---|
[9f77d98] | 1271 | struct hash<unsigned short>
|
---|
| 1272 | {
|
---|
| 1273 | size_t operator()(unsigned short x) const noexcept
|
---|
| 1274 | {
|
---|
[22ba300] | 1275 | return aux::hash(x);
|
---|
[9f77d98] | 1276 | }
|
---|
| 1277 |
|
---|
| 1278 | using argument_type = unsigned short;
|
---|
| 1279 | using result_type = size_t;
|
---|
| 1280 | };
|
---|
[e679283] | 1281 |
|
---|
| 1282 | template<>
|
---|
[9f77d98] | 1283 | struct hash<int>
|
---|
| 1284 | {
|
---|
| 1285 | size_t operator()(int x) const noexcept
|
---|
| 1286 | {
|
---|
[22ba300] | 1287 | return aux::hash(x);
|
---|
[9f77d98] | 1288 | }
|
---|
| 1289 |
|
---|
| 1290 | using argument_type = int;
|
---|
| 1291 | using result_type = size_t;
|
---|
| 1292 | };
|
---|
[e679283] | 1293 |
|
---|
| 1294 | template<>
|
---|
[9f77d98] | 1295 | struct hash<unsigned int>
|
---|
| 1296 | {
|
---|
| 1297 | size_t operator()(unsigned int x) const noexcept
|
---|
| 1298 | {
|
---|
[22ba300] | 1299 | return aux::hash(x);
|
---|
[9f77d98] | 1300 | }
|
---|
| 1301 |
|
---|
| 1302 | using argument_type = unsigned int;
|
---|
| 1303 | using result_type = size_t;
|
---|
| 1304 | };
|
---|
[e679283] | 1305 |
|
---|
| 1306 | template<>
|
---|
[9f77d98] | 1307 | struct hash<long>
|
---|
| 1308 | {
|
---|
| 1309 | size_t operator()(long x) const noexcept
|
---|
| 1310 | {
|
---|
[22ba300] | 1311 | return aux::hash(x);
|
---|
[9f77d98] | 1312 | }
|
---|
| 1313 |
|
---|
| 1314 | using argument_type = long;
|
---|
| 1315 | using result_type = size_t;
|
---|
| 1316 | };
|
---|
[e679283] | 1317 |
|
---|
| 1318 | template<>
|
---|
[9f77d98] | 1319 | struct hash<long long>
|
---|
| 1320 | {
|
---|
| 1321 | size_t operator()(long long x) const noexcept
|
---|
| 1322 | {
|
---|
[22ba300] | 1323 | return aux::hash(x);
|
---|
[9f77d98] | 1324 | }
|
---|
| 1325 |
|
---|
| 1326 | using argument_type = long long;
|
---|
| 1327 | using result_type = size_t;
|
---|
| 1328 | };
|
---|
[e679283] | 1329 |
|
---|
| 1330 | template<>
|
---|
[9f77d98] | 1331 | struct hash<unsigned long>
|
---|
| 1332 | {
|
---|
| 1333 | size_t operator()(unsigned long x) const noexcept
|
---|
| 1334 | {
|
---|
[22ba300] | 1335 | return aux::hash(x);
|
---|
[9f77d98] | 1336 | }
|
---|
| 1337 |
|
---|
| 1338 | using argument_type = unsigned long;
|
---|
| 1339 | using result_type = size_t;
|
---|
| 1340 | };
|
---|
[e679283] | 1341 |
|
---|
| 1342 | template<>
|
---|
[9f77d98] | 1343 | struct hash<unsigned long long>
|
---|
| 1344 | {
|
---|
| 1345 | size_t operator()(unsigned long long x) const noexcept
|
---|
| 1346 | {
|
---|
[22ba300] | 1347 | return aux::hash(x);
|
---|
[9f77d98] | 1348 | }
|
---|
| 1349 |
|
---|
| 1350 | using argument_type = unsigned long long;
|
---|
| 1351 | using result_type = size_t;
|
---|
| 1352 | };
|
---|
[e679283] | 1353 |
|
---|
| 1354 | template<>
|
---|
[9f77d98] | 1355 | struct hash<float>
|
---|
| 1356 | {
|
---|
| 1357 | size_t operator()(float x) const noexcept
|
---|
| 1358 | {
|
---|
[22ba300] | 1359 | return aux::hash(x);
|
---|
[9f77d98] | 1360 | }
|
---|
| 1361 |
|
---|
| 1362 | using argument_type = float;
|
---|
| 1363 | using result_type = size_t;
|
---|
| 1364 | };
|
---|
[e679283] | 1365 |
|
---|
| 1366 | template<>
|
---|
[22ba300] | 1367 | struct hash<double>
|
---|
[9f77d98] | 1368 | {
|
---|
| 1369 | size_t operator()(double x) const noexcept
|
---|
| 1370 | {
|
---|
[22ba300] | 1371 | return aux::hash(x);
|
---|
[9f77d98] | 1372 | }
|
---|
| 1373 |
|
---|
| 1374 | using argument_type = double;
|
---|
| 1375 | using result_type = size_t;
|
---|
| 1376 | };
|
---|
[e679283] | 1377 |
|
---|
| 1378 | template<>
|
---|
[22ba300] | 1379 | struct hash<long double>
|
---|
[9f77d98] | 1380 | {
|
---|
| 1381 | size_t operator()(long double x) const noexcept
|
---|
| 1382 | {
|
---|
[22ba300] | 1383 | return aux::hash(x);
|
---|
[9f77d98] | 1384 | }
|
---|
| 1385 |
|
---|
| 1386 | using argument_type = long double;
|
---|
| 1387 | using result_type = size_t;
|
---|
| 1388 | };
|
---|
[e679283] | 1389 |
|
---|
| 1390 | template<class T>
|
---|
[9f77d98] | 1391 | struct hash<T*>
|
---|
| 1392 | {
|
---|
| 1393 | size_t operator()(T* x) const noexcept
|
---|
| 1394 | {
|
---|
[22ba300] | 1395 | return aux::hash(x);
|
---|
[9f77d98] | 1396 | }
|
---|
| 1397 |
|
---|
| 1398 | using argument_type = T*;
|
---|
| 1399 | using result_type = size_t;
|
---|
| 1400 | };
|
---|
[e679283] | 1401 | }
|
---|
| 1402 |
|
---|
| 1403 | #endif
|
---|