|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#include <type_traits> |
| 6 | + |
| 7 | +namespace libscratchcpp |
| 8 | +{ |
| 9 | + |
| 10 | +// https://andreasfertig.com/blog/2024/01/cpp20-concepts-applied |
| 11 | +// OR operator | |
| 12 | +template<typename T> |
| 13 | +constexpr std::enable_if_t<std::conjunction_v<std::is_enum<T>, std::is_same<bool, decltype(enable_enum_bitmask(std::declval<T>()))>>, T> operator|(const T lhs, const T rhs) |
| 14 | +{ |
| 15 | + using underlying = std::underlying_type_t<T>; |
| 16 | + return static_cast<T>(static_cast<underlying>(lhs) | static_cast<underlying>(rhs)); |
| 17 | +} |
| 18 | + |
| 19 | +// OR assignment operator |= |
| 20 | +template<typename T> |
| 21 | +constexpr std::enable_if_t<std::conjunction_v<std::is_enum<T>, std::is_same<bool, decltype(enable_enum_bitmask(std::declval<T>()))>>, T &> operator|=(T &lhs, const T rhs) |
| 22 | +{ |
| 23 | + using underlying = std::underlying_type_t<T>; |
| 24 | + lhs = static_cast<T>(static_cast<underlying>(lhs) | static_cast<underlying>(rhs)); |
| 25 | + return lhs; |
| 26 | +} |
| 27 | + |
| 28 | +// AND operator & |
| 29 | +template<typename T> |
| 30 | +constexpr std::enable_if_t<std::conjunction_v<std::is_enum<T>, std::is_same<bool, decltype(enable_enum_bitmask(std::declval<T>()))>>, T> operator&(const T lhs, const T rhs) |
| 31 | +{ |
| 32 | + using underlying = std::underlying_type_t<T>; |
| 33 | + return static_cast<T>(static_cast<underlying>(lhs) & static_cast<underlying>(rhs)); |
| 34 | +} |
| 35 | + |
| 36 | +// AND assignment operator &= |
| 37 | +template<typename T> |
| 38 | +constexpr std::enable_if_t<std::conjunction_v<std::is_enum<T>, std::is_same<bool, decltype(enable_enum_bitmask(std::declval<T>()))>>, T &> operator&=(T &lhs, const T rhs) |
| 39 | +{ |
| 40 | + using underlying = std::underlying_type_t<T>; |
| 41 | + lhs = static_cast<T>(static_cast<underlying>(lhs) & static_cast<underlying>(rhs)); |
| 42 | + return lhs; |
| 43 | +} |
| 44 | + |
| 45 | +// NOT operator ~ |
| 46 | +template<typename T> |
| 47 | +constexpr std::enable_if_t<std::conjunction_v<std::is_enum<T>, std::is_same<bool, decltype(enable_enum_bitmask(std::declval<T>()))>>, T> operator~(const T value) |
| 48 | +{ |
| 49 | + using underlying = std::underlying_type_t<T>; |
| 50 | + return static_cast<T>(~static_cast<underlying>(value)); |
| 51 | +} |
| 52 | + |
| 53 | +} // namespace libscratchcpp |
0 commit comments