My Project
basic_errc_type.hpp
Go to the documentation of this file.
1 
28 #ifndef BSL_BASIC_ERRC_TYPE_HPP
29 #define BSL_BASIC_ERRC_TYPE_HPP
30 
31 #include "cstdint.hpp"
32 #include "string_view.hpp"
33 #include "discard.hpp"
34 #include "move.hpp"
35 
36 namespace bsl
37 {
54  template<typename T = bsl::int32, T SUCCESS = 0>
55  class basic_errc_type final
56  {
57  public:
59  using value_type = T;
61  using reference_type = T &;
63  using const_reference_type = T const &;
64 
80  constexpr basic_errc_type( // PRQA S 2180 // NOLINT
81  value_type const &errc = SUCCESS) noexcept
82  : m_errc{errc}
83  {}
84 
88  ~basic_errc_type() noexcept = default;
89 
96  constexpr basic_errc_type(basic_errc_type const &o) noexcept = default;
97 
104  constexpr basic_errc_type(basic_errc_type &&o) noexcept = default;
105 
113  constexpr basic_errc_type &operator=(basic_errc_type const &o) &noexcept = default;
114 
122  constexpr basic_errc_type &operator=(basic_errc_type &&o) &noexcept = default;
123 
139  template<typename O>
140  basic_errc_type(O val) noexcept = delete; // PRQA S 2180
141 
152  [[nodiscard]] constexpr const_reference_type
153  get() const noexcept
154  {
155  return m_errc;
156  }
157 
169  [[nodiscard]] constexpr bool
170  success() const noexcept
171  {
172  return m_errc == SUCCESS;
173  }
174 
186  [[nodiscard]] constexpr bool
187  failure() const noexcept
188  {
189  return m_errc != SUCCESS;
190  }
191 
203  [[nodiscard]] constexpr bool
204  is_checked() const noexcept
205  {
206  return m_errc < 0;
207  }
208 
220  [[nodiscard]] constexpr bool
221  is_unchecked() const noexcept
222  {
223  return m_errc > 0;
224  }
225 
237  [[nodiscard]] constexpr bsl::string_view message() const noexcept;
238 
239  private:
241  T m_errc;
242  };
243 
254  template<typename T1, T1 SUCCESS1, typename T2, T2 SUCCESS2>
255  constexpr bool
256  operator==(
257  basic_errc_type<T1, SUCCESS1> const &lhs, basic_errc_type<T2, SUCCESS2> const &rhs) noexcept
258  {
259  return lhs.get() == rhs.get();
260  }
261 
272  template<typename T1, T1 SUCCESS1, typename T2, T2 SUCCESS2>
273  constexpr bool
275  basic_errc_type<T1, SUCCESS1> const &lhs, basic_errc_type<T2, SUCCESS2> const &rhs) noexcept
276  {
277  return !(lhs == rhs);
278  }
279 }
280 
281 // -----------------------------------------------------------------------------
282 // Pre-defined Error Codes
283 // -----------------------------------------------------------------------------
284 
285 namespace bsl
286 {
297 
304 
315 }
316 
317 // -----------------------------------------------------------------------------
318 // Implementation
319 // -----------------------------------------------------------------------------
320 
321 namespace bsl
322 {
334  template<typename T, T SUCCESS>
335  [[nodiscard]] constexpr bsl::string_view
337  {
338  bsl::string_view msg{};
339 
340  switch (m_errc) {
341  case errc_success.get(): {
342  msg = "success";
343  break;
344  }
345 
346  case errc_failure.get(): {
347  msg = "general failure";
348  break;
349  }
350 
351  case errc_precondition.get(): {
352  msg = "general precondition failure";
353  break;
354  }
355 
356  case errc_postcondition.get(): {
357  msg = "general postcondition failure";
358  break;
359  }
360 
361  case errc_assetion.get(): {
362  msg = "general assertion failure";
363  break;
364  }
365 
366  case errc_invalid_argument.get(): {
367  msg = "invalid argument (precondition) failure";
368  break;
369  }
370 
371  case errc_index_out_of_bounds.get(): {
372  msg = "index out of bounds (precondition) failure";
373  break;
374  }
375 
376  case errc_bad_function.get(): {
377  msg = "function not callable (precondition) failure";
378  break;
379  }
380 
381  case errc_unsigned_wrap.get(): {
382  msg = "unsigned wrap (assertion) failure";
383  break;
384  }
385 
386  case errc_narrow_overflow.get(): {
387  msg = "narrow overflow (assertion) failure";
388  break;
389  }
390 
391  case errc_signed_overflow.get(): {
392  msg = "signed overflow (assertion) failure";
393  break;
394  }
395 
396  case errc_divide_by_zero.get(): {
397  msg = "divide by zero (assertion) failure";
398  break;
399  }
400 
401  case errc_nullptr_dereference.get(): {
402  msg = "null dereference (assertion) failure";
403  break;
404  }
405 
406  default: {
407  break;
408  }
409  }
410 
411  return msg;
412  }
413 }
414 
415 #endif
constexpr basic_errc_type errc_index_out_of_bounds
Defines an out of bounds error code.
Definition: basic_errc_type.hpp:301
constexpr const_reference_type get() const noexcept
Returns the integer value that represents the error code. Normally, this function should not be used,...
Definition: basic_errc_type.hpp:153
T const & const_reference_type
alias for: T const &
Definition: basic_errc_type.hpp:63
constexpr bool success() const noexcept
Returns true if the error code contains SUCCESS, otherwise, if the error code contains an error code,...
Definition: basic_errc_type.hpp:170
constexpr basic_errc_type errc_narrow_overflow
Defines an overflow, underflow or unsigned wrap error.
Definition: basic_errc_type.hpp:308
constexpr basic_errc_type errc_bad_function
Defines an out of bounds error code.
Definition: basic_errc_type.hpp:303
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),...
Definition: basic_errc_type.hpp:221
constexpr basic_errc_type errc_signed_overflow
Defines an overflow, underflow or unsigned wrap error.
Definition: basic_errc_type.hpp:310
constexpr basic_errc_type errc_failure
Defines the general unchecked error case.
Definition: basic_errc_type.hpp:290
~basic_errc_type() noexcept=default
Destroyes a previously created bsl::basic_errc_type.
Defines an error code. We do not use the same pattern as the standard library. The goal is to ensure ...
Definition: basic_errc_type.hpp:55
T value_type
alias for: T
Definition: basic_errc_type.hpp:59
constexpr bsl::string_view message() const noexcept
Returns a human readable version of the error code. If the error code is a custom,...
Definition: basic_errc_type.hpp:336
T & reference_type
alias for: T &
Definition: basic_errc_type.hpp:61
constexpr basic_errc_type errc_postcondition
Defines the general postcondition error case.
Definition: basic_errc_type.hpp:294
constexpr basic_errc_type errc_success
Defines the "no error" case.
Definition: basic_errc_type.hpp:288
constexpr basic_errc_type errc_divide_by_zero
Defines a divide by zero error.
Definition: basic_errc_type.hpp:312
constexpr basic_errc_type(value_type const &errc=SUCCESS) noexcept
Value initialization constructor.
Definition: basic_errc_type.hpp:80
constexpr basic_errc_type errc_unsigned_wrap
Defines an overflow, underflow or unsigned wrap error.
Definition: basic_errc_type.hpp:306
constexpr basic_errc_type errc_invalid_argument
Defines an invalid argument error code.
Definition: basic_errc_type.hpp:299
constexpr basic_errc_type errc_precondition
Defines the general precondition error case.
Definition: basic_errc_type.hpp:292
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.
Definition: basic_errc_type.hpp:274
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),...
Definition: basic_errc_type.hpp:204
constexpr basic_errc_type errc_nullptr_dereference
Defines an out of bounds error code.
Definition: basic_errc_type.hpp:314
constexpr basic_errc_type errc_assetion
Defines the general assertion error case.
Definition: basic_errc_type.hpp:296
constexpr bool failure() const noexcept
Returns true if the error code contains an error code, otherwise, if the error code contains SUCCESS,...
Definition: basic_errc_type.hpp:187