git repos / blockattack-game

blame: source/misc/embedded_libs/fmt-11.0.2/test/mock-allocator.h

normal view · raw

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:
47fd5c32 Poul Sander 2024-10-05 08:44 20
  using value_type = T;
47fd5c32 Poul Sander 2024-10-05 08:44 21
  using size_type = size_t;
47fd5c32 Poul Sander 2024-10-05 08:44 22
47fd5c32 Poul Sander 2024-10-05 08:44 23
  using pointer = T*;
47fd5c32 Poul Sander 2024-10-05 08:44 24
  using const_pointer = const T*;
47fd5c32 Poul Sander 2024-10-05 08:44 25
  using reference = T&;
47fd5c32 Poul Sander 2024-10-05 08:44 26
  using const_reference = const T&;
47fd5c32 Poul Sander 2024-10-05 08:44 27
  using difference_type = ptrdiff_t;
47fd5c32 Poul Sander 2024-10-05 08:44 28
47fd5c32 Poul Sander 2024-10-05 08:44 29
  template <typename U> struct rebind {
47fd5c32 Poul Sander 2024-10-05 08:44 30
    using other = mock_allocator<U>;
47fd5c32 Poul Sander 2024-10-05 08:44 31
  };
47fd5c32 Poul Sander 2024-10-05 08:44 32
f8deb5b9 Poul Sander 2022-01-15 13:55 33
  mock_allocator() {}
f8deb5b9 Poul Sander 2022-01-15 13:55 34
  mock_allocator(const mock_allocator&) {}
47fd5c32 Poul Sander 2024-10-05 08:44 35
47fd5c32 Poul Sander 2024-10-05 08:44 36
  MOCK_METHOD(T*, allocate, (size_t));
47fd5c32 Poul Sander 2024-10-05 08:44 37
  MOCK_METHOD(void, deallocate, (T*, size_t));
f8deb5b9 Poul Sander 2022-01-15 13:55 38
};
f8deb5b9 Poul Sander 2022-01-15 13:55 39
f8deb5b9 Poul Sander 2022-01-15 13:55 40
template <typename Allocator> class allocator_ref {
f8deb5b9 Poul Sander 2022-01-15 13:55 41
 private:
f8deb5b9 Poul Sander 2022-01-15 13:55 42
  Allocator* alloc_;
f8deb5b9 Poul Sander 2022-01-15 13:55 43
f8deb5b9 Poul Sander 2022-01-15 13:55 44
  void move(allocator_ref& other) {
f8deb5b9 Poul Sander 2022-01-15 13:55 45
    alloc_ = other.alloc_;
f8deb5b9 Poul Sander 2022-01-15 13:55 46
    other.alloc_ = nullptr;
f8deb5b9 Poul Sander 2022-01-15 13:55 47
  }
f8deb5b9 Poul Sander 2022-01-15 13:55 48
f8deb5b9 Poul Sander 2022-01-15 13:55 49
 public:
f8deb5b9 Poul Sander 2022-01-15 13:55 50
  using value_type = typename Allocator::value_type;
f8deb5b9 Poul Sander 2022-01-15 13:55 51
f8deb5b9 Poul Sander 2022-01-15 13:55 52
  explicit allocator_ref(Allocator* alloc = nullptr) : alloc_(alloc) {}
f8deb5b9 Poul Sander 2022-01-15 13:55 53
f8deb5b9 Poul Sander 2022-01-15 13:55 54
  allocator_ref(const allocator_ref& other) : alloc_(other.alloc_) {}
f8deb5b9 Poul Sander 2022-01-15 13:55 55
  allocator_ref(allocator_ref&& other) { move(other); }
f8deb5b9 Poul Sander 2022-01-15 13:55 56
f8deb5b9 Poul Sander 2022-01-15 13:55 57
  allocator_ref& operator=(allocator_ref&& other) {
f8deb5b9 Poul Sander 2022-01-15 13:55 58
    assert(this != &other);
f8deb5b9 Poul Sander 2022-01-15 13:55 59
    move(other);
f8deb5b9 Poul Sander 2022-01-15 13:55 60
    return *this;
f8deb5b9 Poul Sander 2022-01-15 13:55 61
  }
f8deb5b9 Poul Sander 2022-01-15 13:55 62
f8deb5b9 Poul Sander 2022-01-15 13:55 63
  allocator_ref& operator=(const allocator_ref& other) {
f8deb5b9 Poul Sander 2022-01-15 13:55 64
    alloc_ = other.alloc_;
f8deb5b9 Poul Sander 2022-01-15 13:55 65
    return *this;
f8deb5b9 Poul Sander 2022-01-15 13:55 66
  }
f8deb5b9 Poul Sander 2022-01-15 13:55 67
f8deb5b9 Poul Sander 2022-01-15 13:55 68
 public:
f8deb5b9 Poul Sander 2022-01-15 13:55 69
  Allocator* get() const { return alloc_; }
f8deb5b9 Poul Sander 2022-01-15 13:55 70
f8deb5b9 Poul Sander 2022-01-15 13:55 71
  value_type* allocate(size_t n) {
f8deb5b9 Poul Sander 2022-01-15 13:55 72
    return std::allocator_traits<Allocator>::allocate(*alloc_, n);
f8deb5b9 Poul Sander 2022-01-15 13:55 73
  }
f8deb5b9 Poul Sander 2022-01-15 13:55 74
  void deallocate(value_type* p, size_t n) { alloc_->deallocate(p, n); }
f8deb5b9 Poul Sander 2022-01-15 13:55 75
};
f8deb5b9 Poul Sander 2022-01-15 13:55 76
f8deb5b9 Poul Sander 2022-01-15 13:55 77
#endif  // FMT_MOCK_ALLOCATOR_H_
1970-01-01 00:00 78