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