TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec::TStatus< TCode, TDesc > Class Template Reference

Represents the status of an execution with error details. More...

#include <tec_status.hpp>

Public Member Functions

constexpr bool ok () const
 Checks if the status indicates success.
 
constexpr operator bool () const
 Conversion operator to check if the status is successful.
 
std::string as_string ()
 Converts the status to a string representation.
 
 TStatus ()
 Constructs a successful status.
 
 TStatus (Error::Kind _kind)
 Constructs an error status with an unspecified code.
 
 TStatus (const TDesc &_desc, Error::Kind _kind=Error::Kind::Err)
 Constructs an error status with a description.
 
 TStatus (const TCode &_code, Error::Kind _kind=Error::Kind::Err)
 Constructs an error status with a code.
 
 TStatus (const TCode &_code, const TDesc &_desc, Error::Kind _kind=Error::Kind::Err)
 Constructs an error status with code and description.
 

Public Attributes

Error::Kind kind
 The error category.
 
std::optional< TCodecode
 Optional error code.
 
std::optional< TDescdesc
 Optional error description.
 

Friends

std::ostream & operator<< (std::ostream &out, const TStatus &status)
 Outputs the status to an output stream.
 

Detailed Description

template<typename TCode, typename TDesc>
class tec::TStatus< TCode, TDesc >

Represents the status of an execution with error details.

A templated class that encapsulates an error kind, an optional error code, and an optional description to represent the outcome of an operation.

Template Parameters
TCodeThe type used for error codes (e.g., int).
TDescThe type used for error descriptions (e.g., std::string).

Constructor & Destructor Documentation

◆ TStatus() [1/5]

template<typename TCode , typename TDesc >
tec::TStatus< TCode, TDesc >::TStatus ( )
inline

Constructs a successful status.

Initializes the status with Error::Kind::Ok and no code or description.

tec::Status do_something1() {
// ...
// Everything is OK.
return {};
}

◆ TStatus() [2/5]

template<typename TCode , typename TDesc >
tec::TStatus< TCode, TDesc >::TStatus ( Error::Kind  _kind)
inline

Constructs an error status with an unspecified code.

Initializes the status with the specified error kind and an unspecified error code.

Parameters
_kindThe error kind (from Error::Kind).
tec::Status do_something2() {
// ...
// IO error with unspecified error code [-1].
}
@ IOErr
Input/output operation failure.

◆ TStatus() [3/5]

template<typename TCode , typename TDesc >
tec::TStatus< TCode, TDesc >::TStatus ( const TDesc _desc,
Error::Kind  _kind = Error::Kind::Err 
)
inline

Constructs an error status with a description.

Initializes the status with a generic error kind (or specified kind), an unspecified error code, and the provided description.

Parameters
_descThe error description.
_kindThe error kind (defaults to Error::Kind::Err).
tec::Status do_something3() {
// ...
// IO error with description and unspecified error code [-1].
return {"Cannot open a file", tec::Error::Kind::IOErr};
}

◆ TStatus() [4/5]

template<typename TCode , typename TDesc >
tec::TStatus< TCode, TDesc >::TStatus ( const TCode _code,
Error::Kind  _kind = Error::Kind::Err 
)
inline

Constructs an error status with a code.

Initializes the status with a generic error kind (or specified kind) and the provided error code, with no description.

Parameters
_codeThe error code.
_kindThe error kind (defaults to Error::Kind::Err).
tec::Status do_something4() {
// ...
// Generic error with error code 200.
return {200};
}

◆ TStatus() [5/5]

template<typename TCode , typename TDesc >
tec::TStatus< TCode, TDesc >::TStatus ( const TCode _code,
const TDesc _desc,
Error::Kind  _kind = Error::Kind::Err 
)
inline

Constructs an error status with code and description.

Initializes the status with the specified error kind, code, and description.

Parameters
_codeThe error code.
_descThe error description.
_kindThe error kind (defaults to Error::Kind::Err).
tec::Status do_something5() {
std::string addr{"127.0.0.1"};
// ...
// Network error with code and description.
return {404,
tec::format("Cannot connect to {}", addr),
}
@ NetErr
Network-related error.
std::string format(const T &arg)
Formats a single argument into a string.
Definition tec_print.hpp:171

Member Function Documentation

◆ as_string()

template<typename TCode , typename TDesc >
std::string tec::TStatus< TCode, TDesc >::as_string ( )
inline

Converts the status to a string representation.

Uses the stream output operator to create a string representation of the status.

Returns
std::string The string representation of the status.

◆ ok()

template<typename TCode , typename TDesc >
constexpr bool tec::TStatus< TCode, TDesc >::ok ( ) const
inlineconstexpr

Checks if the status indicates success.

Returns true if the error kind is Error::Kind::Ok, false otherwise.

Returns
bool True if the status is successful, false otherwise.

◆ operator bool()

template<typename TCode , typename TDesc >
constexpr tec::TStatus< TCode, TDesc >::operator bool ( ) const
inlineconstexpr

Conversion operator to check if the status is successful.

Implicitly converts the status to a boolean, equivalent to calling ok().

Returns
bool True if the status is successful, false otherwise.

Friends And Related Symbol Documentation

◆ operator<<

template<typename TCode , typename TDesc >
std::ostream & operator<< ( std::ostream &  out,
const TStatus< TCode, TDesc > &  status 
)
friend

Outputs the status to an output stream.

Formats the status as a string, including the error kind, and optionally the error code and description if the status is not Ok.

Parameters
outThe output stream to write to.
statusThe TStatus object to output.
Returns
std::ostream& The modified output stream.

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