Embedded Template Library 1.0
Loading...
Searching...
No Matches
enum_type.h File Reference
#include "platform.h"

Go to the source code of this file.

Macros

#define ETL_DECLARE_ENUM_TYPE(TypeName, ValueType)
#define ETL_ENUM_TYPE(value, name)
#define ETL_END_ENUM_TYPE

Macro Definition Documentation

◆ ETL_DECLARE_ENUM_TYPE

#define ETL_DECLARE_ENUM_TYPE ( TypeName,
ValueType )
Value:
typedef ValueType value_type; \
ETL_CONSTEXPR TypeName() : value(static_cast<enum_type>(value_type())) {} \
ETL_CONSTEXPR TypeName(enum_type value_) : value(value_) {} \
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) \
{
struct CompassDirection
{
enum enum_type
{
North = 0,
South = 180,
East = 90,
West = 270
};
ETL_DECLARE_ENUM_TYPE(CompassDirection, int)
ETL_ENUM_TYPE(North, "North")
ETL_ENUM_TYPE(South, "South")
ETL_ENUM_TYPE(East, "East")
ETL_ENUM_TYPE(West, "West")
ETL_END_ENUM_TYPE
};
#define ETL_DECLARE_ENUM_TYPE(TypeName, ValueType)
Definition enum_type.h:85

Using the enumeration.

CompassDirection direction; // Default construction.
direction = CompassDirection::North; // Assignment from an enumeration constant;
int value = direction; // Implicit conversion to 'int'.
direction = CompassDirection(value); // Explicit conversion from 'int'.
direction = CompassDirection(3); // Explicit conversion from an invalid value. This unfortunately cannot be avoided. Caveat emptor!
direction = value; // Implicit conversion from 'int'. **** Compilation error ****
std::cout << "Direction = " << direction.c_str(); // Prints "Direction = North"

If a conversion to a string is not required then the 'ETL_ENUM_TYPE' declaration may be omitted. In that case the c_str() function will return a "?". This will also be the case for any enumeration value that does not have an ETL_ENUM_TYPE entry.

◆ ETL_END_ENUM_TYPE

#define ETL_END_ENUM_TYPE
Value:
default: \
return "?"; \
} \
} \
private: \
enum_type value;

◆ ETL_ENUM_TYPE

#define ETL_ENUM_TYPE ( value,
name )
Value:
case value: \
return name; \