11 #ifndef GAUDIKERNEL_STATUSCODE_H 12 #define GAUDIKERNEL_STATUSCODE_H 14 #include "boost/preprocessor/facilities/overload.hpp" 17 #include <type_traits> 62 #if __cplusplus >= 201703L && !defined( __CLING__ ) 79 constexpr
Category() noexcept =
default;
83 virtual const char*
name()
const = 0;
93 virtual bool isRecoverable(
code_t code )
const {
return code == static_cast<code_t>( ErrorCode::RECOVERABLE ); }
97 static const Category& default_category() noexcept;
102 constexpr const static auto RECOVERABLE =
ErrorCode::RECOVERABLE;
116 bool checked =
false ) noexcept
117 : m_cat( &cat ), m_code( code ), m_checked( checked ) {}
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;
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;
134 if (
UNLIKELY( s_checking ) ) check();
140 m_checked = std::exchange( rhs.m_checked,
true );
144 bool isSuccess()
const;
146 bool isRecoverable()
const;
149 explicit operator bool()
const {
return isSuccess(); }
197 template <
typename F,
typename... ARGS>
199 if ( isFailure() )
return *
this;
200 return i_invoke( std::forward<F>( f ), std::forward<ARGS>(
args )... );
219 template <
typename F,
typename... ARGS>
221 if ( isSuccess() )
return *
this;
222 return i_invoke( std::forward<F>( f ), std::forward<ARGS>(
args )... );
309 mutable bool m_checked{
false};
319 template <
typename F,
typename... ARGS,
typename = std::enable_if_t<std::is_invocable_v<F, ARGS...>>>
321 if constexpr ( std::is_invocable_r_v<StatusCode, F, ARGS...> ) {
322 return std::invoke( std::forward<F>( f ), std::forward<ARGS>(
args )... );
325 std::invoke( std::forward<F>( f ), std::forward<ARGS>(
args )... );
337 #define STATUSCODE_ENUM_DECL( ENUM ) \ 339 struct is_StatusCode_enum<ENUM> : std::true_type { \ 340 static const StatusCode::Category& instance; \ 346 #define STATUSCODE_ENUM_IMPL( ... ) BOOST_PP_OVERLOAD( STATUSCODE_ENUM_IMPL_, __VA_ARGS__ )( __VA_ARGS__ ) 348 #define STATUSCODE_ENUM_IMPL_1( ENUM ) \ 349 const StatusCode::Category& is_StatusCode_enum<ENUM>::instance = StatusCode::default_category(); 351 #define STATUSCODE_ENUM_IMPL_2( ENUM, CATEGORY ) \ 352 const StatusCode::Category& is_StatusCode_enum<ENUM>::instance = CATEGORY{}; 425 #endif // GAUDIKERNEL_STATUSCODE_H code_t getCode() const
Retrieve value ("checks" the StatusCode)
bool checked() const
Has the StatusCode been checked?
StatusCode operator|(StatusCode lhs, const StatusCode &rhs)
Ternary OR operator.
code_t m_code
The status code value.
friend bool operator<(const StatusCode &lhs, const StatusCode &rhs)
Comparison (values are grouped by category first)
bool m_checked
If the StatusCode has been checked.
The category assigned to a StatusCode.
StatusCode & operator=(const StatusCode &rhs) noexcept
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)
StatusCode(code_t code, bool checked) noexcept
Constructor from code_t and category (explicit conversion only)
static GAUDI_API void enableChecking()
StatusCode(const StatusCode &rhs) noexcept
Copy constructor.
StatusCode & setChecked(bool checked=true)
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
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...
const StatusCode::Category & getCategory() const
Retrieve category (does not "check" the StatusCode)
virtual bool isSuccess(code_t code) const
Is code considered success ?
friend std::ostream & operator<<(std::ostream &s, const StatusCode &sc)
#define STATUSCODE_ENUM_DECL(ENUM)
Declare an enum to be used as StatusCode value.
virtual std::string message(code_t code) const
Description for code within this category.
This class is used for returning status codes from appropriate routines.
virtual bool isRecoverable(code_t code) const
Is code considered recoverable ?
friend bool operator!=(const StatusCode &lhs, const StatusCode &rhs)
Simple RAII class to ignore unchecked StatusCode instances in a scope.
static bool s_checking
Global flag to control if StatusCode need to be checked.
std::string message() const
Description (or name) of StatusCode value.
StatusCode operator &(StatusCode lhs, const StatusCode &rhs)
Ternary AND operator.
bool operator==(const StatusCode &lhs, const StatusCode &rhs)
const StatusCode & ignore() const
Ignore/check StatusCode.
StatusCode orThrow(std::string message, std::string tag) const
Throw a GaudiException in case of failures.
bool & operator|=(bool &lhs, const StatusCode &sc)
Boolean OR assignment operator.
bool isRecoverable() const
static GAUDI_API void disableChecking()
bool & operator&=(bool &lhs, const StatusCode &sc)
Boolean AND assignment operator.
StatusCode(StatusCode &&rhs) noexcept
Move constructor.
void i_doThrow(std::string message, std::string tag) const
Helper function to avoid circular dependency between GaudiException.h and StatusCode....
const StatusCode & setChecked(bool checked=true) const
Check/uncheck StatusCode.
ErrorCode default_value() const
Project onto the default StatusCode values.
const Category * m_cat
The status code category.
StatusCode & operator|=(const StatusCode &rhs)
Ternary logic operator with RECOVERABLE being the "third" state.
StatusCode & operator&=(const StatusCode &rhs)
Ternary logic operator with RECOVERABLE being the "third" state.
StatusCode orElse(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a failure result.
unsigned long code_t
type of StatusCode value