Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::TaggedUnion< Union > Struct Template Reference

Type safe union with an enum type, where each type has an associated enum value. More...

#include <TaggedUnion.h>

Classes

struct  EnumToType
 Extracts type T corresponding to enumeration wantedEnum at compile time. More...
 
struct  EnumToType< wantedEnum, 0 >
 

Public Types

using EnumType = typename TypeAt< 0 >::enumType
 

Public Member Functions

 ~TaggedUnion ()
 Destroys the TaggedUnion object. More...
 
 TaggedUnion (const TaggedUnion &other)
 Copy constructor. More...
 
 TaggedUnion (TaggedUnion &&other)
 Move constructor. More...
 
TaggedUnionoperator= (const TaggedUnion &other)
 Copy assignment operator. More...
 
TaggedUnionoperator= (TaggedUnion &&other)
 Move assignment operator. More...
 
EnumType getType () const
 Returns enumeration value of currently active union type. More...
 
void setType (EnumType newType)
 Sets the currently active type at runtime, destructing and (default) constructing the new type. More...
 
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. More...
 
template<EnumType wantedType>
TypeAt< EnumToType< wantedType >::index >::type & changeTo ()
 Changes current active type in union to a different one. More...
 
template<EnumType wantedType>
TypeAt< EnumToType< wantedType >::index >::type * field ()
 Get a pointer to currently active field. More...
 
template<EnumType wantedType>
const TypeAt< EnumToType< wantedType >::index >::type * field () const
 Get a pointer to currently active field. More...
 

Static Public Attributes

static constexpr auto NumTypes = Union::FieldsTypes::size
 

Detailed Description

template<typename Union>
struct SC::TaggedUnion< Union >

Type safe union with an enum type, where each type has an associated enum value.

Template Parameters
Unionwith FieldTypes = TypeList<TaggedType<EnumType, EnumValue, Type>, ...>

Example:

namespace SC
{
struct TaggedUnionTest;
// Create an arbitrary enumeration with some values
enum TestType
{
TypeString = 10,
TypeInt = 110,
};
// Create the union definition containing a FieldTypes nested type
struct TestUnion
{
// Helper to save some typing
template <TestType E, typename T>
using Tag = TaggedType<TestType, E, T>;
// FieldsTypes MUST be defined to be a TypeList of TaggedType(s)
using FieldsTypes = TypeTraits::TypeList< // List all TargetType associations
Tag<TypeString, String>, // Associate TypeString with String
Tag<TypeInt, int>>; // Associate TypeInt with init
};
void taggedUnionUsageSnippet(Console& console)
{
// Create the tagged union on the TestUnion definition
TaggedUnion<TestUnion> test; // default initialized to first type (String)
// Access / Change type
String* ptr = test.field<TypeString>();
if (ptr) // If TypeString is not active type, ptr will be == nullptr
{
*ptr = "SomeValue";
}
test.changeTo<TypeInt>() = 2; // Change active type to TypeInt (compile time known)
// Switch on currently active type (TypeInt)
switch (test.getType())
{
case TypeString: console.print("String = {}", *test.field<TypeString>()); break;
case TypeInt: console.print("Int = {}", *test.field<TypeInt>()); break;
}
// Set current active type at runtime back to TypeString
test.setType(TypeString);
*test.field<TypeString>() = "Some new string";
}
} // namespace SC

Constructor & Destructor Documentation

◆ ~TaggedUnion()

template<typename Union >
SC::TaggedUnion< Union >::~TaggedUnion ( )
inline

Destroys the TaggedUnion object.

◆ TaggedUnion() [1/2]

template<typename Union >
SC::TaggedUnion< Union >::TaggedUnion ( const TaggedUnion< Union > &  other)
inline

Copy constructor.

Parameters
otherAnother tagged union

◆ TaggedUnion() [2/2]

template<typename Union >
SC::TaggedUnion< Union >::TaggedUnion ( TaggedUnion< Union > &&  other)
inline

Move constructor.

Parameters
otherAnother tagged union

Member Function Documentation

◆ assign()

template<typename Union >
template<EnumType wantedType, typename U >
void SC::TaggedUnion< Union >::assign ( U &&  other)
inline

Assigns a compile time known enum type with an object U.

Template Parameters
UType of object to be assigned.
wantedTypeCompile time known enum type
Parameters
otherobject to be assigned (move / copy)

◆ changeTo()

template<typename Union >
template<EnumType wantedType>
TypeAt< EnumToType< wantedType >::index >::type & SC::TaggedUnion< Union >::changeTo ( )
inline

Changes current active type in union to a different one.

Template Parameters
wantedTypeCompile time know enum value associated to wanted type
Returns
A reference to the wanted type. If wantedType is not the active type, a new value will be default initialized
Note
Return type is made explicit instead of using auto to help intellisense deducing type

◆ field() [1/2]

template<typename Union >
template<EnumType wantedType>
TypeAt< EnumToType< wantedType >::index >::type * SC::TaggedUnion< Union >::field ( )
inline

Get a pointer to currently active field.

Template Parameters
wantedTypeCompile time know enum value associated to wanted type
Returns
A pointer to currently active field or nullptr if wantedType doesn't match currently active type.
Note
Return type is made explicit instead of using auto to help intellisense deducing type

◆ field() [2/2]

template<typename Union >
template<EnumType wantedType>
const TypeAt< EnumToType< wantedType >::index >::type * SC::TaggedUnion< Union >::field ( ) const
inline

Get a pointer to currently active field.

Template Parameters
wantedTypeCompile time know enum value associated to wanted type
Returns
A pointer to currently active field or nullptr if wantedType doesn't match currently active type.
Note
Return type is made explicit instead of using auto to help intellisense deducing type

◆ getType()

template<typename Union >
EnumType SC::TaggedUnion< Union >::getType ( ) const
inline

Returns enumeration value of currently active union type.

◆ operator=() [1/2]

template<typename Union >
TaggedUnion & SC::TaggedUnion< Union >::operator= ( const TaggedUnion< Union > &  other)
inline

Copy assignment operator.

Parameters
otherAnother tagged union

◆ operator=() [2/2]

template<typename Union >
TaggedUnion & SC::TaggedUnion< Union >::operator= ( TaggedUnion< Union > &&  other)
inline

Move assignment operator.

Parameters
otherAnother tagged union

◆ setType()

template<typename Union >
void SC::TaggedUnion< Union >::setType ( EnumType  newType)
inline

Sets the currently active type at runtime, destructing and (default) constructing the new type.


The documentation for this struct was generated from the following file: