Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-17 10:08:28

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file corecel/cont/InitializedValue.hh
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 }  // namespace detail
0026 
0027 //---------------------------------------------------------------------------//
0028 /*!
0029  * Clear and finalize default values when moving and destroying.
0030  *
0031  * This helper class is used to simplify the "rule of 5" for classes that have
0032  * to treat one member data specially but can use default assign/construct for
0033  * the other elements. The default behavior is just to default-initialize when
0034  * assigning and clearing the RHS when moving; this is useful for handling
0035  * managed memory. The \em finalizer is called every time a \em non-default
0036  * value is lost by assignment or destruction.
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     //! \name Constructors
0049 
0050     //! Construct implicitly with default value
0051     InitializedValue() = default;
0052     //! Implicit construct from lvalue
0053     InitializedValue(T const& value) : value_(value) {}
0054     //! Implicit construct from rvalue
0055     InitializedValue(T&& value) : value_(std::move(value)) {}
0056 
0057     //! Default copy constructor
0058     InitializedValue(InitializedValue const&) noexcept(
0059         std::is_nothrow_copy_constructible_v<T>) = default;
0060 
0061     // Move constructor
0062     InitializedValue(InitializedValue&& other) noexcept(
0063         std::is_nothrow_move_constructible_v<T>);
0064 
0065     // Destructor
0066     ~InitializedValue() noexcept(noexcept_finalize_);
0067 
0068     //!@}
0069 
0070     //!@{
0071     //! \name Assignment
0072     // Copy assignment
0073     InitializedValue& operator=(InitializedValue const& other) noexcept(
0074         noexcept_finalize_ && std::is_nothrow_copy_assignable_v<T>);
0075     // Move assignment
0076     InitializedValue& operator=(InitializedValue&& other) noexcept(
0077         noexcept_finalize_ && std::is_nothrow_move_assignable_v<T>);
0078     //!@}
0079 
0080     //!@{
0081     //! \name Conversion
0082 
0083     //! Implicit reference to stored value
0084     operator T const&() const noexcept { return value_; }
0085     operator T&() noexcept { return value_; }
0086 
0087     //! Explicit reference to stored value
0088     T const& value() const& { return value_; }
0089     T& value() & { return value_; }
0090 
0091     //!@}
0092 
0093   private:
0094     T value_{};
0095 };
0096 
0097 //---------------------------------------------------------------------------//
0098 // INLINE DEFINITIONS
0099 //---------------------------------------------------------------------------//
0100 //! Exchange with other value on move construct
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 //! Call finalizer on destruct
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 //! Finalize our value when assigning
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 //! Clear other value on move assign
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 }  // namespace celeritas