🟥 Foundation types built using some simple C++ meta-programming
Features
Classes
TaggedUnion
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";
}
}
TaggedMap
Map of SC::TaggedUnion, where the key is TaggedUnion enumeration value.
- Template Parameters
-
Type | Enumeration type used |
Union | with FieldTypes = TypeList<TaggedType<EnumType, EnumValue, Type>, ...> |
Example:
namespace SC
{
struct Compile
{
enum Type
{
libraryPath = 10,
enableRTTI = 110,
};
};
struct CompileFlags
{
template <Compile::Type E, typename T>
using Tag = TaggedType<Compile::Type, E, T>;
using FieldsTypes = TypeTraits::TypeList<
Tag<Compile::libraryPath, String>,
Tag<Compile::enableRTTI, bool>>;
using Union = TaggedUnion<CompileFlags>;
};
}
void SC::TaggedMapTest::basic()
{
TaggedMap<Compile::Type, CompileFlags::Union> taggedMap;
*taggedMap.getOrCreate<Compile::libraryPath>() = "My String";
SC_TEST_EXPECT(*taggedMap.get<Compile::libraryPath>() ==
"My String");
*taggedMap.getOrCreate<Compile::enableRTTI>() = true;
}
#define SC_TEST_EXPECT(e)
Records a test expectation (eventually aborting or breaking o n failed test)
Definition: Testing.h:113