]> git.proxmox.com Git - ceph.git/blob - ceph/src/fmt/test/mock-allocator.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / fmt / test / mock-allocator.h
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:
16 mock_allocator() {}
17 mock_allocator(const mock_allocator&) {}
18 typedef T value_type;
19 MOCK_METHOD1_T(allocate, T*(std::size_t n));
20 MOCK_METHOD2_T(deallocate, void(T* p, std::size_t n));
21 };
22
23 template <typename Allocator> class allocator_ref {
24 private:
25 Allocator* alloc_;
26
27 void move(allocator_ref& other) {
28 alloc_ = other.alloc_;
29 other.alloc_ = FMT_NULL;
30 }
31
32 public:
33 typedef typename Allocator::value_type value_type;
34
35 explicit allocator_ref(Allocator* alloc = FMT_NULL) : alloc_(alloc) {}
36
37 allocator_ref(const allocator_ref& other) : alloc_(other.alloc_) {}
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:
52 Allocator* get() const { return alloc_; }
53
54 value_type* allocate(std::size_t n) {
55 return fmt::internal::allocate(*alloc_, n);
56 }
57 void deallocate(value_type* p, std::size_t n) { alloc_->deallocate(p, n); }
58 };
59
60 #endif // FMT_MOCK_ALLOCATOR_H_