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 
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,
31  RECOVERABLE = 2
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 ?
47  StatusCode::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),
58  m_severity(rhs.m_severity)
59  { rhs.m_checked = true; }
60 
62  StatusCode( StatusCode&& rhs ) noexcept:
63  d_code(rhs.d_code), m_checked(rhs.m_checked),
64  m_severity( std::move(rhs.m_severity) )
65  { rhs.m_checked = true; }
66 
69  { if (UNLIKELY(s_checking)) check(); }
70 
74  bool isSuccess() const {
75  m_checked = true;
76  return (d_code == SUCCESS);
77  }
78 
84  bool isFailure() const { return !isSuccess(); }
85  bool isRecoverable() const {
86  m_checked = true;
87  return (d_code == RECOVERABLE);
88  }
89 
91  unsigned long getCode() const{
92  m_checked = true;
93  return d_code;
94  }
95 
97  void setCode(unsigned long value) {
98  m_checked = false;
99  d_code = value;
100  }
101 
103  void setChecked() const{
104  m_checked = true;
105  }
106  void ignore() const { setChecked(); }
107 
109  operator unsigned long() const { return getCode(); }
110 
112  GAUDI_API const IssueSeverity& severity() const;
113 
115  StatusCode& operator=(unsigned long value) {
116  setCode(value);
117  return *this;
118  }
120  if (this == &rhs) return *this; // Protection against self-assignment
121  d_code = rhs.d_code;
122  m_checked = rhs.m_checked;
123  rhs.m_checked = true;
124  m_severity = rhs.m_severity;
125  return *this;
126  }
127 
129  friend bool operator< ( const StatusCode& a, const StatusCode& b ) {
130  return a.d_code < b.d_code;
131  }
132 
134  friend bool operator> ( const StatusCode& a, const StatusCode& b ) {
135  return a.d_code > b.d_code;
136  }
137 
138 #ifndef _WIN32
139  operator IgnoreError() const {
140  m_checked = true;
141  return IgnoreError();
142  }
143 #endif
144 
145  static GAUDI_API void enableChecking();
146  static GAUDI_API void disableChecking();
147  static GAUDI_API bool checkingEnabled();
148 
164  bool m_enabled;
165  public:
166  ScopedDisableChecking(): m_enabled(StatusCode::checkingEnabled()) {
167  if (m_enabled) StatusCode::disableChecking();
168  }
170  if (m_enabled) StatusCode::enableChecking();
171  }
172  };
173 
174 protected:
176  unsigned long d_code = SUCCESS;
177  mutable bool m_checked = false;
179 
180  static bool s_checking;
181 
182 private:
183  void check();
184 };
185 
187 {
188  if ( sc.isSuccess() ) { return s << "SUCCESS" ; }
189  if ( sc.isRecoverable() ) { return s << "RECOVERABLE" ; }
190  s << "FAILURE" ;
191  return ( StatusCode::FAILURE != sc.getCode() ) ? s << "(" << sc.getCode() << ")"
192  : s ;
193 }
194 
195 #endif // GAUDIKERNEL_STATUSCODES_H
196 
197 
198 
StatusCode(unsigned long code, IssueSeverity &&sev)
Definition: StatusCode.h:37
StatusCode(unsigned long code, bool checked=false)
Definition: StatusCode.h:53
void setCode(unsigned long value)
Set the status code by value.
Definition: StatusCode.h:97
unsigned long getCode() const
Get the status code by value.
Definition: StatusCode.h:91
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
bool m_checked
If the Status code has been checked.
Definition: StatusCode.h:177
#define UNLIKELY(x)
Definition: Kernel.h:126
STL namespace.
~StatusCode()
Destructor.
Definition: StatusCode.h:68
static GAUDI_API void enableChecking()
Definition: StatusCode.cpp:19
std::ostream & operator<<(std::ostream &s, const StatusCode &sc)
Definition: StatusCode.h:186
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:84
bool operator>(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:208
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:163
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:180
unsigned long d_code
The status code.
Definition: StatusCode.h:176
bool isRecoverable() const
Definition: StatusCode.h:85
StatusCode(const StatusCode &rhs)
Definition: StatusCode.h:56
T move(T...args)
StatusCode & operator=(const StatusCode &rhs)
Definition: StatusCode.h:119
bool operator<(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:185
string s
Definition: gaudirun.py:245
static GAUDI_API void disableChecking()
Definition: StatusCode.cpp:23
void ignore() const
Definition: StatusCode.h:106
StatusCode(StatusCode &&rhs) noexcept
Move constructor.
Definition: StatusCode.h:62
#define GAUDI_API
Definition: Kernel.h:107
STL class.
std::shared_ptr< const IssueSeverity > m_severity
Pointer to a IssueSeverity.
Definition: StatusCode.h:178
void setChecked() const
Ignore the checking code;.
Definition: StatusCode.h:103
StatusCode & operator=(unsigned long value)
Assignment operator.
Definition: StatusCode.h:115