My Project
Public Types | Public Member Functions | Related Functions | List of all members
bsl::basic_errc_type< T, SUCCESS > Class Template Referencefinal

Defines an error code. We do not use the same pattern as the standard library. The goal is to ensure an error code can consume a single register to ensure maximum compatibility with different CPU archiectures that only have a 32bit return register. We also do not use an enum to ensure custom error codes can be created. This also means there are no error code categories. Instead, an error code is checked if it is negative, and unchecked if it is positive to align with AUTOSAR. Finally, we provide the ability to change the type that an error code uses under the hood which allows you to use a "long" type, or some other integer type depending on your requirements (i.e., NTSTATUS). More...

#include <basic_errc_type.hpp>

Public Types

using value_type = T
 alias for: T
 
using reference_type = T &
 alias for: T &
 
using const_reference_type = T const &
 alias for: T const &
 

Public Member Functions

constexpr basic_errc_type (value_type const &errc=SUCCESS) noexcept
 Value initialization constructor. More...
 
 ~basic_errc_type () noexcept=default
 Destroyes a previously created bsl::basic_errc_type.
 
constexpr basic_errc_type (basic_errc_type const &o) noexcept=default
 copy constructor More...
 
constexpr basic_errc_type (basic_errc_type &&o) noexcept=default
 move constructor More...
 
constexpr basic_errc_typeoperator= (basic_errc_type const &o) &noexcept=default
 copy assignment More...
 
constexpr basic_errc_typeoperator= (basic_errc_type &&o) &noexcept=default
 move assignment More...
 
template<typename O >
 basic_errc_type (O val) noexcept=delete
 This constructor allows for single argument constructors without the need to mark them as explicit as it will absorb any incoming potential implicit conversion and prevent it. More...
 
constexpr const_reference_type get () const noexcept
 Returns the integer value that represents the error code. Normally, this function should not be used, and instead, you should use the other functions like ==, !=, operator bool(), is_checked() and is_unchecked(). More...
 
constexpr bool success () const noexcept
 Returns true if the error code contains SUCCESS, otherwise, if the error code contains an error code, returns false. More...
 
constexpr bool failure () const noexcept
 Returns true if the error code contains an error code, otherwise, if the error code contains SUCCESS, returns false. More...
 
constexpr bool is_checked () const noexcept
 Returns true if the error code is a checked error (i.e., that is the error code is negative), false otherwise. If this error code is bsl::errc_success, returns false. More...
 
constexpr bool is_unchecked () const noexcept
 Returns true if the error code is an unchecked error (i.e., that is the error code is positive), false otherwise. If this error code is bsl::errc_success, returns false. More...
 
constexpr bsl::string_view message () const noexcept
 Returns a human readable version of the error code. If the error code is a custom, user defined error code, returns a nullptr. More...
 

Related Functions

(Note that these are not member functions.)

using errc_type = basic_errc_type<>
 provides the default errc_type prototype More...
 
template<typename T1 , T1 SUCCESS1, typename T2 , T2 SUCCESS2>
constexpr bool operator== (basic_errc_type< T1, SUCCESS1 > const &lhs, basic_errc_type< T2, SUCCESS2 > const &rhs) noexcept
 Returns true if the lhs is equal to the rhs, false otherwise. More...
 
template<typename T1 , T1 SUCCESS1, typename T2 , T2 SUCCESS2>
constexpr bool operator!= (basic_errc_type< T1, SUCCESS1 > const &lhs, basic_errc_type< T2, SUCCESS2 > const &rhs) noexcept
 Returns false if the lhs is equal to the rhs, true otherwise. More...
 

Detailed Description

template<typename T = bsl::int32, T SUCCESS = 0>
class bsl::basic_errc_type< T, SUCCESS >

Defines an error code. We do not use the same pattern as the standard library. The goal is to ensure an error code can consume a single register to ensure maximum compatibility with different CPU archiectures that only have a 32bit return register. We also do not use an enum to ensure custom error codes can be created. This also means there are no error code categories. Instead, an error code is checked if it is negative, and unchecked if it is positive to align with AUTOSAR. Finally, we provide the ability to change the type that an error code uses under the hood which allows you to use a "long" type, or some other integer type depending on your requirements (i.e., NTSTATUS).

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_basic_errc_type_overview() noexcept
{
bsl::print() << "success\n";
}
bsl::print() << "success\n";
}
constexpr errc_type my_errc{42};
if (my_errc.failure()) {
bsl::print() << "success\n";
}
if (my_errc.is_unchecked()) {
bsl::print() << "success\n";
}
}
}

