zpp
Zephyr C++20 Framework
utils.hpp
Go to the documentation of this file.
1 
7 #ifndef ZPP_INCLUDE_ZPP_UTILS_HPP
8 #define ZPP_INCLUDE_ZPP_UTILS_HPP
9 
10 #include <cstdint>
11 
12 namespace zpp {
13 
20 template<class T_LHS, class T_RHS>
21 constexpr bool operator==(const T_LHS& lhs, const T_RHS& rhs) noexcept
22 {
23  if constexpr (std::is_pointer_v<T_LHS> && std::is_pointer_v<T_RHS>) {
24  return lhs == rhs;
25  }
26 
27  if constexpr (std::is_pointer_v<T_LHS> && !std::is_pointer_v<T_RHS>) {
28  return lhs == rhs.native_handle();
29  }
30 
31  if constexpr (!std::is_pointer_v<T_LHS> && std::is_pointer_v<T_RHS>) {
32  return lhs.native_handle() == rhs;
33  }
34 
35  if constexpr (!std::is_pointer_v<T_LHS> && !std::is_pointer_v<T_RHS>) {
36  return lhs.native_handle() == rhs.native_handle();
37  }
38 
39  static_assert(true, "should never be reached");
40  return true;
41 }
42 
49 template<class T_LHS, class T_RHS>
50 constexpr bool operator!=(const T_LHS& lhs, const T_RHS& rhs) noexcept
51 {
52  if constexpr (std::is_pointer_v<T_LHS> && std::is_pointer_v<T_RHS>) {
53  return lhs != rhs;
54  }
55 
56  if constexpr (std::is_pointer_v<T_LHS> && !std::is_pointer_v<T_RHS>) {
57  return lhs != rhs.native_handle();
58  }
59 
60  if constexpr (!std::is_pointer_v<T_LHS> && std::is_pointer_v<T_RHS>) {
61  return lhs.native_handle() != rhs;
62  }
63 
64  if constexpr (!std::is_pointer_v<T_LHS> && !std::is_pointer_v<T_RHS>) {
65  return lhs.native_handle() != rhs.native_handle();
66  }
67 
68  static_assert(true, "should never be reached");
69  return false;
70 }
71 
79 consteval uint32_t power_of_two(uint32_t power) noexcept
80 {
81  uint32_t res = 1;
82 
83  for (uint32_t i = 1; i <= power; ++i) {
84  res *= 2;
85  }
86 
87  return res;
88 }
89 
97 consteval bool is_power_of_two(uint32_t value) noexcept
98 {
99  uint32_t power {0};
100  uint32_t calc_val;
101 
102  do {
103  calc_val = power_of_two(power++);
104  } while (calc_val < value);
105 
106  return calc_val == value;
107 }
108 
117 consteval bool is_multiple_of(uint32_t value, uint32_t base)
118 {
119  if (value == 0 || base == 0) {
120  return false;
121  } else {
122  return (value % base) == 0;
123  }
124 }
125 
126 } // namespace zpp
127 
128 #endif // ZPP_INCLUDE_ZPP_UTILS_HPP
consteval bool is_power_of_two(uint32_t value) noexcept
check if a value is a power of two
Definition: utils.hpp:97
constexpr bool operator==(const result< T_Ok, T_Error > &lhs, bool rhs) noexcept
compare result with bool
Definition: result.hpp:655
consteval bool is_multiple_of(uint32_t value, uint32_t base)
Check if a value is a multiple of another value.
Definition: utils.hpp:117
consteval uint32_t power_of_two(uint32_t power) noexcept
calculate a power of 2
Definition: utils.hpp:79
constexpr bool operator!=(const result< T_Ok, T_Error > &lhs, bool rhs) noexcept
compare result with bool
Definition: result.hpp:681