The Gaudi Framework  v36r1 (3e2fb5a8)
StatusCode Class Referencefinal

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

Collaboration diagram for StatusCode:

Classes

class  Category
 
class  ScopedDisableChecking
 Simple RAII class to ignore unchecked StatusCode instances in a scope. More...
 

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...
 
template<typename T , typename = std::enable_if_t<is_StatusCode_enum<T>::value>>
 StatusCode (T sc, bool) 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, const StatusCode::Category &cat, bool) noexcept
 Constructor from code_t in the default category (explicit conversion only) More...
 
 StatusCode (code_t code) noexcept
 Constructor from code_t in the default category (explicit conversion only) More...
 
 StatusCode (code_t code, bool) noexcept
 Constructor from code_t and 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 ("checks" the StatusCode) More...
 
const StatusCodesetChecked (bool=true) const
 Check/uncheck StatusCode. More...
 
StatusCodesetChecked (bool=true)
 
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...
 
bool checked () const
 Has the StatusCode been checked? More...
 
const StatusCode::CategorygetCategory () const
 Retrieve category (does not "check" the StatusCode) 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 GAUDI_API void enableChecking ()
 
static GAUDI_API void disableChecking ()
 
static GAUDI_API bool checkingEnabled ()
 

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 check ()
 Do StatusCode check. 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/9]

StatusCode::StatusCode ( )
default

Default constructor.

◆ StatusCode() [2/9]

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/9]

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

Constructor from enum type (allowing implicit conversion)

Definition at line 113 of file StatusCode.h.

113 : StatusCode{sc} {}

◆ StatusCode() [4/9]

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

Constructor from code_t and category (explicit conversion only)

Definition at line 116 of file StatusCode.h.

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

◆ StatusCode() [5/9]

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

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

Definition at line 119 of file StatusCode.h.

121  : StatusCode{code, cat} {}

◆ StatusCode() [6/9]

StatusCode::StatusCode ( code_t  code)
inlineexplicitnoexcept

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

Definition at line 124 of file StatusCode.h.

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

◆ StatusCode() [7/9]

StatusCode::StatusCode ( code_t  code,
bool   
)
inlineexplicitnoexcept

Constructor from code_t and category (explicit conversion only)

Definition at line 127 of file StatusCode.h.

128  : StatusCode{code} {}

◆ StatusCode() [8/9]

StatusCode::StatusCode ( const StatusCode rhs)
defaultnoexcept

Copy constructor.

◆ StatusCode() [9/9]

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 180 of file StatusCode.h.

180  {
181  if ( isFailure() ) return *this;
182  return i_invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
183  }

◆ check()

void StatusCode::check ( )
private

Do StatusCode check.

◆ checked()

bool StatusCode::checked ( ) const
inline

Has the StatusCode been checked?

Definition at line 238 of file StatusCode.h.

238 { return true; }

◆ checkingEnabled()

bool StatusCode::checkingEnabled ( )
static

Definition at line 53 of file StatusCode.cpp.

53 { return false; }

◆ default_category()

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

Default Gaudi StatusCode category.

Definition at line 351 of file StatusCode.h.

◆ default_value()

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

Project onto the default StatusCode values.

Definition at line 361 of file StatusCode.h.

361  {
363  return r;
364 }

◆ disableChecking()

void StatusCode::disableChecking ( )
static

Definition at line 51 of file StatusCode.cpp.

51 {}

◆ enableChecking()

void StatusCode::enableChecking ( )
static

Definition at line 49 of file StatusCode.cpp.

49 {}

◆ getCategory()

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

Retrieve category (does not "check" the StatusCode)

Definition at line 241 of file StatusCode.h.

241 { return *m_cat; }

◆ getCode()

code_t StatusCode::getCode ( ) const
inline

Retrieve value ("checks" the StatusCode)

Definition at line 149 of file StatusCode.h.

