8 #ifndef FMT_MOCK_ALLOCATOR_H_ 9 #define FMT_MOCK_ALLOCATOR_H_ 16 #include "gmock/gmock.h" 18 template <
typename T>
class mock_allocator {
21 using size_type = size_t;
24 using const_pointer =
const T*;
26 using const_reference =
const T&;
27 using difference_type = ptrdiff_t;
29 template <
typename U>
struct rebind {
30 using other = mock_allocator<U>;
34 mock_allocator(
const mock_allocator&) {}
36 MOCK_METHOD(T*, allocate, (
size_t));
37 MOCK_METHOD(
void, deallocate, (T*,
size_t));
40 template <
typename Allocator,
bool PropagateOnMove = true>
class allocator_ref {
44 void move(allocator_ref& other) {
45 alloc_ = other.alloc_;
46 other.alloc_ =
nullptr;
50 using value_type =
typename Allocator::value_type;
51 using propagate_on_container_move_assignment =
52 fmt::bool_constant<PropagateOnMove>;
54 explicit allocator_ref(Allocator* alloc =
nullptr) : alloc_(alloc) {}
56 allocator_ref(
const allocator_ref& other) : alloc_(other.alloc_) {}
57 allocator_ref(allocator_ref&& other) { move(other); }
59 allocator_ref& operator=(allocator_ref&& other) {
60 assert(
this != &other);
65 allocator_ref& operator=(
const allocator_ref& other) {
66 alloc_ = other.alloc_;
71 auto get()
const -> Allocator* {
return alloc_; }
73 auto allocate(
size_t n) -> value_type* {
74 return std::allocator_traits<Allocator>::allocate(*alloc_, n);
76 void deallocate(value_type* p,
size_t n) { alloc_->deallocate(p, n); }
78 friend auto operator==(allocator_ref a, allocator_ref b) noexcept ->
bool {
79 if (a.alloc_ == b.alloc_)
return true;
80 return a.alloc_ && b.alloc_ && *a.alloc_ == *b.alloc_;
83 friend auto operator!=(allocator_ref a, allocator_ref b) noexcept ->
bool {
88 #endif // FMT_MOCK_ALLOCATOR_H_