The Gaudi Framework  master (594c33fa)
StatusCode Class Referencefinal

#include </builds/gaudi/Gaudi/GaudiKernel/include/GaudiKernel/StatusCode.h>

Collaboration diagram for StatusCode:

Classes

class  Category
 

Public Types

enum  ErrorCode : code_t { ErrorCode::FAILURE = 0, ErrorCode::SUCCESS = 1, ErrorCode::RECOVERABLE = 2 }
 
typedef unsigned long code_t
 type of StatusCode value More...
 

Public Member Functions

 StatusCode ()=default
 Default constructor. More...
 
template<typename T , typename = std::enable_if_t<is_StatusCode_enum<T>::value>>
 StatusCode (T sc) noexcept
 Constructor from enum type (allowing implicit conversion) More...
 
 StatusCode (code_t code, const StatusCode::Category &cat) noexcept
 Constructor from code_t and category (explicit conversion only) More...
 
 StatusCode (code_t code) noexcept
 Constructor from code_t in the default category (explicit conversion only) More...
 
 StatusCode (const StatusCode &rhs) noexcept=default
 Copy constructor. More...
 
 StatusCode (StatusCode &&rhs) noexcept=default
 Move constructor. More...
 
 ~StatusCode ()=default
 Destructor. More...
 
StatusCodeoperator= (const StatusCode &rhs) noexcept=default
 
bool isSuccess () const
 
bool isFailure () const
 
bool isRecoverable () const
 
 operator bool () const
 Shorthand for isSuccess() More...
 
code_t getCode () const
 Retrieve value. More...
 
const StatusCodeignore () const
 Allow discarding a StatusCode without warning. More...
 
StatusCodeignore ()
 
template<typename F , typename... ARGS>
StatusCode andThen (F &&f, ARGS &&... args) const
 Chain code blocks making the execution conditional a success result. More...
 
template<typename F , typename... ARGS>
StatusCode orElse (F &&f, ARGS &&... args) const
 Chain code blocks making the execution conditional a failure result. More...
 
const StatusCodeorThrow (std::string_view message, std::string_view tag) const
 Throw a GaudiException in case of failures. More...
 
const StatusCodeorThrow (std::string_view tag="") const
 Throw a GaudiException in case of failures. More...
 
const StatusCode::CategorygetCategory () const
 Retrieve category. More...
 
std::string message () const
 Description (or name) of StatusCode value. More...
 
StatusCodeoperator&= (const StatusCode &rhs)
 Ternary logic operator with RECOVERABLE being the "third" state. More...
 
StatusCodeoperator|= (const StatusCode &rhs)
 Ternary logic operator with RECOVERABLE being the "third" state. More...
 

Static Public Member Functions

static const Categorydefault_category () noexcept
 Default Gaudi StatusCode category. More...
 

Static Public Attributes

constexpr static const auto SUCCESS = ErrorCode::SUCCESS
 
constexpr static const auto FAILURE = ErrorCode::FAILURE
 
constexpr static const auto RECOVERABLE = ErrorCode::RECOVERABLE
 

Private Member Functions

ErrorCode default_value () const
 Project onto the default StatusCode values. More...
 
void i_doThrow (std::string_view message, std::string_view tag) const
 Helper function to avoid circular dependency between GaudiException.h and StatusCode.h. More...
 
template<typename F , typename... ARGS, typename = std::enable_if_t<std::is_invocable_v<F, ARGS...>>>
StatusCode i_invoke (F &&f, ARGS &&... args) const
 Helper to invoke a callable and return the resulting StatusCode or this, if the callable returns void. More...
 

Private Attributes

const Categorym_cat { &default_category() }
 The status code category. More...
 
code_t m_code { static_cast<code_t>( ErrorCode::SUCCESS ) }
 The status code value. More...
 

Friends

std::ostreamoperator<< (std::ostream &s, const StatusCode &sc)
 
bool operator== (const StatusCode &lhs, const StatusCode &rhs)
 Check if StatusCode value and category are the same. More...
 
bool operator!= (const StatusCode &lhs, const StatusCode &rhs)
 
