Flow
Documentation for the Flow C++ Library
Loading...
Searching...
No Matches
flow_polymorphic_allocator.h
Go to the documentation of this file.
1#pragma once
3#include <cstddef>
4
5namespace flow {
6
9 template <typename T = std::byte>
11 public:
12 using value_type = T;
13
16 : resource_(&DefaultMemoryResource::getResource()) {
17 }
18
21 explicit PolymorphicAllocator(MemoryResource& resource) noexcept
22 : resource_(&resource) {
23 }
24
27 template <typename U>
29 : resource_(allocator.resource_) {
30 }
31
35 T* allocate(std::size_t count) {
36 return static_cast<T*>(resource_->allocate(sizeof(T) * count, alignof(T)));
37 }
38
42 void deallocate(T* address, std::size_t count) noexcept {
43 resource_->deallocate(address, sizeof(T) * count, alignof(T));
44 }
45
50 template <typename U, typename... Args>
51 void construct(U* address, Args&&... args) {
52 ::new (address) U(std::forward<Args>(args)...);
53 }
54
57 template <typename U>
58 void destroy(U* address) noexcept {
59 address->~U();
60 }
61
62 private:
64
65 template <typename U>
67
68 template <typename U>
69 friend bool operator==(const PolymorphicAllocator<T>& lhs, const PolymorphicAllocator<U>& rhs) noexcept {
70 return lhs.resource_ == rhs.resource_;
71 }
72
73 template <typename U>
74 friend bool operator!=(const PolymorphicAllocator<T>& lhs, const PolymorphicAllocator<U>& rhs) noexcept {
75 return !(lhs == rhs);
76 }
77 };
78
79}
A default memory resource that wraps global operator new and operator delete.
A memory resource holder interface for the PolymorphicAllocator. Responsible for allocate and dealloc...
MemoryResource * resource_
Non-owning pointer to memory resource.
void deallocate(T *address, std::size_t count) noexcept
Deallocate memory.
friend bool operator==(const PolymorphicAllocator< T > &lhs, const PolymorphicAllocator< U > &rhs) noexcept
PolymorphicAllocator(MemoryResource &resource) noexcept
Construct allocator with a specific memory resource.
PolymorphicAllocator() noexcept
Default constructor using the default memory resource.
T * allocate(std::size_t count)
Allocate memory for count objects.
PolymorphicAllocator(const PolymorphicAllocator< U > &allocator) noexcept
Copy constructor from another polymorphic allocator.
void destroy(U *address) noexcept
Destroy the object at the given address.
void construct(U *address, Args &&... args)
Construct an object in place at the given address.
friend bool operator!=(const PolymorphicAllocator< T > &lhs, const PolymorphicAllocator< U > &rhs) noexcept