Diag-Client-Lib
Public Types | Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
core_type::Result< T, E > Class Template Referencefinal

Class type to contains a value (of type ValueType), or an error (of type ErrorType) More...

#include <result.h>

Public Types

using value_type = T
 Type alias for the type T of values. More...
 
using error_type = E
 Type alias for the type E of errors. More...
 

Public Member Functions

constexpr Result (const T &t) noexcept
 Construct a new Result from the specified value (given as lvalue) More...
 
constexpr Result (T &&t) noexcept
 Construct a new Result from the specified value (given as rvalue) More...
 
constexpr Result (const E &e) noexcept
 Construct a new Result from the specified error (given as lvalue) More...
 
constexpr Result (E &&e) noexcept
 Construct a new Result from the specified error (given as rvalue) More...
 
 Result (const Result &other)=default
 Copy-construct a new Result from another instance. More...
 
Resultoperator= (const Result &other)=default
 Copy-assign another Result to this instance. More...
 
 Result (Result &&other) noexcept(std::is_nothrow_move_constructible< T >::value &&std::is_nothrow_move_constructible< E >::value)=default
 Move-construct a new Result from another instance. More...
 
Resultoperator= (Result &&other) noexcept(std::is_nothrow_move_constructible< T >::value &&std::is_nothrow_move_assignable< T >::value &&std::is_nothrow_move_constructible< E >::value &&std::is_nothrow_move_assignable< E >::value)=default
 Move-assign another Result to this instance. More...
 
 ~Result () noexcept=default
 Destruct an instance of Result. More...
 
template<typename... Args>
void EmplaceValue (Args &&...args) noexcept
 Put a new value into this instance, constructed in-place from the given arguments. More...
 
template<typename... Args>
void EmplaceError (Args &&...args) noexcept
 Put a new error into this instance, constructed in-place from the given arguments. More...
 
bool HasValue () const noexcept
 Check whether *this contains a value. More...
 
 operator bool () const noexcept
 Check whether *this contains a value. More...
 
const T & operator* () const &noexcept
 Access the contained value. More...
 
T && operator* () &&noexcept
 Access the contained value. More...
 
const T * operator-> () const noexcept
 Access the contained value. More...
 
const T & Value () const &noexcept
 Access the contained value. More...
 
T && Value () &&noexcept
 Access the contained value. More...
 
const E & Error () const &noexcept
 Access the contained error. More...
 
E && Error () &&noexcept
 Access the contained error. More...
 
std::optional< T > Ok () const &noexcept
 Return the contained value as an Optional. More...
 
std::optional< T > Ok () &&noexcept
 Return the contained value as an Optional. More...
 
std::optional< E > Err () const &noexcept
 Return the contained error as an Optional. More...
 
std::optional< E > Err () &&noexcept
 Return the contained error as an Optional. More...
 
template<typename F , typename T2 = typename std::invoke_result_t<F, T>>
Result< T2, E > AndThen (F &&fn) &&noexcept
 Returns the result of the given function on the contained value if it exists; otherwise, returns the result itself. More...
 
template<typename F , typename E2 = std::invoke_result_t<F, E>>
Result< T, E2 > MapError (F &&fn)
 Returns the result of the given function on the contained value if it exists; otherwise, returns the result itself. More...
 
template<typename F >
Result OrElse (F &&fn) &&noexcept
 Returns the result itself if it contains a value; otherwise, returns the result of the given function on the error value. More...
 
template<typename U >
ValueOr (U &&defaultValue) const &noexcept
 Return the contained value or the given default value. More...
 
template<typename U >
ValueOr (U &&defaultValue) &&noexcept
 Return the contained value or the given default value. More...
 
template<typename G >
ErrorOr (G &&defaultError) const &noexcept
 Return the contained error or the given default error. More...
 
template<typename G >
ErrorOr (G &&defaultError) &&noexcept
 Return the contained error or the given default error. More...
 
template<typename F >
Resolve (F &&f) const
 Return the contained value or return the result of a function call. More...
 

Static Public Member Functions

static Result FromValue (T &t) noexcept
 Build a new Result from the specified value (given as lvalue) More...
 
static Result FromValue (T &&t) noexcept
 Build a new Result from the specified value (given as rvalue) More...
 
