The Gaudi Framework  v36r4 (0a924e98)
StatusCode.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2021 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 
106 
108  template <typename T, typename = std::enable_if_t<is_StatusCode_enum<T>::value>>
109  StatusCode( T sc ) noexcept : StatusCode{ static_cast<StatusCode::code_t>( sc ), is_StatusCode_enum<T>::instance } {}
110 
112  template <typename T, typename = std::enable_if_t<is_StatusCode_enum<T>::value>>
113  [[deprecated( "use StatusCode(T) instead" )]] StatusCode( T sc, bool ) noexcept : StatusCode{ sc } {}
114 
116  explicit StatusCode( code_t code, const StatusCode::Category& cat ) noexcept : m_cat( &cat ), m_code( code ) {}
117 
119  [[deprecated( "use StatusCode(code_t, Category) instead" )]] explicit StatusCode( code_t code,
120  const StatusCode::Category& cat,
121  bool ) noexcept
122  : StatusCode{ code, cat } {}
123 
125  explicit StatusCode( code_t code ) noexcept : StatusCode( code, default_category() ) {}
126 
128  [[deprecated( "use StatusCode(code_t) instead" )]] explicit StatusCode( code_t code, bool ) noexcept
129  : StatusCode{ code } {}
130 
132  StatusCode( const StatusCode& rhs ) noexcept = default;
133 
135  StatusCode( StatusCode&& rhs ) noexcept = default;
136 
138  ~StatusCode() = default;
139 
140  StatusCode& operator=( const StatusCode& rhs ) noexcept = default;
141 
142  bool isSuccess() const;
143  bool isFailure() const { return !isSuccess(); }
144  bool isRecoverable() const;
145 
147  explicit operator bool() const { return isSuccess(); }
148 
150  code_t getCode() const { return m_code; }
151 
153  [[deprecated( "will be removed" )]] const StatusCode& setChecked( bool = true ) const { return *this; }
154  [[deprecated( "will be removed" )]] StatusCode& setChecked( bool = true ) { return *this; }
155 
157  const StatusCode& ignore() const { return *this; }
158  StatusCode& ignore() { return *this; }
159 
180  template <typename F, typename... ARGS>
181  StatusCode andThen( F&& f, ARGS&&... args ) const {
182  if ( isFailure() ) return *this;
183  return i_invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
184  }
185 
202  template <typename F, typename... ARGS>
203  StatusCode orElse( F&& f, ARGS&&... args ) const {
204  if ( isSuccess() ) return *this;
205  return i_invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
206  }
207 
224  const StatusCode& orThrow( std::string_view message, std::string_view tag ) const {
225  if ( isFailure() ) i_doThrow( message, tag );
226  return *this;
227  }
228 
233  const StatusCode& orThrow( std::string_view tag = "" ) const {
234  if ( isFailure() ) i_doThrow( message(), tag ); // make sure `message()` is only called on error path
235  return *this;
236  }
237 
239  [[deprecated( "will be removed" )]] bool checked() const { return true; }
240 
242  const StatusCode::Category& getCategory() const { return *m_cat; }
243 
245  std::string message() const { return getCategory().message( m_code ); }
246 
248  s << sc.message();
249  return s;
250  }
251 
255  friend bool operator==( const StatusCode& lhs, const StatusCode& rhs );
256  friend bool operator!=( const StatusCode& lhs, const StatusCode& rhs ) { return !( lhs == rhs ); }
257 
259  friend bool operator<( const StatusCode& lhs, const StatusCode& rhs ) {
260  return ( lhs.m_cat < rhs.m_cat || ( lhs.m_cat == rhs.m_cat && lhs.m_code < rhs.m_code ) );
261  }
262 
264  StatusCode& operator&=( const StatusCode& rhs );
265  StatusCode& operator|=( const StatusCode& rhs );
266  //
268  friend StatusCode operator&( StatusCode lhs, const StatusCode& rhs ) { return lhs &= rhs; }
269 
271  friend StatusCode operator|( StatusCode lhs, const StatusCode& rhs ) { return lhs |= rhs; }
272 
274  friend bool& operator&=( bool& lhs, const StatusCode& sc ) { return lhs &= sc.isSuccess(); }
275 
277  friend bool& operator|=( bool& lhs, const StatusCode& sc ) { return lhs |= sc.isSuccess(); }
278 
279  [[deprecated( "will be removed" )]] static GAUDI_API void enableChecking();
280  [[deprecated( "will be removed" )]] static GAUDI_API void disableChecking();
281  [[deprecated( "will be removed" )]] static GAUDI_API bool checkingEnabled();
282 
297  class [[deprecated( "will be removed" )]] ScopedDisableChecking{};
298 
299 private:
300  const Category* m_cat{ &default_category() };
301  code_t m_code{ static_cast<code_t>( ErrorCode::SUCCESS ) };
302 
303  ErrorCode default_value() const;
304  void check();
305 
307  [[noreturn]] void i_doThrow( std::string_view message, std::string_view tag ) const;
308 
310  template <typename F, typename... ARGS, typename = std::enable_if_t<std::is_invocable_v<F, ARGS...>>>
311  StatusCode i_invoke( F&& f, ARGS&&... args ) const {
312  if constexpr ( std::is_invocable_r_v<StatusCode, F, ARGS...> ) {
313  return std::invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
314  } else {
315  // static_assert( std::is_same_v<void,std::invoke_result_t<F,ARGS...>>); // how paranoid should this be?
316  std::invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
317  return *this;
318  }
319  }
320 };
321 
322 /*
323  * Macros to declare/implement StatusCode enums/categories
324  */
325 
328 #define STATUSCODE_ENUM_DECL( ENUM ) \
329  template <> \
330  struct is_StatusCode_enum<ENUM> : std::true_type { \
331  static const StatusCode::Category& instance; \
332  };
333 
337 #define STATUSCODE_ENUM_IMPL( ... ) BOOST_PP_OVERLOAD( STATUSCODE_ENUM_IMPL_, __VA_ARGS__ )( __VA_ARGS__ )
338 
339 #define STATUSCODE_ENUM_IMPL_1( ENUM ) \
340  const StatusCode::Category& is_StatusCode_enum<ENUM>::instance = StatusCode::default_category();
341 
342 #define STATUSCODE_ENUM_IMPL_2( ENUM, CATEGORY ) \
343  const StatusCode::Category& is_StatusCode_enum<ENUM>::instance = CATEGORY{};
344 
345 // Declare the default StatusCode enum
347 
348 /*
349  * Inline methods
350  */
351 
352 inline const StatusCode::Category& StatusCode::default_category() noexcept {
354 }
355 
356 inline bool StatusCode::isSuccess() const {
357  return ( m_code == static_cast<code_t>( ErrorCode::SUCCESS ) || m_cat->isSuccess( m_code ) );
358 }
359 
360 inline bool StatusCode::isRecoverable() const { return m_cat->isRecoverable( m_code ); }
361 
364  return r;
365 }
366 
367 inline bool operator==( const StatusCode& lhs, const StatusCode& rhs ) {
368  return ( lhs.m_code == rhs.m_code ) &&
369  ( lhs.m_code == static_cast<StatusCode::code_t>( StatusCode::ErrorCode::SUCCESS ) ||
371  ( lhs.m_cat == rhs.m_cat ) );
372 }
373 
375  // Ternary AND lookup matrix
376  static constexpr StatusCode::code_t AND[3][3] = { { 0, 0, 0 }, { 0, 1, 2 }, { 0, 2, 2 } };
377 
379  StatusCode::code_t r = static_cast<StatusCode::code_t>( rhs.default_value() );
380  m_code = AND[l][r];
381  return *this;
382 }
383 
385  // Ternary OR lookup matrix
386  static constexpr StatusCode::code_t OR[3][3] = { { 0, 1, 2 }, { 1, 1, 1 }, { 2, 1, 2 } };
387 
389  StatusCode::code_t r = static_cast<StatusCode::code_t>( rhs.default_value() );
390  m_code = OR[l][r];
391  return *this;
392 }
393 
394 #endif // GAUDIKERNEL_STATUSCODE_H
GaudiPython.Bindings.FAILURE
FAILURE
Definition: Bindings.py:90
StatusCode::getCategory
const StatusCode::Category & getCategory() const
Retrieve category (does not "check" the StatusCode)
Definition: StatusCode.h:242
StatusCode::ErrorCode::SUCCESS
@ SUCCESS
StatusCode::Category::Category
constexpr Category() noexcept=default
GaudiHive.precedence.message
message
Definition: precedence.py:22
StatusCode::isRecoverable
bool isRecoverable() const
Definition: StatusCode.h:360
std::false_type
StatusCode::i_invoke
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:311
std::string
STL class.
StatusCode::Category::isSuccess
virtual bool isSuccess(code_t code) const
Is code considered success ?
Definition: StatusCode.h:90
StatusCode::andThen
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition: StatusCode.h:181
StatusCode::orThrow
const StatusCode & orThrow(std::string_view message, std::string_view tag) const
Throw a GaudiException in case of failures.
Definition: StatusCode.h:224
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:356
StatusCode::checked
bool checked() const
Has the StatusCode been checked?
Definition: StatusCode.h:239
StatusCode::setChecked
StatusCode & setChecked(bool=true)
Definition: StatusCode.h:154
gaudirun.s
string s
Definition: gaudirun.py:346
StatusCode::operator|=
StatusCode & operator|=(const StatusCode &rhs)
Ternary logic operator with RECOVERABLE being the "third" state.
Definition: StatusCode.h:384
StatusCode::message
std::string message() const
Description (or name) of StatusCode value.
Definition: StatusCode.h:245
StatusCode::orElse
StatusCode orElse(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a failure result.
Definition: StatusCode.h:203
StatusCode::check
void check()
Do StatusCode check.
StatusCode::m_code
code_t m_code
The status code value.
Definition: StatusCode.h:301
StatusCode::operator!=
friend bool operator!=(const StatusCode &lhs, const StatusCode &rhs)
Definition: StatusCode.h:256
StatusCode::orThrow
const StatusCode & orThrow(std::string_view tag="") const
Throw a GaudiException in case of failures.
Definition: StatusCode.h:233
StatusCode::code_t
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:67
StatusCode::operator<
friend bool operator<(const StatusCode &lhs, const StatusCode &rhs)
Comparison (values are grouped by category first)
Definition: StatusCode.h:259
operator==
bool operator==(const StatusCode &lhs, const StatusCode &rhs)
Definition: StatusCode.h:367
StatusCode::StatusCode
StatusCode(T sc, bool) noexcept
Constructor from enum type (allowing implicit conversion)
Definition: StatusCode.h:113
gaudirun.default
default
Definition: gaudirun.py:188
StatusCode::operator&
friend StatusCode operator&(StatusCode lhs, const StatusCode &rhs)
Ternary AND operator.
Definition: StatusCode.h:268
StatusCode
Definition: StatusCode.h:65
StatusCode::Category
Definition: StatusCode.h:78
HistoDumpEx.r
r
Definition: HistoDumpEx.py:20
StatusCode::StatusCode
StatusCode(code_t code, const StatusCode::Category &cat) noexcept
Constructor from code_t and category (explicit conversion only)
Definition: StatusCode.h:116
std::ostream
STL class.
StatusCode::StatusCode
StatusCode(const StatusCode &rhs) noexcept=default
Copy constructor.
StatusCode::operator|
friend StatusCode operator|(StatusCode lhs, const StatusCode &rhs)
Ternary OR operator.
Definition: StatusCode.h:271
StatusCode::setChecked
const StatusCode & setChecked(bool=true) const
Check/uncheck StatusCode.
Definition: StatusCode.h:153
StatusCode::default_value
ErrorCode default_value() const
Project onto the default StatusCode values.
Definition: StatusCode.h:362
Gaudi::Tests::Histograms::CustomAxis::Category
Category
Definition: HistogramsTests.cpp:16
std::to_string
T to_string(T... args)
StatusCode::operator<<
friend std::ostream & operator<<(std::ostream &s, const StatusCode &sc)
Definition: StatusCode.h:247
StatusCode::operator&=
friend bool & operator&=(bool &lhs, const StatusCode &sc)
Boolean AND assignment operator.
Definition: StatusCode.h:274
StatusCode::operator&=
StatusCode & operator&=(const StatusCode &rhs)
Ternary logic operator with RECOVERABLE being the "third" state.
Definition: StatusCode.h:374
StatusCode::ScopedDisableChecking
Simple RAII class to ignore unchecked StatusCode instances in a scope.
Definition: StatusCode.h:297
StatusCode::m_cat
const Category * m_cat
The status code category.
Definition: StatusCode.h:300
StatusCode::operator|=
friend bool & operator|=(bool &lhs, const StatusCode &sc)
Boolean OR assignment operator.
Definition: StatusCode.h:277
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:157
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:143
StatusCode::ErrorCode
ErrorCode
Definition: StatusCode.h:69
StatusCode::ignore
StatusCode & ignore()
Definition: StatusCode.h:158
STATUSCODE_ENUM_DECL
#define STATUSCODE_ENUM_DECL(ENUM)
Declare an enum to be used as StatusCode value.
Definition: StatusCode.h:328
gaudirun.l
dictionary l
Definition: gaudirun.py:580
StatusCode::ErrorCode::RECOVERABLE
@ RECOVERABLE
gaudirun.args
args
Definition: gaudirun.py:336
std
STL namespace.
Kernel.h
StatusCode::operator=
StatusCode & operator=(const StatusCode &rhs) noexcept=default
StatusCode::StatusCode
StatusCode(code_t code) noexcept
Constructor from code_t in the default category (explicit conversion only)
Definition: StatusCode.h:125
StatusCode::ErrorCode::FAILURE
@ FAILURE
StatusCode::StatusCode
StatusCode(code_t code, bool) noexcept
Constructor from code_t and category (explicit conversion only)
Definition: StatusCode.h:128
StatusCode::getCode
code_t getCode() const
Retrieve value ("checks" the StatusCode)
Definition: StatusCode.h:150
StatusCode::StatusCode
StatusCode(StatusCode &&rhs) noexcept=default
Move constructor.
StatusCode::Category::name
virtual const char * name() const =0
Name of the category.
is_StatusCode_enum
Definition: StatusCode.h:23
bug_38882.SUCCESS
SUCCESS
Definition: bug_38882.py:24
StatusCode::~StatusCode
~StatusCode()=default
Destructor.
StatusCode::Category::isRecoverable
virtual bool isRecoverable(code_t code) const
Is code considered recoverable ?
Definition: StatusCode.h:93
StatusCode::StatusCode
StatusCode(code_t code, const StatusCode::Category &cat, bool) noexcept
Constructor from code_t in the default category (explicit conversion only)
Definition: StatusCode.h:119
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
StatusCode::Category::message
virtual std::string message(code_t code) const
Description for code within this category.
Definition: StatusCode.h:86