| 1 | |
| 2 | // Copyright 2006-2009 Daniel James. |
| 3 | // Copyright 2022 Christian Mazakas |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | #if !defined(BOOST_UNORDERED_EXCEPTION_TEST_OBJECTS_HEADER) |
| 8 | #define |
| 9 | |
| 10 | #include "../helpers/exception_test.hpp" |
| 11 | |
| 12 | #include "../helpers/count.hpp" |
| 13 | #include "../helpers/fwd.hpp" |
| 14 | #include "../helpers/generators.hpp" |
| 15 | #include "../helpers/memory.hpp" |
| 16 | #include "./fwd.hpp" |
| 17 | #include <boost/limits.hpp> |
| 18 | #include <cstddef> |
| 19 | #include <new> |
| 20 | |
| 21 | namespace test { |
| 22 | namespace exception { |
| 23 | class object; |
| 24 | class hash; |
| 25 | class equal_to; |
| 26 | template <class T> class allocator; |
| 27 | object generate(object const*, random_generator); |
| 28 | std::pair<object, object> generate( |
| 29 | std::pair<object, object> const*, random_generator); |
| 30 | |
| 31 | struct true_type |
| 32 | { |
| 33 | enum |
| 34 | { |
| 35 | value = true |
| 36 | }; |
| 37 | }; |
| 38 | |
| 39 | struct false_type |
| 40 | { |
| 41 | enum |
| 42 | { |
| 43 | value = false |
| 44 | }; |
| 45 | }; |
| 46 | |
| 47 | class object : private counted_object |
| 48 | { |
| 49 | public: |
| 50 | int tag1_, tag2_; |
| 51 | |
| 52 | explicit object() : tag1_(0), tag2_(0) |
| 53 | { |
| 54 | UNORDERED_SCOPE(object::object()) |
| 55 | { |
| 56 | UNORDERED_EPOINT("Mock object default constructor." ) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | explicit object(int t1, int t2 = 0) : tag1_(t1), tag2_(t2) |
| 61 | { |
| 62 | UNORDERED_SCOPE(object::object(int)) |
| 63 | { |
| 64 | UNORDERED_EPOINT("Mock object constructor by value." ) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | object(object const& x) |
| 69 | : counted_object(x), tag1_(x.tag1_), tag2_(x.tag2_) |
| 70 | { |
| 71 | UNORDERED_SCOPE(object::object(object)) |
| 72 | { |
| 73 | UNORDERED_EPOINT("Mock object copy constructor." ) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | ~object() |
| 78 | { |
| 79 | tag1_ = -1; |
| 80 | tag2_ = -1; |
| 81 | } |
| 82 | |
| 83 | object& operator=(object const& x) |
| 84 | { |
| 85 | UNORDERED_SCOPE(object::operator=(object)) |
| 86 | { |
| 87 | tag1_ = x.tag1_; |
| 88 | UNORDERED_EPOINT("Mock object assign operator 1." ) |
| 89 | tag2_ = x.tag2_; |
| 90 | // UNORDERED_EPOINT("Mock object assign operator 2."); |
| 91 | } |
| 92 | return *this; |
| 93 | } |
| 94 | |
| 95 | friend bool operator==(object const& x1, object const& x2) |
| 96 | { |
| 97 | UNORDERED_SCOPE(operator==(object, object)) |
| 98 | { |
| 99 | UNORDERED_EPOINT("Mock object equality operator." ) |
| 100 | } |
| 101 | |
| 102 | return x1.tag1_ == x2.tag1_ && x1.tag2_ == x2.tag2_; |
| 103 | } |
| 104 | |
| 105 | friend bool operator!=(object const& x1, object const& x2) |
| 106 | { |
| 107 | UNORDERED_SCOPE(operator!=(object, object)) |
| 108 | { |
| 109 | UNORDERED_EPOINT("Mock object inequality operator." ) |
| 110 | } |
| 111 | |
| 112 | return !(x1.tag1_ == x2.tag1_ && x1.tag2_ == x2.tag2_); |
| 113 | } |
| 114 | |
| 115 | // None of the last few functions are used by the unordered associative |
| 116 | // containers - so there aren't any exception points. |
| 117 | friend bool operator<(object const& x1, object const& x2) |
| 118 | { |
| 119 | return x1.tag1_ < x2.tag1_ || |
| 120 | (x1.tag1_ == x2.tag1_ && x1.tag2_ < x2.tag2_); |
| 121 | } |
| 122 | |
| 123 | friend object generate(object const*, random_generator g) |
| 124 | { |
| 125 | int* x = 0; |
| 126 | return object(::test::generate(x, g), ::test::generate(x, g)); |
| 127 | } |
| 128 | |
| 129 | friend std::ostream& operator<<(std::ostream& out, object const& o) |
| 130 | { |
| 131 | return out << "(" << o.tag1_ << "," << o.tag2_ << ")" ; |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | std::pair<object, object> generate( |
| 136 | std::pair<object, object> const*, random_generator g) |
| 137 | { |
| 138 | int* x = 0; |
| 139 | return std::make_pair( |
| 140 | x: object(::test::generate(x, g), ::test::generate(x, g)), |
| 141 | y: object(::test::generate(x, g), ::test::generate(x, g))); |
| 142 | } |
| 143 | |
| 144 | class hash |
| 145 | { |
| 146 | int tag_; |
| 147 | |
| 148 | public: |
| 149 | hash(int t = 0) : tag_(t) |
| 150 | { |
| 151 | UNORDERED_SCOPE(hash::object()) |
| 152 | { |
| 153 | UNORDERED_EPOINT("Mock hash default constructor." ) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | hash(hash const& x) : tag_(x.tag_) |
| 158 | { |
| 159 | UNORDERED_SCOPE(hash::hash(hash)) |
| 160 | { |
| 161 | UNORDERED_EPOINT("Mock hash copy constructor." ) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | hash& operator=(hash const& x) |
| 166 | { |
| 167 | UNORDERED_SCOPE(hash::operator=(hash)) |
| 168 | { |
| 169 | UNORDERED_EPOINT("Mock hash assign operator 1." ) |
| 170 | tag_ = x.tag_; |
| 171 | UNORDERED_EPOINT("Mock hash assign operator 2." ) |
| 172 | } |
| 173 | return *this; |
| 174 | } |
| 175 | |
| 176 | std::size_t operator()(object const& x) const |
| 177 | { |
| 178 | UNORDERED_SCOPE(hash::operator()(object)) |
| 179 | { |
| 180 | UNORDERED_EPOINT("Mock hash function." ) |
| 181 | } |
| 182 | |
| 183 | return hash_impl(x); |
| 184 | } |
| 185 | |
| 186 | std::size_t operator()(std::pair<object, object> const& x) const |
| 187 | { |
| 188 | UNORDERED_SCOPE(hash::operator()(std::pair<object, object>)) |
| 189 | { |
| 190 | UNORDERED_EPOINT("Mock hash pair function." ) |
| 191 | } |
| 192 | |
| 193 | return hash_impl(x: x.first) * 193ul + hash_impl(x: x.second) * 97ul + 29ul; |
| 194 | } |
| 195 | |
| 196 | std::size_t hash_impl(object const& x) const |
| 197 | { |
| 198 | unsigned result; |
| 199 | switch (tag_) { |
| 200 | case 1: |
| 201 | result = static_cast<unsigned>(x.tag1_); |
| 202 | break; |
| 203 | case 2: |
| 204 | result = static_cast<unsigned>(x.tag2_); |
| 205 | break; |
| 206 | default: |
| 207 | result = |
| 208 | static_cast<unsigned>(x.tag1_) + static_cast<unsigned>(x.tag2_); |
| 209 | } |
| 210 | return result; |
| 211 | } |
| 212 | |
| 213 | friend bool operator==(hash const& x1, hash const& x2) |
| 214 | { |
| 215 | UNORDERED_SCOPE(operator==(hash, hash)) |
| 216 | { |
| 217 | UNORDERED_EPOINT("Mock hash equality function." ) |
| 218 | } |
| 219 | return x1.tag_ == x2.tag_; |
| 220 | } |
| 221 | |
| 222 | friend bool operator!=(hash const& x1, hash const& x2) |
| 223 | { |
| 224 | UNORDERED_SCOPE(hash::operator!=(hash, hash)) |
| 225 | { |
| 226 | UNORDERED_EPOINT("Mock hash inequality function." ) |
| 227 | } |
| 228 | return x1.tag_ != x2.tag_; |
| 229 | } |
| 230 | |
| 231 | #if defined(BOOST_UNORDERED_FOA_TESTS) |
| 232 | friend void swap(hash&, hash&) noexcept; |
| 233 | #endif |
| 234 | }; |
| 235 | |
| 236 | #if defined(BOOST_UNORDERED_FOA_TESTS) |
| 237 | void swap(hash& lhs, hash& rhs) noexcept |
| 238 | { |
| 239 | int tag = lhs.tag_; |
| 240 | lhs.tag_ = rhs.tag_; |
| 241 | rhs.tag_ = tag; |
| 242 | } |
| 243 | #endif |
| 244 | |
| 245 | class less |
| 246 | { |
| 247 | int tag_; |
| 248 | |
| 249 | public: |
| 250 | less(int t = 0) : tag_(t) {} |
| 251 | |
| 252 | less(less const& x) : tag_(x.tag_) {} |
| 253 | |
| 254 | bool operator()(object const& x1, object const& x2) const |
| 255 | { |
| 256 | return less_impl(x1, x2); |
| 257 | } |
| 258 | |
| 259 | bool operator()(std::pair<object, object> const& x1, |
| 260 | std::pair<object, object> const& x2) const |
| 261 | { |
| 262 | if (less_impl(x1: x1.first, x2: x2.first)) { |
| 263 | return true; |
| 264 | } |
| 265 | if (less_impl(x1: x2.first, x2: x1.first)) { |
| 266 | return false; |
| 267 | } |
| 268 | return less_impl(x1: x1.second, x2: x2.second); |
| 269 | } |
| 270 | |
| 271 | bool less_impl(object const& x1, object const& x2) const |
| 272 | { |
| 273 | switch (tag_) { |
| 274 | case 1: |
| 275 | return x1.tag1_ < x2.tag1_; |
| 276 | case 2: |
| 277 | return x1.tag2_ < x2.tag2_; |
| 278 | default: |
| 279 | return x1 < x2; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | friend bool operator==(less const& x1, less const& x2) |
| 284 | { |
| 285 | return x1.tag_ == x2.tag_; |
| 286 | } |
| 287 | |
| 288 | friend bool operator!=(less const& x1, less const& x2) |
| 289 | { |
| 290 | return x1.tag_ != x2.tag_; |
| 291 | } |
| 292 | }; |
| 293 | |
| 294 | class equal_to |
| 295 | { |
| 296 | int tag_; |
| 297 | |
| 298 | public: |
| 299 | equal_to(int t = 0) : tag_(t) |
| 300 | { |
| 301 | UNORDERED_SCOPE(equal_to::equal_to()) |
| 302 | { |
| 303 | UNORDERED_EPOINT("Mock equal_to default constructor." ) |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | equal_to(equal_to const& x) : tag_(x.tag_) |
| 308 | { |
| 309 | UNORDERED_SCOPE(equal_to::equal_to(equal_to)) |
| 310 | { |
| 311 | UNORDERED_EPOINT("Mock equal_to copy constructor." ) |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | equal_to& operator=(equal_to const& x) |
| 316 | { |
| 317 | UNORDERED_SCOPE(equal_to::operator=(equal_to)) |
| 318 | { |
| 319 | UNORDERED_EPOINT("Mock equal_to assign operator 1." ) |
| 320 | tag_ = x.tag_; |
| 321 | UNORDERED_EPOINT("Mock equal_to assign operator 2." ) |
| 322 | } |
| 323 | return *this; |
| 324 | } |
| 325 | |
| 326 | bool operator()(object const& x1, object const& x2) const |
| 327 | { |
| 328 | UNORDERED_SCOPE(equal_to::operator()(object, object)) |
| 329 | { |
| 330 | UNORDERED_EPOINT("Mock equal_to function." ) |
| 331 | } |
| 332 | |
| 333 | return equal_impl(x1, x2); |
| 334 | } |
| 335 | |
| 336 | bool operator()(std::pair<object, object> const& x1, |
| 337 | std::pair<object, object> const& x2) const |
| 338 | { |
| 339 | UNORDERED_SCOPE(equal_to::operator()( |
| 340 | std::pair<object, object>, std::pair<object, object>)) |
| 341 | { |
| 342 | UNORDERED_EPOINT("Mock equal_to function." ) |
| 343 | } |
| 344 | |
| 345 | return equal_impl(x1: x1.first, x2: x2.first) && |
| 346 | equal_impl(x1: x1.second, x2: x2.second); |
| 347 | } |
| 348 | |
| 349 | bool equal_impl(object const& x1, object const& x2) const |
| 350 | { |
| 351 | switch (tag_) { |
| 352 | case 1: |
| 353 | return x1.tag1_ == x2.tag1_; |
| 354 | case 2: |
| 355 | return x1.tag2_ == x2.tag2_; |
| 356 | default: |
| 357 | return x1 == x2; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | friend bool operator==(equal_to const& x1, equal_to const& x2) |
| 362 | { |
| 363 | UNORDERED_SCOPE(operator==(equal_to, equal_to)) |
| 364 | { |
| 365 | UNORDERED_EPOINT("Mock equal_to equality function." ) |
| 366 | } |
| 367 | return x1.tag_ == x2.tag_; |
| 368 | } |
| 369 | |
| 370 | friend bool operator!=(equal_to const& x1, equal_to const& x2) |
| 371 | { |
| 372 | UNORDERED_SCOPE(operator!=(equal_to, equal_to)) |
| 373 | { |
| 374 | UNORDERED_EPOINT("Mock equal_to inequality function." ) |
| 375 | } |
| 376 | return x1.tag_ != x2.tag_; |
| 377 | } |
| 378 | |
| 379 | friend less create_compare(equal_to x) { return less(x.tag_); } |
| 380 | #if defined(BOOST_UNORDERED_FOA_TESTS) |
| 381 | friend void swap(equal_to&, equal_to&) noexcept; |
| 382 | #endif |
| 383 | }; |
| 384 | |
| 385 | #if defined(BOOST_UNORDERED_FOA_TESTS) |
| 386 | void swap(equal_to& lhs, equal_to& rhs) noexcept |
| 387 | { |
| 388 | int tag = lhs.tag_; |
| 389 | lhs.tag_ = rhs.tag_; |
| 390 | rhs.tag_ = tag; |
| 391 | } |
| 392 | #endif |
| 393 | |
| 394 | template <class T> class allocator |
| 395 | { |
| 396 | public: |
| 397 | int tag_; |
| 398 | typedef std::size_t size_type; |
| 399 | typedef std::ptrdiff_t difference_type; |
| 400 | typedef T* pointer; |
| 401 | typedef T const* const_pointer; |
| 402 | typedef T& reference; |
| 403 | typedef T const& const_reference; |
| 404 | typedef T value_type; |
| 405 | |
| 406 | template <class U> struct rebind |
| 407 | { |
| 408 | typedef allocator<U> other; |
| 409 | }; |
| 410 | |
| 411 | explicit allocator(int t = 0) : tag_(t) |
| 412 | { |
| 413 | UNORDERED_SCOPE(allocator::allocator()) |
| 414 | { |
| 415 | UNORDERED_EPOINT("Mock allocator default constructor." ) |
| 416 | } |
| 417 | test::detail::tracker.allocator_ref(); |
| 418 | } |
| 419 | |
| 420 | template <class Y> allocator(allocator<Y> const& x) : tag_(x.tag_) |
| 421 | { |
| 422 | test::detail::tracker.allocator_ref(); |
| 423 | } |
| 424 | |
| 425 | allocator(allocator const& x) : tag_(x.tag_) |
| 426 | { |
| 427 | test::detail::tracker.allocator_ref(); |
| 428 | } |
| 429 | |
| 430 | ~allocator() { test::detail::tracker.allocator_unref(); } |
| 431 | |
| 432 | allocator& operator=(allocator const& x) |
| 433 | { |
| 434 | tag_ = x.tag_; |
| 435 | return *this; |
| 436 | } |
| 437 | |
| 438 | // If address throws, then it can't be used in erase or the |
| 439 | // destructor, which is very limiting. I need to check up on |
| 440 | // this. |
| 441 | |
| 442 | pointer address(reference r) |
| 443 | { |
| 444 | // UNORDERED_SCOPE(allocator::address(reference)) { |
| 445 | // UNORDERED_EPOINT("Mock allocator address function."); |
| 446 | //} |
| 447 | return pointer(&r); |
| 448 | } |
| 449 | |
| 450 | const_pointer address(const_reference r) |
| 451 | { |
| 452 | // UNORDERED_SCOPE(allocator::address(const_reference)) { |
| 453 | // UNORDERED_EPOINT("Mock allocator const address function."); |
| 454 | //} |
| 455 | return const_pointer(&r); |
| 456 | } |
| 457 | |
| 458 | pointer allocate(size_type n) |
| 459 | { |
| 460 | T* ptr = 0; |
| 461 | UNORDERED_SCOPE(allocator::allocate(size_type)) |
| 462 | { |
| 463 | UNORDERED_EPOINT("Mock allocator allocate function." ) |
| 464 | |
| 465 | using namespace std; |
| 466 | ptr = (T*)malloc(size: n * sizeof(T)); |
| 467 | if (!ptr) |
| 468 | throw std::bad_alloc(); |
| 469 | } |
| 470 | test::detail::tracker.track_allocate(ptr: (void*)ptr, n, size: sizeof(T), tag: tag_); |
| 471 | return pointer(ptr); |
| 472 | |
| 473 | // return pointer(static_cast<T*>(::operator new(n * sizeof(T)))); |
| 474 | } |
| 475 | |
| 476 | pointer allocate(size_type n, void const*) |
| 477 | { |
| 478 | T* ptr = 0; |
| 479 | UNORDERED_SCOPE(allocator::allocate(size_type, const_pointer)) |
| 480 | { |
| 481 | UNORDERED_EPOINT("Mock allocator allocate function." ) |
| 482 | |
| 483 | using namespace std; |
| 484 | ptr = (T*)malloc(size: n * sizeof(T)); |
| 485 | if (!ptr) |
| 486 | throw std::bad_alloc(); |
| 487 | } |
| 488 | test::detail::tracker.track_allocate(ptr: (void*)ptr, n, size: sizeof(T), tag: tag_); |
| 489 | return pointer(ptr); |
| 490 | |
| 491 | // return pointer(static_cast<T*>(::operator new(n * sizeof(T)))); |
| 492 | } |
| 493 | |
| 494 | void deallocate(pointer p, size_type n) |
| 495 | { |
| 496 | //::operator delete((void*) p); |
| 497 | if (p) { |
| 498 | test::detail::tracker.track_deallocate(ptr: (void*)p, n, size: sizeof(T), tag: tag_); |
| 499 | using namespace std; |
| 500 | free(p); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | template <class U, class... Args> void construct(U* p, Args&&... args) |
| 505 | { |
| 506 | UNORDERED_SCOPE(allocator::construct(U*, Args&&...)) |
| 507 | { |
| 508 | UNORDERED_EPOINT("Mock allocator construct function." ) |
| 509 | new (p) U(std::forward<Args>(args)...); |
| 510 | } |
| 511 | test::detail::tracker.track_construct((void*)p, sizeof(U), tag_); |
| 512 | } |
| 513 | |
| 514 | template <class U> |
| 515 | void destroy(U* p) |
| 516 | { |
| 517 | test::detail::tracker.track_destroy((void*)p, sizeof(U), tag_); |
| 518 | p->~U(); |
| 519 | } |
| 520 | |
| 521 | size_type max_size() const |
| 522 | { |
| 523 | UNORDERED_SCOPE(allocator::construct(pointer, T)) |
| 524 | { |
| 525 | UNORDERED_EPOINT("Mock allocator max_size function." ) |
| 526 | } |
| 527 | return (std::numeric_limits<std::size_t>::max)(); |
| 528 | } |
| 529 | |
| 530 | typedef true_type propagate_on_container_copy_assignment; |
| 531 | typedef true_type propagate_on_container_move_assignment; |
| 532 | typedef true_type propagate_on_container_swap; |
| 533 | }; |
| 534 | |
| 535 | template <class T> void swap(allocator<T>& x, allocator<T>& y) |
| 536 | { |
| 537 | std::swap(x.tag_, y.tag_); |
| 538 | } |
| 539 | |
| 540 | // It's pretty much impossible to write a compliant swap when these |
| 541 | // two can throw. So they don't. |
| 542 | |
| 543 | template <class T> |
| 544 | inline bool operator==(allocator<T> const& x, allocator<T> const& y) |
| 545 | { |
| 546 | // UNORDERED_SCOPE(operator==(allocator, allocator)) { |
| 547 | // UNORDERED_EPOINT("Mock allocator equality operator."); |
| 548 | //} |
| 549 | return x.tag_ == y.tag_; |
| 550 | } |
| 551 | |
| 552 | template <class T> |
| 553 | inline bool operator!=(allocator<T> const& x, allocator<T> const& y) |
| 554 | { |
| 555 | // UNORDERED_SCOPE(operator!=(allocator, allocator)) { |
| 556 | // UNORDERED_EPOINT("Mock allocator inequality operator."); |
| 557 | //} |
| 558 | return x.tag_ != y.tag_; |
| 559 | } |
| 560 | |
| 561 | template <class T> class allocator2 |
| 562 | { |
| 563 | public: |
| 564 | int tag_; |
| 565 | typedef std::size_t size_type; |
| 566 | typedef std::ptrdiff_t difference_type; |
| 567 | typedef T* pointer; |
| 568 | typedef T const* const_pointer; |
| 569 | typedef T& reference; |
| 570 | typedef T const& const_reference; |
| 571 | typedef T value_type; |
| 572 | |
| 573 | template <class U> struct rebind |
| 574 | { |
| 575 | typedef allocator2<U> other; |
| 576 | }; |
| 577 | |
| 578 | explicit allocator2(int t = 0) : tag_(t) |
| 579 | { |
| 580 | UNORDERED_SCOPE(allocator2::allocator2()) |
| 581 | { |
| 582 | UNORDERED_EPOINT("Mock allocator2 default constructor." ) |
| 583 | } |
| 584 | test::detail::tracker.allocator_ref(); |
| 585 | } |
| 586 | |
| 587 | allocator2(allocator<T> const& x) : tag_(x.tag_) |
| 588 | { |
| 589 | test::detail::tracker.allocator_ref(); |
| 590 | } |
| 591 | |
| 592 | template <class Y> allocator2(allocator2<Y> const& x) : tag_(x.tag_) |
| 593 | { |
| 594 | test::detail::tracker.allocator_ref(); |
| 595 | } |
| 596 | |
| 597 | allocator2(allocator2 const& x) : tag_(x.tag_) |
| 598 | { |
| 599 | test::detail::tracker.allocator_ref(); |
| 600 | } |
| 601 | |
| 602 | ~allocator2() { test::detail::tracker.allocator_unref(); } |
| 603 | |
| 604 | allocator2& operator=(allocator2 const&) { return *this; } |
| 605 | |
| 606 | // If address throws, then it can't be used in erase or the |
| 607 | // destructor, which is very limiting. I need to check up on |
| 608 | // this. |
| 609 | |
| 610 | pointer address(reference r) |
| 611 | { |
| 612 | // UNORDERED_SCOPE(allocator2::address(reference)) { |
| 613 | // UNORDERED_EPOINT("Mock allocator2 address function."); |
| 614 | //} |
| 615 | return pointer(&r); |
| 616 | } |
| 617 | |
| 618 | const_pointer address(const_reference r) |
| 619 | { |
| 620 | // UNORDERED_SCOPE(allocator2::address(const_reference)) { |
| 621 | // UNORDERED_EPOINT("Mock allocator2 const address function."); |
| 622 | //} |
| 623 | return const_pointer(&r); |
| 624 | } |
| 625 | |
| 626 | pointer allocate(size_type n) |
| 627 | { |
| 628 | T* ptr = 0; |
| 629 | UNORDERED_SCOPE(allocator2::allocate(size_type)) |
| 630 | { |
| 631 | UNORDERED_EPOINT("Mock allocator2 allocate function." ) |
| 632 | |
| 633 | using namespace std; |
| 634 | ptr = (T*)malloc(size: n * sizeof(T)); |
| 635 | if (!ptr) |
| 636 | throw std::bad_alloc(); |
| 637 | } |
| 638 | test::detail::tracker.track_allocate(ptr: (void*)ptr, n, size: sizeof(T), tag: tag_); |
| 639 | return pointer(ptr); |
| 640 | |
| 641 | // return pointer(static_cast<T*>(::operator new(n * sizeof(T)))); |
| 642 | } |
| 643 | |
| 644 | pointer allocate(size_type n, void const*) |
| 645 | { |
| 646 | T* ptr = 0; |
| 647 | UNORDERED_SCOPE(allocator2::allocate(size_type, const_pointer)) |
| 648 | { |
| 649 | UNORDERED_EPOINT("Mock allocator2 allocate function." ) |
| 650 | |
| 651 | using namespace std; |
| 652 | ptr = (T*)malloc(size: n * sizeof(T)); |
| 653 | if (!ptr) |
| 654 | throw std::bad_alloc(); |
| 655 | } |
| 656 | test::detail::tracker.track_allocate(ptr: (void*)ptr, n, size: sizeof(T), tag: tag_); |
| 657 | return pointer(ptr); |
| 658 | |
| 659 | // return pointer(static_cast<T*>(::operator new(n * sizeof(T)))); |
| 660 | } |
| 661 | |
| 662 | void deallocate(pointer p, size_type n) |
| 663 | { |
| 664 | //::operator delete((void*) p); |
| 665 | if (p) { |
| 666 | test::detail::tracker.track_deallocate(ptr: (void*)p, n, size: sizeof(T), tag: tag_); |
| 667 | using namespace std; |
| 668 | free(p); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | template <class U, class... Args> |
| 673 | void construct(U* p, Args&&... args) |
| 674 | { |
| 675 | UNORDERED_SCOPE(allocator2::construct(U*, Args&&...)) |
| 676 | { |
| 677 | UNORDERED_EPOINT("Mock allocator2 construct function." ) |
| 678 | new (p) U(std::forward<Args>(args)...); |
| 679 | } |
| 680 | test::detail::tracker.track_construct((void*)p, sizeof(U), tag_); |
| 681 | } |
| 682 | |
| 683 | template <class U> |
| 684 | void destroy(U* p) |
| 685 | { |
| 686 | test::detail::tracker.track_destroy((void*)p, sizeof(U), tag_); |
| 687 | p->~U(); |
| 688 | } |
| 689 | |
| 690 | size_type max_size() const |
| 691 | { |
| 692 | UNORDERED_SCOPE(allocator2::construct(pointer, T)) |
| 693 | { |
| 694 | UNORDERED_EPOINT("Mock allocator2 max_size function." ) |
| 695 | } |
| 696 | return (std::numeric_limits<std::size_t>::max)(); |
| 697 | } |
| 698 | |
| 699 | typedef false_type propagate_on_container_copy_assignment; |
| 700 | typedef false_type propagate_on_container_move_assignment; |
| 701 | typedef false_type propagate_on_container_swap; |
| 702 | }; |
| 703 | |
| 704 | template <class T> void swap(allocator2<T>& x, allocator2<T>& y) |
| 705 | { |
| 706 | std::swap(x.tag_, y.tag_); |
| 707 | } |
| 708 | |
| 709 | // It's pretty much impossible to write a compliant swap when these |
| 710 | // two can throw. So they don't. |
| 711 | |
| 712 | template <class T> |
| 713 | inline bool operator==(allocator2<T> const& x, allocator2<T> const& y) |
| 714 | { |
| 715 | // UNORDERED_SCOPE(operator==(allocator2, allocator2)) { |
| 716 | // UNORDERED_EPOINT("Mock allocator2 equality operator."); |
| 717 | //} |
| 718 | return x.tag_ == y.tag_; |
| 719 | } |
| 720 | |
| 721 | template <class T> |
| 722 | inline bool operator!=(allocator2<T> const& x, allocator2<T> const& y) |
| 723 | { |
| 724 | // UNORDERED_SCOPE(operator!=(allocator2, allocator2)) { |
| 725 | // UNORDERED_EPOINT("Mock allocator2 inequality operator."); |
| 726 | //} |
| 727 | return x.tag_ != y.tag_; |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | namespace test { |
| 733 | template <typename X> struct equals_to_compare; |
| 734 | template <> struct equals_to_compare<test::exception::equal_to> |
| 735 | { |
| 736 | typedef test::exception::less type; |
| 737 | }; |
| 738 | } |
| 739 | |
| 740 | // Workaround for ADL deficient compilers |
| 741 | #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) |
| 742 | namespace test { |
| 743 | test::exception::object generate( |
| 744 | test::exception::object const* x, random_generator g) |
| 745 | { |
| 746 | return test::exception::generate(x, g); |
| 747 | } |
| 748 | |
| 749 | std::pair<test::exception::object, test::exception::object> generate( |
| 750 | std::pair<test::exception::object, test::exception::object> const* x, |
| 751 | random_generator g) |
| 752 | { |
| 753 | return test::exception::generate(x, g); |
| 754 | } |
| 755 | } |
| 756 | #endif |
| 757 | |
| 758 | #endif |
| 759 | |