| The Gaudi Framework v29r1 (5cd95fa2)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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/IssueSeverity.h"
7 #include "GaudiKernel/Kernel.h"
8
9 #include <memory>
10
21 class IMessageSvc;
22 class IStatusCodeSvc;
23
24 class IgnoreError
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
44 StatusCode( IssueSeverity&& is )
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
67 ~StatusCode()
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 }
123 StatusCode& operator=( const StatusCode& rhs )
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
165 class ScopedDisableChecking
166 {
167 bool m_enabled;
168
169 public:
170 ScopedDisableChecking() : m_enabled( StatusCode::checkingEnabled() )
171 {
172 if ( m_enabled ) StatusCode::disableChecking();
173 }
174 ~ScopedDisableChecking()
175 {
176 if ( m_enabled ) StatusCode::enableChecking();
177 }
178 };
179
180 protected:
182 unsigned long d_code = SUCCESS;
183 mutable bool m_checked = false;
184 std::shared_ptr<const IssueSeverity> m_severity;
185
186 static bool s_checking;
187
188 private:
189 void check();
190 };
191
192 inline std::ostream& operator<<( std::ostream& s, const StatusCode& sc )
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::StatusCode
StatusCode(unsigned long code, IssueSeverity &&sev)
Definition: StatusCode.h:36
StatusCode::StatusCode
StatusCode(unsigned long code, bool checked=false)
Definition: StatusCode.h:52
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:128
std::shared_ptr< const IssueSeverity >
Kernel.h
StatusCode::setCode
void setCode(unsigned long value)
Set the status code by value.
Definition: StatusCode.h:101
IssueSeverity
Definition: IssueSeverity.h:23
StatusCode::FAILURE
Definition: StatusCode.h:31
StatusCode::getCode
unsigned long getCode() const
Get the status code by value.
Definition: StatusCode.h:94
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
StatusCode::m_checked
bool m_checked
If the Status code has been checked.
Definition: StatusCode.h:183
GaudiPython.Bindings.SUCCESS
SUCCESS
Definition: Bindings.py:56
StatusCode::ScopedDisableChecking::~ScopedDisableChecking
~ScopedDisableChecking()
Definition: StatusCode.h:174
std
STL namespace.
StatusCode::~StatusCode
~StatusCode()
Destructor.
Definition: StatusCode.h:67
StatusCode::enableChecking
static GAUDI_API void enableChecking()
Definition: StatusCode.cpp:19
operator<<
std::ostream & operator<<(std::ostream &s, const StatusCode &sc)
Definition: StatusCode.h:192
StatusCode::isFailure
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
GaudiPython.Bindings.FAILURE
FAILURE
Definition: Bindings.py:57
StatusCode::operator<
friend bool operator<(const StatusCode &a, const StatusCode &b)
Comparison operator.
Definition: StatusCode.h:134
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
IssueSeverity.h
StatusCode::ScopedDisableChecking
Simple RAII class to ignore unchecked StatusCode instances in a scope.
Definition: StatusCode.h:165
StatusCode::StatusCode
StatusCode(IssueSeverity &&is)
Definition: StatusCode.h:44
IMessageSvc
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:38
StatusCode::s_checking
static bool s_checking
Global flag to control if StatusCode need to be checked.
Definition: StatusCode.h:186
StatusCode::d_code
unsigned long d_code
The status code.
Definition: StatusCode.h:182
StatusCode::isRecoverable
bool isRecoverable() const
Definition: StatusCode.h:87
StatusCode::StatusCode
StatusCode(const StatusCode &rhs)
Definition: StatusCode.h:54
std::move
T move(T...args)
StatusCode::operator=
StatusCode & operator=(const StatusCode &rhs)
Definition: StatusCode.h:123
IgnoreError
Definition: StatusCode.h:24
gaudirun.s
string s
Definition: gaudirun.py:253
MSG::ERROR
Definition: IMessageSvc.h:16
StatusCode::disableChecking
static GAUDI_API void disableChecking()
Definition: StatusCode.cpp:21
StatusCode::ignore
void ignore() const
Definition: StatusCode.h:109
StatusCode::operator>
friend bool operator>(const StatusCode &a, const StatusCode &b)
Comparison operator.
Definition: StatusCode.h:137
StatusCode::StatusCode
StatusCode(StatusCode &&rhs) noexcept
Move constructor.
Definition: StatusCode.h:60
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:110
IStatusCodeSvc
Definition: IStatusCodeSvc.h:7
std::ostream
STL class.
StatusCode::m_severity
std::shared_ptr< const IssueSeverity > m_severity
Pointer to a IssueSeverity.
Definition: StatusCode.h:184
StatusCode::ScopedDisableChecking::ScopedDisableChecking
ScopedDisableChecking()
Definition: StatusCode.h:170
StatusCode::setChecked
void setChecked() const
Ignore the checking code;.
Definition: StatusCode.h:108
StatusCode::ScopedDisableChecking::m_enabled
bool m_enabled
Definition: StatusCode.h:167
StatusCode::operator=
StatusCode & operator=(unsigned long value)
Assignment operator.
Definition: StatusCode.h:118