KTL
Loading...
Searching...
No Matches
global.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 "global_fwd.h"
9
10#include <memory>
11#include <type_traits>
12
13namespace ktl
14{
15 template<typename Alloc>
16 class global
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 global() noexcept {}
28
29 global(const global& other) noexcept {}
30
31 global(global&& other) noexcept {}
32
33 global& operator=(const global& rhs) noexcept
34 {
35 return *this;
36 }
37
38 global& operator=(global&& rhs) noexcept
39 {
40 return *this;
41 }
42
43 bool operator==(const global& rhs) const noexcept
44 {
45 return true;
46 }
47
48 bool operator!=(const global& rhs) const noexcept
49 {
50 return true;
51 }
52
53#pragma region Allocation
54 void* allocate(size_t n, const source_location source = KTL_SOURCE())
55 noexcept(detail::has_nothrow_allocate_v<Alloc>)
56 {
57 return detail::allocate(s_Alloc, n, source);
58 }
59
60 void deallocate(void* p, size_t n)
62 {
63 s_Alloc.deallocate(p, n);
64 }
65#pragma endregion
66
67#pragma region Construction
68 template<typename T, typename... Args>
69 typename std::enable_if<detail::has_construct_v<Alloc, T*, Args...>, void>::type
70 construct(T* p, Args&&... args)
71 noexcept(detail::has_nothrow_construct_v<Alloc, T*, Args...>)
72 {
73 s_Alloc.construct(p, std::forward<Args>(args)...);
74 }
75
76 template<typename T>
77 typename std::enable_if<detail::has_destroy_v<Alloc, T*>, void>::type
78 destroy(T* p)
80 {
81 s_Alloc.destroy(p);
82 }
83#pragma endregion
84
85#pragma region Utility
86 template<typename A = Alloc>
87 typename std::enable_if<detail::has_max_size_v<A>, size_type>::type
88 max_size() const
89 noexcept(detail::has_nothrow_max_size_v<A>)
90 {
91 return s_Alloc.max_size();
92 }
93
94 template<typename A = Alloc>
95 typename std::enable_if<detail::has_owns_v<A>, bool>::type
96 owns(void* p) const
98 {
99 return s_Alloc.owns(p);
100 }
101#pragma endregion
102
103 void set_allocator(Alloc&& value) noexcept
104 {
105 s_Alloc = std::move(value);
106 }
107
108 Alloc& get_allocator() noexcept
109 {
110 return s_Alloc;
111 }
112
113 const Alloc& get_allocator() const noexcept
114 {
115 return s_Alloc;
116 }
117
118 private:
119 static inline Alloc s_Alloc{};
120 };
121}
Definition global.h:17
bool operator==(const global &rhs) const noexcept
Definition global.h:43
const Alloc & get_allocator() const noexcept
Definition global.h:113
global & operator=(global &&rhs) noexcept
Definition global.h:38
std::enable_if< detail::has_owns_v< A >, bool >::type owns(void *p) const noexcept(detail::has_nothrow_owns_v< A >)
Definition global.h:96
detail::get_size_type_t< Alloc > size_type
Definition global.h:22
void set_allocator(Alloc &&value) noexcept
Definition global.h:103
global & operator=(const global &rhs) noexcept
Definition global.h:33
bool operator!=(const global &rhs) const noexcept
Definition global.h:48
global(global &&other) noexcept
Definition global.h:31
std::enable_if< detail::has_destroy_v< Alloc, T * >, void >::type destroy(T *p) noexcept(detail::has_nothrow_destroy_v< Alloc, T * >)
Definition global.h:78
std::enable_if< detail::has_max_size_v< A >, size_type >::type max_size() const noexcept(detail::has_nothrow_max_size_v< A >)
Definition global.h:88
Alloc & get_allocator() noexcept
Definition global.h:108
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 global.h:70
global() noexcept
Constructor for forwarding any arguments to the underlying allocator.
Definition global.h:27
global(const global &other) noexcept
Definition global.h:29
void deallocate(void *p, size_t n) noexcept(detail::has_nothrow_deallocate_v< Alloc >)
Definition global.h:60
void * allocate(size_t n, const source_location source=KTL_SOURCE()) noexcept(detail::has_nothrow_allocate_v< Alloc >)
Definition global.h:54
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