4#ifndef GREM_CORE_DATA_INDIRECT_HPP
5#define GREM_CORE_DATA_INDIRECT_HPP
15#include <memory_resource>
21template <
typename T,
typename Allocator = std::allocator<T>>
28 using pointer =
typename std::allocator_traits<Allocator>::pointer;
29 using const_pointer =
typename std::allocator_traits<Allocator>::const_pointer;
31 constexpr Indirect()
requires(std::is_default_constructible_v<T> && std::is_default_constructible_v<Allocator>)
37 : allocator(allocator) {
41 template <
typename Arg,
typename... Args>
42 explicit(!std::is_convertible_v<Arg&&, T>)
constexpr Indirect(std::allocator_arg_t,
const Allocator& allocator, Arg&& arg, Args&&...
args)
43 requires(std::is_constructible_v<T, Arg &&, Args && ...>)
44 : allocator(allocator) {
45 emplace(std::forward<Arg>(arg), std::forward<Args>(
args)...);
48 template <
typename U,
typename... Args>
49 constexpr Indirect(std::allocator_arg_t,
const Allocator& allocator, std::initializer_list<U> ilist, Args&&...
args)
50 requires(std::is_constructible_v<T, std::initializer_list<U>&, Args && ...>)
51 : allocator(allocator) {
52 emplace(ilist, std::forward<Args>(
args)...);
55 template <
typename Arg,
typename... Args>
56 explicit(!std::is_convertible_v<Arg&&, T>)
constexpr Indirect(Arg&& arg, Args&&...
args)
57 requires(!std::is_same_v<std::remove_cvref_t<Arg>,
Indirect> && !std::is_convertible_v<Arg &&, std::allocator_arg_t> && std::is_constructible_v<T, Arg &&, Args && ...>)
60 template <
typename U,
typename... Args>
61 explicit constexpr Indirect(std::initializer_list<U> ilist, Args&&...
args)
requires(std::is_constructible_v<T, std::initializer_list<U>&, Args && ...>)
68 : allocator(allocator) {
75 :
Indirect(std::allocator_arg, other.get_allocator(), std::move(other)) {}
78 : object(std::exchange(other.object,
nullptr))
79 , allocator(allocator) {}
89 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
90 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
101 *
this =
Indirect(std::allocator_arg, allocator, other);
110 std::allocator_traits<
111 Allocator>::propagate_on_container_move_assignment::value ||
112 std::allocator_traits<Allocator>::is_always_equal::value) {
113 if (
this == &other) {
116 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value || std::allocator_traits<Allocator>::is_always_equal::value) {
118 object = std::exchange(other.object,
nullptr);
119 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
120 allocator = other.allocator;
123 if (allocator == other.allocator) {
125 object = std::exchange(other.object,
nullptr);
128 *
this =
Indirect(std::allocator_arg, allocator, other);
134 template <
typename U = T>
136 requires(!std::is_same_v<std::remove_cvref_t<U>,
Indirect> && std::is_constructible_v<T, U> && std::is_assignable_v<T&, U> &&
137 (!std::is_scalar_v<T> || !std::is_same_v<T, std::decay_t<U>>)) {
140#pragma clang diagnostic push
141#pragma clang diagnostic ignored "-Wshorten-64-to-32"
142#pragma clang diagnostic ignored "-Wfloat-conversion"
143#elif defined(__GNUC__)
144#pragma GCC diagnostic push
145#pragma GCC diagnostic ignored "-Wconversion"
146#pragma GCC diagnostic ignored "-Wfloat-conversion"
148 **
this = std::forward<U>(value);
150#pragma clang diagnostic pop
151#elif defined(__GNUC__)
152#pragma GCC diagnostic pop
155 emplace(std::forward<U>(value));
160 template <
typename U>
162 requires(std::is_constructible_v<T, const U&> && std::is_assignable_v<T&, const U&> && !std::is_constructible_v<T, Indirect<U>&> &&
163 !std::is_constructible_v<T, const Indirect<U>&> && !std::is_constructible_v<T, Indirect<U> &&> && !std::is_constructible_v<T, const Indirect<U> &&> &&
164 !std::is_convertible_v<Indirect<U>&, T> && !std::is_convertible_v<const Indirect<U>&, T> && !std::is_convertible_v<Indirect<U> &&, T> &&
165 !std::is_convertible_v<const Indirect<U> &&, T> && !std::is_assignable_v<T&, Indirect<U>&> && !std::is_assignable_v<T&, const Indirect<U>&> &&
166 !std::is_assignable_v<T&, Indirect<U> &&> && !std::is_assignable_v<T&, const Indirect<U> &&>) {
173 }
else if (other.object) {
179 template <
typename U>
181 requires(std::is_constructible_v<T, U> && std::is_assignable_v<T&, U> && !std::is_constructible_v<T, Indirect<U>&> && !std::is_constructible_v<T, const Indirect<U>&> &&
182 !std::is_constructible_v<T, Indirect<U> &&> && !std::is_constructible_v<T, const Indirect<U> &&> && !std::is_convertible_v<Indirect<U>&, T> &&
183 !std::is_convertible_v<const Indirect<U>&, T> && !std::is_convertible_v<Indirect<U> &&, T> && !std::is_convertible_v<const Indirect<U> &&, T> &&
184 !std::is_assignable_v<T&, Indirect<U>&> && !std::is_assignable_v<T&, const Indirect<U>&> && !std::is_assignable_v<T&, Indirect<U> &&> &&
185 !std::is_assignable_v<T&, const Indirect<U> &&>) {
188 **
this = std::move(**other);
192 }
else if (other.object) {
193 emplace(std::move(*other));
217 template <
typename OtherT,
typename OtherAllocator>
220 void reset() noexcept {
222 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(
object));
223 std::allocator_traits<Allocator>::deallocate(allocator,
object, 1);
228 template <
typename... Args>
231 object = std::allocator_traits<Allocator>::allocate(allocator, 1);
233 std::allocator_traits<Allocator>::construct(allocator, std::to_address(
object), std::forward<Args>(
args)...);
235 std::allocator_traits<Allocator>::deallocate(allocator,
object, 1);
242 [[no_unique_address]]
Allocator allocator;
245template <
typename A,
typename AllocatorA,
typename B,
typename AllocatorB>
247 if (a.get() && b.get()) {
250 return static_cast<bool>(a.get()) ==
static_cast<bool>(b.get());
253template <
typename A,
typename AllocatorA,
typename B>
258#pragma clang diagnostic push
259#pragma clang diagnostic ignored "-Wsign-compare"
260#elif defined(__GNUC__)
261#pragma GCC diagnostic push
262#pragma GCC diagnostic ignored "-Wsign-compare"
264 return (a.get()) ? *a == b :
false;
266#pragma clang diagnostic pop
267#elif defined(__GNUC__)
268#pragma GCC diagnostic pop
272template <
typename A,
typename AllocatorA,
typename B,
typename AllocatorB>
274 if (a.get() && b.get()) {
277 return static_cast<bool>(a.get()) <=>
static_cast<bool>(b.get());
280template <
typename A,
typename AllocatorA,
typename B>
284 return (a.get()) ? *a <=> b : std::strong_ordering::less;
289template <
typename T,
typename Allocator>
290struct std::hash<
grem::Indirect<T, Allocator>> {
292 return (indirect.
get()) ? hasher(*indirect) : 0;
296 [[no_unique_address]] std::hash<T> hasher;
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition Indirect.hpp:22
constexpr Indirect(std::allocator_arg_t, const Allocator &allocator, const Indirect &other)
Definition Indirect.hpp:67
typename std::allocator_traits< Allocator >::const_pointer const_pointer
Definition Indirect.hpp:29
Args && args
Definition Indirect.hpp:58
constexpr Indirect(const Indirect &other)
Definition Indirect.hpp:64
T value_type
Definition Indirect.hpp:24
constexpr Indirect()
Definition Indirect.hpp:31
const T & const_reference
Definition Indirect.hpp:27
constexpr Indirect(std::allocator_arg_t, const Allocator &allocator, std::initializer_list< U > ilist, Args &&... args)
Definition Indirect.hpp:49
constexpr Indirect(Indirect &&other) noexcept
Definition Indirect.hpp:74
constexpr Indirect(std::initializer_list< U > ilist, Args &&... args)
Definition Indirect.hpp:61
constexpr Indirect(std::allocator_arg_t, const Allocator &allocator=Allocator())
Definition Indirect.hpp:36
constexpr Indirect & operator=(const Indirect &other)
Definition Indirect.hpp:85
~Indirect()
Definition Indirect.hpp:81
constexpr Indirect & operator=(const Indirect< U > &other)
Definition Indirect.hpp:161
constexpr pointer operator->() const noexcept
Definition Indirect.hpp:206
Allocator allocator_type
Definition Indirect.hpp:25
allocator_type get_allocator() const noexcept
Definition Indirect.hpp:198
constexpr Indirect & operator=(Indirect &&other) noexcept(std::allocator_traits< Allocator >::propagate_on_container_move_assignment::value||//NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor) std::allocator_traits< Allocator >::is_always_equal::value)
Definition Indirect.hpp:109
constexpr Indirect & operator=(U &&value)
Definition Indirect.hpp:135
friend class Indirect
Definition Indirect.hpp:218
constexpr Indirect(std::allocator_arg_t, const Allocator &allocator, Indirect &&other) noexcept
Definition Indirect.hpp:77
T & reference
Definition Indirect.hpp:26
typename std::allocator_traits< Allocator >::pointer pointer
Definition Indirect.hpp:28
constexpr reference operator*() const noexcept
Definition Indirect.hpp:211
constexpr pointer get() const noexcept
Definition Indirect.hpp:202
constexpr Indirect & operator=(Indirect< U > &&other)
Definition Indirect.hpp:180
Definition concepts.hpp:160
Definition concepts.hpp:106
Definition concepts.hpp:41
Definition concepts.hpp:163
Definition Allocation.hpp:554
grem::Indirect< T, std::pmr::polymorphic_allocator< T > > Indirect
Definition Indirect.hpp:302
constexpr bool operator==(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:246
constexpr std::compare_three_way_result_t< A, B > operator<=>(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:273
std::size_t operator()(const grem::Indirect< T, Allocator > &indirect) const
Definition Indirect.hpp:291