Electroneum
Loading...
Searching...
No Matches
expect< T > Class Template Reference

#include <expect.h>

Public Types

using value_type = T
using error_type = std::error_code

Public Member Functions

 expect ()=delete
 expect (std::error_code const &code) noexcept
 expect (T val) noexcept(std::is_nothrow_move_constructible< T >())
 Store a value, val, in the expect object.
 expect (expect const &src) noexcept(std::is_nothrow_copy_constructible< T >())
template<typename U, typename = detail::enable_if<is_convertible<U const&>()>>
 expect (expect< U > const &src) noexcept(std::is_nothrow_constructible< T, U const & >())
 Copy conversion from U to T.
 expect (expect &&src) noexcept(std::is_nothrow_move_constructible< T >())
template<typename U, typename = detail::enable_if<is_convertible<U>()>>
 expect (expect< U > &&src) noexcept(std::is_nothrow_constructible< T, U >())
 Move conversion from U to T.
 ~expect () noexcept
expectoperator= (expect const &src) noexcept(std::is_nothrow_copy_constructible< T >() &&std::is_nothrow_copy_assignable< T >())
T && value () &&
*return pre has_value ()`. T *operator->() noexcept
*return pre has_value ()`. T const *operator->() const noexcept
*return pre has_value ()`. T &operator*() noexcept
*return pre has_value ()`. T const &operator*() const noexcept
template<typename U>
bool equal (expect< U > const &rhs) const noexcept(noexcept(*std::declval< expect< T > >()== *rhs))
*return False if has_value ()`
*return False if otherwise error ()

Public Attributes

*return Value

Detailed Description

template<typename T>
class expect< T >

expect<T> is a value or error implementation, similar to Rust std::result or various C++ proposals (boost::expected, boost::outcome). This implementation currently has a strict error type, std::error_code, and a templated value type T. expect<T> is implicitly convertible from T or std::error_code, and one expect<T> object type is implicitly convertible to another expect<U> object iff the destination value type can be implicitly constructed from the source value type (i.e. struct U { ... U(T src) { ...} ... };).

operator== and operator!= are the only comparison operators provided; comparison between different value types is allowed provided the two values types have a operator== defined between them (i.e. assert(expect<int>{100} == expect<short>{100});). Comparisons can also be done against std::error_code objects or error code enums directly (i.e. assert(expect<int>{make_error_code(common_error::kInvalidArgument)} == error::kInvalidArgument)). Comparison of default constructed std::error_code will always fail. "Generic" comparisons can be done with std::error_condition via the matches method only (i.e. assert(expect<int>{make_error_code{common_error::kInvalidErrorCode}.matches(std::errc::invalid_argument))), operator== and operator!= will not work with std::errc or std::error_condition. A comparison with matches is more expensive because an equivalency between error categories is computed, but is recommended when an error can be one of several categories (this is going to be the case in nearly every situation when calling a function from another C++ struct/class).

expect<void> is a special case with no stored value. It is used by functions that can fail, but otherwise would return void. It is useful for consistency; all macros, standalone functions, and comparison operators work with expect<void>.

Note
See src/common/error.h for creating a custom error enum.

Definition at line 132 of file expect.h.

Member Typedef Documentation

◆ error_type

template<typename T>
using expect< T >::error_type = std::error_code

Definition at line 175 of file expect.h.

◆ value_type

template<typename T>
using expect< T >::value_type = T

Definition at line 174 of file expect.h.

Constructor & Destructor Documentation

◆ expect() [1/7]

template<typename T>
expect< T >::expect ( )
delete
Here is the caller graph for this function:

◆ expect() [2/7]

template<typename T>
expect< T >::expect ( std::error_code const & code)
inlinenoexcept

Store an error, code, in the expect object. If code creates a std::error_code object whose .value() == 0, then error() will be set to common_error::kInvalidErrorCode.

Definition at line 182 of file expect.h.

183 : code_(code), storage_()
184 {
185 if (!has_error())
187 }

◆ expect() [3/7]

template<typename T>
expect< T >::expect ( T val)
inlinenoexcept

Store a value, val, in the expect object.

Definition at line 190 of file expect.h.

191 : code_(), storage_()
192 {
193 store(std::move(val));
194 }

◆ expect() [4/7]

template<typename T>
expect< T >::expect ( expect< T > const & src)
inlinenoexcept

Definition at line 196 of file expect.h.

197 : code_(src.error()), storage_()
198 {
199 if (src.has_value())
200 store(src.get());
201 }
*return False if otherwise error()
*return pre has_value()`. T *operator->() noexcept
Definition expect.h:300
Here is the call graph for this function:

◆ expect() [5/7]

template<typename T>
template<typename U, typename = detail::enable_if<is_convertible<U const&>()>>
expect< T >::expect ( expect< U > const & src)
inlinenoexcept

Copy conversion from U to T.

Definition at line 205 of file expect.h.

