The Gaudi Framework  master (37c0b60a)
StatusCode.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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  explicit StatusCode( code_t code, const StatusCode::Category& cat ) noexcept : m_cat( &cat ), m_code( code ) {}
113 
115  explicit StatusCode( code_t code ) noexcept : StatusCode( code, default_category() ) {}
116 
118  StatusCode( const StatusCode& rhs ) noexcept = default;
119 
121  StatusCode( StatusCode&& rhs ) noexcept = default;
122 
124  ~StatusCode() = default;
125 
126  StatusCode& operator=( const StatusCode& rhs ) noexcept = default;
127 
128  bool isSuccess() const;
129  bool isFailure() const { return !isSuccess(); }
130  bool isRecoverable() const;
131 
133  explicit operator bool() const { return isSuccess(); }
134 
136  code_t getCode() const { return m_code; }
137 
139  const StatusCode& ignore() const { return *this; }
140  StatusCode& ignore() { return *this; }
141 
162  template <typename F, typename... ARGS>
163  StatusCode andThen( F&& f, ARGS&&... args ) const {
164  if ( isFailure() ) return *this;
165  return i_invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
166  }
167 
184  template <typename F, typename... ARGS>
185  StatusCode orElse( F&& f, ARGS&&... args ) const {
186  if ( isSuccess() ) return *this;
187  return i_invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
188  }
189 
206  const StatusCode& orThrow( std::string_view message, std::string_view tag ) const {
207  if ( isFailure() ) i_doThrow( message, tag );
208  return *this;
209  }
210 
215  const StatusCode& orThrow( std::string_view tag = "" ) const {
216  if ( isFailure() ) i_doThrow( message(), tag ); // make sure `message()` is only called on error path
217  return *this;
218  }
219 
221  const StatusCode::Category& getCategory() const { return *m_cat; }
222 
224  std::string message() const { return getCategory().message( m_code ); }
225 
227  s << sc.message();
228  return s;
229  }
230 
234  friend bool operator==( const StatusCode& lhs, const StatusCode& rhs );
235  friend bool operator!=( const StatusCode& lhs, const StatusCode& rhs ) { return !( lhs == rhs ); }
236 
238  friend bool operator<( const StatusCode& lhs, const StatusCode& rhs ) {
239  return ( lhs.m_cat < rhs.m_cat || ( lhs.m_cat == rhs.m_cat && lhs.m_code < rhs.m_code ) );
240  }
241 
243  StatusCode& operator&=( const StatusCode& rhs );
244  StatusCode& operator|=( const StatusCode& rhs );
245  //
247  friend StatusCode operator&( StatusCode lhs, const StatusCode& rhs ) { return lhs &= rhs; }
248 
250  friend StatusCode operator|( StatusCode lhs, const StatusCode& rhs ) { return lhs |= rhs; }
251 
253  friend bool& operator&=( bool& lhs, const StatusCode& sc ) { return lhs &= sc.isSuccess(); }
254 
256  friend bool& operator|=( bool& lhs, const StatusCode& sc ) { return lhs |= sc.isSuccess(); }
257 
258 private:
259  const Category* m_cat{ &default_category() };
260  code_t m_code{ static_cast<code_t>( ErrorCode::SUCCESS ) };
261 
262  ErrorCode default_value() const;
263 
265  [[noreturn]] void i_doThrow( std::string_view message, std::string_view tag ) const;
266 
268  template <typename F, typename... ARGS, typename = std::enable_if_t<std::is_invocable_v<F, ARGS...>>>
269  StatusCode i_invoke( F&& f, ARGS&&... args ) const {
270  if constexpr ( std::is_invocable_r_v<StatusCode, F, ARGS...> ) {
271  return std::invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
272  } else {
273  // static_assert( std::is_same_v<void,std::invoke_result_t<F,ARGS...>>); // how paranoid should this be?
274  std::invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
275  return *this;
276  }
277  }
278 };
279 
280 /*
281  * Macros to declare/implement StatusCode enums/categories
282  */
283 
286 #define STATUSCODE_ENUM_DECL( ENUM ) \
287  template <> \
288  struct is_StatusCode_enum<ENUM> : std::true_type { \
289  static const StatusCode::Category& instance; \
290  };
291 
295 #define STATUSCODE_ENUM_IMPL( ... ) BOOST_PP_OVERLOAD( STATUSCODE_ENUM_IMPL_, __VA_ARGS__ )( __VA_ARGS__ )
296 
297 #define STATUSCODE_ENUM_IMPL_1( ENUM ) \
298  const StatusCode::Category& is_StatusCode_enum<ENUM>::instance = StatusCode::default_category();
299 
300 #define STATUSCODE_ENUM_IMPL_2( ENUM, CATEGORY ) \
301  const StatusCode::Category& is_StatusCode_enum<ENUM>::instance = CATEGORY{};
302 
303 // Declare the default StatusCode enum
305 
306 /*
307  * Inline methods
308  */
309 
310 inline const StatusCode::Category& StatusCode::default_category() noexcept {
312 }
313 
314 inline bool StatusCode::isSuccess() const {
315  return ( m_code == static_cast<code_t>( ErrorCode::SUCCESS ) || m_cat->isSuccess( m_code ) );
316 }
317 
318 inline bool StatusCode::isRecoverable() const { return m_cat->isRecoverable( m_code ); }
319 
322  return r;
323 }
324 
325 inline bool operator==( const StatusCode& lhs, const StatusCode& rhs ) {
326  return ( lhs.m_code == rhs.m_code ) &&
327  ( lhs.m_code == static_cast<StatusCode::code_t>( StatusCode::ErrorCode::SUCCESS ) ||
329  ( lhs.m_cat == rhs.m_cat ) );
330 }
331 
333  // Ternary AND lookup matrix
334  static constexpr StatusCode::code_t AND[3][3] = { { 0, 0, 0 }, { 0, 1, 2 }, { 0, 2, 2 } };
335 
337  StatusCode::code_t r = static_cast<StatusCode::code_t>( rhs.default_value() );
338  m_code = AND[l][r];
339  return *this;
340 }
341 
343  // Ternary OR lookup matrix
344  static constexpr StatusCode::code_t OR[3][3] = { { 0, 1, 2 }, { 1, 1, 1 }, { 2, 1, 2 } };
345 
347  StatusCode::code_t r = static_cast<StatusCode::code_t>( rhs.default_value() );
348  m_code = OR[l][r];
349  return *this;
350 }
351 
352 #endif // GAUDIKERNEL_STATUSCODE_H
GaudiPython.Bindings.FAILURE
FAILURE
Definition: Bindings.py:84
StatusCode::getCategory
const StatusCode::Category & getCategory() const
Retrieve category.
Definition: StatusCode.h:221
StatusCode::ErrorCode::SUCCESS
@ SUCCESS
StatusCode::Category::Category
constexpr Category() noexcept=default
StatusCode::isRecoverable
bool isRecoverable() const
Definition: StatusCode.h:318
std::false_type
precedence.message
message
Definition: precedence.py:19
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:269
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:163
StatusCode::orThrow
const StatusCode & orThrow(std::string_view message, std::string_view tag) const
Throw a GaudiException in case of failures.
Definition: StatusCode.h:206
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
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:342
StatusCode::message
std::string message() const
Description (or name) of StatusCode value.
Definition: StatusCode.h:224
StatusCode::orElse
StatusCode orElse(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a failure result.
Definition: StatusCode.h:185
StatusCode::m_code
code_t m_code
The status code value.
Definition: StatusCode.h:260
StatusCode::operator!=
friend bool operator!=(const StatusCode &lhs, const StatusCode &rhs)
Definition: StatusCode.h:235
StatusCode::orThrow
const StatusCode & orThrow(std::string_view tag="") const
Throw a GaudiException in case of failures.
Definition: StatusCode.h:215
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:238
operator==
bool operator==(const StatusCode &lhs, const StatusCode &rhs)
Definition: StatusCode.h:325
gaudirun.default
default
Definition: gaudirun.py:188
StatusCode::operator&
friend StatusCode operator&(StatusCode lhs, const StatusCode &rhs)
Ternary AND operator.
Definition: StatusCode.h:247
StatusCode
Definition: StatusCode.h:65
StatusCode::Category
Definition: StatusCode.h:78
StatusCode::StatusCode
StatusCode(code_t code, const StatusCode::Category &cat) noexcept
Constructor from code_t and category (explicit conversion only)
Definition: StatusCode.h:112
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:250
StatusCode::default_value
ErrorCode default_value() const
Project onto the default StatusCode values.
Definition: StatusCode.h:320
Gaudi::Tests::Histograms::CustomAxis::Category
Category
Definition: HistogramsTests.cpp:17
std::to_string
T to_string(T... args)
StatusCode::operator<<
friend std::ostream & operator<<(std::ostream &s, const StatusCode &sc)
Definition: StatusCode.h:226
StatusCode::operator&=
friend bool & operator&=(bool &lhs, const StatusCode &sc)
Boolean AND assignment operator.
Definition: StatusCode.h:253
StatusCode::operator&=
StatusCode & operator&=(const StatusCode &rhs)
Ternary logic operator with RECOVERABLE being the "third" state.
Definition: StatusCode.h:332
GaudiPython.Bindings.SUCCESS
SUCCESS
Definition: Bindings.py:83
StatusCode::m_cat
const Category * m_cat
The status code category.
Definition: StatusCode.h:259
StatusCode::operator|=
friend bool & operator|=(bool &lhs, const StatusCode &sc)
Boolean OR assignment operator.
Definition: StatusCode.h:256
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
StatusCode::ErrorCode
ErrorCode
Definition: StatusCode.h:69
StatusCode::ignore
StatusCode & ignore()
Definition: StatusCode.h:140
STATUSCODE_ENUM_DECL
#define STATUSCODE_ENUM_DECL(ENUM)
Declare an enum to be used as StatusCode value.
Definition: StatusCode.h:286
gaudirun.l
dictionary l
Definition: gaudirun.py:583
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:115
StatusCode::ErrorCode::FAILURE
@ FAILURE
StatusCode::getCode
code_t getCode() const
Retrieve value.
Definition: StatusCode.h:136
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
StatusCode::~StatusCode
~StatusCode()=default
Destructor.
StatusCode::Category::isRecoverable
virtual bool isRecoverable(code_t code) const
Is code considered recoverable ?
Definition: StatusCode.h:93
StatusCode::Category::message
virtual std::string message(code_t code) const
Description for code within this category.
Definition: StatusCode.h:86