template<typename... Args>
static Result FromValue (Args &&...args) noexcept
 Build a new Result from a value that is constructed in-place from the given arguments. More...
 
static Result FromError (const E &e) noexcept
 Build a new Result from the specified error (given as lvalue) More...
 
static Result FromError (E &&e) noexcept
 Build a new Result from the specified error (given as rvalue) More...
 
template<typename... Args>
static Result FromError (Args &&...args) noexcept
 Build a new Result from an error that is constructed in-place from the given arguments. More...
 

Private Attributes

std::variant< T, E > storage_
 Storage to contain value of type T or error of type E. More...
 

Detailed Description

template<typename T, typename E = ErrorCode>
class core_type::Result< T, E >

Class type to contains a value (of type ValueType), or an error (of type ErrorType)

This class is implemented based on API specification of ara::core::Result from Adaptive Platform Core AUTOSAR AP R21-11

Template Parameters
TThe type of value
EThe type of error

Definition at line 29 of file result.h.

Member Typedef Documentation

◆ error_type

template<typename T , typename E = ErrorCode>
using core_type::Result< T, E >::error_type = E

Type alias for the type E of errors.

Definition at line 39 of file result.h.

◆ value_type

template<typename T , typename E = ErrorCode>
using core_type::Result< T, E >::value_type = T

Type alias for the type T of values.

Definition at line 34 of file result.h.

Constructor & Destructor Documentation

◆ Result() [1/6]

template<typename T , typename E = ErrorCode>
constexpr core_type::Result< T, E >::Result ( const T &  t)
inlineexplicitconstexprnoexcept

Construct a new Result from the specified value (given as lvalue)

Parameters
[in]tThe value to put into the Result

Definition at line 116 of file result.h.

116 : storage_{t} {}
std::variant< T, E > storage_
Storage to contain value of type T or error of type E.
Definition: result.h:443

◆ Result() [2/6]

template<typename T , typename E = ErrorCode>
constexpr core_type::Result< T, E >::Result ( T &&  t)
inlineexplicitconstexprnoexcept

Construct a new Result from the specified value (given as rvalue)

Parameters
[in]tThe value to put into the Result

Definition at line 123 of file result.h.

123 : storage_{std::move(t)} {}

◆ Result() [3/6]

template<typename T , typename E = ErrorCode>
constexpr core_type::Result< T, E >::Result ( const E &  e)
inlineexplicitconstexprnoexcept

Construct a new Result from the specified error (given as lvalue)

Parameters
[in]eThe error to put into the Result

Definition at line 130 of file result.h.

130 : storage_{e} {}

◆ Result() [4/6]

template<typename T , typename E = ErrorCode>
constexpr core_type::Result< T, E >::Result ( E &&  e)
inlineexplicitconstexprnoexcept

Construct a new Result from the specified error (given as rvalue)

Parameters
[in]eThe error to put into the Result

Definition at line 137 of file result.h.

137 : storage_{std::move(e)} {}

◆ Result() [5/6]

template<typename T , typename E = ErrorCode>
core_type::Result< T, E >::Result ( const Result< T, E > &  other)
default

Copy-construct a new Result from another instance.

Parameters
[in]otherThe other instance

◆ Result() [6/6]

template<typename T , typename E = ErrorCode>
core_type::Result< T, E >::Result ( Result< T, E > &&  other) const &&
defaultnoexcept

Move-construct a new Result from another instance.

Parameters
[in]otherThe other instance

◆ ~Result()

template<typename T , typename E = ErrorCode>
core_type::Result< T, E >::~Result ( )
defaultnoexcept

Destruct an instance of Result.

Member Function Documentation

◆ AndThen()

template<typename T , typename E = ErrorCode>
template<typename F , typename T2 = typename std::invoke_result_t<F, T>>
Result<T2, E> core_type::Result< T, E >::AndThen ( F &&  fn) &&
inlinenoexcept

Returns the result of the given function on the contained value if it exists; otherwise, returns the result itself.

Template Parameters
FFunctor type
Parameters
[in]fnCallable function
Returns
Result An Result

Definition at line 328 of file result.h.

