1 // Formatting library for C++ - mock allocator 2 // 3 // Copyright (c) 2012 - present, Victor Zverovich 4 // All rights reserved. 5 // 6 // For the license information refer to format.h. 7 8 #ifndef FMT_MOCK_ALLOCATOR_H_ 9 #define FMT_MOCK_ALLOCATOR_H_ 10 11 #include "fmt/format.h" 12 #include "gmock.h" 13 14 template <typename T> class mock_allocator { 15 public: mock_allocator()16 mock_allocator() {} mock_allocator(const mock_allocator &)17 mock_allocator(const mock_allocator&) {} 18 typedef T value_type; 19 MOCK_METHOD1_T(allocate, T*(size_t n)); 20 MOCK_METHOD2_T(deallocate, void(T* p, size_t n)); 21 }; 22 23 template <typename Allocator> class allocator_ref { 24 private: 25 Allocator* alloc_; 26 move(allocator_ref & other)27 void move(allocator_ref& other) { 28 alloc_ = other.alloc_; 29 other.alloc_ = nullptr; 30 } 31 32 public: 33 typedef typename Allocator::value_type value_type; 34 alloc_(alloc)35 explicit allocator_ref(Allocator* alloc = nullptr) : alloc_(alloc) {} 36 allocator_ref(const allocator_ref & other)37 allocator_ref(const allocator_ref& other) : alloc_(other.alloc_) {} allocator_ref(allocator_ref && other)38 allocator_ref(allocator_ref&& other) { move(other); } 39 40 allocator_ref& operator=(allocator_ref&& other) { 41 assert(this != &other); 42 move(other); 43 return *this; 44 } 45 46 allocator_ref& operator=(const allocator_ref& other) { 47 alloc_ = other.alloc_; 48 return *this; 49 } 50 51 public: get()52 Allocator* get() const { return alloc_; } 53 allocate(size_t n)54 value_type* allocate(size_t n) { 55 return std::allocator_traits<Allocator>::allocate(*alloc_, n); 56 } deallocate(value_type * p,size_t n)57 void deallocate(value_type* p, size_t n) { alloc_->deallocate(p, n); } 58 }; 59 60 #endif // FMT_MOCK_ALLOCATOR_H_ 61