Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
StatusCode Class Referencefinal

This class is used for returning status codes from appropriate routines. More...

#include <GaudiKernel/StatusCode.h>

Collaboration diagram for StatusCode:

Classes

class  Category
 The category assigned to a StatusCode. More...
 
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 = typename std::enable_if<is_StatusCode_enum<T>::value>::type>
 StatusCode (T sc, bool checked=false) noexcept
 Constructor from enum type (allowing implicit conversion) More...
 
 StatusCode (code_t code, const StatusCode::Category &cat=default_category(), bool checked=false) noexcept
 Constructor from code_t in the default category (explicit conversion only) More...
 
 StatusCode (code_t code, bool checked) noexcept
 Constructor from code_t and category (explicit conversion only) More...
 
 StatusCode (const StatusCode &rhs) noexcept
 Copy constructor. More...
 
 StatusCode (StatusCode &&rhs) noexcept
 Move constructor. More...
 
 ~StatusCode ()
 Destructor. More...
 
StatusCodeoperator= (const StatusCode &rhs) noexcept
 
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 checked=true) const
 Check/uncheck StatusCode. More...
 
StatusCodesetChecked (bool checked=true)
 
const StatusCodeignore () const
 Ignore/check StatusCode. More...
 
StatusCodeignore ()
 
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...
 

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...
 
bool m_checked {false}
 If the StatusCode has been checked. More...
 

Static Private Attributes

static bool s_checking
 Global flag to control if StatusCode need to be checked. 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...
 

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

Member Typedef Documentation

typedef unsigned long StatusCode::code_t

type of StatusCode value

Definition at line 52 of file StatusCode.h.

Member Enumeration Documentation

Enumerator
FAILURE 
SUCCESS 
RECOVERABLE 

Definition at line 54 of file StatusCode.h.

54 : code_t { FAILURE = 0, SUCCESS = 1, RECOVERABLE = 2 };
constexpr static const auto RECOVERABLE
Definition: StatusCode.h:87
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
constexpr static const auto FAILURE
Definition: StatusCode.h:86
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:52

Constructor & Destructor Documentation

StatusCode::StatusCode ( )
default

Default constructor.

template<typename T , typename = typename std::enable_if<is_StatusCode_enum<T>::value>::type>
StatusCode::StatusCode ( sc,
bool  checked = false 
)
inlinenoexcept

Constructor from enum type (allowing implicit conversion)

Definition at line 94 of file StatusCode.h.

94  {
95  *this = StatusCode( static_cast<StatusCode::code_t>( sc ), is_StatusCode_enum<T>::instance );
97  }
bool checked() const
Has the StatusCode been checked?
Definition: StatusCode.h:163
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226
StatusCode()=default
Default constructor.
StatusCode::StatusCode ( code_t  code,
const StatusCode::Category cat = default_category(),
bool  checked = false 
)
inlineexplicitnoexcept

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

Definition at line 100 of file StatusCode.h.

102  : m_cat( &cat ), m_code( code ), m_checked( checked ) {}
code_t m_code
The status code value.
Definition: StatusCode.h:225
bool checked() const
Has the StatusCode been checked?
Definition: StatusCode.h:163
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226
const Category * m_cat
The status code category.
Definition: StatusCode.h:224
StatusCode::StatusCode ( code_t  code,
bool  checked 
)
inlineexplicitnoexcept

Constructor from code_t and category (explicit conversion only)

Definition at line 105 of file StatusCode.h.

105 : StatusCode( code, default_category(), checked ) {}
bool checked() const
Has the StatusCode been checked?
Definition: StatusCode.h:163
static const Category & default_category() noexcept
Default Gaudi StatusCode category.
Definition: StatusCode.h:263
StatusCode()=default
Default constructor.
StatusCode::StatusCode ( const StatusCode rhs)
inlinenoexcept

Copy constructor.

Definition at line 108 of file StatusCode.h.

108  : m_cat( rhs.m_cat ), m_code( rhs.m_code ), m_checked( rhs.m_checked ) {
109  rhs.m_checked = true;
110  }
code_t m_code
The status code value.
Definition: StatusCode.h:225
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226
const Category * m_cat
The status code category.
Definition: StatusCode.h:224
StatusCode::StatusCode ( StatusCode &&  rhs)
inlinenoexcept

