Loading...
Searching...
No Matches
byte.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2022 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_BYTE_INCLUDED
#define ETL_BYTE_INCLUDED
#include "platform.h"
#include "type_traits.h"
namespace etl
{
#if ETL_USING_CPP11 && !defined(ETL_BYTE_FORCE_CPP03_IMPLEMENTATION)
enum class byte : unsigned char
{
};
//*************************************************************************
//*************************************************************************
template <typename TInteger>
constexpr typename etl::enable_if<etl::is_integral<TInteger>::value, TInteger>::type to_integer(etl::byte b) ETL_NOEXCEPT
{
return TInteger(b);
}
//*************************************************************************
//*************************************************************************
template <typename TInteger>
constexpr typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte>::type operator<<(etl::byte b, TInteger shift) ETL_NOEXCEPT
{
return etl::byte(static_cast<unsigned int>(b) << shift);
}
//*************************************************************************
//*************************************************************************
template <typename TInteger>
constexpr typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte>::type operator>>(etl::byte b, TInteger shift) ETL_NOEXCEPT
{
return etl::byte(static_cast<unsigned int>(b) >> shift);
}
//*************************************************************************
//*************************************************************************
template <typename TInteger>
constexpr typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte&>::type operator<<=(etl::byte& b, TInteger shift) ETL_NOEXCEPT
{
return b = b << shift;
}
//*************************************************************************
//*************************************************************************
template <typename TInteger>
constexpr typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte&>::type operator>>=(etl::byte& b, TInteger shift) ETL_NOEXCEPT
{
return b = b >> shift;
}
//*************************************************************************
//*************************************************************************
inline constexpr etl::byte operator|(etl::byte lhs, etl::byte rhs) ETL_NOEXCEPT
{
return etl::byte(static_cast<unsigned int>(lhs) | static_cast<unsigned int>(rhs));
}
//*************************************************************************
//*************************************************************************
inline constexpr etl::byte operator&(etl::byte lhs, etl::byte rhs) ETL_NOEXCEPT
{
return etl::byte(static_cast<unsigned int>(lhs) & static_cast<unsigned int>(rhs));
}
//*************************************************************************
//*************************************************************************
inline constexpr etl::byte operator^(etl::byte lhs, etl::byte rhs) ETL_NOEXCEPT
{
return etl::byte(static_cast<unsigned int>(lhs) ^ static_cast<unsigned int>(rhs));
}
//*************************************************************************
//*************************************************************************
inline ETL_CONSTEXPR14 etl::byte& operator|=(etl::byte& lhs, etl::byte rhs) ETL_NOEXCEPT
{
return lhs = lhs | rhs;
}
//*************************************************************************
//*************************************************************************
inline ETL_CONSTEXPR14 etl::byte& operator&=(etl::byte& lhs, etl::byte rhs) ETL_NOEXCEPT
{
return lhs = lhs & rhs;
}
//*************************************************************************
//*************************************************************************
inline ETL_CONSTEXPR14 etl::byte& operator^=(etl::byte& lhs, etl::byte rhs) ETL_NOEXCEPT
{
return lhs = lhs ^ rhs;
}
//*************************************************************************
//*************************************************************************
inline constexpr etl::byte operator~(etl::byte b) ETL_NOEXCEPT
{
return etl::byte(~static_cast<unsigned int>(b));
}
#else
//*************************************************************************
//*************************************************************************
class byte
{
public:
// Friend functions
template <typename TInteger>
friend typename etl::enable_if<etl::is_integral<TInteger>::value, TInteger>::type to_integer(etl::byte b);
friend bool operator==(etl::byte lhs, etl::byte rhs);
// Default constructor
byte()
: value(0U)
{
}
// Construct from a value castable to unsigned char
template <typename T>
explicit byte(T v)
: value(static_cast<unsigned char>(v))
{
}
// Cast to a T
template <typename T>
operator T() const
{
return static_cast<T>(value);
}
private:
// The byte value
unsigned char value;
};
//*************************************************************************
//*************************************************************************
inline bool operator==(etl::byte lhs, etl::byte rhs)
{
return (lhs.value == rhs.value);
}
//*************************************************************************
//*************************************************************************
inline bool operator!=(etl::byte lhs, etl::byte rhs)
{
return !(lhs == rhs);
}
//*************************************************************************
//*************************************************************************
template <typename TInteger>
typename etl::enable_if<etl::is_integral<TInteger>::value, TInteger>::type to_integer(etl::byte b)
{
return TInteger(b);
}
//*************************************************************************
//*************************************************************************
template <typename TInteger>
typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte>::type operator<<(etl::byte b, TInteger shift)
{
return etl::byte(to_integer<unsigned int>(b) << shift);
}
//*************************************************************************
//*************************************************************************
template <typename TInteger>
typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte>::type operator>>(etl::byte b, TInteger shift)
{
return etl::byte(to_integer<unsigned int>(b) >> shift);
}
//*************************************************************************
//*************************************************************************
template <typename TInteger>
typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte&>::type operator<<=(etl::byte& b, TInteger shift)
{
b = b << shift;
return b;
}
//*************************************************************************
//*************************************************************************
template <typename TInteger>
typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte&>::type operator>>=(etl::byte& b, TInteger shift)
{
b = b >> shift;
return b;
}
//*************************************************************************
//*************************************************************************
inline etl::byte operator|(etl::byte lhs, etl::byte rhs)
{
return etl::byte(to_integer<unsigned int>(lhs) | to_integer<unsigned int>(rhs));
}
//*************************************************************************
//*************************************************************************
inline etl::byte operator&(etl::byte lhs, etl::byte rhs)
{
return etl::byte(to_integer<unsigned int>(lhs) & to_integer<unsigned int>(rhs));
}
//*************************************************************************
//*************************************************************************
inline etl::byte operator^(etl::byte lhs, etl::byte rhs)
{
return etl::byte(to_integer<unsigned int>(lhs) ^ to_integer<unsigned int>(rhs));
}
//*************************************************************************
//*************************************************************************
inline etl::byte& operator|=(etl::byte& lhs, etl::byte rhs)
{
return lhs = lhs | rhs;
}
//*************************************************************************
//*************************************************************************
inline etl::byte& operator&=(etl::byte& lhs, etl::byte rhs)
{
return lhs = lhs & rhs;
}
//*************************************************************************
//*************************************************************************
inline etl::byte& operator^=(etl::byte& lhs, etl::byte rhs)
{
return lhs = lhs ^ rhs;
}
//*************************************************************************
//*************************************************************************
inline etl::byte operator~(etl::byte b)
{
return etl::byte(~to_integer<unsigned char>(b));
}
#endif
} // namespace etl
#endif
etl::byte
The byte class.
Definition byte.h:147
etl::byte::operator==
friend bool operator==(etl::byte lhs, etl::byte rhs)
Equality test.
Definition byte.h:185
etl::byte::to_integer
friend etl::enable_if< etl::is_integral< TInteger >::value, TInteger >::type to_integer(etl::byte b)
To integer.
Definition byte.h:202
etl
Definition absolute.h:40
etl::operator==
ETL_CONSTEXPR14 bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1078
etl::operator<<
std::basic_ostream< T, std::char_traits< T > > & operator<<(std::basic_ostream< T, std::char_traits< T > > &os, const etl::ibasic_string< T > &str)
Definition basic_string.h:3198
etl::operator~
etl::byte operator~(etl::byte b)
Not.
Definition byte.h:298
etl::operator^=
etl::byte & operator^=(etl::byte &lhs, etl::byte rhs)
Exclusive or equals.
Definition byte.h:290
etl::operator<<=
etl::enable_if< etl::is_integral< TInteger >::value, etl::byte & >::type operator<<=(etl::byte &b, TInteger shift)
Shift left equals.
Definition byte.h:229
etl::operator|
etl::byte operator|(etl::byte lhs, etl::byte rhs)
Or.
Definition byte.h:250
etl::operator|=
etl::byte & operator|=(etl::byte &lhs, etl::byte rhs)
Or equals.
Definition byte.h:274
etl::to_integer
etl::enable_if< etl::is_integral< TInteger >::value, TInteger >::type to_integer(etl::byte b)
To integer.
Definition byte.h:202
etl::operator&
etl::byte operator&(etl::byte lhs, etl::byte rhs)
And.
Definition byte.h:258
etl::operator>>
etl::enable_if< etl::is_integral< TInteger >::value, etl::byte >::type operator>>(etl::byte b, TInteger shift)
Shift right.
Definition byte.h:220
etl::operator!=
ETL_CONSTEXPR14 bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1090
etl::operator^
etl::byte operator^(etl::byte lhs, etl::byte rhs)
Exclusive Or.
Definition byte.h:266
etl::operator>>=
etl::enable_if< etl::is_integral< TInteger >::value, etl::byte & >::type operator>>=(etl::byte &b, TInteger shift)
Shift right equals.
Definition byte.h:240
etl::operator&=
etl::byte & operator&=(etl::byte &lhs, etl::byte rhs)
And equals.
Definition byte.h:282
platform.h
etl::type
type_traits.h