206 : code_(src.error()), storage_()
207 {
208 if (src.has_value())
209 store(*src);
210 }
Here is the call graph for this function:

◆ expect() [6/7]

template<typename T>
expect< T >::expect ( expect< T > && src)
inlinenoexcept

Definition at line 212 of file expect.h.

213 : code_(src.error()), storage_()
214 {
215 if (src.has_value())
216 store(std::move(src.get()));
217 }
Here is the call graph for this function:

◆ expect() [7/7]

template<typename T>
template<typename U, typename = detail::enable_if<is_convertible<U>()>>
expect< T >::expect ( expect< U > && src)
inlinenoexcept

Move conversion from U to T.

Definition at line 221 of file expect.h.

222 : code_(src.error()), storage_()
223 {
224 if (src.has_value())
225 store(std::move(*src));
226 }
Here is the call graph for this function:

◆ ~expect()

template<typename T>
expect< T >::~expect ( )
inlinenoexcept

Definition at line 228 of file expect.h.

229 {
230 if (has_value())
231 get().~T();
232 }
Here is the call graph for this function:

Member Function Documentation

◆ equal()

template<typename T>
template<typename U>
bool expect< T >::equal ( expect< U > const & rhs) const
inlinenoexcept
Note
This function is noexcept when U == T is noexcept.
Returns
True if has_value() == rhs.has_value() and if values or errors are equal.

Definition at line 314 of file expect.h.

315 {
316 return has_value() && rhs.has_value() ?
317 get() == *rhs : error() == rhs.error();
318 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ error()

template<typename T>
*return False if otherwise expect< T >::error ( )
Here is the call graph for this function:
Here is the caller graph for this function:

◆ has_value() [1/5]

template<typename T>
*return False if expect< T >::has_value ( )

◆ has_value() [2/5]

template<typename T>
*return pre expect< T >::has_value ( ) &
inlinenoexcept

Definition at line 304 of file expect.h.

305 { return get(); }

◆ has_value() [3/5]

template<typename T>
*return pre expect< T >::has_value ( ) ->() noexcept
inline

Definition at line 300 of file expect.h.

301 { return std::addressof(get()); }
Here is the caller graph for this function:

◆ has_value() [4/5]

template<typename T>
*return pre expect< T >::has_value ( ) const &
inlinenoexcept

Definition at line 306 of file expect.h.

307 { return get(); }

◆ has_value() [5/5]

template<typename T>
*return pre expect< T >::has_value ( ) const ->() const noexcept
inline

Definition at line 302 of file expect.h.

303 { return std::addressof(get()); }

◆ operator=()

template<typename T>
expect & expect< T >::operator= ( expect< T > const & src)
inlinenoexcept

Definition at line 234 of file expect.h.

235 {
236 if (this != std::addressof(src))
237 {
238 if (has_value() && src.has_value())
239 get() = src.get();
240 else if (has_value())
241 get().~T();
242 else if (src.has_value())
243 store(src.get());
244 code_ = src.error();
245 }
246 return *this;
247 }
Here is the call graph for this function:

◆ value()

template<typename T>
T && expect< T >::value ( ) &&
inline

Move src into this. If src.has_value() && addressof(src) != this then src.value() will be in a "moved from state". */ expect& operator=(expect&& src) noexcept(std::is_nothrow_move_constructible<T>() && std::is_nothrow_move_assignable<T>()) { if (this != std::addressof(src)) { if (has_value() && src.has_value()) get() = std::move(src.get()); else if (has_value()) get().~T(); else if (src.has_value()) store(std::move(src.get())); code_ = src.error(); } return *this; } @iverbatim \return True if this is storing a value instead of an error. @endiverbatim\ilinebr explicit operator bool() const noexcept { return has_value(); } @iverbatim \return True if this is storing an error instead of a value. @endiverbatim\ilinebr bool has_error() const noexcept { return bool(code_); } @iverbatim \return True if this is storing a value instead of an error. @endiverbatim\ilinebr bool has_value() const noexcept { return !has_error(); } @iverbatim \return Error - always safe to call. Empty when !has_error(). @endiverbatim\ilinebr std::error_code error() const noexcept { return code_; } @iverbatim \return Value if has_value() otherwise \throw std::system_error{error()}. @endiverbatim\ilinebr T& value() & { maybe_throw(); return get(); } @iverbatim \return Value if has_value() otherwise \throw std::system_error{error()}`. T const& value() const & { maybe_throw(); return get(); }

/*! Same as other overloads, but expressions such as foo(bar().value()) will automatically perform moves with no copies.

Definition at line 294 of file expect.h.

295 {
296 maybe_throw();
297 return std::move(get());
298 }
Here is the caller graph for this function:

Member Data Documentation

◆ Value

template<typename T>
*return expect< T >::Value

Definition at line 300 of file expect.h.


The documentation for this class was generated from the following file:
  • /home/abuild/rpmbuild/BUILD/electroneum-5.1.3.1-build/electroneum-5.1.3.1/src/common/expect.h