Added converting constructors for NotNullable.

This commit is contained in:
Patrick 2025-06-23 00:18:47 +02:00
parent 05bc3d5147
commit 9c4765dbaf

View File

@ -29,7 +29,10 @@
namespace mijin namespace mijin
{ {
template<typename T> requires(!std::is_same_v<T, std::nullptr_t>) && requires(T t) { t == nullptr; } template<typename T>
concept nullable_type = !std::is_same_v<T, std::nullptr_t> && requires(T t) { t == nullptr; };
template<nullable_type T>
class NotNullable class NotNullable
{ {
private: private:
@ -41,6 +44,18 @@ public:
{ {
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr."); MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
} }
template<typename TOther> requires(std::is_constructible_v<T, const TOther&>)
constexpr NotNullable(const NotNullable<TOther>& other) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, const TOther&>))
: base_(other.base_)
{
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
}
template<typename TOther> requires(std::is_constructible_v<T, TOther&&>)
constexpr NotNullable(NotNullable<TOther>&& other) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, TOther&&>))
: base_(std::exchange(other.base_, nullptr))
{
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
}
template<typename TArg, typename... TArgs> requires(!std::is_same_v<TArg, std::nullptr_t> template<typename TArg, typename... TArgs> requires(!std::is_same_v<TArg, std::nullptr_t>
&& (!std::is_same_v<TArg, T> && sizeof...(TArgs) == 0) && (!std::is_same_v<TArg, T> && sizeof...(TArgs) == 0)
&& std::is_constructible_v<T, TArg&&, TArgs&&...>) && std::is_constructible_v<T, TArg&&, TArgs&&...>)
@ -129,6 +144,9 @@ public:
[[nodiscard]] [[nodiscard]]
constexpr const T& get() const MIJIN_NOEXCEPT { return base_; } constexpr const T& get() const MIJIN_NOEXCEPT { return base_; }
template<nullable_type TOther>
friend class NotNullable;
}; };
#if MIJIN_USE_GSL #if MIJIN_USE_GSL