KTL
Loading...
Searching...
No Matches
reference.h
Go to the documentation of this file.
1#pragma once
2
3#include "../utility/aligned_malloc.h"
4#include "../utility/alignment.h"
5#include "../utility/empty_base.h"
6#include "../utility/meta.h"
7#include "../utility/source_location.h"
8#include "reference_fwd.h"
9
10#include <memory>
11#include <type_traits>
12
13namespace ktl
14{
15 template<typename Alloc>
17 {
18 private:
19 static_assert(detail::has_no_value_type_v<Alloc>, "Building on top of typed allocators is not allowed. Use allocators without a type");
20
21 public:
23
27 explicit reference(Alloc& alloc) noexcept:
28 m_Alloc(&alloc) {}
29
30 reference(const reference& other) noexcept :
31 m_Alloc(other.m_Alloc) {}
32
33 reference(reference&& other) noexcept :
34 m_Alloc(other.m_Alloc) {}
35
36 reference& operator=(const reference& rhs) noexcept
37 {
38 m_Alloc = rhs.m_Alloc;
39
40 return *this;
41 }
42
43 reference& operator=(reference&& rhs) noexcept
44 {
45 m_Alloc = rhs.m_Alloc;
46
47 return *this;
48 }
49
50 bool operator==(const reference& rhs) const
52 {
53 return m_Alloc == rhs.m_Alloc && *m_Alloc == *rhs.m_Alloc;
54 }
55
56 bool operator!=(const reference& rhs) const
58 {
59 return m_Alloc != rhs.m_Alloc || *m_Alloc != *rhs.m_Alloc;
60 }
61
62#pragma region Allocation
63 void* allocate(size_t n, const source_location source = KTL_SOURCE())
64 noexcept(detail::has_nothrow_allocate_v<Alloc>)
65 {
66 return detail::allocate(*m_Alloc, n, source);
67 }
68
69 void deallocate(void* p, size_t n)
71 {
72 m_Alloc->deallocate(p, n);
73 }
74#pragma endregion
75
76#pragma region Construction
77 template<typename T, typename... Args>
78 typename std::enable_if<detail::has_construct_v<Alloc, T*, Args...>, void>::type
79 construct(T* p, Args&&... args)
80 noexcept(detail::has_nothrow_construct_v<Alloc, T*, Args...>)
81 {
82 m_Alloc->construct(p, std::forward<Args>(args)...);
83 }
84
85 template<typename T>
86 typename std::enable_if<detail::has_destroy_v<Alloc, T*>, void>::type
87 destroy(T* p)
89 {
90 m_Alloc->destroy(p);
91 }
92#pragma endregion
93
94#pragma region Utility
95 template<typename A = Alloc>
96 typename std::enable_if<detail::has_max_size_v<A>, size_type>::type
97 max_size() const
98 noexcept(detail::has_nothrow_max_size_v<A>)
99 {
100 return m_Alloc->max_size();
101 }
102
103 template<typename A = Alloc>
104 typename std::enable_if<detail::has_owns_v<A>, bool>::type
105 owns(void* p) const
107 {
108 return m_Alloc->owns(p);
109 }
110#pragma endregion
111
112 Alloc& get_allocator() noexcept
113 {
114 return *m_Alloc;
115 }
116
117 const Alloc& get_allocator() const noexcept
118 {
119 return *m_Alloc;
120 }
121
122 private:
123 Alloc* m_Alloc;
124 };
125}
Definition reference.h:17
void deallocate(void *p, size_t n) noexcept(detail::has_nothrow_deallocate_v< Alloc >)
Definition reference.h:69
detail::get_size_type_t< Alloc > size_type
Definition reference.h:22
Alloc & get_allocator() noexcept
Definition reference.h:112
const Alloc & get_allocator() const noexcept
Definition reference.h:117
void * allocate(size_t n, const source_location source=KTL_SOURCE()) noexcept(detail::has_nothrow_allocate_v< Alloc >)
Definition reference.h:63
std::enable_if< detail::has_destroy_v< Alloc, T * >, void >::type destroy(T *p) noexcept(detail::has_nothrow_destroy_v< Alloc, T * >)
Definition reference.h:87
bool operator==(const reference &rhs) const noexcept(detail::has_nothrow_equal_v< Alloc >)
Definition reference.h:50
bool operator!=(const reference &rhs) const noexcept(detail::has_nothrow_not_equal_v< Alloc >)
Definition reference.h:56
reference & operator=(reference &&rhs) noexcept
Definition reference.h:43
reference(Alloc &alloc) noexcept
Constructor for forwarding any arguments to the underlying allocator.
Definition reference.h:27
reference(const reference &other) noexcept
Definition reference.h:30
reference(reference &&other) noexcept
Definition reference.h:33
reference & operator=(const reference &rhs) noexcept
Definition reference.h:36
std::enable_if< detail::has_construct_v< Alloc, T *, Args... >, void >::type construct(T *p, Args &&... args) noexcept(detail::has_nothrow_construct_v< Alloc, T *, Args... >)
Definition reference.h:79
std::enable_if< detail::has_owns_v< A >, bool >::type owns(void *p) const noexcept(detail::has_nothrow_owns_v< A >)
Definition reference.h:105
std::enable_if< detail::has_max_size_v< A >, size_type >::type max_size() const noexcept(detail::has_nothrow_max_size_v< A >)
Definition reference.h:97
typename get_size_type< Alloc, void >::type get_size_type_t
Definition meta.h:33
constexpr bool has_nothrow_construct_v
Definition meta.h:123
constexpr bool has_construct_v
Definition meta.h:53
void * allocate(Alloc &alloc, size_t n, const source_location source) noexcept(false)
Definition meta.h:161
constexpr bool has_no_value_type_v
Definition meta.h:17
Definition cascading.h:16
#define KTL_SOURCE()
Definition source_location.h:11
Definition source_location.h:19