Constructor & Destructor Documentation

◆ basic_errc_type() [1/4]

template<typename T = bsl::int32, T SUCCESS = 0>
constexpr bsl::basic_errc_type< T, SUCCESS >::basic_errc_type ( value_type const &  errc = SUCCESS)
inlinenoexcept

Value initialization constructor.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_basic_errc_type_constructor_t() noexcept
{
constexpr bsl::int32 errc{42};
constexpr basic_errc_type<> my_errc{errc};
if (my_errc.get() == errc) {
bsl::print() << "success\n";
}
}
}

SUPPRESSION: PRQA 2180 - exception required

  • We suppress this because A12-1-4 states that all constructors that are callable from a fundamental type should be marked as explicit. This is a fundamental type, but all implicit conversions are disabled through the use of the implicit general template constructor that is deleted which absorbs all incoming potential implicit conversions.
Parameters
errcthe error code to store

◆ basic_errc_type() [2/4]

template<typename T = bsl::int32, T SUCCESS = 0>
constexpr bsl::basic_errc_type< T, SUCCESS >::basic_errc_type ( basic_errc_type< T, SUCCESS > const &  o)
defaultnoexcept

copy constructor

Parameters
othe object being copied

◆ basic_errc_type() [3/4]

template<typename T = bsl::int32, T SUCCESS = 0>
constexpr bsl::basic_errc_type< T, SUCCESS >::basic_errc_type ( basic_errc_type< T, SUCCESS > &&  o)
defaultnoexcept

move constructor

Parameters
othe object being moved

◆ basic_errc_type() [4/4]

template<typename T = bsl::int32, T SUCCESS = 0>
template<typename O >
bsl::basic_errc_type< T, SUCCESS >::basic_errc_type ( val)
deletenoexcept

This constructor allows for single argument constructors without the need to mark them as explicit as it will absorb any incoming potential implicit conversion and prevent it.

SUPPRESSION: PRQA 2180 - false positive

  • We suppress this because A12-1-4 states that all constructors that are callable from a fundamental type should be marked as explicit. This is callable with a fundamental type, but it is marked as "delete" which means it does not apply.
Template Parameters
Othe type that could be implicitly converted
Parameters
valthe value that could be implicitly converted

Member Function Documentation

◆ operator=() [1/2]

template<typename T = bsl::int32, T SUCCESS = 0>
constexpr basic_errc_type& bsl::basic_errc_type< T, SUCCESS >::operator= ( basic_errc_type< T, SUCCESS > const &  o) &
defaultnoexcept

copy assignment

Parameters
othe object being copied
Returns
a reference to *this

◆ operator=() [2/2]

template<typename T = bsl::int32, T SUCCESS = 0>
constexpr basic_errc_type& bsl::basic_errc_type< T, SUCCESS >::operator= ( basic_errc_type< T, SUCCESS > &&  o) &
defaultnoexcept

move assignment

Parameters
othe object being moved
Returns
a reference to *this

◆ get()

template<typename T = bsl::int32, T SUCCESS = 0>
constexpr const_reference_type bsl::basic_errc_type< T, SUCCESS >::get ( ) const
inlinenoexcept

Returns the integer value that represents the error code. Normally, this function should not be used, and instead, you should use the other functions like ==, !=, operator bool(), is_checked() and is_unchecked().

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_basic_errc_type_get() noexcept
{
constexpr bsl::int32 errc{42};
constexpr basic_errc_type<> my_errc{errc};
if (my_errc.get() == errc) {
bsl::print() << "success\n";
}
}
}
Returns
Returns the integer value that represents the error code.

◆ success()

template<typename T = bsl::int32, T SUCCESS = 0>
constexpr bool bsl::basic_errc_type< T, SUCCESS >::success ( ) const
inlinenoexcept

Returns true if the error code contains SUCCESS, otherwise, if the error code contains an error code, returns false.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_basic_errc_type_success() noexcept
{
bsl::print() << "success\n";
}
}
}
Returns
Returns true if the error code contains SUCCESS, otherwise, if the error code contains an error code, returns false.

