Skip to content

Commit 661ece0

Browse files
committed
Add mbed::byte as per C++17 std::byte
1 parent 1191ad0 commit 661ece0

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

platform/mbed_cxxsupport.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,87 @@ template<class T>
737737
void as_const(T &&) = delete;
738738
#endif
739739

740+
/* C++17 byte */
741+
#if __cplusplus >= 201703 || __cpp_lib_byte >= 201603
742+
using std::byte;
743+
#else
744+
enum class byte : unsigned char {};
745+
746+
template <class IntType>
747+
constexpr std::enable_if_t<std::is_integral<IntType>::value,
748+
byte &> operator<<=(byte &b, IntType shift) noexcept
749+
{
750+
return b = byte(static_cast<unsigned char>(b) << shift);
751+
}
752+
753+
template <class IntType>
754+
constexpr std::enable_if_t<std::is_integral<IntType>::value,
755+
byte> operator<<(byte b, IntType shift) noexcept
756+
{
757+
return byte(static_cast<unsigned char>(b) << shift);
758+
}
759+
760+
template <class IntType>
761+
constexpr std::enable_if_t<std::is_integral<IntType>::value,
762+
byte &> operator>>=(byte &b, IntType shift) noexcept {
763+
return b = byte(static_cast<unsigned char>(b) >> shift);
764+
}
765+
766+
template <class IntType>
767+
constexpr std::enable_if_t<std::is_integral<IntType>::value,
768+
byte> operator>>(byte b, IntType shift) noexcept
769+
{
770+
return byte(static_cast<unsigned char>(b) >> shift);
771+
}
772+
773+
template <class IntType>
774+
constexpr byte &operator|=(byte &l, byte r) noexcept
775+
{
776+
return l = byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
777+
}
778+
779+
template <class IntType>
780+
constexpr byte operator|(byte &l, byte r) noexcept {
781+
return byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
782+
}
783+
784+
template <class IntType>
785+
constexpr byte &operator&=(byte &l, byte r) noexcept
786+
{
787+
return l = byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
788+
}
789+
790+
template <class IntType>
791+
constexpr byte operator&(byte &l, byte r) noexcept
792+
{
793+
return byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
794+
}
795+
796+
template <class IntType>
797+
constexpr byte &operator^=(byte &l, byte r) noexcept
798+
{
799+
return l = byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
800+
}
801+
802+
template <class IntType>
803+
constexpr byte operator^(byte &l, byte r) noexcept
804+
{
805+
return byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
806+
}
807+
808+
template <class IntType>
809+
constexpr byte operator~(byte b) noexcept
810+
{
811+
return byte(~static_cast<unsigned char>(b));
812+
}
813+
814+
template <class IntType>
815+
constexpr std::enable_if_t<std::is_integral<IntType>::value,
816+
IntType> to_integer(byte b) noexcept
817+
{
818+
return IntType(b);
819+
}
820+
#endif
740821
} // namespace mbed
741822

742823
#endif // MBED_CXXSUPPORT_H

0 commit comments

Comments
 (0)