The Gaudi Framework  v29r0 (ff2e7097)
StatusCode.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_STATUSCODE_H
2 #define GAUDIKERNEL_STATUSCODE_H
3 
4 #include <ostream>
5 
7 #include "GaudiKernel/Kernel.h"
8 
9 #include <memory>
10 
21 class IMessageSvc;
22 class IStatusCodeSvc;
23 
25 {
26 };
27 
28 class StatusCode final
29 {
30 public:
31  enum { FAILURE = 0, SUCCESS = 1, RECOVERABLE = 2 };
32 
34  StatusCode() = default;
35 
36  StatusCode( unsigned long code, IssueSeverity&& sev ) : d_code( code )
37  {
38  try { // ensure that we do not throw even if we cannot move the severity
39  m_severity = std::make_shared<const IssueSeverity>( std::move( sev ) );
40  } catch ( ... ) {
41  }
42  }
43 
45  : StatusCode( is.getLevel() == IssueSeverity::RECOVERABLE
46  ? StatusCode::RECOVERABLE
47  : ( is.getLevel() < IssueSeverity::ERROR ? StatusCode::SUCCESS : StatusCode::FAILURE ),
48  std::move( is ) )
49  {
50  }
51 
52  StatusCode( unsigned long code, bool checked = false ) : d_code( code ), m_checked( checked ) {}
53 
54  StatusCode( const StatusCode& rhs ) : d_code( rhs.d_code ), m_checked( rhs.m_checked ), m_severity( rhs.m_severity )
55  {
56  rhs.m_checked = true;
57  }
58 
60  StatusCode( StatusCode&& rhs ) noexcept
61  : d_code( rhs.d_code ), m_checked( rhs.m_checked ), m_severity( std::move( rhs.m_severity ) )
62  {
63  rhs.m_checked = true;
64  }
65 
68  {
69  if ( UNLIKELY( s_checking ) ) check();
70  }
71 
75  bool isSuccess() const
76  {
77  m_checked = true;
78  return ( d_code == SUCCESS );
79  }
80 
86  bool isFailure() const { return !isSuccess(); }
87  bool isRecoverable() const
88  {
89  m_checked = true;
90  return ( d_code == RECOVERABLE );
91  }
92 
94  unsigned long getCode() const
95  {
96  m_checked = true;
97  return d_code;
98  }
99 
101  void setCode( unsigned long value )
102  {
103  m_checked = false;
104  d_code = value;
105  }
106 
108  void setChecked() const { m_checked = true; }
109  void ignore() const { setChecked(); }
110 
112  operator unsigned long() const { return getCode(); }
113 
115  GAUDI_API const IssueSeverity& severity() const;
116 
118  StatusCode& operator=( unsigned long value )
119  {
120  setCode( value );
121  return *this;
122  }
124  {
125  if ( this == &rhs ) return *this; // Protection against self-assignment
126  d_code = rhs.d_code;
127  m_checked = rhs.m_checked;
128  rhs.m_checked = true;
129  m_severity = rhs.m_severity;
130  return *this;
131  }
132 
134  friend bool operator<( const StatusCode& a, const StatusCode& b ) { return a.d_code < b.d_code; }
135 
137  friend bool operator>( const StatusCode& a, const StatusCode& b ) { return a.d_code > b.d_code; }
138 
139 #ifndef _WIN32
140  operator IgnoreError() const
141  {
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  {
167  bool m_enabled;
168 
169  public:
170  ScopedDisableChecking() : m_enabled( StatusCode::checkingEnabled() )
171  {
172  if ( m_enabled ) StatusCode::disableChecking();
173  }
175  {
176  if ( m_enabled ) StatusCode::enableChecking();
177  }
178  };
179 
180 protected:
182  unsigned long d_code = SUCCESS;
183  mutable bool m_checked = false;
185 
186  static bool s_checking;
187 
188 private:
189  void check();
190 };
191 
193 {
194  if ( sc.isSuccess() ) {
195  return s << "SUCCESS";
196  }
197  if ( sc.isRecoverable() ) {
198  return s << "RECOVERABLE";
199  }
200  s << "FAILURE";
201  return ( StatusCode::FAILURE != sc.getCode() ) ? s << "(" << sc.getCode() << ")" : s;
202 }
203 
204 #endif // GAUDIKERNEL_STATUSCODES_H
StatusCode(unsigned long code, IssueSeverity &&sev)
Definition: StatusCode.h:36
StatusCode(unsigned long code, bool checked=false)
Definition: StatusCode.h:52
#define UNLIKELY(x)
Definition: Kernel.h:128
void setCode(unsigned long value)
Set the status code by value.
Definition: StatusCode.h:101
unsigned long getCode() const
Get the status code by value.
Definition: StatusCode.h:94
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
bool m_checked
If the Status code has been checked.
Definition: StatusCode.h:183
STL namespace.
~StatusCode()
Destructor.
Definition: StatusCode.h:67
static GAUDI_API void enableChecking()
Definition: StatusCode.cpp:19
std::ostream & operator<<(std::ostream &s, const StatusCode &sc)
Definition: StatusCode.h:192
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:134
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
Simple RAII class to ignore unchecked StatusCode instances in a scope.
Definition: StatusCode.h:165
StatusCode(IssueSeverity &&is)
Definition: StatusCode.h:44
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:186
unsigned long d_code
The status code.
Definition: StatusCode.h:182
bool isRecoverable() const
Definition: StatusCode.h:87
StatusCode(const StatusCode &rhs)
Definition: StatusCode.h:54
T move(T...args)
StatusCode & operator=(const StatusCode &rhs)
Definition: StatusCode.h:123
string s
Definition: gaudirun.py:253
static GAUDI_API void disableChecking()
Definition: StatusCode.cpp:21
void ignore() const
Definition: StatusCode.h:109
friend bool operator>(const StatusCode &a, const StatusCode &b)
Comparison operator.
Definition: StatusCode.h:137
StatusCode(StatusCode &&rhs) noexcept
Move constructor.
Definition: StatusCode.h:60
#define GAUDI_API
Definition: Kernel.h:110
STL class.
std::shared_ptr< const IssueSeverity > m_severity
Pointer to a IssueSeverity.
Definition: StatusCode.h:184
void setChecked() const
Ignore the checking code;.
Definition: StatusCode.h:108
StatusCode & operator=(unsigned long value)
Assignment operator.
Definition: StatusCode.h:118