Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 58 additions & 4 deletions include/etl/type_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,28 @@ namespace etl
}

//*********************************************************************
ETL_CONSTEXPR14 type_def& operator <<=(int rhs) ETL_NOEXCEPT
template <typename T>
ETL_CONSTEXPR14
#if ETL_USING_CPP11
typename etl::enable_if<etl::is_integral<T>::value, type_def&>::type
#else
type_def&
#endif
operator <<=(T rhs) ETL_NOEXCEPT
{
value <<= rhs;
return *this;
}

//*********************************************************************
ETL_CONSTEXPR14 type_def& operator >>=(int rhs) ETL_NOEXCEPT
template <typename T>
ETL_CONSTEXPR14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

etl::is_integral<T> is valid for C++03

#if ETL_USING_CPP11
typename etl::enable_if<etl::is_integral<T>::value, type_def&>::type
#else
type_def&
#endif
operator >>=(T rhs) ETL_NOEXCEPT
{
value >>= rhs;
return *this;
Expand Down Expand Up @@ -589,19 +603,59 @@ namespace etl
//*********************************************************************
// << operator
//*********************************************************************
friend ETL_CONSTEXPR type_def operator <<(const type_def& lhs, int rhs) ETL_NOEXCEPT
template <typename T>
friend ETL_CONSTEXPR
#if ETL_USING_CPP11
typename etl::enable_if<etl::is_integral<T>::value, type_def>::type
#else
type_def
#endif
operator <<(const type_def& lhs, T rhs) ETL_NOEXCEPT
{
return type_def(lhs.value << rhs);
}

//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR
#if ETL_USING_CPP11
typename etl::enable_if<(etl::is_integral<T>::value && etl::is_integral<TValue>::value), T>::type
#else
T
#endif
operator <<(T lhs, const type_def& rhs) ETL_NOEXCEPT
{
return lhs << rhs.value;
}

//*********************************************************************
// >> operator
//*********************************************************************
friend ETL_CONSTEXPR type_def operator >>(const type_def& lhs, int rhs) ETL_NOEXCEPT
template <typename T>
friend ETL_CONSTEXPR
#if ETL_USING_CPP11
typename etl::enable_if<etl::is_integral<T>::value, type_def>::type
#else
type_def
#endif
operator >>(const type_def& lhs, T rhs) ETL_NOEXCEPT
{
return type_def(lhs.value >> rhs);
}

//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR
#if ETL_USING_CPP11
typename etl::enable_if<(etl::is_integral<T>::value && etl::is_integral<TValue>::value), T>::type
#else
T
#endif
operator >>(T lhs, const type_def& rhs) ETL_NOEXCEPT
{
return lhs >> rhs.value;
}

//*********************************************************************
// < operator
//*********************************************************************
Expand Down
19 changes: 16 additions & 3 deletions test/test_type_def.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ namespace
CHECK_EQUAL(i |= 0x003DU, uint32_t(t |= type_t(0x003DU)));
CHECK_EQUAL(i ^= 0xAA55U, uint32_t(t ^= 0xAA55U));
CHECK_EQUAL(i ^= 0xAA55U, uint32_t(t ^= type_t(0xAA55U)));
CHECK_EQUAL(i <<= 2, uint32_t(t <<= 2));
CHECK_EQUAL(i <<= 2U, uint32_t(t <<= 2U));
CHECK_EQUAL(i >>= 2, uint32_t(t >>= 2));
CHECK_EQUAL(i >>= 2U, uint32_t(t >>= 2U));
CHECK_EQUAL(i %= 23, uint32_t(t %= 23));

Expand All @@ -195,8 +197,10 @@ namespace

uint32_t i1 = 0x5A3DUL;
uint32_t i2 = 0xB47AUL;
uint32_t i3 = 3UL;
type_t t1(0x5A3DUL);
type_t t2(0xB47AUL);
type_t t3(3UL);

CHECK_EQUAL(i1 + Two, t1 + Two);
CHECK_EQUAL(Two + i1, Two + t1);
Expand All @@ -205,11 +209,11 @@ namespace
CHECK_EQUAL(i1 - Two, t1 - Two);
CHECK_EQUAL(i2 - i1, i2 - t1);
CHECK_EQUAL(i2 - i1, t2 - t1);

CHECK_EQUAL(i1 * Two, t1 * Two);
CHECK_EQUAL(Two * i1, Two * t1);
CHECK_EQUAL(i1 * i2, t1 * t2);

CHECK_EQUAL(i1 / Two, t1 / Two);
CHECK_EQUAL(i2 / i1, i2 / t1);
CHECK_EQUAL(i2 / i1, t2 / t1);
Expand All @@ -227,7 +231,16 @@ namespace
CHECK_EQUAL(uint32_t(0xAA55) ^ i1, type_t(0xAA55UL) ^ t1);

CHECK_EQUAL(i1 << 2, t1 << 2);
CHECK_EQUAL(i1 << 2U, t1 << 2U);
CHECK_EQUAL(2 << i3, 2 << t3);
CHECK_EQUAL(2U << i3, 2U << t3);
CHECK_EQUAL(i1 << i3, t1 << t3);

CHECK_EQUAL(i1 >> 2, t1 >> 2);
CHECK_EQUAL(i1 >> 2U, t1 >> 2U);
CHECK_EQUAL(42 >> i3, 42 >> t3);
CHECK_EQUAL(42U >> i3, 42U >> t3);
CHECK_EQUAL(i1 >> i3, t1 >> t3);

CHECK_EQUAL(i1 % uint32_t(23), t1 % uint32_t(23));
CHECK_EQUAL(uint32_t(23) % i1, uint32_t(23) % t1);
Expand Down Expand Up @@ -313,7 +326,7 @@ namespace

return value;
}

TEST(test_arithmetic_constexpr)
{
constexpr arithmetic_type_t value_plus = CreatePlus();
Expand Down
Loading