Loading...
Searching...
No Matches
type_def.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) 2016 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_TYPE_DEF_INCLUDED
#define ETL_TYPE_DEF_INCLUDED
#include "platform.h"
#include "type_traits.h"
namespace etl
{
#define ETL_TYPEDEF(T, name) \
class name##_tag; \
typedef etl::type_def<name##_tag, T> name
#define ETL_USING(name, T) \
class name##_tag; \
typedef etl::type_def<name##_tag, T> name
#if ETL_USING_CPP11
#define ETL_TYPE_DEF_RETURN(T, TValue, TReturn) typename etl::enable_if<etl::is_convertible<T, TValue>::value, TReturn>::type
#else
#define ETL_TYPE_DEF_RETURN(T, TValue, ReturnType) ReturnType
#endif
//*************************************************************************
//*************************************************************************
template <typename TIdType, typename TValue>
class type_def
{
public:
typedef TValue type;
typedef TValue value_type;
typedef TIdType id_type;
//*********************************************************************
ETL_CONSTEXPR type_def() ETL_NOEXCEPT
: value(TValue())
{
}
//*********************************************************************
#if ETL_USING_CPP11
template <typename T, typename = typename etl::enable_if< etl::is_convertible<T, TValue>::value, void>::type>
#else
template <typename T>
#endif
ETL_CONSTEXPR type_def(T value_) ETL_NOEXCEPT
: value(static_cast<TValue>(value_))
{
}
//*********************************************************************
#if ETL_USING_CPP11
ETL_CONSTEXPR type_def(const type_def& other) = default;
#endif
//*********************************************************************
ETL_CONSTEXPR operator TValue() const ETL_NOEXCEPT
{
return value;
}
//*********************************************************************
ETL_CONSTEXPR14 type_def& operator++() ETL_NOEXCEPT
{
++value;
return *this;
}
//*********************************************************************
ETL_CONSTEXPR14 type_def operator++(int) ETL_NOEXCEPT
{
type_def temp(*this);
type_def::operator++();
return temp;
}
//*********************************************************************
ETL_CONSTEXPR14 type_def& operator--() ETL_NOEXCEPT
{
--value;
return *this;
}
//*********************************************************************
ETL_CONSTEXPR14 type_def operator--(int) ETL_NOEXCEPT
{
type_def temp(*this);
type_def::operator--();
return temp;
}
//*********************************************************************
template <typename T>
ETL_CONSTEXPR14 ETL_TYPE_DEF_RETURN(T, TValue, type_def&) operator+=(T rhs)ETL_NOEXCEPT
{
value += static_cast<TValue>(rhs);
return *this;
}
//*********************************************************************
ETL_CONSTEXPR14 type_def& operator+=(const type_def& rhs) ETL_NOEXCEPT
{
value += rhs.value;
return *this;
}
//*********************************************************************
template <typename T>
ETL_CONSTEXPR14 ETL_TYPE_DEF_RETURN(T, TValue, type_def&) operator-=(T rhs)ETL_NOEXCEPT
{
value -= static_cast<TValue>(rhs);
return *this;
}
//*********************************************************************
ETL_CONSTEXPR14 type_def& operator-=(const type_def& rhs) ETL_NOEXCEPT
{
value -= rhs.value;
return *this;
}
//*********************************************************************
template <typename T>
ETL_CONSTEXPR14 ETL_TYPE_DEF_RETURN(T, TValue, type_def&) operator*=(T rhs)ETL_NOEXCEPT
{
value *= static_cast<TValue>(rhs);
return *this;
}
//*********************************************************************
ETL_CONSTEXPR14 type_def& operator*=(const type_def& rhs) ETL_NOEXCEPT
{
value *= rhs.value;
return *this;
}
//*********************************************************************
template <typename T>
ETL_CONSTEXPR14 ETL_TYPE_DEF_RETURN(T, TValue, type_def&) operator/=(T rhs)ETL_NOEXCEPT
{
value /= static_cast<TValue>(rhs);
return *this;
}
//*********************************************************************
ETL_CONSTEXPR14 type_def& operator/=(const type_def& rhs) ETL_NOEXCEPT
{
value /= rhs.value;
return *this;
}
//*********************************************************************
template <typename T>
ETL_CONSTEXPR14 ETL_TYPE_DEF_RETURN(T, TValue, type_def&) operator%=(T rhs)ETL_NOEXCEPT
{
value %= static_cast<TValue>(rhs);
return *this;
}
//*********************************************************************
ETL_CONSTEXPR14 type_def& operator%=(const type_def& rhs) ETL_NOEXCEPT
{
value %= rhs.value;
return *this;
}
//*********************************************************************
template <typename T>
ETL_CONSTEXPR14 ETL_TYPE_DEF_RETURN(T, TValue, type_def&) operator&=(T rhs)ETL_NOEXCEPT
{
value &= static_cast<TValue>(rhs);
return *this;
}
//*********************************************************************
ETL_CONSTEXPR14 type_def& operator&=(const type_def& rhs) ETL_NOEXCEPT
{
value &= rhs.value;
return *this;
}
//*********************************************************************
template <typename T>
ETL_CONSTEXPR14 ETL_TYPE_DEF_RETURN(T, TValue, type_def&) operator|=(T rhs)ETL_NOEXCEPT
{
value |= static_cast<TValue>(rhs);
return *this;
}
//*********************************************************************
ETL_CONSTEXPR14 type_def& operator|=(const type_def& rhs) ETL_NOEXCEPT
{
value |= rhs.value;
return *this;
}
//*********************************************************************
template <typename T>
ETL_CONSTEXPR14 ETL_TYPE_DEF_RETURN(T, TValue, type_def&) operator^=(T rhs)ETL_NOEXCEPT
{
value ^= static_cast<TValue>(rhs);
return *this;
}
//*********************************************************************
ETL_CONSTEXPR14 type_def& operator^=(const type_def& rhs) ETL_NOEXCEPT
{
value ^= rhs.value;
return *this;
}
//*********************************************************************
template <typename T>
ETL_CONSTEXPR14 typename etl::enable_if<etl::is_integral<T>::value, type_def&>::type operator<<=(T rhs) ETL_NOEXCEPT
{
value <<= static_cast<TValue>(rhs);
return *this;
}
//*********************************************************************
template <typename T>
ETL_CONSTEXPR14 typename etl::enable_if<etl::is_integral<T>::value, type_def&>::type operator>>=(T rhs) ETL_NOEXCEPT
{
value >>= static_cast<TValue>(rhs);
return *this;
}
//*********************************************************************
#if ETL_USING_CPP11
ETL_CONSTEXPR14 type_def& operator=(const type_def& rhs) = default;
#endif
//*********************************************************************
TValue& get() ETL_NOEXCEPT
{
return value;
}
//*********************************************************************
ETL_CONSTEXPR const TValue& get() const ETL_NOEXCEPT
{
return value;
}
//*********************************************************************
// + operator
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, type_def) operator+(const type_def & lhs, T rhs) ETL_NOEXCEPT
{
return type_def(lhs.value + static_cast<TValue>(rhs));
}
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR type_def operator+(T lhs, const type_def& rhs) ETL_NOEXCEPT
{
return type_def(static_cast<TValue>(lhs) + rhs.value);
}
//*********************************************************************
friend ETL_CONSTEXPR type_def operator+(const type_def& lhs, const type_def& rhs)
{
return type_def(lhs.value + rhs.value);
}
//*********************************************************************
// - operator
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, type_def) operator-(const type_def & lhs, T rhs) ETL_NOEXCEPT
{
return type_def(lhs.value - static_cast<TValue>(rhs));
}
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, type_def) operator-(T lhs, const type_def & rhs) ETL_NOEXCEPT
{
return type_def(static_cast<TValue>(lhs) - rhs.value);
}
//*********************************************************************
friend ETL_CONSTEXPR type_def operator-(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
{
return type_def(lhs.value - rhs.value);
}
//*********************************************************************
// * operator
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, type_def) operator*(const type_def & lhs, T rhs) ETL_NOEXCEPT
{
return type_def(lhs.value * static_cast<TValue>(rhs));
}
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, type_def) operator*(T lhs, const type_def & rhs) ETL_NOEXCEPT
{
return type_def(static_cast<TValue>(lhs) * rhs.value);
}
//*********************************************************************
friend ETL_CONSTEXPR type_def operator*(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
{
return type_def(lhs.value * rhs.value);
}
//*********************************************************************
// / operator
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, type_def) operator/(const type_def & lhs, T rhs) ETL_NOEXCEPT
{
return type_def(lhs.value / static_cast<TValue>(rhs));
}
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, type_def) operator/(T lhs, const type_def & rhs) ETL_NOEXCEPT
{
return type_def(static_cast<TValue>(lhs) / rhs.value);
}
//*********************************************************************
friend ETL_CONSTEXPR type_def operator/(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
{
return type_def(lhs.value / rhs.value);
}
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, type_def) operator%(const type_def & lhs, T rhs) ETL_NOEXCEPT
{
return type_def(lhs.value % static_cast<TValue>(rhs));
}
//*********************************************************************
// % operator
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, type_def) operator%(T lhs, const type_def & rhs) ETL_NOEXCEPT
{
return type_def(static_cast<TValue>(lhs) % rhs.value);
}
//*********************************************************************
friend ETL_CONSTEXPR type_def operator%(const type_def& lhs, const type_def& rhs)
{
return type_def(lhs.value % rhs.value);
}
//*********************************************************************
// & operator
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, type_def) operator&(const type_def & lhs, T rhs) ETL_NOEXCEPT
{
return type_def(lhs.value & static_cast<TValue>(rhs));
}
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, type_def) operator&(T lhs, const type_def & rhs) ETL_NOEXCEPT
{
return type_def(static_cast<TValue>(lhs) & rhs.value);
}
//*********************************************************************
friend ETL_CONSTEXPR type_def operator&(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
{
return type_def(lhs.value & rhs.value);
}
//*********************************************************************
// | operator
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, type_def) operator|(const type_def & lhs, T rhs) ETL_NOEXCEPT
{
return type_def(lhs.value | static_cast<TValue>(rhs));
}
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, type_def) operator|(T lhs, const type_def & rhs) ETL_NOEXCEPT
{
return type_def(static_cast<TValue>(lhs) | rhs.value);
}
//*********************************************************************
friend ETL_CONSTEXPR type_def operator|(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
{
return type_def(lhs.value | rhs.value);
}
//*********************************************************************
// ^ operator
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, type_def) operator^(const type_def & lhs, T rhs) ETL_NOEXCEPT
{
return type_def(lhs.value ^ static_cast<TValue>(rhs));
}
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, type_def) operator^(T lhs, const type_def & rhs) ETL_NOEXCEPT
{
return type_def(static_cast<TValue>(lhs) ^ rhs.value);
}
//*********************************************************************
friend ETL_CONSTEXPR type_def operator^(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
{
return type_def(lhs.value ^ rhs.value);
}
//*********************************************************************
// << operator
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR typename etl::enable_if<etl::is_integral<T>::value, type_def>::type operator<<(const type_def& lhs, T rhs) ETL_NOEXCEPT
{
return type_def(lhs.value << static_cast<TValue>(rhs));
}
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR typename etl::enable_if< (etl::is_integral<T>::value && etl::is_integral<TValue>::value), T>::type
operator<<(T lhs, const type_def& rhs) ETL_NOEXCEPT
{
return lhs << rhs.value;
}
//*********************************************************************
// >> operator
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR typename etl::enable_if<etl::is_integral<T>::value, type_def>::type operator>>(const type_def& lhs, T rhs) ETL_NOEXCEPT
{
return type_def(lhs.value >> static_cast<TValue>(rhs));
}
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR typename etl::enable_if< (etl::is_integral<T>::value && etl::is_integral<TValue>::value), T>::type
operator>>(T lhs, const type_def& rhs) ETL_NOEXCEPT
{
return lhs >> rhs.value;
}
//*********************************************************************
// < operator
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, bool) operator<(const type_def & lhs, T rhs) ETL_NOEXCEPT
{
return lhs.value < rhs;
}
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, bool) operator<(T lhs, const type_def & rhs) ETL_NOEXCEPT
{
return lhs < rhs.value;
}
//*********************************************************************
friend ETL_CONSTEXPR bool operator<(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
{
return lhs.value < rhs.value;
}
//*********************************************************************
// <= operator
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, bool) operator<=(const type_def & lhs, T rhs) ETL_NOEXCEPT
{
return lhs.value <= rhs;
}
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, bool) operator<=(T lhs, const type_def & rhs) ETL_NOEXCEPT
{
return lhs <= rhs.value;
}
//*********************************************************************
friend ETL_CONSTEXPR bool operator<=(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
{
return lhs.value <= rhs.value;
}
//*********************************************************************
// > operator
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, bool) operator>(const type_def & lhs, T rhs) ETL_NOEXCEPT
{
return lhs.value > rhs;
}
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, bool) operator>(T lhs, const type_def & rhs) ETL_NOEXCEPT
{
return lhs > rhs.value;
}
//*********************************************************************
friend ETL_CONSTEXPR bool operator>(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
{
return lhs.value > rhs.value;
}
//*********************************************************************
// >= operator
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, bool) operator>=(const type_def & lhs, T rhs) ETL_NOEXCEPT
{
return lhs.value >= rhs;
}
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, bool) operator>=(T lhs, const type_def & rhs) ETL_NOEXCEPT
{
return lhs >= rhs.value;
}
//*********************************************************************
friend ETL_CONSTEXPR bool operator>=(const type_def& lhs, const type_def& rhs)
{
return lhs.value >= rhs.value;
}
//*********************************************************************
// == operator
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, bool) operator==(const type_def & lhs, T rhs) ETL_NOEXCEPT
{
return lhs.value == rhs;
}
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, bool) operator==(T lhs, const type_def & rhs)
{
return lhs == rhs.value;
}
//*********************************************************************
friend ETL_CONSTEXPR bool operator==(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
{
return lhs.value == rhs.value;
}
//*********************************************************************
// != operator
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, bool) operator!=(const type_def & lhs, T rhs) ETL_NOEXCEPT
{
return lhs.value != rhs;
}
//*********************************************************************
template <typename T>
friend ETL_CONSTEXPR ETL_TYPE_DEF_RETURN(T, TValue, bool) operator!=(T lhs, const type_def & rhs) ETL_NOEXCEPT
{
return lhs != rhs.value;
}
//*********************************************************************
friend ETL_CONSTEXPR bool operator!=(const type_def& lhs, const type_def& rhs) ETL_NOEXCEPT
{
return lhs.value != rhs.value;
}
private:
TValue value;
};
} // namespace etl
#undef ETL_TYPE_DEF_RETURN
#endif
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_CONSTEXPR14 etl::circular_iterator< TIterator > operator-(etl::circular_iterator< TIterator > &lhs, typename etl::iterator_traits< TIterator >::difference_type offset)
Definition circular_iterator.h:675
etl::operator>
bool operator>(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1130
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_CONSTEXPR14 enable_if<!etl::is_specialization< TRep2, etl::chrono::duration >::value, etl::chrono::duration< typenameetl::common_type< TRep1, TRep2 >::type, TPeriod1 > >::type operator/(const etl::chrono::duration< TRep1, TPeriod1 > &lhs, const TRep2 &rhs) ETL_NOEXCEPT
Operator /.
Definition duration.h:570
etl::operator|=
etl::byte & operator|=(etl::byte &lhs, etl::byte rhs)
Or equals.
Definition byte.h:274
etl::operator%
ETL_CONSTEXPR14 etl::chrono::duration< typename etl::common_type< TRep1, TRep2 >::type, TPeriod1 > operator%(const etl::chrono::duration< TRep1, TPeriod1 > &lhs, const TRep2 &rhs) ETL_NOEXCEPT
Operator %.
Definition duration.h:600
etl::operator&
etl::byte operator&(etl::byte lhs, etl::byte rhs)
And.
Definition byte.h:258
etl::operator>=
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1144
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_CONSTEXPR14 enable_if<!etl::is_specialization< TRep2, etl::chrono::duration >::value, etl::chrono::duration< typenameetl::common_type< TRep1, TRep2 >::type, TPeriod1 > >::type operator*(const etl::chrono::duration< TRep1, TPeriod1 > &lhs, const TRep2 &rhs) ETL_NOEXCEPT
Operator *.
Definition duration.h:541
etl::operator+
ETL_CONSTEXPR14 etl::circular_iterator< TIterator > operator+(etl::circular_iterator< TIterator > &lhs, typename etl::iterator_traits< TIterator >::difference_type offset)
Definition circular_iterator.h:662
etl::operator&=
etl::byte & operator&=(etl::byte &lhs, etl::byte rhs)
And equals.
Definition byte.h:282
etl::get
T & get(array< T, Size > &a)
Definition array.h:1158
etl::operator<
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1103
etl::operator<=
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1117
platform.h
type_traits.h