bool operator< (const StatusCode &lhs, const StatusCode &rhs)
 Comparison (values are grouped by category first) More...
 
StatusCode operator& (StatusCode lhs, const StatusCode &rhs)
 Ternary AND operator. More...
 
StatusCode operator| (StatusCode lhs, const StatusCode &rhs)
 Ternary OR operator. More...
 
bool & operator&= (bool &lhs, const StatusCode &sc)
 Boolean AND assignment operator. More...
 
bool & operator|= (bool &lhs, const StatusCode &sc)
 Boolean OR assignment operator. More...
 

Detailed Description

This class is used for returning status codes from appropriate routines. A StatusCode is comprised of an (integer) value and a category (similar to std::error_code). By default StatusCodes are created within a default StatusCode::Category, which defines the isSuccess(), isFailure() and isRecoverable() behaviour depending on the value of the StatusCode. This behaviour can be modified by defining a custom StatusCode::Category and overriding the respective methods.

To define a new StatusCode::Category, do the following:

Combining StatusCodes
  • The bitwise logic operators (&, |, &=, |=) can be used to combine StatusCodes and follows three-valued (ternary) logic with RECOVERABLE being the third state. No short-circuiting is applied:
    StatusCode sc = sc1 & sc2;
    sc |= sc3;
  • The boolean logic operators (&&, ||) perform regular two-valued logic only considering success/failure. The usual short-circuiting applies:
    bool b = sc1 && sc2;
Note
The StatusCode values 0 and 1 are considered FAILURE/SUCCESS for all categories, i.e.
Remarks
See https://akrzemi1.wordpress.com/2017/07/12/your-own-error-code for details on the underlying design

Definition at line 61 of file StatusCode.h.

Member Typedef Documentation

◆ code_t

typedef unsigned long StatusCode::code_t

type of StatusCode value

Definition at line 67 of file StatusCode.h.

Member Enumeration Documentation

◆ ErrorCode

Enumerator
FAILURE 
SUCCESS 
RECOVERABLE 

Definition at line 69 of file StatusCode.h.

69 : code_t { FAILURE = 0, SUCCESS = 1, RECOVERABLE = 2 };

Constructor & Destructor Documentation

◆ StatusCode() [1/6]

StatusCode::StatusCode ( )
default

Default constructor.

◆ StatusCode() [2/6]

template<typename T , typename = std::enable_if_t<is_StatusCode_enum<T>::value>>
StatusCode::StatusCode ( sc)
inlinenoexcept

Constructor from enum type (allowing implicit conversion)

Definition at line 109 of file StatusCode.h.

◆ StatusCode() [3/6]

StatusCode::StatusCode ( code_t  code,
const StatusCode::Category cat 
)
inlineexplicitnoexcept

Constructor from code_t and category (explicit conversion only)

Definition at line 112 of file StatusCode.h.

112 : m_cat( &cat ), m_code( code ) {}

◆ StatusCode() [4/6]

StatusCode::StatusCode ( code_t  code)
inlineexplicitnoexcept

Constructor from code_t in the default category (explicit conversion only)

Definition at line 115 of file StatusCode.h.

115 : StatusCode( code, default_category() ) {}

◆ StatusCode() [5/6]

StatusCode::StatusCode ( const StatusCode rhs)
defaultnoexcept

Copy constructor.

◆ StatusCode() [6/6]

StatusCode::StatusCode ( StatusCode &&  rhs)
defaultnoexcept

Move constructor.

◆ ~StatusCode()

StatusCode::~StatusCode ( )
default

Destructor.

Member Function Documentation

◆ andThen()

template<typename F , typename... ARGS>
StatusCode StatusCode::andThen ( F &&  f,
ARGS &&...  args 
) const
inline

Chain code blocks making the execution conditional a success result.

The chained execution stops on the first non-success StatusCode in the chain and returns it, or continues until the end and returns the last produced StatusCode.

For example:

StatusCode myFunction() {
return subFunction()
.andThen([]() {
do_something();
})
.andThen(anotherFunction)
.andThen([]() {
if (is_special_case())
return do_something_else();
});
}

Definition at line 163 of file StatusCode.h.

163  {
164  if ( isFailure() ) return *this;
165  return i_invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
166  }

