CCSDSPack
C++ Library for CCSDS Space Packet manipulation. i.e. generation, extraction, analisys and more
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
CCSDS::Result< T > Class Template Reference

Encapsulates a result that can hold either a value or an Error. More...

#include <CCSDSResult.h>

Public Member Functions

 Result (T value)
 Constructor for success case.
 
 Result (Error error)
 Constructor for failure case.
 
bool has_value () const
 Checks if the result contains a valid value.
 
T & value ()
 Retrieves the stored value.
 
const T & value () const
 
Error error () const
 Retrieves the stored error.
 
 operator bool () const
 Implicit conversion to bool, allowing usage like if (result).
 

Private Attributes

std::variant< T, Errordata
 Holds either a valid value or an Error.
 

Detailed Description

template<typename T>
class CCSDS::Result< T >

Encapsulates a result that can hold either a value or an Error.

This class simplifies error handling by allowing functions to return either a valid result or an Error, reducing the need for exception handling.

Template Parameters
TThe type of value to be stored in the result.

Definition at line 81 of file CCSDSResult.h.

Constructor & Destructor Documentation

◆ Result() [1/2]

template<typename T >
CCSDS::Result< T >::Result ( value)
inline

Constructor for success case.

Parameters
valueThe successful result value.

Definition at line 89 of file CCSDSResult.h.

89 : data(std::move(value)) {
90 }
T & value()
Retrieves the stored value.
std::variant< T, Error > data
Holds either a valid value or an Error.
Definition CCSDSResult.h:82

◆ Result() [2/2]

template<typename T >
CCSDS::Result< T >::Result ( Error  error)
inline

Constructor for failure case.

Parameters
errorThe Error.

Definition at line 96 of file CCSDSResult.h.

96 : data(error) {
97 }
Error error() const
Retrieves the stored error.

Member Function Documentation

◆ error()

template<typename T >
Error CCSDS::Result< T >::error ( ) const
inline

Retrieves the stored error.

Returns
The error.

Definition at line 119 of file CCSDSResult.h.

119 {
120 return std::get<Error>(data);
121 }

◆ has_value()

template<typename T >
bool CCSDS::Result< T >::has_value ( ) const
inline

Checks if the result contains a valid value.

Returns
True if a valid value is present, false otherwise.

Definition at line 103 of file CCSDSResult.h.

103 {
104 return std::holds_alternative<T>(data);
105 }
Here is the caller graph for this function:

◆ operator bool()

template<typename T >
CCSDS::Result< T >::operator bool ( ) const
inlineexplicit

Implicit conversion to bool, allowing usage like if (result).

Returns
True if the result contains a value, false otherwise.

Definition at line 127 of file CCSDSResult.h.

127{ return has_value(); }
bool has_value() const
Checks if the result contains a valid value.
Here is the call graph for this function:

◆ value() [1/2]

template<typename T >
T & CCSDS::Result< T >::value ( )
inline

Retrieves the stored value.

Returns
A reference to the stored value.
Note
Ensure has_value() returns true before calling this.

Definition at line 112 of file CCSDSResult.h.

112{ return std::get<T>(data); }

◆ value() [2/2]

template<typename T >
const T & CCSDS::Result< T >::value ( ) const
inline

Definition at line 113 of file CCSDSResult.h.

113{ return std::get<T>(data); }

Member Data Documentation

◆ data

template<typename T >
std::variant<T, Error> CCSDS::Result< T >::data
private

Holds either a valid value or an Error.

Definition at line 82 of file CCSDSResult.h.


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