328  {
329  return HasValue() ? Result<T2, E>{fn(std::move(*this).Value())} : Result<T2, E>{*this};
330  }
bool HasValue() const noexcept
Check whether *this contains a value.
Definition: result.h:208

References core_type::Result< T, E >::HasValue().

Here is the call graph for this function:

◆ EmplaceError()

template<typename T , typename E = ErrorCode>
template<typename... Args>
void core_type::Result< T, E >::EmplaceError ( Args &&...  args)
inlinenoexcept

Put a new error into this instance, constructed in-place from the given arguments.

Template Parameters
ArgsThe types of arguments given to this function
Parameters
[in]argsThe arguments used for constructing the error

Definition at line 199 of file result.h.

199  {
200  storage_.template emplace<error_type>(std::forward<Args>(args)...);
201  }

References core_type::Result< T, E >::storage_.

◆ EmplaceValue()

template<typename T , typename E = ErrorCode>
template<typename... Args>
void core_type::Result< T, E >::EmplaceValue ( Args &&...  args)
inlinenoexcept

Put a new value into this instance, constructed in-place from the given arguments.

Template Parameters
ArgsThe types of arguments given to this function
Parameters
[in]argsThe arguments used for constructing the value

Definition at line 187 of file result.h.

187  {
188  storage_.template emplace<value_type>(std::forward<Args>(args)...);
189  }

References core_type::Result< T, E >::storage_.

Referenced by boost_support::socket::udp::UdpClientSocket::Destroy(), and boost_support::parser::Read().

Here is the caller graph for this function:

◆ Err() [1/2]

template<typename T , typename E = ErrorCode>
std::optional<E> core_type::Result< T, E >::Err ( ) &&
inlinenoexcept

Return the contained error as an Optional.

Returns
std::optional<E> An Optional with the error, if present

Definition at line 311 of file result.h.

311  {
312  std::optional<E> opt_err{};
313  if (!HasValue()) { opt_err.emplace(std::move(Error())); }
314  return opt_err;
315  }
const E & Error() const &noexcept
Access the contained error.
Definition: result.h:263

References core_type::Result< T, E >::Error(), and core_type::Result< T, E >::HasValue().

Here is the call graph for this function:

◆ Err() [2/2]

template<typename T , typename E = ErrorCode>
std::optional<E> core_type::Result< T, E >::Err ( ) const &
inlinenoexcept

Return the contained error as an Optional.

Returns
std::optional<E> An Optional with the error, if present

Definition at line 300 of file result.h.

300  {
301  std::optional<E> opt_err{};
302  if (!HasValue()) { opt_err.emplace(Error()); }
303  return opt_err;
304  }

References core_type::Result< T, E >::Error(), and core_type::Result< T, E >::HasValue().

Here is the call graph for this function:

◆ Error() [1/2]

template<typename T , typename E = ErrorCode>
E&& core_type::Result< T, E >::Error ( ) &&
inlinenoexcept

Access the contained error.

This function’s behavior is undefined if *this does not contain an error

Returns
E && An rvalue reference to the contained error

Definition at line 271 of file result.h.

271 { return std::move(std::get<error_type>(storage_)); }

References core_type::Result< T, E >::storage_.

◆ Error() [2/2]

template<typename T , typename E = ErrorCode>
const E& core_type::Result< T, E >::Error ( ) const &
inlinenoexcept

Access the contained error.

This function’s behavior is undefined if *this does not contain an error

Returns
const E & A const reference to the contained error

Definition at line 263 of file result.h.

263 { return std::get<error_type>(storage_); }

References core_type::Result< T, E >::storage_.

Referenced by core_type::Result< void, E >::CheckError(), core_type::Result< T, E >::Err(), core_type::Result< void, E >::Err(), core_type::Result< void, E >::MapError(), core_type::Result< void, E >::OrElse(), core_type::Result< T, E >::Resolve(), and core_type::Result< void, E >::Resolve().

Here is the caller graph for this function:

◆ ErrorOr() [1/2]

template<typename T , typename E = ErrorCode>
template<typename G >
E core_type::Result< T, E >::ErrorOr ( G &&  defaultError) &&
inlinenoexcept

Return the contained error or the given default error.

If *this contains an error, it is returned. Otherwise, the specified default error is returned, static_cast’d to E