◆ default_category()

const StatusCode::Category & StatusCode::default_category ( )
inlinestaticnoexcept

Default Gaudi StatusCode category.

Definition at line 310 of file StatusCode.h.

◆ default_value()

StatusCode::ErrorCode StatusCode::default_value ( ) const
inlineprivate

Project onto the default StatusCode values.

Definition at line 320 of file StatusCode.h.

320  {
322  return r;
323 }

◆ getCategory()

const StatusCode::Category& StatusCode::getCategory ( ) const
inline

Retrieve category.

Definition at line 221 of file StatusCode.h.

221 { return *m_cat; }

◆ getCode()

code_t StatusCode::getCode ( ) const
inline

Retrieve value.

Definition at line 136 of file StatusCode.h.

136 { return m_code; }

◆ i_doThrow()

void StatusCode::i_doThrow ( std::string_view  message,
std::string_view  tag 
) const
private

Helper function to avoid circular dependency between GaudiException.h and StatusCode.h.

Definition at line 49 of file StatusCode.cpp.

49  {
50  throw GaudiException{ std::string{ message }, std::string{ tag }, *this };
51 }

◆ i_invoke()

template<typename F , typename... ARGS, typename = std::enable_if_t<std::is_invocable_v<F, ARGS...>>>
StatusCode StatusCode::i_invoke ( F &&  f,
ARGS &&...  args 
) const
inlineprivate

Helper to invoke a callable and return the resulting StatusCode or this, if the callable returns void.

Definition at line 269 of file StatusCode.h.

269  {
270  if constexpr ( std::is_invocable_r_v<StatusCode, F, ARGS...> ) {
271  return std::invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
272  } else {
273  // static_assert( std::is_same_v<void,std::invoke_result_t<F,ARGS...>>); // how paranoid should this be?
274  std::invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
275  return *this;
276  }
277  }

◆ ignore() [1/2]

StatusCode& StatusCode::ignore ( )
inline

Definition at line 140 of file StatusCode.h.

140 { return *this; }

◆ ignore() [2/2]

const StatusCode& StatusCode::ignore ( ) const
inline

Allow discarding a StatusCode without warning.

Definition at line 139 of file StatusCode.h.

139 { return *this; }

◆ isFailure()

bool StatusCode::isFailure ( ) const
inline

Definition at line 129 of file StatusCode.h.

129 { return !isSuccess(); }

◆ isRecoverable()

bool StatusCode::isRecoverable ( ) const
inline

Definition at line 318 of file StatusCode.h.

318 { return m_cat->isRecoverable( m_code ); }

◆ isSuccess()

bool StatusCode::isSuccess ( ) const
inline

Definition at line 314 of file StatusCode.h.

314  {
315  return ( m_code == static_cast<code_t>( ErrorCode::SUCCESS ) || m_cat->isSuccess( m_code ) );
316 }

◆ message()

std::string StatusCode::message ( ) const
inline

Description (or name) of StatusCode value.

Definition at line 224 of file StatusCode.h.

224 { return getCategory().message( m_code ); }

◆ operator bool()

StatusCode::operator bool ( ) const
inlineexplicit

Shorthand for isSuccess()

Definition at line 133 of file StatusCode.h.

133 { return isSuccess(); }

◆ operator&=()

StatusCode & StatusCode::operator&= ( const StatusCode rhs)
inline

Ternary logic operator with RECOVERABLE being the "third" state.

Definition at line 332 of file StatusCode.h.

332  {
333  // Ternary AND lookup matrix
334  static constexpr StatusCode::code_t AND[3][3] = { { 0, 0, 0 }, { 0, 1, 2 }, { 0, 2, 2 } };
335 
337  StatusCode::code_t r = static_cast<StatusCode::code_t>( rhs.default_value() );
338  m_code = AND[l][r];
339  return *this;
340 }

◆ operator=()

StatusCode& StatusCode::operator= ( const StatusCode rhs)
defaultnoexcept

◆ operator|=()

StatusCode & StatusCode::operator|= ( const StatusCode rhs)
inline

Ternary logic operator with RECOVERABLE being the "third" state.

