Type safe union with an enum type, where each type has an associated enum value.
More...
|
| ~TaggedUnion () |
| Destroys the TaggedUnion object.
|
|
| TaggedUnion (const TaggedUnion &other) |
| Copy constructor.
|
|
| TaggedUnion (TaggedUnion &&other) |
| Move constructor.
|
|
TaggedUnion & | operator= (const TaggedUnion &other) |
| Copy assignment operator.
|
|
TaggedUnion & | operator= (TaggedUnion &&other) |
| Move assignment operator.
|
|
EnumType | getType () const |
| Returns enumeration value of currently active union type.
|
|
void | setType (EnumType newType) |
| Sets the currently active type at runtime, destructing and (default) constructing the new type.
|
|
bool | operator== (const TaggedUnion &other) const |
|
template<EnumType wantedType, typename U > |
void | assign (U &&other) |
| Assigns a compile time known enum type with an object U.
|
|
template<EnumType wantedType> |
TypeAt< EnumToType< wantedType >::index >::type & | changeTo () |
| Changes current active type in union to a different one.
|
|
template<EnumType wantedType> |
TypeAt< EnumToType< wantedType >::index >::type * | field () |
| Get a pointer to currently active field.
|
|
template<EnumType wantedType> |
const TypeAt< EnumToType< wantedType >::index >::type * | field () const |
| Get a pointer to currently active field.
|
|
template<typename Union>
struct SC::TaggedUnion< Union >
Type safe union with an enum type, where each type has an associated enum value.
- Template Parameters
-
Union | with FieldTypes = TypeList<TaggedType<EnumType, EnumValue, Type>, ...> |
Example:
namespace SC
{
struct TaggedUnionTest;
enum TestType
{
TypeString = 10,
TypeInt = 110,
};
struct TestUnion
{
template <TestType E, typename T>
using Tag = TaggedType<TestType, E, T>;
using FieldsTypes = TypeTraits::TypeList<
Tag<TypeString, String>,
Tag<TypeInt, int>>;
};
void taggedUnionUsageSnippet(Console& console)
{
TaggedUnion<TestUnion> test;
String* ptr = test.field<TypeString>();
if (ptr)
{
*ptr = "SomeValue";
}
test.changeTo<TypeInt>() = 2;
switch (test.getType())
{
case TypeString: console.print("String = {}", *test.field<TypeString>()); break;
case TypeInt: console.print("Int = {}", *test.field<TypeInt>()); break;
}
test.setType(TypeString);
*test.field<TypeString>() = "Some new string";
}
}