Template Parameters
GThe type of defaultError
Parameters
[in]defaultErrorThe error to use if *this does not contain an error
Returns
E The error

Definition at line 418 of file result.h.

418  {
419  return !HasValue() ? Result{std::move(*this)} : static_cast<E>(defaultError);
420  }
constexpr Result(const T &t) noexcept
Construct a new Result from the specified value (given as lvalue)
Definition: result.h:116

References core_type::Result< T, E >::HasValue().

Here is the call graph for this function:

◆ ErrorOr() [2/2]

template<typename T , typename E = ErrorCode>
template<typename G >
E core_type::Result< T, E >::ErrorOr ( G &&  defaultError) const &
inlinenoexcept

Return the contained error or the given default error.

If *this contains an error, it is returned. Otherwise, the specified default error is returned, static_cast’d to E

Template Parameters
GThe type of defaultError
Parameters
[in]defaultErrorThe error to use if *this does not contain an error
Returns
E The error

Definition at line 403 of file result.h.

403  {
404  return !HasValue() ? Result{*this} : static_cast<E>(defaultError);
405  }

References core_type::Result< T, E >::HasValue().

Here is the call graph for this function:

◆ FromError() [1/3]

template<typename T , typename E = ErrorCode>
template<typename... Args>
static Result core_type::Result< T, E >::FromError ( Args &&...  args)
inlinestaticnoexcept

Build a new Result from an error that is constructed in-place from the given arguments.

This function shall not participate in overload resolution unless: std::is_const Args&&...>::value is true, and the first type of the expanded parameter pack first type of the expanded parameter pack is not a specialization of Result

Template Parameters
ArgsThe types of arguments given to this function
Parameters
[in]argsThe arguments used for constructing the error
Returns
Result A Result that contains an error

Definition at line 107 of file result.h.

107  {
108  return Result{std::forward<Args>(args)...};
109  }

◆ FromError() [2/3]

template<typename T , typename E = ErrorCode>
static Result core_type::Result< T, E >::FromError ( const E &  e)
inlinestaticnoexcept

Build a new Result from the specified error (given as lvalue)

Parameters
[in]eThe error to put into the Result
Returns
Result A Result that contains the error e

Definition at line 83 of file result.h.

83 { return Result{e}; }

Referenced by diag::client::conversation::DmConversation::SendDiagnosticRequest(), and diag::client::conversation::Conversation::SendDiagnosticRequest().

Here is the caller graph for this function:

◆ FromError() [3/3]

template<typename T , typename E = ErrorCode>
static Result core_type::Result< T, E >::FromError ( E &&  e)
inlinestaticnoexcept

Build a new Result from the specified error (given as rvalue)

Parameters
[in]eThe error to put into the Result
Returns
Result A Result that contains the error e

Definition at line 92 of file result.h.

92 { return Result{std::move(e)}; }

◆ FromValue() [1/3]

template<typename T , typename E = ErrorCode>
template<typename... Args>
static Result core_type::Result< T, E >::FromValue ( Args &&...  args)
inlinestaticnoexcept

Build a new Result from a value that is constructed in-place from the given arguments.

This function shall not participate in overload resolution unless: std::is_constructible<T, Args&&...>::value is true, and the first type of the expanded parameter pack is not T, and the first type of the expanded parameter pack is not a specialization of Result

Template Parameters
ArgsThe types of arguments given to this function
Parameters
[in]argsThe arguments used for constructing the value
Returns
Result A Result that contains a value

Definition at line 72 of file result.h.

72  {
73  return Result{std::forward<Args>(args)...};
74  }

◆ FromValue() [2/3]

template<typename T , typename E = ErrorCode>
static Result core_type::Result< T, E >::FromValue ( T &&  t)
inlinestaticnoexcept

Build a new Result from the specified value (given as rvalue)

Parameters
[in]tThe value to put into the Result
Returns
Result A Result that contains the value t

Definition at line 57 of file result.h.

57 { return Result{std::move(t)}; }

◆ FromValue() [3/3]

template<typename T , typename E = ErrorCode>
static Result core_type::Result< T, E >::FromValue ( T &  t)
inlinestaticnoexcept

