"ScL Feature"/
SCL Feature 0.0.1
C++20 composable proxy wrapper with pluggable executor strategy
Loading...
Searching...
No Matches
uninitialized.h
1#pragma once
2
3#include <concepts>
4#include <cstddef>
5#include <functional>
6#include <utility>
7
8#include <scl/utility/type_traits/forward_like.h>
9
10#define SCL_EXECUTOR_CONSTRUCTOR_FOR_SELF_PROTOTYPE(cv_ref) \
11 constexpr explicit uninitialized(self_type cv_ref other) noexcept \
12 { \
13 for (::std::size_t i = 0; i < sizeof(value_type); ++i) \
14 m_storage[i] = other.m_storage[i]; \
15 }
16
17#define SCL_EXECUTOR_CONSTRUCTOR_FOR_SELF \
18 SCL_EXECUTOR_CONSTRUCTOR_FOR_SELF_PROTOTYPE(&) \
19 SCL_EXECUTOR_CONSTRUCTOR_FOR_SELF_PROTOTYPE(const &) \
20 SCL_EXECUTOR_CONSTRUCTOR_FOR_SELF_PROTOTYPE(volatile &) \
21 SCL_EXECUTOR_CONSTRUCTOR_FOR_SELF_PROTOTYPE(const volatile &) \
22 SCL_EXECUTOR_CONSTRUCTOR_FOR_SELF_PROTOTYPE(&&) \
23 SCL_EXECUTOR_CONSTRUCTOR_FOR_SELF_PROTOTYPE(const &&) \
24 SCL_EXECUTOR_CONSTRUCTOR_FOR_SELF_PROTOTYPE(volatile &&) \
25 SCL_EXECUTOR_CONSTRUCTOR_FOR_SELF_PROTOTYPE(const volatile &&)
26
27namespace scl::feature::inplace
28{
41 // NOLINTBEGIN(cppcoreguidelines-special-member-functions, cppcoreguidelines-missing-std-forward, cppcoreguidelines-pro-type-member-init)
42 template <typename Value>
43 class uninitialized
44 {
45 using self_type = uninitialized<Value>;
46
47 public:
48 using value_type = Value;
49
50 public:
51 constexpr uninitialized() noexcept = default;
52
53 SCL_EXECUTOR_CONSTRUCTOR_FOR_SELF
54
55 public:
56 template <typename Self, typename Func, typename... Args>
57 static constexpr decltype(auto) execute(Self && self, Func && func, Args &&... args)
58 requires(::std::same_as<::std::remove_cvref_t<Self>, self_type> && ::std::invocable<Func, Args && ...>)
59 {
60 return ::std::invoke(::std::forward<Func>(func), ::std::forward<Args>(args)...);
61 }
62
63 template <typename Self>
64 static constexpr decltype(auto) value(Self && self)
65 requires ::std::same_as<::std::remove_cvref_t<Self>, self_type>
66 {
67 // reinterpret_cast<value_type*> for raw storage access;
68 // const correctness is restored by forward_like<Self>.
69 return ::scl::forward_like<Self>(*reinterpret_cast<value_type *>(self.m_storage)); // NOLINT(*-reinterpret-cast)
70 }
71
72 private:
73 alignas(value_type)::std::byte m_storage[sizeof(value_type)]; // NOLINT(*-avoid-c-arrays)
74 };
75 // NOLINTEND(cppcoreguidelines-special-member-functions, cppcoreguidelines-missing-std-forward, cppcoreguidelines-pro-type-member-init)
76} // namespace scl::feature::inplace
77
78#undef SCL_EXECUTOR_CONSTRUCTOR_FOR_SELF
79#undef SCL_EXECUTOR_CONSTRUCTOR_FOR_SELF_PROTOTYPE