]> git.proxmox.com Git - ceph.git/blame - ceph/src/fmt/test/mock-allocator.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / fmt / test / mock-allocator.h
CommitLineData
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
20effc67
TL
11#include <assert.h> // assert
12#include <stddef.h> // size_t
13
14#include <memory> // std::allocator_traits
15
16#include "gmock/gmock.h"
11fdf7f2 17
9f95a23c 18template <typename T> class mock_allocator {
11fdf7f2
TL
19 public:
20 mock_allocator() {}
9f95a23c 21 mock_allocator(const mock_allocator&) {}
20effc67 22 using value_type = T;
f67539c2
TL
23 MOCK_METHOD1_T(allocate, T*(size_t n));
24 MOCK_METHOD2_T(deallocate, void(T* p, size_t n));
11fdf7f2
TL
25};
26
9f95a23c 27template <typename Allocator> class allocator_ref {
11fdf7f2 28 private:
9f95a23c 29 Allocator* alloc_;
11fdf7f2 30
9f95a23c 31 void move(allocator_ref& other) {
11fdf7f2 32 alloc_ = other.alloc_;
f67539c2 33 other.alloc_ = nullptr;
11fdf7f2
TL
34 }
35
36 public:
20effc67 37 using value_type = typename Allocator::value_type;
11fdf7f2 38
f67539c2 39 explicit allocator_ref(Allocator* alloc = nullptr) : alloc_(alloc) {}
11fdf7f2 40
9f95a23c
TL
41 allocator_ref(const allocator_ref& other) : alloc_(other.alloc_) {}
42 allocator_ref(allocator_ref&& other) { move(other); }
11fdf7f2 43
9f95a23c 44 allocator_ref& operator=(allocator_ref&& other) {
11fdf7f2
TL
45 assert(this != &other);
46 move(other);
47 return *this;
48 }
49
9f95a23c 50 allocator_ref& operator=(const allocator_ref& other) {
11fdf7f2
TL
51 alloc_ = other.alloc_;
52 return *this;
53 }
54
55 public:
9f95a23c 56 Allocator* get() const { return alloc_; }
11fdf7f2 57
f67539c2
TL
58 value_type* allocate(size_t n) {
59 return std::allocator_traits<Allocator>::allocate(*alloc_, n);
11fdf7f2 60 }
f67539c2 61 void deallocate(value_type* p, size_t n) { alloc_->deallocate(p, n); }
11fdf7f2
TL
62};
63
64#endif // FMT_MOCK_ALLOCATOR_H_