Build a new Result from the specified value (given as lvalue)

Parameters
[in]tThe value to put into the Result
Returns
Result A Result that contains the value t

Definition at line 48 of file result.h.

48 { return Result{t}; }

◆ HasValue()

template<typename T , typename E = ErrorCode>
bool core_type::Result< T, E >::HasValue ( ) const
inlinenoexcept

◆ MapError()

template<typename T , typename E = ErrorCode>
template<typename F , typename E2 = std::invoke_result_t<F, E>>
Result<T, E2> core_type::Result< T, E >::MapError ( F &&  fn)
inline

Returns the result of the given function on the contained value if it exists; otherwise, returns the result itself.

Template Parameters
FFunctor type
Parameters
[in]fnCallable function
Returns
Result An Result

Definition at line 343 of file result.h.

343  {
344  return HasValue() ? Result<T, E2>{std::move(*this).Value()} : Result<T, E2>{fn(std::move(*this).Error())};
345  }

References core_type::Result< T, E >::HasValue(), and core_type::Result< T, E >::Value().

Here is the call graph for this function:

◆ Ok() [1/2]

template<typename T , typename E = ErrorCode>
std::optional<T> core_type::Result< T, E >::Ok ( ) &&
inlinenoexcept

Return the contained value as an Optional.

Returns
std::optional<T> An Optional with the value, if present

Definition at line 289 of file result.h.

289  {
290  std::optional<T> opt_val{};
291  if (HasValue()) { opt_val.emplace(std::move(Value())); }
292  return opt_val;
293  }
const T & Value() const &noexcept
Access the contained value.
Definition: result.h:247

References core_type::Result< T, E >::HasValue(), and core_type::Result< T, E >::Value().

Here is the call graph for this function:

◆ Ok() [2/2]

template<typename T , typename E = ErrorCode>
std::optional<T> core_type::Result< T, E >::Ok ( ) const &
inlinenoexcept

Return the contained value as an Optional.

Returns
std::optional<T> An Optional with the value, if present

Definition at line 278 of file result.h.

278  {
279  std::optional<T> opt_val{};
280  if (HasValue()) { opt_val.emplace(Value()); }
281  return opt_val;
282  }

References core_type::Result< T, E >::HasValue(), and core_type::Result< T, E >::Value().

Here is the call graph for this function:

◆ operator bool()

template<typename T , typename E = ErrorCode>
core_type::Result< T, E >::operator bool ( ) const
inlineexplicitnoexcept

Check whether *this contains a value.

Returns
bool True if *this contains a value, false otherwise

Definition at line 215 of file result.h.

215 { return HasValue(); }

References core_type::Result< T, E >::HasValue().

Here is the call graph for this function:

◆ operator*() [1/2]

template<typename T , typename E = ErrorCode>
T&& core_type::Result< T, E >::operator* ( ) &&
inlinenoexcept

Access the contained value.

This function’s behavior is undefined if *this does not contain a value

Returns
T && An rvalue reference to the contained value

Definition at line 231 of file result.h.

231 { return std::move(std::get<value_type>(storage_)); }

References core_type::Result< T, E >::storage_.

◆ operator*() [2/2]

template<typename T , typename E = ErrorCode>
const T& core_type::Result< T, E >::operator* ( ) const &
inlinenoexcept

Access the contained value.

This function’s behavior is undefined if *this does not contain a value

Returns
const T & A const_reference to the contained value

Definition at line 223 of file result.h.

223 { return std::get<value_type>(storage_); }

References core_type::Result< T, E >::storage_.

◆ operator->()

template<typename T , typename E = ErrorCode>
const T* core_type::Result< T, E >::operator-> ( ) const
inlinenoexcept

Access the contained value.

This function’s behavior is undefined if *this does not contain a value

Returns
const T * A pointer to the contained value

Definition at line 239 of file result.h.

239 { return std::get_if<value_type>(storage_); }

References core_type::Result< T, E >::storage_.

◆ operator=() [1/2]

template<typename T , typename E = ErrorCode>
Result& core_type::Result< T, E >::operator= ( const Result< T, E > &  other)
default

Copy-assign another Result to this instance.

Parameters
[in]otherThe other instance
Returns
Result & *this, containing the contents of other

