allocators.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef ALLOCATORS_H
  9. #define ALLOCATORS_H
  10. #include <type_traits>
  11. #include <utility>
  12. #include "test_macros.h"
  13. #if TEST_STD_VER >= 11
  14. template <class T>
  15. class A1
  16. {
  17. int id_;
  18. public:
  19. explicit A1(int id = 0) TEST_NOEXCEPT : id_(id) {}
  20. typedef T value_type;
  21. int id() const {return id_;}
  22. static bool copy_called;
  23. static bool move_called;
  24. static bool allocate_called;
  25. static std::pair<T*, std::size_t> deallocate_called;
  26. A1(const A1& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
  27. A1(A1&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
  28. A1& operator=(const A1& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
  29. A1& operator=(A1&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
  30. template <class U>
  31. A1(const A1<U>& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
  32. template <class U>
  33. A1(A1<U>&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
  34. T* allocate(std::size_t n)
  35. {
  36. allocate_called = true;
  37. return (T*)n;
  38. }
  39. void deallocate(T* p, std::size_t n)
  40. {
  41. deallocate_called = std::pair<T*, std::size_t>(p, n);
  42. }
  43. std::size_t max_size() const {return id_;}
  44. };
  45. template <class T> bool A1<T>::copy_called = false;
  46. template <class T> bool A1<T>::move_called = false;
  47. template <class T> bool A1<T>::allocate_called = false;
  48. template <class T> std::pair<T*, std::size_t> A1<T>::deallocate_called;
  49. template <class T, class U>
  50. inline
  51. bool operator==(const A1<T>& x, const A1<U>& y)
  52. {
  53. return x.id() == y.id();
  54. }
  55. template <class T, class U>
  56. inline
  57. bool operator!=(const A1<T>& x, const A1<U>& y)
  58. {
  59. return !(x == y);
  60. }
  61. template <class T>
  62. class A2
  63. {
  64. int id_;
  65. public:
  66. explicit A2(int id = 0) TEST_NOEXCEPT : id_(id) {}
  67. typedef T value_type;
  68. typedef unsigned size_type;
  69. typedef int difference_type;
  70. typedef std::true_type propagate_on_container_move_assignment;
  71. int id() const {return id_;}
  72. static bool copy_called;
  73. static bool move_called;
  74. static bool allocate_called;
  75. A2(const A2& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
  76. A2(A2&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
  77. A2& operator=(const A2& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
  78. A2& operator=(A2&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
  79. T* allocate(std::size_t, const void* hint)
  80. {
  81. allocate_called = true;
  82. return (T*) const_cast<void *>(hint);
  83. }
  84. };
  85. template <class T> bool A2<T>::copy_called = false;
  86. template <class T> bool A2<T>::move_called = false;
  87. template <class T> bool A2<T>::allocate_called = false;
  88. template <class T, class U>
  89. inline
  90. bool operator==(const A2<T>& x, const A2<U>& y)
  91. {
  92. return x.id() == y.id();
  93. }
  94. template <class T, class U>
  95. inline
  96. bool operator!=(const A2<T>& x, const A2<U>& y)
  97. {
  98. return !(x == y);
  99. }
  100. template <class T>
  101. class A3
  102. {
  103. int id_;
  104. public:
  105. explicit A3(int id = 0) TEST_NOEXCEPT : id_(id) {}
  106. typedef T value_type;
  107. typedef std::true_type propagate_on_container_copy_assignment;
  108. typedef std::true_type propagate_on_container_swap;
  109. int id() const {return id_;}
  110. static bool copy_called;
  111. static bool move_called;
  112. static bool constructed;
  113. static bool destroy_called;
  114. A3(const A3& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
  115. A3(A3&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
  116. A3& operator=(const A3& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
  117. A3& operator=(A3&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
  118. template <class U, class ...Args>
  119. void construct(U* p, Args&& ...args)
  120. {
  121. ::new (p) U(std::forward<Args>(args)...);
  122. constructed = true;
  123. }
  124. template <class U>
  125. void destroy(U* p)
  126. {
  127. p->~U();
  128. destroy_called = true;
  129. }
  130. A3 select_on_container_copy_construction() const {return A3(-1);}
  131. };
  132. template <class T> bool A3<T>::copy_called = false;
  133. template <class T> bool A3<T>::move_called = false;
  134. template <class T> bool A3<T>::constructed = false;
  135. template <class T> bool A3<T>::destroy_called = false;
  136. template <class T, class U>
  137. inline
  138. bool operator==(const A3<T>& x, const A3<U>& y)
  139. {
  140. return x.id() == y.id();
  141. }
  142. template <class T, class U>
  143. inline
  144. bool operator!=(const A3<T>& x, const A3<U>& y)
  145. {
  146. return !(x == y);
  147. }
  148. #endif // TEST_STD_VER >= 11
  149. #endif // ALLOCATORS_H