]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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 "gmock.h" | |
12 | #include "fmt/format.h" | |
13 | ||
14 | template <typename T> | |
15 | class mock_allocator { | |
16 | public: | |
17 | mock_allocator() {} | |
18 | mock_allocator(const mock_allocator &) {} | |
19 | typedef T value_type; | |
20 | MOCK_METHOD1_T(allocate, T* (std::size_t n)); | |
21 | MOCK_METHOD2_T(deallocate, void (T* p, std::size_t n)); | |
22 | }; | |
23 | ||
24 | template <typename Allocator> | |
25 | class allocator_ref { | |
26 | private: | |
27 | Allocator *alloc_; | |
28 | ||
29 | void move(allocator_ref &other) { | |
30 | alloc_ = other.alloc_; | |
eafe8130 | 31 | other.alloc_ = FMT_NULL; |
11fdf7f2 TL |
32 | } |
33 | ||
34 | public: | |
35 | typedef typename Allocator::value_type value_type; | |
36 | ||
eafe8130 | 37 | explicit allocator_ref(Allocator *alloc = FMT_NULL) : alloc_(alloc) {} |
11fdf7f2 TL |
38 | |
39 | allocator_ref(const allocator_ref &other) : alloc_(other.alloc_) {} | |
40 | allocator_ref(allocator_ref &&other) { move(other); } | |
41 | ||
42 | allocator_ref& operator=(allocator_ref &&other) { | |
43 | assert(this != &other); | |
44 | move(other); | |
45 | return *this; | |
46 | } | |
47 | ||
48 | allocator_ref& operator=(const allocator_ref &other) { | |
49 | alloc_ = other.alloc_; | |
50 | return *this; | |
51 | } | |
52 | ||
53 | public: | |
54 | Allocator *get() const { return alloc_; } | |
55 | ||
56 | value_type* allocate(std::size_t n) { | |
57 | return fmt::internal::allocate(*alloc_, n); | |
58 | } | |
59 | void deallocate(value_type* p, std::size_t n) { alloc_->deallocate(p, n); } | |
60 | }; | |
61 | ||
62 | #endif // FMT_MOCK_ALLOCATOR_H_ |