Definition at line 342 of file StatusCode.h.

342  {
343  // Ternary OR lookup matrix
344  static constexpr StatusCode::code_t OR[3][3] = { { 0, 1, 2 }, { 1, 1, 1 }, { 2, 1, 2 } };
345 
347  StatusCode::code_t r = static_cast<StatusCode::code_t>( rhs.default_value() );
348  m_code = OR[l][r];
349  return *this;
350 }

◆ orElse()

template<typename F , typename... ARGS>
StatusCode StatusCode::orElse ( F &&  f,
ARGS &&...  args 
) const
inline

Chain code blocks making the execution conditional a failure result.

Inverse of StatusCode::andThen(), the passed function gets invoked only if if the StatusCode is a failure, in which case it either pass it on or overrides it, If the StatusCode is a success, it is passed on.

For example:

StatusCode myFunction() {
return subFunction()
.andThen([]() {
do_something();
})
.orElse(reportProblem);
}

Definition at line 185 of file StatusCode.h.

185  {
186  if ( isSuccess() ) return *this;
187  return i_invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
188  }

◆ orThrow() [1/2]

const StatusCode& StatusCode::orThrow ( std::string_view  message,
std::string_view  tag 
) const
inline

Throw a GaudiException in case of failures.

It can be chained with StatusCode::then, behaving as a pass-through for a success StatusCode, while for non-success a GaudiException is thrown, propagating the failure into the exception (using the StatusCode field of the exception).

For example:

void myFunction() {
doSomething()
.andThen( doSomethingElse )
.orThrow( "some error", "myFunction" )
.andThen( moreActions )
.orThrow( "too bad, we were nearly there", "myFunction" );
}

Definition at line 206 of file StatusCode.h.

206  {
207  if ( isFailure() ) i_doThrow( message, tag );
208  return *this;
209  }

◆ orThrow() [2/2]

const StatusCode& StatusCode::orThrow ( std::string_view  tag = "") const
inline

Throw a GaudiException in case of failures.

See above, but in this case the message is not specified explicitly, but taken from message()

Definition at line 215 of file StatusCode.h.

215  {
216  if ( isFailure() ) i_doThrow( message(), tag ); // make sure `message()` is only called on error path
217  return *this;
218  }

Friends And Related Function Documentation

◆ operator!=

bool operator!= ( const StatusCode lhs,
const StatusCode rhs 
)
friend

Definition at line 235 of file StatusCode.h.

235 { return !( lhs == rhs ); }

◆ operator&

StatusCode operator& ( StatusCode  lhs,
const StatusCode rhs 
)
friend

Ternary AND operator.

Definition at line 247 of file StatusCode.h.

247 { return lhs &= rhs; }

◆ operator&=

bool& operator&= ( bool &  lhs,
const StatusCode sc 
)
friend

Boolean AND assignment operator.

Definition at line 253 of file StatusCode.h.

253 { return lhs &= sc.isSuccess(); }

◆ operator<

bool operator< ( const StatusCode lhs,
const StatusCode rhs 
)
friend

Comparison (values are grouped by category first)

Definition at line 238 of file StatusCode.h.

238  {
239  return ( lhs.m_cat < rhs.m_cat || ( lhs.m_cat == rhs.m_cat && lhs.m_code < rhs.m_code ) );
240  }

◆ operator<<

std::ostream& operator<< ( std::ostream s,
const StatusCode sc 
)
friend

Definition at line 226 of file StatusCode.h.

226  {
227  s << sc.message();
228  return s;
229  }

◆ operator==

bool operator== ( const StatusCode lhs,
const StatusCode rhs 
)
friend

Check if StatusCode value and category are the same.

Note
For code values 0(FAILURE) and 1(SUCCESS) the category is ignored
e.g. sc==StatusCode::SUCCESS is equivalent to sc.isSuccess() for all categories

Definition at line 325 of file StatusCode.h.

325  {
326  return ( lhs.m_code == rhs.m_code ) &&
327  ( lhs.m_code == static_cast<StatusCode::code_t>( StatusCode::ErrorCode::SUCCESS ) ||
329  ( lhs.m_cat == rhs.m_cat ) );
330 }

