24 template <
typename Type,
typename Error = std::
string>
28 struct UnexpectedWrapper {
41 data.template emplace<Type>();
45 template <typename T, typename = std::enable_if_t<std::is_constructible<Type, T>::value>>
47 data.template emplace<Type>(std::forward<T>(
value));
51 template <typename TError, typename = std::enable_if_t<std::is_constructible<Error, TError>::value>>
53 data.template emplace<UnexpectedWrapper>(UnexpectedWrapper{ std::forward<TError>(
error) });
57 explicit operator bool()
const {
71 return data.template get<Type>();
79 return data.template get<Type>();
96 return data.template get<UnexpectedWrapper>().error;
114 bool isExpected()
const {
124 template <typename T, typename = decltype(std::declval<std::ostream&>() << std::declval<T>())>
125 inline std::ostream&
operator<<(std::ostream& stream,
const Expected<T>& expected) {
127 stream << expected.
value();
129 stream << expected.
error();
137 template <
typename Type>
#define SPH_ASSERT(x,...)
const UnexpectedTag UNEXPECTED
Expected< Type > makeUnexpected(const std::string &error)
Constructs an unexpected value of given type, given error message as std::string.
#define NAMESPACE_SPH_END
Object capable of storing values of different types.
Wrapper of type that either contains a value of given type, or an error message.
Expected(T &&value)
Constructs an expected value.
Expected(UnexpectedTag, TError &&error)
Constructs an unexpected value.
Type * operator->()
Operator -> for convenient access to member variables and functions of expected value.
const Error & error() const
Returns the error message.
bool operator!() const
Negation operator, returns true if object does NOT contain expected value.
const Type & value() const
Returns the const reference to expected value.
Type valueOr(const Type &other) const
Returns the expected value or given alternative if the object contains unexpected value.
Expected()
Construct the expected value using default constructor.
Type & value()
Returns the reference to expected value.
const Type * operator->() const
operator->()
INLINE Size getTypeIdx() const
Returns index of type currently stored in variant.