Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TSdlGeneralEnum.h
Go to the documentation of this file.
1#pragma once
2
6#include "Utility/utility.h"
7
8#include <Common/assertion.h>
9
10#include <type_traits>
11#include <string>
12#include <string_view>
13#include <cstddef>
14#include <utility>
15
16namespace ph
17{
18
24template<typename InEnumType, std::size_t MAX_ENTRIES = 64>
26{
27 // TODO: how the mapping is done should be a template param
28 // TODO: can have other kinds of enum such as TSdlFlagEnum
29public:
30 using EnumType = InEnumType;
31
32 static_assert(std::is_enum_v<EnumType>,
33 "EnumType must be a C++ enum. Currently it is not.");
34
35public:
36 inline explicit TSdlGeneralEnum(std::string name) :
37
38 SdlEnum(std::move(name)),
39
40 m_nameBuffer(),
41 m_entries()
42 {}
43
44 inline Entry getEntry(const std::size_t entryIndex) const override
45 {
46 using EntryValueType = decltype(std::declval<Entry>().value);
47
48 const TEntry<EnumType> entry = getTypedEntry(entryIndex);
49 return {entry.name, static_cast<EntryValueType>(entry.value)};
50 }
51
52 inline std::size_t numEntries() const override
53 {
54 return m_entries.size();
55 }
56
58 const EnumType enumValue,
59 const std::string_view valueName,
60 std::string description = "")
61 {
62 PH_ASSERT_MSG(!m_entries.isFull(),
63 "No space for more entries; increase MAX_ENTRIES parameter for this enum.");
64
65 BasicEnumEntry entry;
66 entry.nameIndex = m_nameBuffer.size();
67 entry.nameSize = valueName.size();
68 entry.value = enumValue;
69
70 m_nameBuffer.append(valueName);
71 PH_ASSERT_EQ(entry.nameIndex + entry.nameSize, m_nameBuffer.size());
72
73 const std::size_t entryIndex = m_entries.size();
74 m_entries.pushBack(entry);
75 setEntryDescription(entryIndex, std::move(description));
76
77 return *this;
78 }
79
80 inline TEntry<EnumType> getTypedEntry(const std::size_t entryIndex) const
81 {
82 const BasicEnumEntry& basicEntry = m_entries[entryIndex];
83
84 const std::string_view entryName(
85 m_nameBuffer.data() + basicEntry.nameIndex,
86 basicEntry.nameSize);
87
88 return {entryName, basicEntry.value};
89 }
90
96 inline TEntry<EnumType> getTypedEntry(const std::string_view entryName) const
97 {
98 // Brute-force search for matched enum entry name
99 for(std::size_t entryIdx = 0; entryIdx < m_entries.size(); ++entryIdx)
100 {
101 const TEntry<EnumType> entry = getTypedEntry(entryIdx);
102 if(entry.name == entryName)
103 {
104 return entry;
105 }
106 }
107
108 throw SdlLoadError("use of invalid enum entry name <" + std::string(entryName) + ">");
109 }
110
116 inline TEntry<EnumType> getTypedEntry(const EnumType enumValue) const
117 {
118 // Brute-force search for matched enum entry value
119 for(std::size_t entryIdx = 0; entryIdx < m_entries.size(); ++entryIdx)
120 {
121 const TEntry<EnumType> entry = getTypedEntry(entryIdx);
122 if(entry.value == enumValue)
123 {
124 return entry;
125 }
126 }
127
128 throw SdlLoadError("use of invalid enum value: " + enum_to_string(enumValue));
129 }
130
131 inline TSdlGeneralEnum& description(std::string descriptionStr)
132 {
133 setDescription(std::move(descriptionStr));
134 return *this;
135 }
136
137private:
138 struct BasicEnumEntry
139 {
140 std::size_t nameIndex;
141 std::size_t nameSize;
142 EnumType value;
143
144 inline BasicEnumEntry() :
145 nameIndex(0), nameSize(0), value(static_cast<EnumType>(0))
146 {}
147 };
148
149 std::string m_nameBuffer;
150 TArrayVector<BasicEnumEntry, MAX_ENTRIES> m_entries;
151};
152
153}// end namespace ph
Describes enum in SDL.
Definition SdlEnum.h:22
SdlEnum & setDescription(std::string description)
Definition SdlEnum.h:68
SdlEnum & setEntryDescription(std::size_t entryIndex, std::string description)
Definition SdlEnum.h:75
Error on the SDL input process.
Definition sdl_exceptions.h:22
SDL enum implementation with common features. Enum value and string mapping are done in a brute-force...
Definition TSdlGeneralEnum.h:26
TSdlGeneralEnum & description(std::string descriptionStr)
Definition TSdlGeneralEnum.h:131
TSdlGeneralEnum(std::string name)
Definition TSdlGeneralEnum.h:36
TEntry< EnumType > getTypedEntry(const std::size_t entryIndex) const
Definition TSdlGeneralEnum.h:80
InEnumType EnumType
Definition TSdlGeneralEnum.h:30
TSdlGeneralEnum & addEntry(const EnumType enumValue, const std::string_view valueName, std::string description="")
Definition TSdlGeneralEnum.h:57
Entry getEntry(const std::size_t entryIndex) const override
Definition TSdlGeneralEnum.h:44
std::size_t numEntries() const override
Definition TSdlGeneralEnum.h:52
TEntry< EnumType > getTypedEntry(const EnumType enumValue) const
Get an enum entry via an enum value. Note that the method cannot distinguish between identical enum v...
Definition TSdlGeneralEnum.h:116
TEntry< EnumType > getTypedEntry(const std::string_view entryName) const
Get an enum entry via an enum name. Note that the method cannot distinguish between identical enum na...
Definition TSdlGeneralEnum.h:96
The root for all renderer implementations.
Definition EEngineProject.h:6
std::string enum_to_string(const EnumType enumValue)
Definition utility.h:173
Definition TAABB2D.h:96
Definition SdlEnum.h:26
ValueType value
Definition SdlEnum.h:28
std::string_view name
Definition SdlEnum.h:27