◆ operator|

StatusCode operator| ( StatusCode  lhs,
const StatusCode rhs 
)
friend

Ternary OR operator.

Definition at line 250 of file StatusCode.h.

250 { return lhs |= rhs; }

◆ operator|=

bool& operator|= ( bool &  lhs,
const StatusCode sc 
)
friend

Boolean OR assignment operator.

Definition at line 256 of file StatusCode.h.

256 { return lhs |= sc.isSuccess(); }

Member Data Documentation

◆ FAILURE

constexpr const StatusCode::ErrorCode StatusCode::FAILURE = ErrorCode::FAILURE
staticconstexpr

Definition at line 101 of file StatusCode.h.

◆ m_cat

const Category* StatusCode::m_cat { &default_category() }
private

The status code category.

Definition at line 259 of file StatusCode.h.

◆ m_code

code_t StatusCode::m_code { static_cast<code_t>( ErrorCode::SUCCESS ) }
private

The status code value.

Definition at line 260 of file StatusCode.h.

◆ RECOVERABLE

constexpr const StatusCode::ErrorCode StatusCode::RECOVERABLE = ErrorCode::RECOVERABLE
staticconstexpr

Definition at line 102 of file StatusCode.h.

◆ SUCCESS

constexpr const StatusCode::ErrorCode StatusCode::SUCCESS = ErrorCode::SUCCESS
staticconstexpr

Definition at line 100 of file StatusCode.h.


The documentation for this class was generated from the following files:
StatusCode::getCategory
const StatusCode::Category & getCategory() const
Retrieve category.
Definition: StatusCode.h:221
StatusCode::ErrorCode::SUCCESS
@ SUCCESS
StatusCode::isRecoverable
bool isRecoverable() const
Definition: StatusCode.h:318
StatusCode::i_invoke
StatusCode i_invoke(F &&f, ARGS &&... args) const
Helper to invoke a callable and return the resulting StatusCode or this, if the callable returns void...
Definition: StatusCode.h:269
std::string
STL class.
StatusCode::Category::isSuccess
virtual bool isSuccess(code_t code) const
Is code considered success ?
Definition: StatusCode.h:90
StatusCode::default_category
static const Category & default_category() noexcept
Default Gaudi StatusCode category.
Definition: StatusCode.h:310
StatusCode::andThen
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition: StatusCode.h:163
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
gaudirun.s
string s
Definition: gaudirun.py:346
GaudiException
Definition: GaudiException.h:31
StatusCode::message
std::string message() const
Description (or name) of StatusCode value.
Definition: StatusCode.h:224
StatusCode::orElse
StatusCode orElse(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a failure result.
Definition: StatusCode.h:185
StatusCode::m_code
code_t m_code
The status code value.
Definition: StatusCode.h:260
StatusCode::code_t
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:67
StatusCode::StatusCode
StatusCode()=default
Default constructor.
StatusCode
Definition: StatusCode.h:65
StatusCode::default_value
ErrorCode default_value() const
Project onto the default StatusCode values.
Definition: StatusCode.h:320
StatusCode::m_cat
const Category * m_cat
The status code category.
Definition: StatusCode.h:259
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
gaudirun.l
dictionary l
Definition: gaudirun.py:581
StatusCode::ErrorCode::RECOVERABLE
@ RECOVERABLE
gaudirun.args
args
Definition: gaudirun.py:336
StatusCode::ErrorCode::FAILURE
@ FAILURE
is_StatusCode_enum
Definition: StatusCode.h:23
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
StatusCode::RECOVERABLE
constexpr static const auto RECOVERABLE
Definition: StatusCode.h:102
StatusCode::Category::isRecoverable
virtual bool isRecoverable(code_t code) const
Is code considered recoverable ?
Definition: StatusCode.h:93
StatusCode::i_doThrow
void i_doThrow(std::string_view message, std::string_view tag) const
Helper function to avoid circular dependency between GaudiException.h and StatusCode....
Definition: StatusCode.cpp:49
StatusCode::Category::message
virtual std::string message(code_t code) const
Description for code within this category.
Definition: StatusCode.h:86