MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::Result< T, ErrType > Struct Template Reference

Represents a Result that may have an error (error code) or a success value A type of "void" means there is no Result value, but there may be a failure. More...

#include <result.hpp>

Inheritance diagram for mtcore::Result< T, ErrType >:

Public Types

using Err = ErrType
 
using ErrVal = Error<Err>
 
using Value = T
 

Public Member Functions

 Result (const T &val)
 Creates a Result object from a success Allows for implicit casting by returning success value.
 
 Result (T &&val)
 Creates a Result object from a success Allows for implicit casting by returning success value.
 
bool operator== (const Result &other) const noexcept
 Compares results.
 
bool operator!= (const Result &other) const noexcept
 
bool operator== (const T &other) const noexcept
 Compares Result to a value.
 
bool operator!= (const T &other) const noexcept
 
bool is_success () const noexcept
 Checks if is a successful Result (aka.
 
value () const noexcept
 Checks if is a successful Result (aka.
 
bool is_error () const noexcept
 Checks if is an error Result.
 
bool copy_if_present (std::remove_const_t< T > &out) const noexcept
 Copies into a reference the successful value if there is one.
 
bool move_if_present (T &out) noexcept
 Moves into a reference the successful value if there is one.
 
ErrVal error () const noexcept
 Returns the associated error Fails if there is no error;.
 
const T & operator* () const noexcept
 Dereference the value (fails if an error value)
 
T const * operator-> () const noexcept
 Dereference the value (fails if an error value)
 
T & operator* ()
 Dereference the value (fails if an error value)
 
T * operator-> ()
 Dereference the value (fails if an error value)
 
Result< void, Errto_void () const
 
template<typename R>
Result< R, Errwith_success_val (const R &s)
 

Static Public Member Functions

static Result from_error (const ErrVal &err)
 Creates a Result object from an error.
 
static Result from_success (const T &val)
 Creates a Result object from a success.
 

Detailed Description

template<typename T, typename ErrType>
struct mtcore::Result< T, ErrType >

Represents a Result that may have an error (error code) or a success value A type of "void" means there is no Result value, but there may be a failure.

Represents a Result that has a success value or an error.

Template Parameters
TType of success object
TSuccess Value
ErrTypeError Type

Definition at line 170 of file result.hpp.

Member Typedef Documentation

◆ Err

template<typename T, typename ErrType>
using mtcore::Result< T, ErrType >::Err = ErrType

Definition at line 171 of file result.hpp.

◆ ErrVal

template<typename T, typename ErrType>
using mtcore::Result< T, ErrType >::ErrVal = Error<Err>

Definition at line 172 of file result.hpp.

◆ Value

template<typename T, typename ErrType>
using mtcore::Result< T, ErrType >::Value = T

Definition at line 173 of file result.hpp.

Constructor & Destructor Documentation

◆ Result() [1/2]

template<typename T, typename ErrType>
mtcore::Result< T, ErrType >::Result ( const T & val)
inline

Creates a Result object from a success Allows for implicit casting by returning success value.

Parameters
valSuccess code
Returns
Result object

Definition at line 185 of file result.hpp.

185: val(val) {}
Here is the caller graph for this function:

◆ Result() [2/2]

template<typename T, typename ErrType>
mtcore::Result< T, ErrType >::Result ( T && val)
inline

Creates a Result object from a success Allows for implicit casting by returning success value.

Parameters
valSuccess code
Returns
Result object

Definition at line 193 of file result.hpp.

193: val(std::forward<T>(val)) {}
Represents a Result that may have an error (error code) or a success value A type of "void" means the...
Definition result.hpp:170

Member Function Documentation

◆ copy_if_present()

template<typename T, typename ErrType>
bool mtcore::Result< T, ErrType >::copy_if_present ( std::remove_const_t< T > & out) const
inlinenodiscardnoexcept

Copies into a reference the successful value if there is one.

Definition at line 271 of file result.hpp.

271 {
272 if (is_success()) {
273 out = value();
274 }
275 return is_success();
276 }
T value() const noexcept
Checks if is a successful Result (aka.
Definition result.hpp:258
bool is_success() const noexcept
Checks if is a successful Result (aka.
Definition result.hpp:253
Here is the call graph for this function:

◆ error()

template<typename T, typename ErrType>
ErrVal mtcore::Result< T, ErrType >::error ( ) const
inlinenodiscardnoexcept

Returns the associated error Fails if there is no error;.

Definition at line 294 of file result.hpp.

294 {
295 ensure(is_error(), "CANNOT GET AN ERROR FROM A SUCCESSFUL RESUTL!");
296 return std::get<ErrVal>(val);
297 }
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
bool is_error() const noexcept
Checks if is an error Result.
Definition result.hpp:266
Here is the call graph for this function:
Here is the caller graph for this function:

◆ from_error()

template<typename T, typename ErrType>
static Result mtcore::Result< T, ErrType >::from_error ( const ErrVal & err)
inlinestatic

Creates a Result object from an error.

Parameters
errError code
Returns
Result object

Definition at line 200 of file result.hpp.

200{ return Result(err, 0); }
Result(const T &val)
Creates a Result object from a success Allows for implicit casting by returning success value.
Definition result.hpp:185
Here is the call graph for this function:
Here is the caller graph for this function:

◆ from_success()

template<typename T, typename ErrType>
static Result mtcore::Result< T, ErrType >::from_success ( const T & val)
inlinestatic

Creates a Result object from a success.

Parameters
valSuccess code
Returns
Result object

Definition at line 207 of file result.hpp.

207{ return Result(val); }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_error()

template<typename T, typename ErrType>
bool mtcore::Result< T, ErrType >::is_error ( ) const
inlinenodiscardnoexcept

Checks if is an error Result.

Definition at line 266 of file result.hpp.

266{ return std::holds_alternative<ErrVal>(val); }
Here is the caller graph for this function:

◆ is_success()

template<typename T, typename ErrType>
bool mtcore::Result< T, ErrType >::is_success ( ) const
inlinenodiscardnoexcept

Checks if is a successful Result (aka.

has a value)

Definition at line 253 of file result.hpp.

253{ return std::holds_alternative<T>(val); }
Here is the caller graph for this function:

◆ move_if_present()

template<typename T, typename ErrType>
bool mtcore::Result< T, ErrType >::move_if_present ( T & out)
inlinenodiscardnoexcept

Moves into a reference the successful value if there is one.

Definition at line 281 of file result.hpp.

281 {
282 if (is_success()) {
283 out = std::move(std::get<T>(val));
284 val = std::monostate{};
285 return true;
286 }
287 return is_success();
288 }
Here is the call graph for this function:

◆ operator!=() [1/2]

template<typename T, typename ErrType>
bool mtcore::Result< T, ErrType >::operator!= ( const Result< T, ErrType > & other) const
inlinenoexcept

Definition at line 234 of file result.hpp.

234{ return !(*this == other); }
Here is the call graph for this function:

◆ operator!=() [2/2]

template<typename T, typename ErrType>
bool mtcore::Result< T, ErrType >::operator!= ( const T & other) const
inlinenoexcept

Definition at line 248 of file result.hpp.

248{ return !(*this == other); }

◆ operator*() [1/2]

template<typename T, typename ErrType>
T & mtcore::Result< T, ErrType >::operator* ( )
inline

Dereference the value (fails if an error value)

Definition at line 318 of file result.hpp.

318 {
319 ensure(is_success(), "CANNNOT DERERFERENCE ERROR RESULT");
320 return std::get<T>(val);
321 }
Here is the call graph for this function:

◆ operator*() [2/2]

template<typename T, typename ErrType>
const T & mtcore::Result< T, ErrType >::operator* ( ) const
inlinenoexcept

Dereference the value (fails if an error value)

Definition at line 302 of file result.hpp.

302 {
303 ensure(is_success(), "CANNNOT DERERFERENCE ERROR RESULT");
304 return std::get<T>(val);
305 }
Here is the call graph for this function:

◆ operator->() [1/2]

template<typename T, typename ErrType>
T * mtcore::Result< T, ErrType >::operator-> ( )
inline

Dereference the value (fails if an error value)

Definition at line 326 of file result.hpp.

326 {
327 ensure(is_success(), "CANNNOT DERERFERENCE ERROR RESULT");
328 return &std::get<T>(val);
329 }
Here is the call graph for this function:

◆ operator->() [2/2]

template<typename T, typename ErrType>
T const * mtcore::Result< T, ErrType >::operator-> ( ) const
inlinenoexcept

Dereference the value (fails if an error value)

Definition at line 310 of file result.hpp.

310 {
311 ensure(is_success(), "CANNNOT DERERFERENCE ERROR RESULT");
312 return &std::get<T>(val);
313 }
Here is the call graph for this function:

◆ operator==() [1/2]

template<typename T, typename ErrType>
bool mtcore::Result< T, ErrType >::operator== ( const Result< T, ErrType > & other) const
inlinenoexcept

Compares results.

Parameters
otherOther Result to compare against
Returns
True if results are equivalent

Definition at line 214 of file result.hpp.

214 {
215 if (is_error()) {
216 if (other.is_error()) {
217 return error() == other.error();
218 }
219 return false;
220 }
221 if (other.is_error()) {
222 return false;
223 }
224
225 if (is_success()) {
226 if (other.is_success()) {
227 return value() == other.value();
228 }
229 return false;
230 }
231 return false;
232 }
ErrVal error() const noexcept
Returns the associated error Fails if there is no error;.
Definition result.hpp:294
Here is the call graph for this function:

◆ operator==() [2/2]

template<typename T, typename ErrType>
bool mtcore::Result< T, ErrType >::operator== ( const T & other) const
inlinenoexcept

Compares Result to a value.

Parameters
otherOther value to compare against
Returns
True if has value && value matches other

Definition at line 241 of file result.hpp.

241 {
242 if (is_error()) {
243 return false;
244 }
245 return value() == other;
246 }
Here is the call graph for this function:

◆ to_void()

template<typename T, typename ErrType>
Result< void, Err > mtcore::Result< T, ErrType >::to_void ( ) const
inline

Definition at line 331 of file result.hpp.

331 {
332 if (is_error()) {
333 return this->error();
334 }
335 return {};
336 }
Here is the call graph for this function:

◆ value()

template<typename T, typename ErrType>
T mtcore::Result< T, ErrType >::value ( ) const
inlinenodiscardnoexcept

Checks if is a successful Result (aka.

has a value)

Definition at line 258 of file result.hpp.

258 {
259 ensure(is_success(), "CANNOT GET VALUE WHEN ONE IS NOT PRESENT");
260 return std::get<T>(val);
261 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ with_success_val()

template<typename T, typename ErrType>
template<typename R>
Result< R, Err > mtcore::Result< T, ErrType >::with_success_val ( const R & s)
inline

Definition at line 339 of file result.hpp.

339 {
340 if (is_error()) {
341 return error();
342 }
343 return s;
344 }
Here is the call graph for this function:

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