Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v29r3 (fa547fc2)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
StatusCode.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_STATUSCODE_H
2 #define GAUDIKERNEL_STATUSCODE_H
3 
4 #include <ostream>
5 #include <utility>
6 
7 #include "GaudiKernel/Kernel.h"
8 
19 class IMessageSvc;
20 class IStatusCodeSvc;
21 
23 {
24 };
25 
26 class StatusCode final
27 {
28 public:
29  enum { FAILURE = 0, SUCCESS = 1, RECOVERABLE = 2 };
30 
32  StatusCode() = default;
33 
34  StatusCode( unsigned long code, bool checked = false ) : d_code( code ), m_checked( checked ) {}
35 
36  StatusCode( const StatusCode& rhs ) : d_code( rhs.d_code ), m_checked( rhs.m_checked ) { rhs.m_checked = true; }
37 
39  StatusCode( StatusCode&& rhs ) noexcept : d_code( rhs.d_code ), m_checked( rhs.m_checked ) { rhs.m_checked = true; }
40 
43  {
44  if ( UNLIKELY( s_checking ) ) check();
45  }
46 
50  bool isSuccess() const
51  {
52  m_checked = true;
53  return ( d_code == SUCCESS );
54  }
55 
61  bool isFailure() const { return !isSuccess(); }
62  bool isRecoverable() const
63  {
64  m_checked = true;
65  return ( d_code == RECOVERABLE );
66  }
67 
69  unsigned long getCode() const
70  {
71  m_checked = true;
72  return d_code;
73  }
74 
76  void setCode( unsigned long value )
77  {
78  m_checked = false;
79  d_code = value;
80  }
81 
83  void setChecked() const { m_checked = true; }
84  void ignore() const { setChecked(); }
85 
87  bool checked() const { return m_checked; }
88 
90  operator unsigned long() const { return getCode(); }
91 
93  StatusCode& operator=( unsigned long value )
94  {
95  setCode( value );
96  return *this;
97  }
99  {
100  d_code = rhs.d_code;
101  m_checked = std::exchange( rhs.m_checked, true );
102  return *this;
103  }
104 
106  friend bool operator<( const StatusCode& a, const StatusCode& b ) { return a.d_code < b.d_code; }
107 
109  friend bool operator>( const StatusCode& a, const StatusCode& b ) { return a.d_code > b.d_code; }
110 
111 #ifndef _WIN32
112  operator IgnoreError() const
113  {
114  m_checked = true;
115  return IgnoreError();
116  }
117 #endif
118 
119  static GAUDI_API void enableChecking();
120  static GAUDI_API void disableChecking();
121  static GAUDI_API bool checkingEnabled();
122 
138  {
139  bool m_enabled;
140 
141  public:
142  ScopedDisableChecking() : m_enabled( StatusCode::checkingEnabled() )
143  {
144  if ( m_enabled ) StatusCode::disableChecking();
145  }
147  {
148  if ( m_enabled ) StatusCode::enableChecking();
149  }
150  };
151 
152 protected:
154  unsigned long d_code = SUCCESS;
155  mutable bool m_checked = false;
156 
157  static bool s_checking;
158 
159 private:
160  void check();
161 };
162 
164 {
165  if ( sc.isSuccess() ) {
166  return s << "SUCCESS";
167  }
168  if ( sc.isRecoverable() ) {
169  return s << "RECOVERABLE";
170  }
171  s << "FAILURE";
172  return ( StatusCode::FAILURE != sc.getCode() ) ? s << "(" << sc.getCode() << ")" : s;
173 }
174 
175 #endif // GAUDIKERNEL_STATUSCODES_H
StatusCode(unsigned long code, bool checked=false)
Definition: StatusCode.h:34
#define UNLIKELY(x)
Definition: Kernel.h:128
void setCode(unsigned long value)
Set the status code by value.
Definition: StatusCode.h:76
unsigned long getCode() const
Get the status code by value.
Definition: StatusCode.h:69
bool checked() const
Has the StatusCode been checked?
Definition: StatusCode.h:87
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:50
bool m_checked
If the Status code has been checked.
Definition: StatusCode.h:155
~StatusCode()
Destructor.
Definition: StatusCode.h:42
static GAUDI_API void enableChecking()
Definition: StatusCode.cpp:18
std::ostream & operator<<(std::ostream &s, const StatusCode &sc)
Definition: StatusCode.h:163
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:61
friend bool operator<(const StatusCode &a, const StatusCode &b)
Comparison operator.
Definition: StatusCode.h:106
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Simple RAII class to ignore unchecked StatusCode instances in a scope.
Definition: StatusCode.h:137
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:38
static bool s_checking
Global flag to control if StatusCode need to be checked.
Definition: StatusCode.h:157
unsigned long d_code
The status code.
Definition: StatusCode.h:154
bool isRecoverable() const
Definition: StatusCode.h:62
StatusCode(const StatusCode &rhs)
Definition: StatusCode.h:36
StatusCode & operator=(const StatusCode &rhs)
Definition: StatusCode.h:98
string s
Definition: gaudirun.py:253
static GAUDI_API void disableChecking()
Definition: StatusCode.cpp:20
void ignore() const
Definition: StatusCode.h:84
friend bool operator>(const StatusCode &a, const StatusCode &b)
Comparison operator.
Definition: StatusCode.h:109
StatusCode(StatusCode &&rhs) noexcept
Move constructor.
Definition: StatusCode.h:39
#define GAUDI_API
Definition: Kernel.h:110
STL class.
void setChecked() const
Ignore the checking code;.
Definition: StatusCode.h:83
StatusCode & operator=(unsigned long value)
Assignment operator.
Definition: StatusCode.h:93