kokkos-execution 0.0.1
Loading...
Searching...
No Matches
optional_storage.hpp
Go to the documentation of this file.
1#ifndef KOKKOS_EXECUTION_IMPL_OPTIONAL_STORAGE_HPP
2#define KOKKOS_EXECUTION_IMPL_OPTIONAL_STORAGE_HPP
3
5
6#include "Kokkos_Macros.hpp"
7#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
8import kokkos.core;
9# include "Kokkos_Assert.hpp"
10#else
11# include "Kokkos_Core.hpp"
12#endif
13
15
26template <typename T>
28 public:
29 constexpr OptionalStorage() noexcept = default;
30
31 constexpr ~OptionalStorage() {
32 if (m_has_value) {
33 m_storage.__destroy();
34 }
35 }
36
41
42 template <typename... Args>
43 constexpr auto emplace(Args&&... args) noexcept(std::is_nothrow_constructible_v<T, Args...>) -> T& {
44 KOKKOS_ASSERT(!m_has_value);
45 auto& ref = m_storage.__construct(std::forward<Args>(args)...);
46 m_has_value = true;
47 return ref;
48 }
49
50 template <typename Func, typename... Args>
51 constexpr auto emplace_from(Func&& func, Args&&... args)
52 noexcept(noexcept(T{std::forward<Func>(func)(std::forward<Args>(args)...)})) -> T& {
53 KOKKOS_ASSERT(!m_has_value);
54 auto& ref = m_storage.__construct_from(std::forward<Func>(func), std::forward<Args>(args)...);
55 m_has_value = true;
56 return ref;
57 }
58
59 constexpr void reset() noexcept {
60 KOKKOS_ASSERT(m_has_value);
61 m_storage.__destroy();
62 m_has_value = false;
63 }
64
65 constexpr auto get() & noexcept -> T& {
66 KOKKOS_ASSERT(m_has_value);
67 return m_storage.__get();
68 }
69
70 constexpr auto get() const & noexcept -> const T& {
71 KOKKOS_ASSERT(m_has_value);
72 return m_storage.__get();
73 }
74
75 constexpr auto get() && noexcept -> T&& {
76 KOKKOS_ASSERT(m_has_value);
77 return std::move(m_storage.__get());
78 }
79
80 constexpr auto get() const && noexcept -> const T&& = delete;
81
82 constexpr auto operator->() noexcept -> T* {
83 KOKKOS_ASSERT(m_has_value);
84 return m_storage.operator->();
85 }
86
87 constexpr auto operator->() const noexcept -> const T* {
88 KOKKOS_ASSERT(m_has_value);
89 return m_storage.operator->();
90 }
91
92 constexpr explicit operator bool() const noexcept {
93 return m_has_value;
94 }
95
96 constexpr bool has_value() const noexcept {
97 return m_has_value;
98 }
99
100 private:
101 stdexec::__manual_lifetime<T> m_storage{};
102 bool m_has_value{false};
103};
104
105} // namespace Kokkos::Execution::Impl
106
107#endif // KOKKOS_EXECUTION_IMPL_OPTIONAL_STORAGE_HPP
OptionalStorage(const OptionalStorage &)=delete
constexpr auto emplace_from(Func &&func, Args &&... args) noexcept(noexcept(T{std::forward< Func >(func)(std::forward< Args >(args)...)})) -> T &
OptionalStorage(OptionalStorage &&)=delete
constexpr auto get() &noexcept -> T &
OptionalStorage & operator=(const OptionalStorage &)=delete
constexpr auto get() const &&noexcept -> const T &&=delete
constexpr OptionalStorage() noexcept=default
constexpr auto emplace(Args &&... args) noexcept(std::is_nothrow_constructible_v< T, Args... >) -> T &
OptionalStorage & operator=(OptionalStorage &&)=delete
constexpr auto get() &&noexcept -> T &&
constexpr bool has_value() const noexcept
constexpr auto get() const &noexcept -> const T &
constexpr auto operator->() const noexcept -> const T *
stdexec::__manual_lifetime< T > m_storage