The Gaudi Framework  v33r1 (b1225454)
StatusCode.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIKERNEL_STATUSCODE_H
12 #define GAUDIKERNEL_STATUSCODE_H
13 
14 #include "boost/preprocessor/facilities/overload.hpp"
15 #include <functional>
16 #include <ostream>
17 #include <type_traits>
18 #include <utility>
19 
20 #include "GaudiKernel/Kernel.h"
21 
22 template <typename T>
24 
61 class
62 #if __cplusplus >= 201703L && !defined( __CLING__ )
63  [[nodiscard]]
64 #endif
65  StatusCode final {
66 public:
67  typedef unsigned long code_t;
68 
69  enum class ErrorCode : code_t { FAILURE = 0, SUCCESS = 1, RECOVERABLE = 2 };
70 
78  struct Category {
79  constexpr Category() noexcept = default;
80  virtual ~Category() {}
81 
83  virtual const char* name() const = 0;
84 
86  virtual std::string message( code_t code ) const { return "UNKNOWN(" + std::to_string( code ) + ")"; }
87 
90  virtual bool isSuccess( code_t code ) const { return code == static_cast<code_t>( ErrorCode::SUCCESS ); }
91 
93  virtual bool isRecoverable( code_t code ) const { return code == static_cast<code_t>( ErrorCode::RECOVERABLE ); }
94  };
95 
97  static const Category& default_category() noexcept;
98 
99  // Provide shorthands for default code values
100  constexpr const static auto SUCCESS = ErrorCode::SUCCESS;
101  constexpr const static auto FAILURE = ErrorCode::FAILURE;
102  constexpr const static auto RECOVERABLE = ErrorCode::RECOVERABLE;
103 
105  StatusCode() = default;
106 
108  template <typename T, typename = std::enable_if_t<is_StatusCode_enum<T>::value>>
109  StatusCode( T sc, bool checked = false ) noexcept {
110  *this = StatusCode( static_cast<StatusCode::code_t>( sc ), is_StatusCode_enum<T>::instance );
111  m_checked = checked;
112  }
113 
115  explicit StatusCode( code_t code, const StatusCode::Category& cat = default_category(),
116  bool checked = false ) noexcept
117  : m_cat( &cat ), m_code( code ), m_checked( checked ) {}
118 
120  explicit StatusCode( code_t code, bool checked ) noexcept : StatusCode( code, default_category(), checked ) {}
121 
123  StatusCode( const StatusCode& rhs ) noexcept : m_cat( rhs.m_cat ), m_code( rhs.m_code ), m_checked( rhs.m_checked ) {
124  rhs.m_checked = true;
125  }
126 
128  StatusCode( StatusCode && rhs ) noexcept : m_cat( rhs.m_cat ), m_code( rhs.m_code ), m_checked( rhs.m_checked ) {
129  rhs.m_checked = true;
130  }
131 
134  if ( UNLIKELY( s_checking ) ) check();
135  }
136 
137  StatusCode& operator=( const StatusCode& rhs ) noexcept {
138  m_cat = rhs.m_cat;
139  m_code = rhs.m_code;
140  m_checked = std::exchange( rhs.m_checked, true );
141  return *this;
142  }
143 
144  bool isSuccess() const;
145  bool isFailure() const { return !isSuccess(); }
146  bool isRecoverable() const;
147 
149  explicit operator bool() const { return isSuccess(); }
150 
152  code_t getCode() const {
153  m_checked = true;
154  return m_code;
155  }
156 
158  const StatusCode& setChecked( bool checked = true ) const {
159  m_checked = checked;
160  return *this;
161  }
162  StatusCode& setChecked( bool checked = true ) {
163  m_checked = checked;
164  return *this;
165  }
166 
168  const StatusCode& ignore() const {
169  setChecked( true );
170  return *this;
171  }
173  setChecked( true );
174  return *this;
175  }
176 
197  template <typename F, typename... ARGS>
198  StatusCode andThen( F && f, ARGS && ... args ) const {
199  if ( isFailure() ) return *this;
200  return i_invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
201  }
202 
219  template <typename F, typename... ARGS>
220  StatusCode orElse( F && f, ARGS && ... args ) const {
221  if ( isSuccess() ) return *this;
222  return i_invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
223  }
224 
241  StatusCode orThrow( std::string message, std::string tag ) const {
242  return orElse( &StatusCode::i_doThrow, this, std::move( message ), std::move( tag ) );
243  }
244 
246  bool checked() const { return m_checked; }
247 
249  const StatusCode::Category& getCategory() const { return *m_cat; }
250 
252  std::string message() const { return getCategory().message( m_code ); }
253 
255  s << sc.message();
256  return s;
257  }
258 
262  friend bool operator==( const StatusCode& lhs, const StatusCode& rhs );
263  friend bool operator!=( const StatusCode& lhs, const StatusCode& rhs ) { return !( lhs == rhs ); }
264 
266  friend bool operator<( const StatusCode& lhs, const StatusCode& rhs ) {
267  lhs.m_checked = true;
268  rhs.m_checked = true;
269  return ( lhs.m_cat < rhs.m_cat || ( lhs.m_cat == rhs.m_cat && lhs.m_code < rhs.m_code ) );
270  }
271 
273  StatusCode& operator&=( const StatusCode& rhs );
274  StatusCode& operator|=( const StatusCode& rhs );
275 
276  static GAUDI_API void enableChecking();
277  static GAUDI_API void disableChecking();
278  static GAUDI_API bool checkingEnabled();
279 
295  bool m_enabled;
296 
297  public:
298  ScopedDisableChecking() : m_enabled( StatusCode::checkingEnabled() ) {
299  if ( m_enabled ) StatusCode::disableChecking();
300  }
302  if ( m_enabled ) StatusCode::enableChecking();
303  }
304  };
305 
306 private:
307  const Category* m_cat{&default_category()};
308  code_t m_code{static_cast<code_t>( ErrorCode::SUCCESS )};
309  mutable bool m_checked{false};
310  static bool s_checking;
311 
312  ErrorCode default_value() const;
313  void check();
314 
316  void i_doThrow( std::string message, std::string tag ) const;
317 
319  template <typename F, typename... ARGS, typename = std::enable_if_t<std::is_invocable_v<F, ARGS...>>>
320  StatusCode i_invoke( F && f, ARGS && ... args ) const {
321  if constexpr ( std::is_invocable_r_v<StatusCode, F, ARGS...> ) {
322  return std::invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
323  } else {
324  // static_assert( std::is_same_v<void,std::invoke_result_t<F,ARGS...>>); // how paranoid should this be?
325  std::invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
326  return *this;
327  }
328  }
329 };
330 
331 /*
332  * Macros to declare/implement StatusCode enums/categories
333  */
334 
337 #define STATUSCODE_ENUM_DECL( ENUM ) \
338  template <> \
339  struct is_StatusCode_enum<ENUM> : std::true_type { \
340  static const StatusCode::Category& instance; \
341  };
342 
346 #define STATUSCODE_ENUM_IMPL( ... ) BOOST_PP_OVERLOAD( STATUSCODE_ENUM_IMPL_, __VA_ARGS__ )( __VA_ARGS__ )
347 
348 #define STATUSCODE_ENUM_IMPL_1( ENUM ) \
349  const StatusCode::Category& is_StatusCode_enum<ENUM>::instance = StatusCode::default_category();
350 
351 #define STATUSCODE_ENUM_IMPL_2( ENUM, CATEGORY ) \
352  const StatusCode::Category& is_StatusCode_enum<ENUM>::instance = CATEGORY{};
353 
354 // Declare the default StatusCode enum
356 
357 /*
358  * Inline methods
359  */
360 
361 inline const StatusCode::Category& StatusCode::default_category() noexcept {
363 }
364 
365 inline bool StatusCode::isSuccess() const {
366  m_checked = true;
367  return ( m_code == static_cast<code_t>( ErrorCode::SUCCESS ) || m_cat->isSuccess( m_code ) );
368 }
369 
370 inline bool StatusCode::isRecoverable() const {
371  m_checked = true;
372  return m_cat->isRecoverable( m_code );
373 }
374 
376  bool save_checked = m_checked; // Preserve checked status
378  m_checked = save_checked;
379  return r;
380 }
381 
382 inline bool operator==( const StatusCode& lhs, const StatusCode& rhs ) {
383  lhs.m_checked = true;
384  rhs.m_checked = true;
385  return ( lhs.m_code == rhs.m_code ) &&
386  ( lhs.m_code == static_cast<StatusCode::code_t>( StatusCode::ErrorCode::SUCCESS ) ||
387  lhs.m_code == static_cast<StatusCode::code_t>( StatusCode::ErrorCode::FAILURE ) ||
388  ( lhs.m_cat == rhs.m_cat ) );
389 }
390 
392  // Ternary AND lookup matrix
393  static constexpr StatusCode::code_t AND[3][3] = {{0, 0, 0}, {0, 1, 2}, {0, 2, 2}};
394 
395  StatusCode::code_t l = static_cast<StatusCode::code_t>( default_value() );
396  StatusCode::code_t r = static_cast<StatusCode::code_t>( rhs.default_value() );
397  m_code = AND[l][r];
398  rhs.m_checked = true;
399  return *this;
400 }
401 
403  // Ternary OR lookup matrix
404  static constexpr StatusCode::code_t OR[3][3] = {{0, 1, 2}, {1, 1, 1}, {2, 1, 2}};
405 
406  StatusCode::code_t l = static_cast<StatusCode::code_t>( default_value() );
407  StatusCode::code_t r = static_cast<StatusCode::code_t>( rhs.default_value() );
408  m_code = OR[l][r];
409  rhs.m_checked = true;
410  return *this;
411 }
412 
414 inline StatusCode operator&( StatusCode lhs, const StatusCode& rhs ) { return lhs &= rhs; }
415 
417 inline StatusCode operator|( StatusCode lhs, const StatusCode& rhs ) { return lhs |= rhs; }
418 
420 inline bool& operator&=( bool& lhs, const StatusCode& sc ) { return lhs &= sc.isSuccess(); }
421 
423 inline bool& operator|=( bool& lhs, const StatusCode& sc ) { return lhs |= sc.isSuccess(); }
424 
425 #endif // GAUDIKERNEL_STATUSCODE_H
code_t getCode() const
Retrieve value ("checks" the StatusCode)
Definition: StatusCode.h:152
#define UNLIKELY(x)
Definition: Kernel.h:106
bool checked() const
Has the StatusCode been checked?
Definition: StatusCode.h:246
StatusCode operator|(StatusCode lhs, const StatusCode &rhs)
Ternary OR operator.
Definition: StatusCode.h:417
code_t m_code
The status code value.
Definition: StatusCode.h:308
friend bool operator<(const StatusCode &lhs, const StatusCode &rhs)
Comparison (values are grouped by category first)
Definition: StatusCode.h:266
bool m_checked
If the StatusCode has been checked.
Definition: StatusCode.h:309
The category assigned to a StatusCode.
Definition: StatusCode.h:78
StatusCode & operator=(const StatusCode &rhs) noexcept
Definition: StatusCode.h:137
T to_string(T... args)
StatusCode(code_t code, const StatusCode::Category &cat=default_category(), bool checked=false) noexcept
Constructor from code_t in the default category (explicit conversion only)
Definition: StatusCode.h:115
STL namespace.
~StatusCode()
Destructor.
Definition: StatusCode.h:133
StatusCode(code_t code, bool checked) noexcept
Constructor from code_t and category (explicit conversion only)
Definition: StatusCode.h:120
static GAUDI_API void enableChecking()
Definition: StatusCode.cpp:53
StatusCode(const StatusCode &rhs) noexcept
Copy constructor.
Definition: StatusCode.h:123
StatusCode & setChecked(bool checked=true)
Definition: StatusCode.h:162
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition: StatusCode.h:198
StatusCode i_invoke(F &&f, ARGS &&... args) const
Helper to invoke a callable and return the resulting StatusCode or this, if the callable returns void...
Definition: StatusCode.h:320
const StatusCode::Category & getCategory() const
Retrieve category (does not "check" the StatusCode)
Definition: StatusCode.h:249
virtual bool isSuccess(code_t code) const
Is code considered success ?
Definition: StatusCode.h:90
STL class.
friend std::ostream & operator<<(std::ostream &s, const StatusCode &sc)
Definition: StatusCode.h:254
StatusCode & ignore()
Definition: StatusCode.h:172
#define STATUSCODE_ENUM_DECL(ENUM)
Declare an enum to be used as StatusCode value.
Definition: StatusCode.h:337
virtual ~Category()
Definition: StatusCode.h:80
virtual std::string message(code_t code) const
Description for code within this category.
Definition: StatusCode.h:86
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
virtual bool isRecoverable(code_t code) const
Is code considered recoverable ?
Definition: StatusCode.h:93
friend bool operator!=(const StatusCode &lhs, const StatusCode &rhs)
Definition: StatusCode.h:263
Simple RAII class to ignore unchecked StatusCode instances in a scope.
Definition: StatusCode.h:294
static bool s_checking
Global flag to control if StatusCode need to be checked.
Definition: StatusCode.h:310
bool isSuccess() const
Definition: StatusCode.h:365
T move(T... args)
std::string message() const
Description (or name) of StatusCode value.
Definition: StatusCode.h:252
dictionary l
Definition: gaudirun.py:543
StatusCode operator &(StatusCode lhs, const StatusCode &rhs)
Ternary AND operator.
Definition: StatusCode.h:414
bool operator==(const StatusCode &lhs, const StatusCode &rhs)
Definition: StatusCode.h:382
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:168
StatusCode orThrow(std::string message, std::string tag) const
Throw a GaudiException in case of failures.
Definition: StatusCode.h:241
bool & operator|=(bool &lhs, const StatusCode &sc)
Boolean OR assignment operator.
Definition: StatusCode.h:423
string s
Definition: gaudirun.py:328
bool isRecoverable() const
Definition: StatusCode.h:370
static GAUDI_API void disableChecking()
Definition: StatusCode.cpp:55
bool isFailure() const
Definition: StatusCode.h:145
bool & operator&=(bool &lhs, const StatusCode &sc)
Boolean AND assignment operator.
Definition: StatusCode.h:420
StatusCode(StatusCode &&rhs) noexcept
Move constructor.
Definition: StatusCode.h:128
#define GAUDI_API
Definition: Kernel.h:81
STL class.
void i_doThrow(std::string message, std::string tag) const
Helper function to avoid circular dependency between GaudiException.h and StatusCode....
Definition: StatusCode.cpp:92
const StatusCode & setChecked(bool checked=true) const
Check/uncheck StatusCode.
Definition: StatusCode.h:158
ErrorCode default_value() const
Project onto the default StatusCode values.
Definition: StatusCode.h:375
const Category * m_cat
The status code category.
Definition: StatusCode.h:307
StatusCode & operator|=(const StatusCode &rhs)
Ternary logic operator with RECOVERABLE being the "third" state.
Definition: StatusCode.h:402
StatusCode & operator&=(const StatusCode &rhs)
Ternary logic operator with RECOVERABLE being the "third" state.
Definition: StatusCode.h:391
StatusCode orElse(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a failure result.
Definition: StatusCode.h:220
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:67