Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v30r3 (a5ef0a68)
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 51 of file StatusCode.h.

Member Typedef Documentation

typedef unsigned long StatusCode::code_t

type of StatusCode value

Definition at line 54 of file StatusCode.h.

Member Enumeration Documentation

Enumerator
FAILURE 
SUCCESS 
RECOVERABLE 

Definition at line 56 of file StatusCode.h.

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

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

97  {
98  *this = StatusCode( static_cast<StatusCode::code_t>( sc ), is_StatusCode_enum<T>::instance );
100  }
bool checked() const
Has the StatusCode been checked?
Definition: StatusCode.h:177
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:245
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 103 of file StatusCode.h.

105  : m_cat( &cat ), m_code( code ), m_checked( checked )
106  {
107  }
code_t m_code
The status code value.
Definition: StatusCode.h:244
bool checked() const
Has the StatusCode been checked?
Definition: StatusCode.h:177
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:245
const Category * m_cat
The status code category.
Definition: StatusCode.h:243
StatusCode::StatusCode ( code_t  code,
bool  checked 
)
inlineexplicitnoexcept

Constructor from code_t and category (explicit conversion only)

Definition at line 110 of file StatusCode.h.

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

Copy 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  {
115  rhs.m_checked = true;
116  }
code_t m_code
The status code value.
Definition: StatusCode.h:244
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:245
const Category * m_cat
The status code category.
Definition: StatusCode.h:243
StatusCode::StatusCode ( StatusCode &&  rhs)
inlinenoexcept

Move constructor.

Definition at line 119 of file StatusCode.h.

119  : m_cat( rhs.m_cat ), m_code( rhs.m_code ), m_checked( rhs.m_checked )
120  {
121  rhs.m_checked = true;
122  }
code_t m_code
The status code value.
Definition: StatusCode.h:244
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:245
const Category * m_cat
The status code category.
Definition: StatusCode.h:243
StatusCode::~StatusCode ( )
inline

Destructor.

Definition at line 125 of file StatusCode.h.

126  {
127  if ( UNLIKELY( s_checking ) ) check();
128  }
#define UNLIKELY(x)
Definition: Kernel.h:122
void check()
Do StatusCode check.
Definition: StatusCode.cpp:46
static bool s_checking
Global flag to control if StatusCode need to be checked.
Definition: StatusCode.h:246

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 46 of file StatusCode.cpp.

