Skip to content

Commit a2e25b9

Browse files
committed
Refactor enum strings.
1 parent d273d84 commit a2e25b9

26 files changed

+122
-212
lines changed

Common/Cpp/EnumStringMap.h

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
#include <string>
1111
#include <initializer_list>
12-
#include "Containers/Pimpl.h"
12+
#include <map>
13+
#include "Common/Cpp/Exceptions.h"
1314

1415
namespace PokemonAutomation{
1516

@@ -18,16 +19,49 @@ namespace PokemonAutomation{
1819
template <typename EnumType>
1920
class EnumStringMap{
2021
public:
21-
EnumStringMap(std::initializer_list<std::pair<const EnumType, std::string>> x);
22-
~EnumStringMap();
22+
EnumStringMap(std::initializer_list<std::pair<const EnumType, std::string>> x)
23+
: m_enum_to_string(std::move(x))
24+
{
25+
for (const auto& item : m_enum_to_string){
26+
auto ret = m_string_to_enum.emplace(item.second, item.first);
27+
if (!ret.second){
28+
throw InternalProgramError(
29+
nullptr, PA_CURRENT_FUNCTION,
30+
"Duplicate Enum String: " + item.second
31+
);
32+
}
33+
}
34+
}
2335

24-
const std::string& get(EnumType type);
25-
EnumType get(const std::string& str);
26-
EnumType get(const std::string& str, EnumType default_value);
36+
const std::string& get_string(EnumType type) const{
37+
auto iter = m_enum_to_string.find(type);
38+
if (iter == m_enum_to_string.end()){
39+
throw ParseException("Invalid Enum: " + std::to_string((uint64_t)type));
40+
}
41+
return iter->second;
42+
}
43+
EnumType get_enum(const std::string& str) const{
44+
auto iter = m_string_to_enum.find(str);
45+
if (iter == m_string_to_enum.end()){
46+
throw ParseException("Invalid Enum String: " + str);
47+
}
48+
return iter->second;
49+
}
50+
EnumType get_enum(const std::string& str, EnumType default_value) const{
51+
auto iter = m_string_to_enum.find(str);
52+
return iter == m_string_to_enum.end() ? default_value : iter->second;
53+
}
54+
55+
auto begin() const{
56+
return m_enum_to_string.begin();
57+
}
58+
auto end() const{
59+
return m_enum_to_string.end();
60+
}
2761

2862
private:
29-
struct Data;
30-
Pimpl<Data> m_data;
63+
std::map<EnumType, std::string> m_enum_to_string;
64+
std::map<std::string, EnumType> m_string_to_enum;
3165
};
3266

3367

Common/Cpp/EnumStringMap.tpp

Lines changed: 0 additions & 69 deletions
This file was deleted.

SerialPrograms/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ file(GLOB MAIN_SOURCES
9898
../Common/Cpp/CpuId/CpuId_x86.h
9999
../Common/Cpp/CpuId/CpuId_x86.tpp
100100
../Common/Cpp/DateTime.h
101-
../Common/Cpp/EnumStringMap.cpp
102101
../Common/Cpp/EnumStringMap.h
103102
../Common/Cpp/EventRateTracker.h
104103
../Common/Cpp/Exceptions.cpp

SerialPrograms/SerialPrograms.pro

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,6 @@ HEADERS += \
11631163
../Common/Cpp/CpuId/CpuId_x86.tpp \
11641164
../Common/Cpp/DateTime.h \
11651165
../Common/Cpp/EnumStringMap.h \
1166-
../Common/Cpp/EnumStringMap.tpp \
11671166
../Common/Cpp/EventRateTracker.h \
11681167
../Common/Cpp/Exceptions.h \
11691168
../Common/Cpp/ExpressionEvaluator.h \

SerialPrograms/Source/Controllers/ControllerCapability.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,25 @@
44
*
55
*/
66

7-
#include "Common/Cpp/EnumStringMap.tpp"
87
#include "ControllerCapability.h"
98

109
namespace PokemonAutomation{
1110

12-
template class EnumStringMap<ControllerType>;
1311

1412

15-
16-
EnumStringMap<ControllerInterface> CONTROLLER_INTERFACE_STRINGS{
13+
const EnumStringMap<ControllerInterface> CONTROLLER_INTERFACE_STRINGS{
1714
{ControllerInterface::SerialPABotBase, "SerialPABotBase"},
1815
};
1916

20-
EnumStringMap<ControllerType> CONTROLLER_TYPE_STRINGS{
17+
const EnumStringMap<ControllerType> CONTROLLER_TYPE_STRINGS{
2118
{ControllerType::None, "None"},
2219
{ControllerType::NintendoSwitch_WiredProController, "NintendoSwitch_WiredProController"},
2320
{ControllerType::NintendoSwitch_WirelessProController, "NintendoSwitch_WirelessProController"},
2421
{ControllerType::NintendoSwitch_LeftJoycon, "NintendoSwitch_LeftJoycon"},
2522
{ControllerType::NintendoSwitch_RightJoycon, "NintendoSwitch_RightJoycon"},
2623
};
2724

28-
EnumStringMap<ControllerFeature> CONTROLLER_FEATURE_STRINGS{
25+
const EnumStringMap<ControllerFeature> CONTROLLER_FEATURE_STRINGS{
2926
{ControllerFeature::TickPrecise, "TickPrecise"},
3027
{ControllerFeature::QueryTickSize, "QueryTickSize"},
3128
{ControllerFeature::QueryCommandQueueSize, "QueryCommandQueueSize"},
@@ -48,7 +45,7 @@ std::string ControllerRequirements::check_compatibility(const std::set<Controlle
4845

4946
for (ControllerFeature feature : m_features){
5047
if (features.find(feature) == features.end()){
51-
return CONTROLLER_FEATURE_STRINGS.get(feature);
48+
return CONTROLLER_FEATURE_STRINGS.get_string(feature);
5249
}
5350
}
5451
return "";

SerialPrograms/Source/Controllers/ControllerCapability.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace PokemonAutomation{
3636
enum class ControllerInterface{
3737
SerialPABotBase,
3838
};
39-
extern EnumStringMap<ControllerInterface> CONTROLLER_INTERFACE_STRINGS;
39+
extern const EnumStringMap<ControllerInterface> CONTROLLER_INTERFACE_STRINGS;
4040

4141

4242
enum class ControllerType{
@@ -46,7 +46,7 @@ enum class ControllerType{
4646
NintendoSwitch_LeftJoycon,
4747
NintendoSwitch_RightJoycon,
4848
};
49-
extern EnumStringMap<ControllerType> CONTROLLER_TYPE_STRINGS;
49+
extern const EnumStringMap<ControllerType> CONTROLLER_TYPE_STRINGS;
5050

5151

5252
enum class ControllerFeature{
@@ -56,7 +56,7 @@ enum class ControllerFeature{
5656
NintendoSwitch_ProController,
5757
NintendoSwitch_DateSkip,
5858
};
59-
extern EnumStringMap<ControllerFeature> CONTROLLER_FEATURE_STRINGS;
59+
extern const EnumStringMap<ControllerFeature> CONTROLLER_FEATURE_STRINGS;
6060

6161

6262

SerialPrograms/Source/Controllers/SerialPABotBase/SerialPABotBase_Interface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ std::unique_ptr<AbstractController> SerialPABotBase_Interface::make_controller(
109109

110110
throw InternalProgramError(
111111
nullptr, PA_CURRENT_FUNCTION,
112-
std::string("Unsupported Controller Type: ") + CONTROLLER_TYPE_STRINGS.get(controller_type)
112+
std::string("Unsupported Controller Type: ") + CONTROLLER_TYPE_STRINGS.get_string(controller_type)
113113
);
114114
}
115115

SerialPrograms/Source/Pokemon/Pokemon_IvJudge.cpp

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,26 @@
44
*
55
*/
66

7-
#include <map>
8-
//#include "Common/Cpp/Exceptions.h"
97
#include "Pokemon_IvJudge.h"
108

119
namespace PokemonAutomation{
1210
namespace Pokemon{
1311

1412

15-
const std::map<std::string, IvJudgeValue> IVCheckerValue_TOKEN_TO_ENUM{
16-
{"No Good", IvJudgeValue::NoGood},
17-
{"Decent", IvJudgeValue::Decent},
18-
{"Pretty Good", IvJudgeValue::PrettyGood},
19-
{"Very Good", IvJudgeValue::VeryGood},
20-
{"Fantastic", IvJudgeValue::Fantastic},
21-
{"Best", IvJudgeValue::Best},
22-
{"Hyper trained!", IvJudgeValue::HyperTrained},
23-
};
24-
const std::map<IvJudgeValue, std::string> IVCheckerValue_ENUM_TO_TOKEN{
25-
{IvJudgeValue::UnableToDetect, "Unable to Detect"},
26-
{IvJudgeValue::NoGood, "No Good"},
27-
{IvJudgeValue::Decent, "Decent"},
28-
{IvJudgeValue::PrettyGood, "Pretty Good"},
29-
{IvJudgeValue::VeryGood, "Very Good"},
30-
{IvJudgeValue::Fantastic, "Fantastic"},
31-
{IvJudgeValue::Best, "Best"},
32-
{IvJudgeValue::HyperTrained, "Hyper trained!"},
33-
};
34-
IvJudgeValue IvJudgeValue_string_to_enum(const std::string& token){
35-
auto iter = IVCheckerValue_TOKEN_TO_ENUM.find(token);
36-
if (iter == IVCheckerValue_TOKEN_TO_ENUM.end()){
37-
return IvJudgeValue::UnableToDetect;
38-
}
39-
return iter->second;
40-
}
41-
4213

14+
const EnumStringMap<IvJudgeValue>& IV_JUDGE_VALUE_STRINGS(){
15+
static EnumStringMap<IvJudgeValue> database{
16+
{IvJudgeValue::UnableToDetect, "Unable to Detect"},
17+
{IvJudgeValue::NoGood, "No Good"},
18+
{IvJudgeValue::Decent, "Decent"},
19+
{IvJudgeValue::PrettyGood, "Pretty Good"},
20+
{IvJudgeValue::VeryGood, "Very Good"},
21+
{IvJudgeValue::Fantastic, "Fantastic"},
22+
{IvJudgeValue::Best, "Best"},
23+
{IvJudgeValue::HyperTrained, "Hyper trained!"},
24+
};
25+
return database;
26+
}
4327
const EnumDropdownDatabase<IvJudgeValue>& IvJudgeValue_Database(){
4428
static EnumDropdownDatabase<IvJudgeValue> database({
4529
{IvJudgeValue::NoGood, "no-good", "No Good (0)"},

SerialPrograms/Source/Pokemon/Pokemon_IvJudge.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#ifndef PokemonAutomation_Pokemon_IvJudge_H
88
#define PokemonAutomation_Pokemon_IvJudge_H
99

10-
#include <string>
10+
#include "Common/Cpp/EnumStringMap.h"
1111
#include "Common/Cpp/Options/EnumDropdownDatabase.h"
1212

1313
namespace PokemonAutomation{
@@ -24,8 +24,8 @@ enum class IvJudgeValue{
2424
Best,
2525
HyperTrained,
2626
};
27+
const EnumStringMap<IvJudgeValue>& IV_JUDGE_VALUE_STRINGS();
2728
const EnumDropdownDatabase<IvJudgeValue>& IvJudgeValue_Database();
28-
IvJudgeValue IvJudgeValue_string_to_enum(const std::string& token);
2929

3030

3131
enum class IvJudgeFilter{

SerialPrograms/Source/Pokemon/Pokemon_NatureChecker.cpp

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,9 @@
1010
namespace PokemonAutomation{
1111
namespace Pokemon{
1212

13-
const std::map<std::string, NatureCheckerValue>& NatureCheckerValue_TOKEN_TO_ENUM(){
14-
static std::map<std::string, NatureCheckerValue> data{
15-
{"Adamant", NatureCheckerValue::Adamant},
16-
{"Bashful", NatureCheckerValue::Bashful},
17-
{"Bold", NatureCheckerValue::Bold},
18-
{"Brave", NatureCheckerValue::Brave},
19-
{"Calm", NatureCheckerValue::Calm},
20-
{"Careful", NatureCheckerValue::Careful},
21-
{"Docile", NatureCheckerValue::Docile},
22-
{"Gentle", NatureCheckerValue::Gentle},
23-
{"Hardy", NatureCheckerValue::Hardy},
24-
{"Hasty", NatureCheckerValue::Hasty},
25-
{"Impish", NatureCheckerValue::Impish},
26-
{"Jolly", NatureCheckerValue::Jolly},
27-
{"Lax", NatureCheckerValue::Lax},
28-
{"Lonely", NatureCheckerValue::Lonely},
29-
{"Mild", NatureCheckerValue::Mild},
30-
{"Modest", NatureCheckerValue::Modest},
31-
{"Naive", NatureCheckerValue::Naive},
32-
{"Naughty", NatureCheckerValue::Naughty},
33-
{"Quiet", NatureCheckerValue::Quiet},
34-
{"Quirky", NatureCheckerValue::Quirky},
35-
{"Rash", NatureCheckerValue::Rash},
36-
{"Relaxed", NatureCheckerValue::Relaxed},
37-
{"Sassy", NatureCheckerValue::Sassy},
38-
{"Serious", NatureCheckerValue::Serious},
39-
{"Timid", NatureCheckerValue::Timid},
40-
};
41-
return data;
42-
}
43-
const std::map<NatureCheckerValue, std::string>& NatureCheckerValue_ENUM_TO_TOKEN(){
44-
static std::map<NatureCheckerValue, std::string> data{
13+
14+
const EnumStringMap<NatureCheckerValue>& NATURE_CHECKER_VALUE_STRINGS(){
15+
static EnumStringMap<NatureCheckerValue> database{
4516
{NatureCheckerValue::UnableToDetect, "Unable to Detect"},
4617
{NatureCheckerValue::Neutral, "Neutral"},
4718
{NatureCheckerValue::Adamant, "Adamant"},
@@ -69,8 +40,10 @@ const std::map<NatureCheckerValue, std::string>& NatureCheckerValue_ENUM_TO_TOKE
6940
{NatureCheckerValue::Serious, "Serious"},
7041
{NatureCheckerValue::Timid, "Timid"},
7142
};
72-
return data;
43+
return database;
7344
}
45+
46+
7447
const std::map<std::pair<int, int>, NatureCheckerValue>& NatureCheckerValue_HELPHINDER_TO_ENUM(){
7548
static std::map<std::pair<int, int>, NatureCheckerValue> data{
7649
{{ 0, 2 }, NatureCheckerValue::Adamant},
@@ -98,13 +71,6 @@ const std::map<std::pair<int, int>, NatureCheckerValue>& NatureCheckerValue_HELP
9871
return data;
9972
}
10073

101-
NatureCheckerValue NatureCheckerValue_string_to_enum(const std::string& token){
102-
auto iter = NatureCheckerValue_TOKEN_TO_ENUM().find(token);
103-
if (iter == NatureCheckerValue_TOKEN_TO_ENUM().end()){
104-
return NatureCheckerValue::UnableToDetect;
105-
}
106-
return iter->second;
107-
}
10874

10975
NatureCheckerValue NatureCheckerValue_helphinder_to_enum(const std::pair<int,int>& token){
11076
auto iter = NatureCheckerValue_HELPHINDER_TO_ENUM().find(token);

0 commit comments

Comments
 (0)