Move constructor.

Definition at line 113 of file StatusCode.h.

113  : m_cat( rhs.m_cat ), m_code( rhs.m_code ), m_checked( rhs.m_checked ) {
114  rhs.m_checked = true;
115  }
code_t m_code
The status code value.
Definition: StatusCode.h:225
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226
const Category * m_cat
The status code category.
Definition: StatusCode.h:224
StatusCode::~StatusCode ( )
inline

Destructor.

Definition at line 118 of file StatusCode.h.

118  {
119  if ( UNLIKELY( s_checking ) ) check();
120  }
#define UNLIKELY(x)
Definition: Kernel.h:89
void check()
Do StatusCode check.
Definition: StatusCode.cpp:48
static bool s_checking
Global flag to control if StatusCode need to be checked.
Definition: StatusCode.h:227

Member Function Documentation

void StatusCode::check ( )
private

Do StatusCode check.

Fix-Me:
: (MCl) use backTrace(std::string&, const int, const int) instead

Definition at line 48 of file StatusCode.cpp.

48  {
49 
51 #if __cplusplus > 201402L // c++17
52  !std::uncaught_exceptions()
53 #else
55 #endif
56  ) {
57 
58  auto msg = Gaudi::svcLocator()->as<IMessageSvc>();
59  auto scs = Gaudi::svcLocator()->service<IStatusCodeSvc>( "StatusCodeSvc" );
60 
61  const size_t depth = 21;
62  void* addresses[depth];
63 
64  std::string lib, fnc;
65  void* addr = nullptr;
67  if ( System::backTrace( addresses, depth ) ) {
68 
69  for ( size_t idx : {2, 3} ) {
70  // When running address sanitizer builds with -fno-omit-frame-pointer
71  // StatusCode::check() might appear as the function name, so skip.
72  if ( System::getStackLevel( addresses[idx], addr, fnc, lib ) && fnc != "StatusCode::~StatusCode()" &&
73  fnc != "StatusCode::check()" ) {
74  if ( scs ) {
75  scs->regFnc( fnc, lib );
76  } else {
77  MsgStream log( msg, "StatusCode" );
78  log << MSG::WARNING << "Unchecked in " << fnc << " (" << lib << ")" << endmsg;
79  }
80  break;
81  }
82  }
83  }
84  }
85 }
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
GAUDI_API bool getStackLevel(void *addresses, void *&addr, std::string &fnc, std::string &lib)
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226
static bool s_proc
"previous" element in the linked list
SmartIF< IFace > as()
Definition: ISvcLocator.h:103
GAUDI_API int backTrace(void **addresses, const int depth)
STL class.
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
GAUDI_API ISvcLocator * svcLocator()
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:37
T uncaught_exception(T...args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
bool StatusCode::checked ( ) const
inline

Has the StatusCode been checked?

Definition at line 163 of file StatusCode.h.

163 { return m_checked; }
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226
bool StatusCode::checkingEnabled ( )
static

Definition at line 46 of file StatusCode.cpp.

46 { return s_checking; }
static bool s_checking
Global flag to control if StatusCode need to be checked.
Definition: StatusCode.h:227
const StatusCode::Category & StatusCode::default_category ( )
inlinestaticnoexcept

Default Gaudi StatusCode category.

Definition at line 263 of file StatusCode.h.

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

Project onto the default StatusCode values.

Definition at line 277 of file StatusCode.h.

277  {
278  bool save_checked = m_checked; // Preserve checked status
280  m_checked = save_checked;
281  return r;
282 }
bool isSuccess() const
Definition: StatusCode.h:267
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226
bool isRecoverable() const
Definition: StatusCode.h:272
void StatusCode::disableChecking ( )
static

Definition at line 44 of file StatusCode.cpp.

44 { s_checking = false; }
static bool s_checking
Global flag to control if StatusCode need to be checked.
Definition: StatusCode.h:227
void StatusCode::enableChecking ( )
static

Definition at line 42 of file StatusCode.cpp.

42 { s_checking = true; }
static bool s_checking
Global flag to control if StatusCode need to be checked.
Definition: StatusCode.h:227
const StatusCode::Category& StatusCode::getCategory ( ) const
inline

Retrieve category (does not "check" the StatusCode)

Definition at line 166 of file StatusCode.h.

166 { return *m_cat; }
const Category * m_cat
The status code category.
Definition: StatusCode.h:224
code_t StatusCode::getCode ( ) const
inline

Retrieve value ("checks" the StatusCode)

Definition at line 137 of file StatusCode.h.

137  {
138  m_checked = true;
139  return m_code;
140  }
code_t m_code
The status code value.
Definition: StatusCode.h:225
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226
const StatusCode& StatusCode::ignore ( ) const
inline

Ignore/check StatusCode.

Definition at line 153 of file StatusCode.h.

153  {
154  setChecked( true );
155  return *this;
156  }
const StatusCode & setChecked(bool checked=true) const
Check/uncheck StatusCode.
Definition: StatusCode.h:143
StatusCode& StatusCode::ignore ( )
inline

Definition at line 157 of file StatusCode.h.

157  {
158  setChecked( true );
159  return *this;
160  }
const StatusCode & setChecked(bool checked=true) const
Check/uncheck StatusCode.
Definition: StatusCode.h:143
bool StatusCode::isFailure ( ) const
inline

Definition at line 130 of file StatusCode.h.

130 { return !isSuccess(); }
bool isSuccess() const
Definition: StatusCode.h:267
bool StatusCode::isRecoverable ( ) const
inline

Definition at line 272 of file StatusCode.h.

272  {
273  m_checked = true;
274  return m_cat->isRecoverable( m_code );
275 }
code_t m_code
The status code value.
Definition: StatusCode.h:225
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226
virtual bool isRecoverable(code_t code) const
Is code considered recoverable ?
Definition: StatusCode.h:78
const Category * m_cat
The status code category.
Definition: StatusCode.h:224
bool StatusCode::isSuccess ( ) const
inline

Definition at line 267 of file StatusCode.h.

267  {
268  m_checked = true;
269  return ( m_code == static_cast<code_t>( ErrorCode::SUCCESS ) || m_cat->isSuccess( m_code ) );
270 }
code_t m_code
The status code value.
Definition: StatusCode.h:225
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226
virtual bool isSuccess(code_t code) const
Is code considered success ?
Definition: StatusCode.h:75
const Category * m_cat
The status code category.
Definition: StatusCode.h:224
std::string StatusCode::message ( ) const
inline

Description (or name) of StatusCode value.

Definition at line 169 of file StatusCode.h.

169 { return getCategory().message( m_code ); }
code_t m_code
The status code value.
Definition: StatusCode.h:225
const StatusCode::Category & getCategory() const
Retrieve category (does not "check" the StatusCode)
Definition: StatusCode.h:166
virtual std::string message(code_t code) const
Description for code within this category.
Definition: StatusCode.h:71
StatusCode::operator bool ( ) const
inlineexplicit

Shorthand for isSuccess()

Definition at line 134 of file StatusCode.h.

134 { return isSuccess(); }
bool isSuccess() const
Definition: StatusCode.h:267
StatusCode & StatusCode::operator&= ( const StatusCode rhs)
inline

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

Definition at line 293 of file StatusCode.h.

293  {
294  // Ternary AND lookup matrix
295  static constexpr StatusCode::code_t AND[3][3] = {{0, 0, 0}, {0, 1, 2}, {0, 2, 2}};
296 
298  StatusCode::code_t r = static_cast<StatusCode::code_t>( rhs.default_value() );
299  m_code = AND[l][r];
300  rhs.m_checked = true;
301  return *this;
302 }
code_t m_code
The status code value.
Definition: StatusCode.h:225
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226
dictionary l
Definition: gaudirun.py:517
ErrorCode default_value() const
Project onto the default StatusCode values.
Definition: StatusCode.h:277
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:52
StatusCode& StatusCode::operator= ( const StatusCode rhs)
inlinenoexcept

Definition at line 122 of file StatusCode.h.

122  {
123  m_cat = rhs.m_cat;
124  m_code = rhs.m_code;
125  m_checked = std::exchange( rhs.m_checked, true );
126  return *this;
127  }
code_t m_code
The status code value.
Definition: StatusCode.h:225
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226
const Category * m_cat
The status code category.
Definition: StatusCode.h:224
StatusCode & StatusCode::operator|= ( const StatusCode rhs)
inline

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

Definition at line 304 of file StatusCode.h.

304  {
305  // Ternary OR lookup matrix
306  static constexpr StatusCode::code_t OR[3][3] = {{0, 1, 2}, {1, 1, 1}, {2, 1, 2}};
307 
309  StatusCode::code_t r = static_cast<StatusCode::code_t>( rhs.default_value() );
310  m_code = OR[l][r];
311  rhs.m_checked = true;
312  return *this;
313 }
code_t m_code
The status code value.
Definition: StatusCode.h:225
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226
dictionary l
Definition: gaudirun.py:517
ErrorCode default_value() const
Project onto the default StatusCode values.
Definition: StatusCode.h:277
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:52
const StatusCode& StatusCode::setChecked ( bool  checked = true) const
inline

Check/uncheck StatusCode.

Definition at line 143 of file StatusCode.h.

143  {
144  m_checked = checked;
145  return *this;
146  }
bool checked() const
Has the StatusCode been checked?
Definition: StatusCode.h:163
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226
StatusCode& StatusCode::setChecked ( bool  checked = true)
inline

Definition at line 147 of file StatusCode.h.

147  {
148  m_checked = checked;
149  return *this;
150  }
bool checked() const
Has the StatusCode been checked?
Definition: StatusCode.h:163
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226

Friends And Related Function Documentation

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

Definition at line 180 of file StatusCode.h.

180 { return !( lhs == rhs ); }
bool operator< ( const StatusCode lhs,
const StatusCode rhs 
)
friend

Comparison (values are grouped by category first)

Definition at line 183 of file StatusCode.h.

183  {
184  lhs.m_checked = true;
185  rhs.m_checked = true;
186  return ( lhs.m_cat < rhs.m_cat || ( lhs.m_cat == rhs.m_cat && lhs.m_code < rhs.m_code ) );
187  }
code_t m_code
The status code value.
Definition: StatusCode.h:225
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226
const Category * m_cat
The status code category.
Definition: StatusCode.h:224
std::ostream& operator<< ( std::ostream s,
const StatusCode sc 
)
friend

Definition at line 171 of file StatusCode.h.

171  {
172  s << sc.message();
173  return s;
174  }
string s
Definition: gaudirun.py:312
std::string message() const
Description (or name) of StatusCode value.
Definition: StatusCode.h:169
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 284 of file StatusCode.h.

284  {
285  lhs.m_checked = true;
286  rhs.m_checked = true;
287  return ( lhs.m_code == rhs.m_code ) &&
288  ( lhs.m_code == static_cast<StatusCode::code_t>( StatusCode::ErrorCode::SUCCESS ) ||
289  lhs.m_code == static_cast<StatusCode::code_t>( StatusCode::ErrorCode::FAILURE ) ||
290  ( lhs.m_cat == rhs.m_cat ) );
291 }
code_t m_code
The status code value.
Definition: StatusCode.h:225
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:226
const Category * m_cat
The status code category.
Definition: StatusCode.h:224
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:52

Member Data Documentation

constexpr const StatusCode::ErrorCode StatusCode::FAILURE = ErrorCode::FAILURE
static

Definition at line 86 of file StatusCode.h.

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

The status code category.

Definition at line 224 of file StatusCode.h.

bool StatusCode::m_checked {false}
mutableprivate

If the StatusCode has been checked.

Definition at line 226 of file StatusCode.h.

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

The status code value.

Definition at line 225 of file StatusCode.h.

constexpr const StatusCode::ErrorCode StatusCode::RECOVERABLE = ErrorCode::RECOVERABLE
static

Definition at line 87 of file StatusCode.h.

bool StatusCode::s_checking
staticprivate

Global flag to control if StatusCode need to be checked.

Definition at line 227 of file StatusCode.h.

constexpr const StatusCode::ErrorCode StatusCode::SUCCESS = ErrorCode::SUCCESS
static

Definition at line 85 of file StatusCode.h.


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