CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Dictionary.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
14
15#include <algorithm>
16
18
19 // Bidirectional map template class, where Key = string, Value = T. 'T' must be an asceding enumerable type.
20 template<typename T> class Dictionary {
21 public:
22 Dictionary() = default;
23 Dictionary(const Dictionary&) = default;
24
27 std::size_t maxIndex = 0;
28
29 for(const auto& pair : stringToEnumPairs) {
30 maxIndex = std::max(maxIndex, static_cast<std::size_t>(pair.second));
31 }
32
33 mEnumToString.resize(maxIndex + 1, nullptr);
34
36 for(const auto& pair : stringToEnumPairs) {
37 const auto idx = static_cast<std::size_t>(pair.second);
38 if(mEnumToString[idx] == nullptr) {
39 auto it = mStringToEnum.find(pair.first);
40 if(it != mStringToEnum.end()) {
41 mEnumToString[idx] = &(it->first);
42 }
43 }
44 }
45 }
46
47 // Returns a pointer to the enumeration entry which is associated to the specified string, or null on failure.
48 [[nodiscard]] const T* stringToEnum(const String& s) const {
49 auto it = mStringToEnum.find(s);
50 if(it != mStringToEnum.end()) {
51 return &(it->second);
52 }
53 return nullptr;
54 }
55
56 // Returns the enumeration entry which is associated to the specified string, or the default value on failure.
57 [[nodiscard]] T stringToEnumOrDefault(const String& s, const T& defaultValue) const {
58 auto it = mStringToEnum.find(s);
59 if(it != mStringToEnum.end()) {
60 return it->second;
61 }
62 return defaultValue;
63 }
64
65 // Returns a pointer to the first string which is associated to the specified enumeration entry, or null on failure.
66 [[nodiscard]] const String* enumToString(const T& e) const {
67 const auto idx = static_cast<std::size_t>(e);
68 if(idx < mEnumToString.size()) {
69 return mEnumToString[idx];
70 }
71 return nullptr;
72 }
73
74 // Returns the first string which is associated to the specified enumeration entry, or the default string on failure.
76 const auto idx = static_cast<std::size_t>(e);
77 if(idx < mEnumToString.size()) {
78 if(const auto s = mEnumToString[idx]) {
79 return *s;
80 }
81 }
82 return defaultString;
83 }
84
85 private:
88 };
89
90} // namespace CeresEngine::ShaderCompiler
Definition Dictionary.hpp:20
String enumToStringOrDefault(const T &e, const String &defaultString) const
Definition Dictionary.hpp:75
Dictionary(const InitializerList< Pair< String, T > > &stringToEnumPairs)
Definition Dictionary.hpp:25
Map< String, T > mStringToEnum
Definition Dictionary.hpp:86
T stringToEnumOrDefault(const String &s, const T &defaultValue) const
Definition Dictionary.hpp:57
Dictionary(const Dictionary &)=default
const T * stringToEnum(const String &s) const
Definition Dictionary.hpp:48
Vector< const String * > mEnumToString
Definition Dictionary.hpp:87
const String * enumToString(const T &e) const
Definition Dictionary.hpp:66
Definition AST.hpp:33
std::initializer_list< T > InitializerList
An object of type InitializerList<T> is a lightweight proxy object that provides access to an array o...
Definition InitializerList.hpp:40
std::pair< First, Second > Pair
Pair is a struct template that provides a way to store two heterogeneous objects as a single unit.
Definition Pair.hpp:18
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
std::map< Key, T, Compare, ScopedAllocatorAdaptor< StdAllocator< Pair< const Key, T >, RawAllocator > > > Map
Map is a sorted associative container that contains key-value pairs with unique keys.
Definition Map.hpp:24