namespace sr::detail
{
    struct NoopGuard
    {
        constexpr void release() const
        {
        }
    };


    template <class T>
    class Wrapper
    {
    public:
        template <class TT, class G = NoopGuard, std::enable_if_t<std::is_constructible_v<T, TT>, int> = 0>
        Wrapper(TT&& v, G&& g = G{}) noexcept(std::is_nothrow_constructible_v<T, TT>)
            : value(std::forward<TT>(v))
        {
            g.release();
        }


        T& get() noexcept
        {
            return value;
        }

        const T& get() const noexcept
        {
            return value;
        }

        void reset(Wrapper<T>&& other) noexcept
        {
            value = std::move(other.value);
        }

        void reset(const Wrapper<T>& other) noexcept(std::is_nothrow_assignable_v<T, const T&>)
        {
            value = other.value;
        }

        void reset(T&& newValue) noexcept(std::is_nothrow_assignable_v<T, decltype(std::move_if_noexcept(newValue))>)
        {
            value = std::forward<T>(newValue);
        }

        void reset(const T& newValue) noexcept(std::is_nothrow_assignable_v<T, const T&>)
        {
            value = newValue;
        }


        using type = T;


    private:
        T value;
    };


    template <class T>
    class Wrapper<T&>
    {
    public:
        template <class TT, class G = NoopGuard, std::enable_if_t<std::is_convertible_v<TT, T&>, int> = 0>
        Wrapper(TT&& v, G&& g = G{}) noexcept(std::is_nothrow_constructible_v<TT, T&>)
            : value(static_cast<T&>(v))
        {
            g.release();
        }


        T& get() noexcept
        {
            return value.get();
        }

        const T& get() const noexcept
        {
            return value.get();
        }

        void reset(Wrapper<T>&& other) noexcept
        {
            value = std::move(other.value);
        }

        void reset(T& newValue) noexcept
        {
            value = std::ref(newValue);
        }


        using type = std::reference_wrapper<std::remove_reference_t<T>>;


    private:
        type value;
    };

}
