emlabcpp
modern opinionated embedded C++ library
error_code.h
Go to the documentation of this file.
1 #pragma once
23 
24 #include <concepts>
25 #include <cstdint>
26 #include <type_traits>
27 
28 namespace emlabcpp
29 {
30 
31 using error_value_type = uint32_t;
32 
34 {
35  [[nodiscard]]
36  virtual char const* message( error_value_type ) const noexcept = 0;
37  virtual ~_error_category() = default;
38 };
39 
40 template < typename T >
42 {
43  [[nodiscard]] constexpr error_value_type cast( T& x ) const
44  {
45  return static_cast< error_value_type >( x );
46  }
47 };
48 
49 template < typename T >
51 {
52  static_assert(
53  sizeof( T ) == 0,
54  "Undefined error category used. Specialize error_category for this type." );
55 };
56 
57 template < typename T >
59 
60 template < typename T >
61 concept error_type = requires { error_category_v< T >; };
62 
64 {
65  [[nodiscard]] constexpr error_value_type cast( bool x ) const
66  {
67  return x ? 0 : 1;
68  }
69 
70  [[nodiscard]] char const* message( error_value_type code ) const noexcept override
71  {
72  return code ? "true" : "false";
73  }
74 };
75 
76 template <>
78 
79 struct [[nodiscard]] error_code
80 {
81  template < typename T >
82  requires( !std::same_as< std::remove_cvref_t< T >, error_code > && error_type< T > )
83  error_code( T x )
84  : code_( error_category_v< T >.cast( x ) )
85  , category_( &error_category_v< T > )
86  {
87  }
88 
89  constexpr error_code( error_code const& ) noexcept = default;
90  constexpr error_code( error_code&& ) noexcept = default;
91  constexpr error_code& operator=( error_code const& ) noexcept = default;
92  constexpr error_code& operator=( error_code&& ) noexcept = default;
93 
94  [[nodiscard]] constexpr char const* message() const noexcept
95  {
96  return category_->message( code_ );
97  }
98 
99  [[nodiscard]] constexpr _error_category const& category() const noexcept
100  {
101  return *category_;
102  }
103 
104  [[nodiscard]] constexpr operator bool() const noexcept
105  {
106  return code_ == 0;
107  }
108 
109  [[nodiscard]] constexpr error_value_type value() const noexcept
110  {
111  return code_;
112  }
113 
114  [[nodiscard]] constexpr bool operator==( error_code const& ) const noexcept = default;
115 
116  template < typename T >
117  requires( !std::same_as< std::remove_cvref_t< T >, error_code > && error_type< T > )
118  [[nodiscard]] constexpr bool operator==( T x ) const noexcept
119  {
120  return *this == error_code( x );
121  }
122 
123 private:
124  error_value_type code_;
126 };
127 
128 } // namespace emlabcpp
message(Ts... inpt) -> message< sizeof...(Ts) >
MIT License.
Definition: impl.h:31
undefined_error_category< T > const error_category_v
Definition: error_code.h:58
concept error_type
Definition: error_code.h:61
bool_category const error_category_v< bool >
Definition: error_code.h:77
requires(!range_container< Container >) const expr std
Returns index of an element in tuple 't', for which call to predicate f(x) holds true,...
Definition: algorithm.h:127
uint32_t error_value_type
Definition: error_code.h:31
constexpr bool operator==(pose const &x, pose const &y)
compares poses on their position and orientation
Definition: pose.h:93
Definition: error_code.h:34
virtual char const * message(error_value_type) const noexcept=0
virtual ~_error_category()=default
Definition: error_code.h:64
char const * message(error_value_type code) const noexcept override
Definition: error_code.h:70
constexpr error_value_type cast(bool x) const
Definition: error_code.h:65
Definition: error_code.h:42
constexpr error_value_type cast(T &x) const
Definition: error_code.h:43
Definition: error_code.h:80
constexpr bool operator==(error_code const &) const noexcept=default
constexpr error_code(error_code &&) noexcept=default
constexpr error_code(error_code const &) noexcept=default
constexpr error_value_type value() const noexcept
Definition: error_code.h:109
requires(!std::same_as< std::remove_cvref_t< T >, error_code > &&error_type< T >) error_code(T x)
Definition: error_code.h:82
_error_category const * category_
Definition: error_code.h:125
requires(!std::same_as< std::remove_cvref_t< T >, error_code > &&error_type< T >) const expr bool operator
constexpr _error_category const & category() const noexcept
Definition: error_code.h:99
Definition: error_code.h:51