123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #pragma once
- #include <system_error>
- #include <memory>
- #include "Utility.Either.tlh"
- #include "Utility.Error.hpp"
- //-----------------------------------------------------------------------------
- // Expected
- // 与 C++ 23 的 std::expected 很像, 但是这里把错误类型固定成 Error::ErrorCode
- //-----------------------------------------------------------------------------
- namespace ECOM::Utility
- {
- template <typename TV>
- struct Expected : public Either <TV, ECOM::Utility::Error::ErrorCode>
- {
- using base = Either <TV, ECOM::Utility::Error::ErrorCode>;
- using value_type = TV;
- using error_type = ECOM::Utility::Error::ErrorCode;
- template<class U>
- using rebind = Expected <U>;
- constexpr Expected () = delete; // ! 不支持默认构造函数, 否则没法知道是左还是右 !
- //< 拷贝构造函数及移动构造函数
- constexpr Expected (Expected <TV> const & e) : base (e) {}
- constexpr Expected (Expected <TV> && e) : base (std::move (e)) {}
- //>
- //< 从 left 或 right 构造
- constexpr Expected (__tLeft <value_type> const & _left ) : base (_left) {}
- constexpr Expected (__tRight <error_type> const & _right) : base (_right) {}
- constexpr Expected (__tLeft <value_type> && _left ) : base (std::move (_left)) {}
- constexpr Expected (__tRight <error_type> && _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 <value_type> const & from)
- {
- base::operator = (from);
- return (*this);
- }
- constexpr Expected & operator = (Expected <value_type> && 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 <int, ECOM::Utility::Error::ErrorCode>;
- //using eiAck = ECOM::Utility::Either <ECOM::Utility::iAck , ECOM::Utility::Error::ErrorCode>;
- //using eiBLOBAck = ECOM::Utility::Either <ECOM::Utility::iBLOBAck, ECOM::Utility::Error::ErrorCode>;
- using eiResult = ECOM::Utility::Expected <int>;
- //< 最常用的返回值
- constexpr auto eiSuccess = ECOM::Utility::left <int> (1);
|