Added etl::type_tag<T>

This commit is contained in:
John Wellbelove 2024-03-09 10:03:26 +00:00
parent cbf08d1efa
commit 59178e495a
2 changed files with 117 additions and 0 deletions

View File

@ -672,6 +672,56 @@ namespace etl
}
};
#endif
//*****************************************************************************
/// A tag that can be used as a concrete object to identify a type.
//*****************************************************************************
template <typename T>
struct type_tag
{
typedef T type;
typedef typename etl::remove_cvref<T>::type underlying_type;
};
//***********************************
/// Equality operator for type_tag
//***********************************
template <typename T1, typename T2>
ETL_CONSTEXPR bool operator ==(etl::type_tag<T1>, etl::type_tag<T2>)
{
return etl::is_same<etl::type_tag<T1>, etl::type_tag<T2> >::value;
}
//***********************************
/// Inequality operator for type_tag
//***********************************
template <typename T1, typename T2>
ETL_CONSTEXPR bool operator !=(etl::type_tag<T1>, etl::type_tag<T2>)
{
return !etl::is_same<etl::type_tag<T1>, etl::type_tag<T2> >::value;
}
#if ETL_USING_CPP11
//***********************************
/// type_tag type
//***********************************
template <typename T>
using type_tag_t = typename etl::type_tag<T>::type;
//***********************************
/// type_tag underlying type
//***********************************
template <typename T>
using type_tag_underlying_t = typename etl::type_tag<T>::underlying_type;
#endif
#if ETL_USING_CPP17
//***********************************
/// constexpr type_tag value
//***********************************
template <typename T>
inline constexpr etl::type_tag<T> type_tag_v = etl::type_tag<T>{};
#endif
}
#endif

View File

@ -33,6 +33,7 @@ SOFTWARE.
#include <map>
#include <vector>
#include <algorithm>
#include <type_traits>
#include "data.h"
@ -665,5 +666,71 @@ namespace
CHECK_EQUAL(forward_like_call_type::ConstRValue, template_function_fl<TFL&&>(etl::move(u4)));
CHECK_EQUAL(forward_like_call_type::ConstRValue, template_function_fl<const TFL&&>(etl::move(u4)));
}
//*********************************
TEST(test_type_tag)
{
using TypeTag1 = etl::type_tag<int>;
using TypeTag2 = etl::type_tag<const int>;
using TypeTag3 = etl::type_tag<double>;
TypeTag1 typeTag1;
TypeTag2 typeTag2;
TypeTag3 typeTag3;
CHECK_TRUE(typeTag1 == typeTag1);
CHECK_TRUE(typeTag2 == typeTag2);
CHECK_TRUE(typeTag3 == typeTag3);
CHECK_FALSE(typeTag1 == typeTag2);
CHECK_FALSE(typeTag2 == typeTag3);
CHECK_FALSE(typeTag3 == typeTag1);
CHECK_FALSE(typeTag1 != typeTag1);
CHECK_FALSE(typeTag2 != typeTag2);
CHECK_FALSE(typeTag3 != typeTag3);
CHECK_TRUE(typeTag1 != typeTag2);
CHECK_TRUE(typeTag2 != typeTag3);
CHECK_TRUE(typeTag3 != typeTag1);
}
//*********************************
TEST(test_type_tag_t)
{
using TypeTagType1 = etl::type_tag_t<int>;
using TypeTagType2 = etl::type_tag_t<const int>;
using TypeTagType3 = etl::type_tag_t<volatile int>;
CHECK_FALSE((std::is_same<TypeTagType1, TypeTagType2>::value));
CHECK_FALSE((std::is_same<TypeTagType2, TypeTagType3>::value));
CHECK_FALSE((std::is_same<TypeTagType3, TypeTagType1>::value));
}
//*********************************
TEST(test_type_tag_underlying_t)
{
using TypeTagType1 = etl::type_tag_underlying_t<int>;
using TypeTagType2 = etl::type_tag_underlying_t<const int>;
using TypeTagType3 = etl::type_tag_underlying_t<volatile int>;
CHECK_TRUE((std::is_same<TypeTagType1, TypeTagType2>::value));
CHECK_TRUE((std::is_same<TypeTagType2, TypeTagType3>::value));
CHECK_TRUE((std::is_same<TypeTagType3, TypeTagType1>::value));
}
//*********************************
#if ETL_USING_CPP17
TEST(test_type_tag_v)
{
constexpr auto typeTag1 = etl::type_tag_v<int>;
constexpr auto typeTag2 = etl::type_tag_v<const int>;
constexpr auto typeTag3 = etl::type_tag_v<volatile int>;
CHECK_TRUE(typeTag1 == typeTag1);
CHECK_TRUE(typeTag2 == typeTag2);
CHECK_TRUE(typeTag3 == typeTag3);
}
#endif
};
}