◆ operator=() [2/2]

template<typename T , typename E = ErrorCode>
Result& core_type::Result< T, E >::operator= ( Result< T, E > &&  other) const &&
defaultnoexcept

Move-assign another Result to this instance.

Parameters
[in]otherThe other instance
Returns
Result & *this, containing the contents of other

◆ OrElse()

template<typename T , typename E = ErrorCode>
template<typename F >
Result core_type::Result< T, E >::OrElse ( F &&  fn) &&
inlinenoexcept

Returns the result itself if it contains a value; otherwise, returns the result of the given function on the error value.

Template Parameters
FFunctor type
Parameters
[in]fnCallable function
Returns
Result An Result

Definition at line 358 of file result.h.

358  {
359  return HasValue() ? Result{std::move(*this)} : Result{fn(std::move(*this).Error())};
360  }

References core_type::Result< T, E >::HasValue().

Here is the call graph for this function:

◆ Resolve()

template<typename T , typename E = ErrorCode>
template<typename F >
T core_type::Result< T, E >::Resolve ( F &&  f) const
inline

Return the contained value or return the result of a function call.

If *this contains a value, it is returned. Otherwise, the specified callable is invoked and its return value which is to be compatible to type T is returned from this function. The Callable is expected to be compatible to this interface: T f(const E&)

Template Parameters
FThe type of the Callable f
Parameters
[in]fThe Callable
Returns
T The value

Definition at line 435 of file result.h.

435  {
436  return HasValue() ? Result{*this} : f(Error());
437  }

References core_type::Result< T, E >::Error(), and core_type::Result< T, E >::HasValue().

Here is the call graph for this function:

◆ Value() [1/2]

template<typename T , typename E = ErrorCode>
T&& core_type::Result< T, E >::Value ( ) &&
inlinenoexcept

Access the contained value.

This function’s behavior is undefined if *this does not contain a value

Returns
T && An rvalue reference to the contained value

Definition at line 255 of file result.h.

255 { return std::move(std::get<value_type>(storage_)); }

References core_type::Result< T, E >::storage_.

◆ Value() [2/2]

template<typename T , typename E = ErrorCode>
const T& core_type::Result< T, E >::Value ( ) const &
inlinenoexcept

Access the contained value.

This function’s behavior is undefined if *this does not contain a value

Returns
const T & A const reference to the contained value

Definition at line 247 of file result.h.

247 { return std::get<value_type>(storage_); }

References core_type::Result< T, E >::storage_.

Referenced by core_type::Result< T, E >::MapError(), and core_type::Result< T, E >::Ok().

Here is the caller graph for this function:

◆ ValueOr() [1/2]

template<typename T , typename E = ErrorCode>
template<typename U >
T core_type::Result< T, E >::ValueOr ( U &&  defaultValue) &&
inlinenoexcept

Return the contained value or the given default value.

If *this contains a value, it is returned. Otherwise, the specified default value is returned, static_cast’d to T

Template Parameters
UThe type of defaultValue
Parameters
[in]defaultValueThe value to use if *this does not contain a value
Returns
T The value

Definition at line 388 of file result.h.

388  {
389  return HasValue() ? Result{std::move(*this)} : static_cast<T>(defaultValue);
390  }

References core_type::Result< T, E >::HasValue().

Here is the call graph for this function:

◆ ValueOr() [2/2]

template<typename T , typename E = ErrorCode>
template<typename U >
T core_type::Result< T, E >::ValueOr ( U &&  defaultValue) const &
inlinenoexcept

Return the contained value or the given default value.

If *this contains a value, it is returned. Otherwise, the specified default value is returned, static_cast’d to T

Template Parameters
UThe type of defaultValue
Parameters
[in]defaultValueThe value to use if *this does not contain a value
Returns
T The value

Definition at line 373 of file result.h.

373  {
374  return HasValue() ? Result{*this} : static_cast<T>(defaultValue);
375  }

References core_type::Result< T, E >::HasValue().

Here is the call graph for this function:

Member Data Documentation

◆ storage_

template<typename T , typename E = ErrorCode>
std::variant<T, E> core_type::Result< T, E >::storage_
private

The documentation for this class was generated from the following file: