File indexing completed on 2026-11-17 10:08:28
0001
0002
0003
0004
0005
0006
0007 #pragma once
0008
0009 #include <type_traits>
0010 #include <utility>
0011
0012 #include "corecel/Macros.hh"
0013
0014 namespace celeritas
0015 {
0016 namespace detail
0017 {
0018
0019 template<class T>
0020 struct DefaultFinalize
0021 {
0022 CELER_FORCEINLINE void operator()(T&) const noexcept {}
0023 };
0024
0025 }
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 template<class T, class Finalizer = detail::DefaultFinalize<T>>
0039 class InitializedValue
0040 {
0041 static_assert(std::is_default_constructible_v<T>);
0042 static_assert(std::is_default_constructible_v<Finalizer>);
0043 static constexpr bool noexcept_finalize_
0044 = noexcept(std::declval<Finalizer>()(std::declval<T&>()));
0045
0046 public:
0047
0048
0049
0050
0051 InitializedValue() = default;
0052
0053 InitializedValue(T const& value) : value_(value) {}
0054
0055 InitializedValue(T&& value) : value_(std::move(value)) {}
0056
0057
0058 InitializedValue(InitializedValue const&) noexcept(
0059 std::is_nothrow_copy_constructible_v<T>) = default;
0060
0061
0062 InitializedValue(InitializedValue&& other) noexcept(
0063 std::is_nothrow_move_constructible_v<T>);
0064
0065
0066 ~InitializedValue() noexcept(noexcept_finalize_);
0067
0068
0069
0070
0071
0072
0073 InitializedValue& operator=(InitializedValue const& other) noexcept(
0074 noexcept_finalize_ && std::is_nothrow_copy_assignable_v<T>);
0075
0076 InitializedValue& operator=(InitializedValue&& other) noexcept(
0077 noexcept_finalize_ && std::is_nothrow_move_assignable_v<T>);
0078
0079
0080
0081
0082
0083
0084 operator T const&() const noexcept { return value_; }
0085 operator T&() noexcept { return value_; }
0086
0087
0088 T const& value() const& { return value_; }
0089 T& value() & { return value_; }
0090
0091
0092
0093 private:
0094 T value_{};
0095 };
0096
0097
0098
0099
0100
0101 template<class T, class Finalizer>
0102 InitializedValue<T, Finalizer>::InitializedValue(
0103 InitializedValue&& other) noexcept(std::is_nothrow_move_constructible_v<T>)
0104 : value_(std::exchange(other.value_, {}))
0105 {
0106 }
0107
0108
0109
0110 template<class T, class Finalizer>
0111 InitializedValue<T, Finalizer>::~InitializedValue() noexcept(noexcept_finalize_)
0112 {
0113 if (value_ != T{})
0114 {
0115 Finalizer{}(value_);
0116 }
0117 }
0118
0119
0120
0121 template<class T, class Finalizer>
0122 InitializedValue<T, Finalizer>&
0123 InitializedValue<T, Finalizer>::operator=(InitializedValue const& other) noexcept(
0124 noexcept_finalize_ && std::is_nothrow_copy_assignable_v<T>)
0125 {
0126 if (value_ != T{})
0127 {
0128 Finalizer{}(value_);
0129 }
0130 value_ = other.value_;
0131 return *this;
0132 }
0133
0134
0135
0136 template<class T, class Finalizer>
0137 InitializedValue<T, Finalizer>& InitializedValue<T, Finalizer>::operator=(
0138 InitializedValue&& other) noexcept(noexcept_finalize_
0139 && std::is_nothrow_move_assignable_v<T>)
0140 {
0141 if (value_ != T{})
0142 {
0143 Finalizer{}(value_);
0144 }
0145 value_ = std::exchange(other.value_, T{});
0146 return *this;
0147 }
0148
0149
0150 }