Iros
 
Loading...
Searching...
No Matches
std_allocator.h
Go to the documentation of this file.
1#pragma once
2
4#include "di/types/size_t.h"
5
6#ifndef DI_NO_USE_STD
7#include <memory>
8#else
9#include "di/util/std_new.h"
10#include "di/util/voidify.h"
11
12namespace std {
13template<typename T, T v>
14struct integral_constant {
15 constexpr static T value = v;
16 using value_type = T;
17 using type = integral_constant;
18 constexpr operator T() const noexcept { return value; }
19 constexpr auto operator()() const noexcept -> T { return value; }
20};
21
22template<typename T>
23struct allocator {
24 using value_type = T;
25 using size_type = size_t;
26 using difference_type = ptrdiff_t;
27 using propagate_on_container_move_assignment = integral_constant<bool, true>;
28
29 constexpr allocator() noexcept = default;
30
31 template<typename U>
32 constexpr allocator(allocator<U> const&) noexcept {}
33
34 constexpr ~allocator() = default;
35
36 [[nodiscard]] constexpr auto allocate(size_t count) -> T* {
37 if consteval {
38 return static_cast<T*>(::operator new(count * sizeof(T)));
39 }
40 if constexpr (alignof(T) > alignof(void*)) {
41 return static_cast<T*>(::operator new(count * sizeof(T), std::align_val_t { alignof(T) }));
42 } else {
43 return static_cast<T*>(::operator new(count * sizeof(T)));
44 }
45 }
46
47 constexpr void deallocate(T* typed_pointer, size_t count) {
48 auto* pointer = di::voidify(typed_pointer);
49 if consteval {
50 return ::operator delete(pointer);
51 }
52 if constexpr (alignof(T) > alignof(void*)) {
53 return ::operator delete(pointer, count * sizeof(T), std::align_val_t { alignof(T) });
54 } else {
55 return ::operator delete(pointer, count * sizeof(T));
56 }
57 }
58};
59
60template<class T, class U>
61constexpr auto operator==(allocator<T> const&, allocator<U> const&) noexcept -> bool {
62 return true;
63}
64}
65#endif
66
67namespace di::container {
68template<typename T>
69using StdAllocator = std::allocator<T>;
70}
__CCPP_BEGIN_DECLARATIONS typedef __SIZE_TYPE__ size_t
Definition size_t.h:7
constexpr auto operator==(Duration< Rep1, Period1 > const &a, Duration< Rep2, Period2 > const &b) -> bool
Definition duration.h:108
Definition sequence.h:12
constexpr auto allocate
Definition allocate.h:26
constexpr auto deallocate
Definition deallocate.h:23
std::allocator< T > StdAllocator
Definition std_allocator.h:69
constexpr auto count
Definition count.h:37
constexpr auto value
Definition value.h:34
std::ptrdiff_t ptrdiff_t
Definition ptrdiff_t.h:12
constexpr auto voidify
Definition voidify.h:13
Definition enable_generate_structed_bindings.h:46
@ T
Definition key.h:29