All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 #if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
10 #include <memory>
11 #else
12 #include "boost/shared_ptr.hpp"
13 #endif
14 
25 class IMessageSvc;
26 class IStatusCodeSvc;
27 
28 class IgnoreError {};
29 
30 class StatusCode {
31 public:
32  enum {
33  FAILURE = 0,
34  SUCCESS = 1,
36  };
37 
40  d_code(SUCCESS), m_checked(false), m_severity() {}
41  StatusCode( unsigned long code, const IssueSeverity& sev ):
42  d_code(code),m_checked(false), m_severity() {
43  try { // ensure that we do not throw even if we cannot copy the severity
44 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L) && ! defined(__GCCXML__)
45  m_severity = std::make_shared<IssueSeverity>(sev);
46 #else
48 #endif
49  }
50  catch (...) {}
51  }
52  StatusCode( unsigned long code, bool checked = false ):
53  d_code(code),m_checked(checked), m_severity() {}
54 
55  StatusCode( const StatusCode& rhs ):
56  d_code(rhs.d_code), m_checked(rhs.m_checked),
58  { rhs.m_checked = true; }
59 
60 #ifndef __GCCXML__
61  StatusCode( StatusCode&& rhs ):
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 #endif
67 
70  { if (UNLIKELY(s_checking)) check(); }
71 
75  bool isSuccess() const {
76  m_checked = true;
77  return (d_code == SUCCESS);
78  }
79 
85  bool isFailure() const { return !isSuccess(); }
86  bool isRecoverable() const {
87  m_checked = true;
88  return (d_code == RECOVERABLE);
89  }
90 
92  unsigned long getCode() const{
93  m_checked = true;
94  return d_code;
95  }
96 
98  void setCode(unsigned long value) {
99  m_checked = false;
100  d_code = value;
101  }
102 
104  void setChecked() const{
105  m_checked = true;
106  }
107  void ignore() const { setChecked(); }
108 
110  operator unsigned long() const { return getCode(); }
111 
113  GAUDI_API const IssueSeverity& severity() const;
114 
116  StatusCode& operator=(unsigned long value) {
117  setCode(value);
118  return *this;
119  }
121  if (this == &rhs) return *this; // Protection against self-assignment
122  d_code = rhs.d_code;
123  m_checked = rhs.m_checked;
124  rhs.m_checked = true;
125  m_severity = rhs.m_severity;
126  return *this;
127  }
128 
130  friend bool operator< ( const StatusCode& a, const StatusCode& b ) {
131  return a.d_code < b.d_code;
132  }
133 
135  friend bool operator> ( const StatusCode& a, const StatusCode& b ) {
136  return a.d_code > b.d_code;
137  }
138 
139 #ifndef _WIN32
140  operator IgnoreError() const {
141  m_checked = true;
142  return IgnoreError();
143  }
144 #endif
145 
146  static GAUDI_API void enableChecking();
147  static GAUDI_API void disableChecking();
148  static GAUDI_API bool checkingEnabled();
149 
165  bool m_enabled;
166  public:
169  }
172  }
173  };
174 
175 protected:
177  unsigned long d_code;
178  mutable bool m_checked;
179 #if defined(__GCCXML__)
180  // This is because GCCXML needs to see something that is not too in conflict with
181  // boost or std
182  typedef IssueSeverity* SeverityPtr;
183 #elif defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
184  typedef std::shared_ptr<IssueSeverity> SeverityPtr;
185 #else
186  typedef boost::shared_ptr<IssueSeverity> SeverityPtr;
187 #endif
189 
190  static bool s_checking;
191 
192 private:
193  void check();
194 };
195 
196 inline std::ostream& operator<< ( std::ostream& s , const StatusCode& sc )
197 {
198  if ( sc.isSuccess() ) { return s << "SUCCESS" ; }
199  else if ( sc.isRecoverable() ) { return s << "RECOVERABLE" ; }
200  s << "FAILURE" ;
201  if ( StatusCode::FAILURE != sc.getCode() )
202  { s << "(" << sc.getCode() << ")" ;}
203  return s ;
204 }
205 
206 #endif // GAUDIKERNEL_STATUSCODES_H
207 
208 
209 
StatusCode(unsigned long code, bool checked=false)
Definition: StatusCode.h:52
#define UNLIKELY(x)
Definition: Kernel.h:127
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:98
void check()
Definition: StatusCode.cpp:37
unsigned long getCode() const
Get the status code by value.
Definition: StatusCode.h:92
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:178
SeverityPtr m_severity
Pointer to a IssueSeverity.
Definition: StatusCode.h:188
StatusCode(unsigned long code, const IssueSeverity &sev)
Definition: StatusCode.h:41
~StatusCode()
Destructor.
Definition: StatusCode.h:69
static GAUDI_API void enableChecking()
Definition: StatusCode.cpp:19
StatusCode()
Constructor.
Definition: StatusCode.h:39
static GAUDI_API bool checkingEnabled()
Definition: StatusCode.cpp:27
std::ostream & operator<<(std::ostream &s, const StatusCode &sc)
Definition: StatusCode.h:196
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:85
friend bool operator<(const StatusCode &a, const StatusCode &b)
Comparison operator.
Definition: StatusCode.h:130
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
Simple RAII class to ignore unchecked StatusCode instances in a scope.
Definition: StatusCode.h:164
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:190
unsigned long d_code
The status code.
Definition: StatusCode.h:177
bool isRecoverable() const
Definition: StatusCode.h:86
StatusCode(const StatusCode &rhs)
Definition: StatusCode.h:55
StatusCode & operator=(const StatusCode &rhs)
Definition: StatusCode.h:120
string s
Definition: gaudirun.py:210
static GAUDI_API void disableChecking()
Definition: StatusCode.cpp:23
void ignore() const
Definition: StatusCode.h:107
friend bool operator>(const StatusCode &a, const StatusCode &b)
Comparison operator.
Definition: StatusCode.h:135
boost::shared_ptr< IssueSeverity > SeverityPtr
Definition: StatusCode.h:186
#define GAUDI_API
Definition: Kernel.h:108
void setChecked() const
Ignore the checking code;.
Definition: StatusCode.h:104
StatusCode & operator=(unsigned long value)
Assignment operator.
Definition: StatusCode.h:116