◆ failure()

template<typename T = bsl::int32, T SUCCESS = 0>
constexpr bool bsl::basic_errc_type< T, SUCCESS >::failure ( ) const
inlinenoexcept

Returns true if the error code contains an error code, otherwise, if the error code contains SUCCESS, returns false.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_basic_errc_type_failure() noexcept
{
constexpr basic_errc_type<> my_errc{42};
if (my_errc.failure()) {
bsl::print() << "success\n";
}
}
}
Returns
Returns true if the error code contains an error code, otherwise, if the error code contains SUCCESS, returns false.

◆ is_checked()

template<typename T = bsl::int32, T SUCCESS = 0>
constexpr bool bsl::basic_errc_type< T, SUCCESS >::is_checked ( ) const
inlinenoexcept

Returns true if the error code is a checked error (i.e., that is the error code is negative), false otherwise. If this error code is bsl::errc_success, returns false.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_basic_errc_type_is_checked() noexcept
{
constexpr basic_errc_type<> my_errc{-42};
if (my_errc.is_checked()) {
bsl::print() << "success\n";
}
}
}
Returns
Returns true if the error code is a checked error (i.e., that is the error code is negative), false otherwise. If this error code is bsl::errc_success, returns false.

◆ is_unchecked()

template<typename T = bsl::int32, T SUCCESS = 0>
constexpr bool bsl::basic_errc_type< T, SUCCESS >::is_unchecked ( ) const
inlinenoexcept

Returns true if the error code is an unchecked error (i.e., that is the error code is positive), false otherwise. If this error code is bsl::errc_success, returns false.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_basic_errc_type_is_unchecked() noexcept
{
constexpr basic_errc_type<> my_errc{42};
if (my_errc.is_unchecked()) {
bsl::print() << "success\n";
}
}
}
Returns
Returns true if the error code is an unchecked error (i.e., that is the error code is positive), false otherwise. If this error code is bsl::errc_success, returns false.

◆ message()

template<typename T , T SUCCESS>
constexpr bsl::string_view bsl::basic_errc_type< T, SUCCESS >::message ( ) const
noexcept

Returns a human readable version of the error code. If the error code is a custom, user defined error code, returns a nullptr.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_basic_errc_type_message() noexcept
{
}
}
Returns
Returns a human readable version of the error code. If the error code is a custom, user defined error code, returns a nullptr.

Friends And Related Function Documentation

◆ errc_type

template<typename T = bsl::int32, T SUCCESS = 0>
using errc_type = basic_errc_type<>
related

provides the default errc_type prototype

◆ operator==()

template<typename T1 , T1 SUCCESS1, typename T2 , T2 SUCCESS2>
constexpr bool operator== ( basic_errc_type< T1, SUCCESS1 > const &  lhs,
basic_errc_type< T2, SUCCESS2 > const &  rhs 
)
related

Returns true if the lhs is equal to the rhs, false otherwise.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_basic_errc_type_equals() noexcept
{
constexpr bsl::int32 errc1{42};
constexpr bsl::int32 errc2{42};
constexpr basic_errc_type<> my_errc1{errc1};
constexpr basic_errc_type<> my_errc2{errc2};
if (my_errc1 == my_errc2) {
bsl::print() << "success\n";
}
}
}
Parameters
lhsthe left hand side of the operator
rhsthe right hand side of the operator
Returns
Returns true if the lhs is equal to the rhs, false otherwise

◆ operator!=()

template<typename T1 , T1 SUCCESS1, typename T2 , T2 SUCCESS2>
constexpr bool operator!= ( basic_errc_type< T1, SUCCESS1 > const &  lhs,
basic_errc_type< T2, SUCCESS2 > const &  rhs 
)
related

Returns false if the lhs is equal to the rhs, true otherwise.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_basic_errc_type_not_equals() noexcept
{
constexpr bsl::int32 errc1{23};
constexpr bsl::int32 errc2{42};
constexpr basic_errc_type<> my_errc1{errc1};
constexpr basic_errc_type<> my_errc2{errc2};
if (my_errc1 != my_errc2) {
bsl::print() << "success\n";
}
}
}
Parameters
lhsthe left hand side of the operator
rhsthe right hand side of the operator
Returns
Returns false if the lhs is equal to the rhs, true otherwise

The documentation for this class was generated from the following files: