mirror of
https://github.com/ETLCPP/etl.git
synced 2026-06-17 09:26:05 +08:00
constexpr for enum_type
This commit is contained in:
parent
c70db16a20
commit
2e927cd85d
@ -84,15 +84,15 @@ SOFTWARE.
|
||||
//*****************************************************************************
|
||||
#define ETL_DECLARE_ENUM_TYPE(TypeName, ValueType) \
|
||||
typedef ValueType value_type; \
|
||||
TypeName() : value(static_cast<enum_type>(value_type())) {} \
|
||||
TypeName(const TypeName &other) : value(other.value) {} \
|
||||
TypeName(enum_type value_) : value(value_) {} \
|
||||
TypeName& operator=(const TypeName &other) {value = other.value; return *this;} \
|
||||
explicit TypeName(value_type value_) : value(static_cast<enum_type>(value_)) {} \
|
||||
operator value_type() const {return static_cast<value_type>(value);} \
|
||||
value_type get_value() const {return static_cast<value_type>(value);} \
|
||||
enum_type get_enum() const {return value;} \
|
||||
const char* c_str() const \
|
||||
ETL_CONSTEXPR TypeName() : value(static_cast<enum_type>(value_type())) {} \
|
||||
ETL_CONSTEXPR TypeName(const TypeName &other) : value(other.value) {} \
|
||||
ETL_CONSTEXPR TypeName(enum_type value_) : value(value_) {} \
|
||||
ETL_CONSTEXPR14 TypeName& operator=(const TypeName &other) {value = other.value; return *this;} \
|
||||
ETL_CONSTEXPR explicit TypeName(value_type value_) : value(static_cast<enum_type>(value_)) {} \
|
||||
ETL_CONSTEXPR operator value_type() const {return static_cast<value_type>(value);} \
|
||||
ETL_CONSTEXPR value_type get_value() const {return static_cast<value_type>(value);} \
|
||||
ETL_CONSTEXPR enum_type get_enum() const {return value;} \
|
||||
ETL_CONSTEXPR14 const char* c_str() const \
|
||||
{ \
|
||||
switch (value) \
|
||||
{
|
||||
|
||||
@ -53,7 +53,7 @@ namespace
|
||||
SUITE(test_enum_type)
|
||||
{
|
||||
//*************************************************************************
|
||||
TEST(Values)
|
||||
TEST(test_values)
|
||||
{
|
||||
CHECK_EQUAL(0, enum_test::value_type(enum_test::ZERO));
|
||||
CHECK_EQUAL(1, enum_test::value_type(enum_test::ONE));
|
||||
@ -61,6 +61,31 @@ namespace
|
||||
CHECK_EQUAL(4, enum_test::value_type(enum_test::FOUR));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_constexpr_values)
|
||||
{
|
||||
constexpr enum_test zero = enum_test::ZERO;
|
||||
constexpr enum_test zero2(zero);
|
||||
constexpr enum_test zero3 = zero;
|
||||
|
||||
constexpr int int_zero = zero;
|
||||
constexpr int int_zero2 = zero.get_value();
|
||||
|
||||
constexpr enum_test enum_zero = zero;
|
||||
constexpr enum_test enum_zero2 = zero.get_enum();
|
||||
|
||||
constexpr const char* string_zero = zero.c_str();
|
||||
|
||||
CHECK_EQUAL(0, zero.get_value());
|
||||
CHECK_EQUAL(0, zero2.get_value());
|
||||
CHECK_EQUAL(0, zero3.get_value());
|
||||
CHECK_EQUAL(0, int_zero);
|
||||
CHECK_EQUAL(0, int_zero2);
|
||||
CHECK_EQUAL(0, enum_zero.get_value());
|
||||
CHECK_EQUAL(0, enum_zero2.get_value());
|
||||
CHECK_EQUAL(std::string("ZERO"), std::string(string_zero));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_c_str)
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user