1 /* 2 Mock allocator. 3 4 Copyright (c) 2014, Victor Zverovich 5 All rights reserved. 6 7 Redistribution and use in source and binary forms, with or without 8 modification, are permitted provided that the following conditions are met: 9 10 1. Redistributions of source code must retain the above copyright notice, this 11 list of conditions and the following disclaimer. 12 2. Redistributions in binary form must reproduce the above copyright notice, 13 this list of conditions and the following disclaimer in the documentation 14 and/or other materials provided with the distribution. 15 16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 20 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef FMT_MOCK_ALLOCATOR_H_ 29 #define FMT_MOCK_ALLOCATOR_H_ 30 31 #include "gmock/gmock.h" 32 33 template <typename T> 34 class MockAllocator { 35 public: MockAllocator()36 MockAllocator() {} MockAllocator(const MockAllocator &)37 MockAllocator(const MockAllocator &) {} 38 typedef T value_type; 39 MOCK_METHOD2_T(allocate, T *(std::size_t n, const T *h)); 40 MOCK_METHOD2_T(deallocate, void (T *p, std::size_t n)); 41 }; 42 43 template <typename Allocator> 44 class AllocatorRef { 45 private: 46 Allocator *alloc_; 47 48 public: 49 typedef typename Allocator::value_type value_type; 50 alloc_(alloc)51 explicit AllocatorRef(Allocator *alloc = 0) : alloc_(alloc) {} 52 AllocatorRef(const AllocatorRef & other)53 AllocatorRef(const AllocatorRef &other) : alloc_(other.alloc_) {} 54 55 AllocatorRef& operator=(const AllocatorRef &other) { 56 alloc_ = other.alloc_; 57 return *this; 58 } 59 60 #if FMT_USE_RVALUE_REFERENCES 61 private: move(AllocatorRef & other)62 void move(AllocatorRef &other) { 63 alloc_ = other.alloc_; 64 other.alloc_ = 0; 65 } 66 67 public: AllocatorRef(AllocatorRef && other)68 AllocatorRef(AllocatorRef &&other) { 69 move(other); 70 } 71 72 AllocatorRef& operator=(AllocatorRef &&other) { 73 assert(this != &other); 74 move(other); 75 return *this; 76 } 77 #endif 78 get()79 Allocator *get() const { return alloc_; } 80 allocate(std::size_t n,const value_type * h)81 value_type *allocate(std::size_t n, const value_type *h) { 82 return alloc_->allocate(n, h); 83 } deallocate(value_type * p,std::size_t n)84 void deallocate(value_type *p, std::size_t n) { alloc_->deallocate(p, n); } 85 }; 86 87 #endif // FMT_MOCK_ALLOCATOR_H_ 88