1*5c90c05cSAndroid Build Coastguard Worker // Formatting library for C++ - mock allocator 2*5c90c05cSAndroid Build Coastguard Worker // 3*5c90c05cSAndroid Build Coastguard Worker // Copyright (c) 2012 - present, Victor Zverovich 4*5c90c05cSAndroid Build Coastguard Worker // All rights reserved. 5*5c90c05cSAndroid Build Coastguard Worker // 6*5c90c05cSAndroid Build Coastguard Worker // For the license information refer to format.h. 7*5c90c05cSAndroid Build Coastguard Worker 8*5c90c05cSAndroid Build Coastguard Worker #ifndef FMT_MOCK_ALLOCATOR_H_ 9*5c90c05cSAndroid Build Coastguard Worker #define FMT_MOCK_ALLOCATOR_H_ 10*5c90c05cSAndroid Build Coastguard Worker 11*5c90c05cSAndroid Build Coastguard Worker #include <assert.h> // assert 12*5c90c05cSAndroid Build Coastguard Worker #include <stddef.h> // size_t 13*5c90c05cSAndroid Build Coastguard Worker 14*5c90c05cSAndroid Build Coastguard Worker #include <memory> // std::allocator_traits 15*5c90c05cSAndroid Build Coastguard Worker 16*5c90c05cSAndroid Build Coastguard Worker #include "gmock/gmock.h" 17*5c90c05cSAndroid Build Coastguard Worker 18*5c90c05cSAndroid Build Coastguard Worker template <typename T> class mock_allocator { 19*5c90c05cSAndroid Build Coastguard Worker public: 20*5c90c05cSAndroid Build Coastguard Worker using value_type = T; 21*5c90c05cSAndroid Build Coastguard Worker using size_type = size_t; 22*5c90c05cSAndroid Build Coastguard Worker 23*5c90c05cSAndroid Build Coastguard Worker using pointer = T*; 24*5c90c05cSAndroid Build Coastguard Worker using const_pointer = const T*; 25*5c90c05cSAndroid Build Coastguard Worker using reference = T&; 26*5c90c05cSAndroid Build Coastguard Worker using const_reference = const T&; 27*5c90c05cSAndroid Build Coastguard Worker using difference_type = ptrdiff_t; 28*5c90c05cSAndroid Build Coastguard Worker 29*5c90c05cSAndroid Build Coastguard Worker template <typename U> struct rebind { 30*5c90c05cSAndroid Build Coastguard Worker using other = mock_allocator<U>; 31*5c90c05cSAndroid Build Coastguard Worker }; 32*5c90c05cSAndroid Build Coastguard Worker mock_allocator()33*5c90c05cSAndroid Build Coastguard Worker mock_allocator() {} mock_allocator(const mock_allocator &)34*5c90c05cSAndroid Build Coastguard Worker mock_allocator(const mock_allocator&) {} 35*5c90c05cSAndroid Build Coastguard Worker 36*5c90c05cSAndroid Build Coastguard Worker MOCK_METHOD(T*, allocate, (size_t)); 37*5c90c05cSAndroid Build Coastguard Worker MOCK_METHOD(void, deallocate, (T*, size_t)); 38*5c90c05cSAndroid Build Coastguard Worker }; 39*5c90c05cSAndroid Build Coastguard Worker 40*5c90c05cSAndroid Build Coastguard Worker template <typename Allocator> class allocator_ref { 41*5c90c05cSAndroid Build Coastguard Worker private: 42*5c90c05cSAndroid Build Coastguard Worker Allocator* alloc_; 43*5c90c05cSAndroid Build Coastguard Worker move(allocator_ref & other)44*5c90c05cSAndroid Build Coastguard Worker void move(allocator_ref& other) { 45*5c90c05cSAndroid Build Coastguard Worker alloc_ = other.alloc_; 46*5c90c05cSAndroid Build Coastguard Worker other.alloc_ = nullptr; 47*5c90c05cSAndroid Build Coastguard Worker } 48*5c90c05cSAndroid Build Coastguard Worker 49*5c90c05cSAndroid Build Coastguard Worker public: 50*5c90c05cSAndroid Build Coastguard Worker using value_type = typename Allocator::value_type; 51*5c90c05cSAndroid Build Coastguard Worker alloc_(alloc)52*5c90c05cSAndroid Build Coastguard Worker explicit allocator_ref(Allocator* alloc = nullptr) : alloc_(alloc) {} 53*5c90c05cSAndroid Build Coastguard Worker allocator_ref(const allocator_ref & other)54*5c90c05cSAndroid Build Coastguard Worker allocator_ref(const allocator_ref& other) : alloc_(other.alloc_) {} allocator_ref(allocator_ref && other)55*5c90c05cSAndroid Build Coastguard Worker allocator_ref(allocator_ref&& other) { move(other); } 56*5c90c05cSAndroid Build Coastguard Worker 57*5c90c05cSAndroid Build Coastguard Worker allocator_ref& operator=(allocator_ref&& other) { 58*5c90c05cSAndroid Build Coastguard Worker assert(this != &other); 59*5c90c05cSAndroid Build Coastguard Worker move(other); 60*5c90c05cSAndroid Build Coastguard Worker return *this; 61*5c90c05cSAndroid Build Coastguard Worker } 62*5c90c05cSAndroid Build Coastguard Worker 63*5c90c05cSAndroid Build Coastguard Worker allocator_ref& operator=(const allocator_ref& other) { 64*5c90c05cSAndroid Build Coastguard Worker alloc_ = other.alloc_; 65*5c90c05cSAndroid Build Coastguard Worker return *this; 66*5c90c05cSAndroid Build Coastguard Worker } 67*5c90c05cSAndroid Build Coastguard Worker 68*5c90c05cSAndroid Build Coastguard Worker public: get()69*5c90c05cSAndroid Build Coastguard Worker Allocator* get() const { return alloc_; } 70*5c90c05cSAndroid Build Coastguard Worker allocate(size_t n)71*5c90c05cSAndroid Build Coastguard Worker value_type* allocate(size_t n) { 72*5c90c05cSAndroid Build Coastguard Worker return std::allocator_traits<Allocator>::allocate(*alloc_, n); 73*5c90c05cSAndroid Build Coastguard Worker } deallocate(value_type * p,size_t n)74*5c90c05cSAndroid Build Coastguard Worker void deallocate(value_type* p, size_t n) { alloc_->deallocate(p, n); } 75*5c90c05cSAndroid Build Coastguard Worker }; 76*5c90c05cSAndroid Build Coastguard Worker 77*5c90c05cSAndroid Build Coastguard Worker #endif // FMT_MOCK_ALLOCATOR_H_ 78