Skip to content

Commit f2ce0a2

Browse files
committed
Add mbed::byte as per C++17 std::byte
1 parent 12424a9 commit f2ce0a2

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
@@ -740,6 +740,87 @@ template<class T>
740740
void as_const(T &&) = delete;
741741
#endif
742742

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

745826
#endif // MBED_CXXSUPPORT_H

0 commit comments

Comments
 (0)