Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (f31105fd)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
StatusCode.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 !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>
110  StatusCode( T sc ) noexcept : StatusCode{ static_cast<StatusCode::code_t>( sc ), is_StatusCode_enum<T>::instance } {}
111 
113  explicit StatusCode( code_t code, const StatusCode::Category& cat ) noexcept : m_cat( &cat ), m_code( code ) {}
114 
116  explicit StatusCode( code_t code ) noexcept : StatusCode( code, default_category() ) {}
117 
119  StatusCode( const StatusCode& rhs ) noexcept = default;
120 
122  StatusCode( StatusCode&& rhs ) noexcept = default;
123 
125  ~StatusCode() = default;
126 
127  StatusCode& operator=( const StatusCode& rhs ) noexcept = default;
128 
129  bool isSuccess() const;
130  bool isFailure() const { return !isSuccess(); }
131  bool isRecoverable() const;
132 
134  explicit operator bool() const { return isSuccess(); }
135 
137  code_t getCode() const { return m_code; }
138 
140  const StatusCode& ignore() const { return *this; }
141  StatusCode& ignore() { return *this; }
142 
163  template <typename F, typename... ARGS>
164  StatusCode andThen( F&& f, ARGS&&... args ) const {
165  if ( isFailure() ) return *this;
166  return i_invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
167  }
168 
185  template <typename F, typename... ARGS>
186  StatusCode orElse( F&& f, ARGS&&... args ) const {
187  if ( isSuccess() ) return *this;
188  return i_invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
189  }
190 
207  const StatusCode& orThrow( std::string_view message, std::string_view tag ) const {
208  if ( isFailure() ) i_doThrow( message, tag );
209  return *this;
210  }
211 
216  const StatusCode& orThrow( std::string_view tag = "" ) const {
217  if ( isFailure() ) i_doThrow( message(), tag ); // make sure `message()` is only called on error path
218  return *this;
219  }
220 
222  const StatusCode::Category& getCategory() const { return *m_cat; }
223 
225  std::string message() const { return getCategory().message( m_code ); }
226 
228  s << sc.message();
229  return s;
230  }
231 
235  friend bool operator==( const StatusCode& lhs, const StatusCode& rhs );
236  friend bool operator!=( const StatusCode& lhs, const StatusCode& rhs ) { return !( lhs == rhs ); }
237 
239  friend bool operator<( const StatusCode& lhs, const StatusCode& rhs ) {
240  return ( lhs.m_cat < rhs.m_cat || ( lhs.m_cat == rhs.m_cat && lhs.m_code < rhs.m_code ) );
241  }
242 
244  StatusCode& operator&=( const StatusCode& rhs );
245  StatusCode& operator|=( const StatusCode& rhs );
246  //
248  friend StatusCode operator&( StatusCode lhs, const StatusCode& rhs ) { return lhs &= rhs; }
249 
251  friend StatusCode operator|( StatusCode lhs, const StatusCode& rhs ) { return lhs |= rhs; }
252 
254  friend bool& operator&=( bool& lhs, const StatusCode& sc ) { return lhs &= sc.isSuccess(); }
255 
257  friend bool& operator|=( bool& lhs, const StatusCode& sc ) { return lhs |= sc.isSuccess(); }
258 
259 private:
260  const Category* m_cat{ &default_category() };
261  code_t m_code{ static_cast<code_t>( ErrorCode::SUCCESS ) };
262 
263  ErrorCode default_value() const;
264 
266  [[noreturn]] void i_doThrow( std::string_view message, std::string_view tag ) const;
267 
269  template <typename... ARGS, std::invocable<ARGS...> F> // requires ( std::is_invocable_v<F, ARGS...> )
270  StatusCode i_invoke( F&& f, ARGS&&... args ) const {
271  if constexpr ( std::is_invocable_r_v<StatusCode, F, ARGS...> ) {
272  return std::invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
273  } else {
274  // static_assert( std::is_same_v<void,std::invoke_result_t<F,ARGS...>>); // how paranoid should this be?
275  std::invoke( std::forward<F>( f ), std::forward<ARGS>( args )... );
276  return *this;
277  }
278  }
279 };
280 
281 /*
282  * Macros to declare/implement StatusCode enums/categories
283  */
284 
287 #define STATUSCODE_ENUM_DECL( ENUM ) \
288  template <> \
289  struct is_StatusCode_enum<ENUM> : std::true_type { \
290  static const StatusCode::Category& instance; \
291  };
292 
296 #define STATUSCODE_ENUM_IMPL( ... ) BOOST_PP_OVERLOAD( STATUSCODE_ENUM_IMPL_, __VA_ARGS__ )( __VA_ARGS__ )
297 
298 #define STATUSCODE_ENUM_IMPL_1( ENUM ) \
299  const StatusCode::Category& is_StatusCode_enum<ENUM>::instance = StatusCode::default_category();
300 
301 #define STATUSCODE_ENUM_IMPL_2( ENUM, CATEGORY ) \
302  const StatusCode::Category& is_StatusCode_enum<ENUM>::instance = CATEGORY{};
303 
304 // Declare the default StatusCode enum
306 
307 /*
308  * Inline methods
309  */
310 
311 inline const StatusCode::Category& StatusCode::default_category() noexcept {
313 }
314 
315 inline bool StatusCode::isSuccess() const {
316  return ( m_code == static_cast<code_t>( ErrorCode::SUCCESS ) || m_cat->isSuccess( m_code ) );
317 }
318 
319 inline bool StatusCode::isRecoverable() const { return m_cat->isRecoverable( m_code ); }
320 
323  return r;
324 }
325 
326 inline bool operator==( const StatusCode& lhs, const StatusCode& rhs ) {
327  return ( lhs.m_code == rhs.m_code ) &&
328  ( lhs.m_code == static_cast<StatusCode::code_t>( StatusCode::ErrorCode::SUCCESS ) ||
330  ( lhs.m_cat == rhs.m_cat ) );
331 }
332 
334  // Ternary AND lookup matrix
335  static constexpr StatusCode::code_t AND[3][3] = { { 0, 0, 0 }, { 0, 1, 2 }, { 0, 2, 2 } };
336 
338  StatusCode::code_t r = static_cast<StatusCode::code_t>( rhs.default_value() );
339  m_code = AND[l][r];
340  return *this;
341 }
342 
344  // Ternary OR lookup matrix
345  static constexpr StatusCode::code_t OR[3][3] = { { 0, 1, 2 }, { 1, 1, 1 }, { 2, 1, 2 } };
346 
348  StatusCode::code_t r = static_cast<StatusCode::code_t>( rhs.default_value() );
349  m_code = OR[l][r];
350  return *this;
351 }
352 
353 #endif // GAUDIKERNEL_STATUSCODE_H
GaudiPython.Bindings.FAILURE
FAILURE
Definition: Bindings.py:84
StatusCode::getCategory
const StatusCode::Category & getCategory() const
Retrieve category.
Definition: StatusCode.h:222
StatusCode::ErrorCode::SUCCESS
@ SUCCESS
StatusCode::Category::Category
constexpr Category() noexcept=default
StatusCode::isRecoverable
bool isRecoverable() const
Definition: StatusCode.h:319
std::false_type
precedence.message
message
Definition: precedence.py:19
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:164
StatusCode::orThrow
const StatusCode & orThrow(std::string_view message, std::string_view tag) const
Throw a GaudiException in case of failures.
Definition: StatusCode.h:207
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:315
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:343
StatusCode::message
std::string message() const
Description (or name) of StatusCode value.
Definition: StatusCode.h:225
StatusCode::orElse
StatusCode orElse(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a failure result.
Definition: StatusCode.h:186
StatusCode::m_code
code_t m_code
The status code value.
Definition: StatusCode.h:261
StatusCode::operator!=
friend bool operator!=(const StatusCode &lhs, const StatusCode &rhs)
Definition: StatusCode.h:236
StatusCode::orThrow
const StatusCode & orThrow(std::string_view tag="") const
Throw a GaudiException in case of failures.
Definition: StatusCode.h:216
StatusCode::code_t
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:67
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:270
StatusCode::operator<
friend bool operator<(const StatusCode &lhs, const StatusCode &rhs)
Comparison (values are grouped by category first)
Definition: StatusCode.h:239
operator==
bool operator==(const StatusCode &lhs, const StatusCode &rhs)
Definition: StatusCode.h:326
gaudirun.default
default
Definition: gaudirun.py:188
StatusCode::operator&
friend StatusCode operator&(StatusCode lhs, const StatusCode &rhs)
Ternary AND operator.
Definition: StatusCode.h:248
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:113
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:251
StatusCode::default_value
ErrorCode default_value() const
Project onto the default StatusCode values.
Definition: StatusCode.h:321
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:227
StatusCode::operator&=
friend bool & operator&=(bool &lhs, const StatusCode &sc)
Boolean AND assignment operator.
Definition: StatusCode.h:254
StatusCode::operator&=
StatusCode & operator&=(const StatusCode &rhs)
Ternary logic operator with RECOVERABLE being the "third" state.
Definition: StatusCode.h:333
GaudiPython.Bindings.SUCCESS
SUCCESS
Definition: Bindings.py:83
StatusCode::m_cat
const Category * m_cat
The status code category.
Definition: StatusCode.h:260
StatusCode::operator|=
friend bool & operator|=(bool &lhs, const StatusCode &sc)
Boolean OR assignment operator.
Definition: StatusCode.h:257
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:140
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:130
StatusCode::ErrorCode
ErrorCode
Definition: StatusCode.h:69
StatusCode::ignore
StatusCode & ignore()
Definition: StatusCode.h:141
STATUSCODE_ENUM_DECL
#define STATUSCODE_ENUM_DECL(ENUM)
Declare an enum to be used as StatusCode value.
Definition: StatusCode.h:287
gaudirun.l
dictionary l
Definition: gaudirun.py:583
StatusCode::ErrorCode::RECOVERABLE
@ RECOVERABLE
gaudirun.args
args
Definition: gaudirun.py:336
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:116
StatusCode::ErrorCode::FAILURE
@ FAILURE
StatusCode::getCode
code_t getCode() const
Retrieve value.
Definition: StatusCode.h:137
StatusCode::StatusCode
StatusCode(StatusCode &&rhs) noexcept=default
Move constructor.
StatusCode::Category::name
virtual const char * name() const =0
Name of the category.
Gaudi::Functional::details::requires
requires(is_optional_v< OptOut >) void put(const OutHandle &out_handle
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