47 {
48 
50 
51  auto msg = Gaudi::svcLocator()->as<IMessageSvc>();
52  auto scs = Gaudi::svcLocator()->service<IStatusCodeSvc>( "StatusCodeSvc" );
53 
54  const size_t depth = 21;
55  void* addresses[depth];
56 
57  std::string lib, fnc;
58  void* addr = nullptr;
60  if ( System::backTrace( addresses, depth ) ) {
61 
62  for ( size_t idx : {2, 3} ) {
63  if ( System::getStackLevel( addresses[idx], addr, fnc, lib ) && fnc != "StatusCode::~StatusCode()" ) {
64 
65  if ( scs ) {
66  scs->regFnc( fnc, lib );
67  } else {
68  MsgStream log( msg, "StatusCode" );
69  log << MSG::WARNING << "Unchecked in " << fnc << " (" << lib << ")" << endmsg;
70  }
71  break;
72  }
73  }
74  }
75  }
76 }
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:245
static bool s_proc
"previous" element in the linked list
SmartIF< IFace > as()
Definition: ISvcLocator.h:109
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:79
GAUDI_API ISvcLocator * svcLocator()
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:38
T uncaught_exception(T...args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
bool StatusCode::checked ( ) const
inline

Has the StatusCode been checked?

Definition at line 177 of file StatusCode.h.

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

Definition at line 44 of file StatusCode.cpp.

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

Default Gaudi StatusCode category.

Definition at line 282 of file StatusCode.h.

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

Project onto the default StatusCode values.

Definition at line 299 of file StatusCode.h.

300 {
301  bool save_checked = m_checked; // Preserve checked status
303  m_checked = save_checked;
304  return r;
305 }
bool isSuccess() const
Definition: StatusCode.h:287
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:245
bool isRecoverable() const
Definition: StatusCode.h:293
void StatusCode::disableChecking ( )
static

Definition at line 42 of file StatusCode.cpp.

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

Definition at line 40 of file StatusCode.cpp.

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

Retrieve category (does not "check" the StatusCode)

Definition at line 180 of file StatusCode.h.

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

Retrieve value ("checks" the StatusCode)

Definition at line 146 of file StatusCode.h.

147  {
148  m_checked = true;
149  return m_code;
150  }
code_t m_code
The status code value.
Definition: StatusCode.h:244
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:245
const StatusCode& StatusCode::ignore ( ) const
inline

Ignore/check StatusCode.

Definition at line 165 of file StatusCode.h.

166  {
167  setChecked( true );
168  return *this;
169  }
const StatusCode & setChecked(bool checked=true) const
Check/uncheck StatusCode.
Definition: StatusCode.h:153
StatusCode& StatusCode::ignore ( )
inline

Definition at line 170 of file StatusCode.h.

171  {
172  setChecked( true );
173  return *this;
174  }
const StatusCode & setChecked(bool checked=true) const
Check/uncheck StatusCode.
Definition: StatusCode.h:153
bool StatusCode::isFailure ( ) const
inline

Definition at line 139 of file StatusCode.h.

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

Definition at line 293 of file StatusCode.h.

294 {
295  m_checked = true;
296  return m_cat->isRecoverable( m_code );
297 }
code_t m_code
The status code value.
Definition: StatusCode.h:244
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:245
virtual bool isRecoverable(code_t code) const
Is code considered recoverable ?
Definition: StatusCode.h:80
const Category * m_cat
The status code category.
Definition: StatusCode.h:243
bool StatusCode::isSuccess ( ) const
inline

Definition at line 287 of file StatusCode.h.

288 {
289  m_checked = true;
290  return ( m_code == static_cast<code_t>( ErrorCode::SUCCESS ) || m_cat->isSuccess( m_code ) );
291 }
code_t m_code
The status code value.
Definition: StatusCode.h:244
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:245
virtual bool isSuccess(code_t code) const
Is code considered success ?
Definition: StatusCode.h:77
const Category * m_cat
The status code category.
Definition: StatusCode.h:243
std::string StatusCode::message ( ) const
inline

Description (or name) of StatusCode value.

Definition at line 183 of file StatusCode.h.

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

Shorthand for isSuccess()

Definition at line 143 of file StatusCode.h.

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

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

Definition at line 317 of file StatusCode.h.

318 {
319  // Ternary AND lookup matrix
320  static constexpr StatusCode::code_t AND[3][3] = {{0, 0, 0}, {0, 1, 2}, {0, 2, 2}};
321 
323  StatusCode::code_t r = static_cast<StatusCode::code_t>( rhs.default_value() );
324  m_code = AND[l][r];
325  rhs.m_checked = true;
326  return *this;
327 }
code_t m_code
The status code value.
Definition: StatusCode.h:244
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:245
dictionary l
Definition: gaudirun.py:440
ErrorCode default_value() const
Project onto the default StatusCode values.
Definition: StatusCode.h:299
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:54
StatusCode& StatusCode::operator= ( const StatusCode rhs)
inlinenoexcept

Definition at line 130 of file StatusCode.h.

131  {
132  m_cat = rhs.m_cat;
133  m_code = rhs.m_code;
134  m_checked = std::exchange( rhs.m_checked, true );
135  return *this;
136  }
code_t m_code
The status code value.
Definition: StatusCode.h:244
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:245
const Category * m_cat
The status code category.
Definition: StatusCode.h:243
StatusCode & StatusCode::operator|= ( const StatusCode rhs)
inline

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

Definition at line 329 of file StatusCode.h.

330 {
331  // Ternary OR lookup matrix
332  static constexpr StatusCode::code_t OR[3][3] = {{0, 1, 2}, {1, 1, 1}, {2, 1, 2}};
333 
335  StatusCode::code_t r = static_cast<StatusCode::code_t>( rhs.default_value() );
336  m_code = OR[l][r];
337  rhs.m_checked = true;
338  return *this;
339 }
code_t m_code
The status code value.
Definition: StatusCode.h:244
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:245
dictionary l
Definition: gaudirun.py:440
ErrorCode default_value() const
Project onto the default StatusCode values.
Definition: StatusCode.h:299
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:54
const StatusCode& StatusCode::setChecked ( bool  checked = true) const
inline

Check/uncheck StatusCode.

Definition at line 153 of file StatusCode.h.

154  {
155  m_checked = checked;
156  return *this;
157  }
bool checked() const
Has the StatusCode been checked?
Definition: StatusCode.h:177
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:245
StatusCode& StatusCode::setChecked ( bool  checked = true)
inline

Definition at line 158 of file StatusCode.h.

159  {
160  m_checked = checked;
161  return *this;
162  }
bool checked() const
Has the StatusCode been checked?
Definition: StatusCode.h:177
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:245

Friends And Related Function Documentation

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

Definition at line 195 of file StatusCode.h.

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

Comparison (values are grouped by category first)

Definition at line 198 of file StatusCode.h.

199  {
200  lhs.m_checked = true;
201  rhs.m_checked = true;
202  return ( lhs.m_cat < rhs.m_cat || ( lhs.m_cat == rhs.m_cat && lhs.m_code < rhs.m_code ) );
203  }
code_t m_code
The status code value.
Definition: StatusCode.h:244
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:245
const Category * m_cat
The status code category.
Definition: StatusCode.h:243
std::ostream& operator<< ( std::ostream s,
const StatusCode sc 
)
friend

Definition at line 185 of file StatusCode.h.

186  {
187  s << sc.message();
188  return s;
189  }
string s
Definition: gaudirun.py:253
std::string message() const
Description (or name) of StatusCode value.
Definition: StatusCode.h:183
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 307 of file StatusCode.h.

308 {
309  lhs.m_checked = true;
310  rhs.m_checked = true;
311  return ( lhs.m_code == rhs.m_code ) &&
312  ( lhs.m_code == static_cast<StatusCode::code_t>( StatusCode::ErrorCode::SUCCESS ) ||
313  lhs.m_code == static_cast<StatusCode::code_t>( StatusCode::ErrorCode::FAILURE ) ||
314  ( lhs.m_cat == rhs.m_cat ) );
315 }
code_t m_code
The status code value.
Definition: StatusCode.h:244
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:245
const Category * m_cat
The status code category.
Definition: StatusCode.h:243
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:54

Member Data Documentation

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

Definition at line 88 of file StatusCode.h.

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

The status code category.

Definition at line 243 of file StatusCode.h.

bool StatusCode::m_checked {false}
mutableprivate

If the StatusCode has been checked.

Definition at line 245 of file StatusCode.h.

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

The status code value.

Definition at line 244 of file StatusCode.h.

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

Definition at line 89 of file StatusCode.h.

bool StatusCode::s_checking
staticprivate

Global flag to control if StatusCode need to be checked.

Definition at line 246 of file StatusCode.h.

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

Definition at line 87 of file StatusCode.h.


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