#pragma once #include #include #include "Utility.Either.tlh" #include "Utility.Error.hpp" //----------------------------------------------------------------------------- // Expected // 与 C++ 23 的 std::expected 很像, 但是这里把错误类型固定成 Error::ErrorCode //----------------------------------------------------------------------------- namespace ECOM::Utility { template struct Expected : public Either { using base = Either ; using value_type = TV; using error_type = ECOM::Utility::Error::ErrorCode; template using rebind = Expected ; constexpr Expected () = delete; // ! 不支持默认构造函数, 否则没法知道是左还是右 ! //< 拷贝构造函数及移动构造函数 constexpr Expected (Expected const & e) : base (e) {} constexpr Expected (Expected && e) : base (std::move (e)) {} //> //< 从 left 或 right 构造 constexpr Expected (__tLeft const & _left ) : base (_left) {} constexpr Expected (__tRight const & _right) : base (_right) {} constexpr Expected (__tLeft && _left ) : base (std::move (_left)) {} constexpr Expected (__tRight && _right) : base (std::move (_right)) {} //> // 直接从 value 构造 constexpr Expected (value_type const & _left) : base { _left } {} constexpr Expected (value_type && _left) : base { std::move (_left) } {} // 直接从 Error 构造 constexpr Expected (error_type const & _right) : base { _right } {} constexpr Expected (error_type && _right) : base { std::move (_right) } {} constexpr bool isLeft () const = delete; constexpr bool isRight () const = delete; constexpr bool isValue () const noexcept { return base::isLeft (); } constexpr bool isError () const noexcept { return base::isRight (); } constexpr const value_type & getLeft () const = delete; constexpr const error_type & getRight () const = delete; constexpr value_type & getLeft () = delete; constexpr error_type & getRight () = delete; constexpr const value_type & value () const { return base::getLeft (); } constexpr const error_type & error () const { return base::getRight (); } constexpr value_type & value () { return base::getLeft (); } constexpr error_type & error () { return base::getRight (); } public: constexpr Expected & operator = (Expected const & from) { base::operator = (from); return (*this); } constexpr Expected & operator = (Expected && from) { base::operator = (std::move (from)); return (*this); } public: constexpr static Expected make (value_type const & _left) { return Expected { _left }; } constexpr static Expected make (value_type && _left) { return Expected { std::move (_left) }; } constexpr static Expected make (error_type const & _right) { return Expected { _right }; } constexpr static Expected make (error_type && _right) { return Expected { std::move (_right) }; } }; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //using eiResult = ECOM::Utility::Either ; //using eiAck = ECOM::Utility::Either ; //using eiBLOBAck = ECOM::Utility::Either ; using eiResult = ECOM::Utility::Expected ; //< 最常用的返回值 constexpr auto eiSuccess = ECOM::Utility::left (1);