Sane C++ Libraries
C++ Platform Abstraction Libraries
TaggedMap.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#include "../../Libraries/Containers/VectorMap.h"
4namespace SC
5{
6template <typename Type, typename Union>
7struct TaggedMap;
8}
9
12
19template <typename Type, typename Union>
21{
23
24 template <Type enumType>
25 [[nodiscard]] typename Union::template EnumToType<enumType>::type* getOrCreate()
26 {
27 auto val = flags.getOrCreate(enumType);
28 if (val)
29 {
30 return &val->template changeTo<enumType>();
31 }
32 return nullptr;
33 }
34
35 template <Type enumType>
36 [[nodiscard]] bool set(const typename Union::template EnumToType<enumType>::type& obj)
37 {
38 auto res = flags.getOrCreate(enumType);
39 if (res)
40 {
41 res->template changeTo<enumType>() = obj;
42 return true;
43 }
44 return false;
45 }
46
47 template <Type enumType>
48 [[nodiscard]] const typename Union::template EnumToType<enumType>::type* get() const
49 {
50 auto res = flags.get(enumType);
51 if (res)
52 {
53 return res->template field<enumType>();
54 }
55 return nullptr;
56 }
57
58 [[nodiscard]] bool clear(Type enumType) { return flags.remove(enumType); }
59
60 template <Type enumType, typename U>
61 [[nodiscard]] bool hasValue(const U& obj) const
62 {
63 const auto entry = flags.get(enumType);
64 if (entry)
65 {
66 auto field = entry->template field<enumType>();
67 if (field)
68 {
69 return *field == obj;
70 }
71 }
72 return false;
73 }
74};
Map of SC::TaggedUnion, where the key is TaggedUnion enumeration value.
Definition: TaggedMap.h:21
A map holding VectorMapItem key-value pairs in an unsorted Vector.
Definition: VectorMap.h:35
const Value * get(const ComparableToKey &key) const
Get the Value associated to the given key.
Definition: VectorMap.h:165
Value * getOrCreate(const ComparableToKey &key)
Get the value associated to the given key, or creates a new one if needed.
Definition: VectorMap.h:195
bool remove(const ComparableToKey &key)
Remove an item with matching key from the Map.
Definition: VectorMap.h:55