149 { 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 55 of file StatusCode.cpp.

55  {
56  throw GaudiException{std::string{message}, std::string{tag}, *this};
57 }

◆ 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 310 of file StatusCode.h.

310  {
311  if constexpr ( std::is_invocable_r_v<StatusCode, F, ARGS...> ) {
312  return std::invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
313  } else {
314  // static_assert( std::is_same_v<void,std::invoke_result_t<F,ARGS...>>); // how paranoid should this be?
315  std::invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
316  return *this;
317  }
318  }

◆ ignore() [1/2]

StatusCode& StatusCode::ignore ( )
inline

Definition at line 157 of file StatusCode.h.

157 { return *this; }

◆ ignore() [2/2]

const StatusCode& StatusCode::ignore ( ) const
inline

Allow discarding a StatusCode without warning.

Definition at line 156 of file StatusCode.h.

156 { return *this; }

◆ isFailure()

bool StatusCode::isFailure ( ) const
inline

Definition at line 142 of file StatusCode.h.

142 { return !isSuccess(); }

◆ isRecoverable()

bool StatusCode::isRecoverable ( ) const
inline

Definition at line 359 of file StatusCode.h.

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

◆ isSuccess()

bool StatusCode::isSuccess ( ) const
inline

Definition at line 355 of file StatusCode.h.

355  {
356  return ( m_code == static_cast<code_t>( ErrorCode::SUCCESS ) || m_cat->isSuccess( m_code ) );
357 }

◆ message()

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

Description (or name) of StatusCode value.

Definition at line 244 of file StatusCode.h.

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

◆ operator bool()

StatusCode::operator bool ( ) const
inlineexplicit

Shorthand for isSuccess()

Definition at line 146 of file StatusCode.h.

146 { return isSuccess(); }

◆ operator&=()

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

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

Definition at line 373 of file StatusCode.h.

373  {
374  // Ternary AND lookup matrix
375  static constexpr StatusCode::code_t AND[3][3] = {{0, 0, 0}, {0, 1, 2}, {0, 2, 2}};
376 
378  StatusCode::code_t r = static_cast<StatusCode::code_t>( rhs.default_value() );
379  m_code = AND[l][r];
380  return *this;
381 }

◆ 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 383 of file StatusCode.h.

383  {
384  // Ternary OR lookup matrix
385  static constexpr StatusCode::code_t OR[3][3] = {{0, 1, 2}, {1, 1, 1}, {2, 1, 2}};
386 
388  StatusCode::code_t r = static_cast<StatusCode::code_t>( rhs.default_value() );
389  m_code = OR[l][r];
390  return *this;
391 }

◆ 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 202 of file StatusCode.h.

202  {
203  if ( isSuccess() ) return *this;
204  return i_invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
205  }

◆ 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 223 of file StatusCode.h.

223  {
224  if ( isFailure() ) i_doThrow( message, tag );
225  return *this;
226  }

◆ 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 232 of file StatusCode.h.

232  {
233  if ( isFailure() ) i_doThrow( message(), tag ); // make sure `message()` is only called on error path
234  return *this;
235  }

◆ setChecked() [1/2]

StatusCode& StatusCode::setChecked ( bool  = true)
inline

Definition at line 153 of file StatusCode.h.

153 { return *this; }

◆ setChecked() [2/2]

const StatusCode& StatusCode::setChecked ( bool  = true) const
inline

Check/uncheck StatusCode.

Definition at line 152 of file StatusCode.h.

152 { return *this; }

Friends And Related Function Documentation

◆ operator!=

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

Definition at line 255 of file StatusCode.h.

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

◆ operator&

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

Ternary AND operator.

Definition at line 267 of file StatusCode.h.

267 { return lhs &= rhs; }

◆ operator&=

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

Boolean AND assignment operator.

Definition at line 273 of file StatusCode.h.

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

◆ operator<

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

Comparison (values are grouped by category first)

Definition at line 258 of file StatusCode.h.

258  {
259  return ( lhs.m_cat < rhs.m_cat || ( lhs.m_cat == rhs.m_cat && lhs.m_code < rhs.m_code ) );
260  }

◆ operator<<

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

Definition at line 246 of file StatusCode.h.

246  {
247  s << sc.message();
248  return s;
249  }

◆ 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 366 of file StatusCode.h.

366  {
367  return ( lhs.m_code == rhs.m_code ) &&
368  ( lhs.m_code == static_cast<StatusCode::code_t>( StatusCode::ErrorCode::SUCCESS ) ||
370  ( lhs.m_cat == rhs.m_cat ) );
371 }

◆ operator|

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

Ternary OR operator.

Definition at line 270 of file StatusCode.h.

270 { return lhs |= rhs; }

◆ operator|=

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

Boolean OR assignment operator.

Definition at line 276 of file StatusCode.h.

276 { 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 299 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 300 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 (does not "check" the StatusCode)
Definition: StatusCode.h:241
StatusCode::ErrorCode::SUCCESS
@ SUCCESS
StatusCode::isRecoverable
bool isRecoverable() const
Definition: StatusCode.h:359
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:310
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:351
StatusCode::andThen
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition: StatusCode.h:180
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:355
gaudirun.s
string s
Definition: gaudirun.py:328
GaudiException
Definition: GaudiException.h:31
StatusCode::message
std::string message() const
Description (or name) of StatusCode value.
Definition: StatusCode.h:244
StatusCode::orElse
StatusCode orElse(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a failure result.
Definition: StatusCode.h:202
StatusCode::m_code
code_t m_code
The status code value.
Definition: StatusCode.h:300
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
HistoDumpEx.r
r
Definition: HistoDumpEx.py:20
StatusCode::default_value
ErrorCode default_value() const
Project onto the default StatusCode values.
Definition: StatusCode.h:361
StatusCode::m_cat
const Category * m_cat
The status code category.
Definition: StatusCode.h:299
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:142
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
gaudirun.l
dictionary l
Definition: gaudirun.py:553
StatusCode::ErrorCode::RECOVERABLE
@ RECOVERABLE
gaudirun.args
args
Definition: gaudirun.py:319
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:55
StatusCode::Category::message
virtual std::string message(code_t code) const
Description for code within this category.
Definition: StatusCode.h:86