StatusCode.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_STATUSCODE_H
2 #define GAUDIKERNEL_STATUSCODE_H
3 
4 #include <ostream>
5 
6 #include "GaudiKernel/Kernel.h"
8 
9 #include <memory>
10 
21 class IMessageSvc;
22 class IStatusCodeSvc;
23 
24 class IgnoreError {};
25 
26 class StatusCode final {
27 public:
28  enum {
29  FAILURE = 0,
30  SUCCESS = 1,
32  };
33 
35  StatusCode() = default;
36 
37  StatusCode( unsigned long code, IssueSeverity&& sev ):
38  d_code(code) {
39  try { // ensure that we do not throw even if we cannot move the severity
40  m_severity = std::make_shared<const IssueSeverity>(std::move(sev));
41  }
42  catch (...) {}
43  }
44 
46  StatusCode( is.getLevel() == IssueSeverity::RECOVERABLE ?
48  ( is.getLevel() < IssueSeverity::ERROR ?
51  , std::move(is) ) { }
52 
53  StatusCode( unsigned long code, bool checked = false ):
54  d_code(code),m_checked(checked) {}
55 
56  StatusCode( const StatusCode& rhs ):
57  d_code(rhs.d_code), m_checked(rhs.m_checked),
59  { rhs.m_checked = true; }
60 
61 #ifndef __GCCXML__
62  StatusCode( StatusCode&& rhs ) noexcept:
64  d_code(rhs.d_code), m_checked(rhs.m_checked),
65  m_severity( std::move(rhs.m_severity) )
66  { rhs.m_checked = true; }
67 #endif
68 
71  { if (UNLIKELY(s_checking)) check(); }
72 
76  bool isSuccess() const {
77  m_checked = true;
78  return (d_code == SUCCESS);
79  }
80 
86  bool isFailure() const { return !isSuccess(); }
87  bool isRecoverable() const {
88  m_checked = true;
89  return (d_code == RECOVERABLE);
90  }
91 
93  unsigned long getCode() const{
94  m_checked = true;
95  return d_code;
96  }
97 
99  void setCode(unsigned long value) {
100  m_checked = false;
101  d_code = value;
102  }
103 
105  void setChecked() const{
106  m_checked = true;
107  }
108  void ignore() const { setChecked(); }
109 
111  operator unsigned long() const { return getCode(); }
112 
114  GAUDI_API const IssueSeverity& severity() const;
115 
117  StatusCode& operator=(unsigned long value) {
118  setCode(value);
119  return *this;
120  }
122  if (this == &rhs) return *this; // Protection against self-assignment
123  d_code = rhs.d_code;
124  m_checked = rhs.m_checked;
125  rhs.m_checked = true;
126  m_severity = rhs.m_severity;
127  return *this;
128  }
129 
131  friend bool operator< ( const StatusCode& a, const StatusCode& b ) {
132  return a.d_code < b.d_code;
133  }
134 
136  friend bool operator> ( const StatusCode& a, const StatusCode& b ) {
137  return a.d_code > b.d_code;
138  }
139 
140 #ifndef _WIN32
141  operator IgnoreError() const {
142  m_checked = true;
143  return IgnoreError();
144  }
145 #endif
146 
147  static GAUDI_API void enableChecking();
148  static GAUDI_API void disableChecking();
149  static GAUDI_API bool checkingEnabled();
150 
166  bool m_enabled;
167  public:
169  if (m_enabled) StatusCode::disableChecking();
170  }
172  if (m_enabled) StatusCode::enableChecking();
173  }
174  };
175 
176 protected:
178  unsigned long d_code = SUCCESS;
179  mutable bool m_checked = false;
181 
182  static bool s_checking;
183 
184 private:
185  void check();
186 };
187 
189 {
190  if ( sc.isSuccess() ) { return s << "SUCCESS" ; }
191  if ( sc.isRecoverable() ) { return s << "RECOVERABLE" ; }
192  s << "FAILURE" ;
193  return ( StatusCode::FAILURE != sc.getCode() ) ? s << "(" << sc.getCode() << ")"
194  : s ;
195 }
196 
197 #endif // GAUDIKERNEL_STATUSCODES_H
198 
199 
200 
StatusCode(unsigned long code, IssueSeverity &&sev)
Definition: StatusCode.h:37
StatusCode(unsigned long code, bool checked=false)
Definition: StatusCode.h:53
#define UNLIKELY(x)
Definition: Kernel.h:126
GAUDI_API const IssueSeverity & severity() const
Severity.
Definition: StatusCode.cpp:31
void setCode(unsigned long value)
Set the status code by value.
Definition: StatusCode.h:99
void check()
Definition: StatusCode.cpp:36
unsigned long getCode() const
Get the status code by value.
Definition: StatusCode.h:93
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
bool m_checked
If the Status code has been checked.
Definition: StatusCode.h:179
STL namespace.
~StatusCode()
Destructor.
Definition: StatusCode.h:70
static GAUDI_API void enableChecking()
Definition: StatusCode.cpp:19
static GAUDI_API bool checkingEnabled()
Definition: StatusCode.cpp:27
std::ostream & operator<<(std::ostream &s, const StatusCode &sc)
Definition: StatusCode.h:188
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
friend bool operator<(const StatusCode &a, const StatusCode &b)
Comparison operator.
Definition: StatusCode.h:131
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:165
StatusCode(IssueSeverity &&is)
Definition: StatusCode.h:45
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:57
static bool s_checking
Global flag to control if StatusCode need to be checked.
Definition: StatusCode.h:182
unsigned long d_code
The status code.
Definition: StatusCode.h:178
bool isRecoverable() const
Definition: StatusCode.h:87
StatusCode(const StatusCode &rhs)
Definition: StatusCode.h:56
T move(T...args)
StatusCode & operator=(const StatusCode &rhs)
Definition: StatusCode.h:121
string s
Definition: gaudirun.py:245
static GAUDI_API void disableChecking()
Definition: StatusCode.cpp:23
StatusCode()=default
Constructor.
void ignore() const
Definition: StatusCode.h:108
friend bool operator>(const StatusCode &a, const StatusCode &b)
Comparison operator.
Definition: StatusCode.h:136
#define GAUDI_API
Definition: Kernel.h:107
STL class.
std::shared_ptr< const IssueSeverity > m_severity
Pointer to a IssueSeverity.
Definition: StatusCode.h:180
void setChecked() const
Ignore the checking code;.
Definition: StatusCode.h:105
StatusCode & operator=(unsigned long value)
Assignment operator.
Definition: StatusCode.h:117