1 /*****************************************************************************
2  * Copyright (c) 2014-2020 OpenRCT2 developers
3  *
4  * For a complete list of all authors, please refer to contributors.md
5  * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
6  *
7  * OpenRCT2 is licensed under the GNU General Public License version 3.
8  *****************************************************************************/
9 
10 #pragma once
11 
12 #include "../core/String.hpp"
13 
14 #include <initializer_list>
15 #include <utility>
16 #include <vector>
17 
18 template<typename T> struct ConfigEnumEntry
19 {
20     std::string Key;
21     T Value;
22 
ConfigEnumEntryConfigEnumEntry23     ConfigEnumEntry(std::string key, T value)
24         : Key(std::move(key))
25         , Value(value)
26     {
27     }
28 };
29 
30 template<typename T> struct IConfigEnum
31 {
32     virtual ~IConfigEnum() = default;
33     virtual std::string GetName(T value) const abstract;
34     virtual T GetValue(const std::string& key, T defaultValue) const abstract;
35 };
36 
37 template<typename T> class ConfigEnum final : public IConfigEnum<T>
38 {
39 private:
40     const std::vector<ConfigEnumEntry<T>> _entries;
41 
42 public:
ConfigEnum(const std::initializer_list<ConfigEnumEntry<T>> & entries)43     ConfigEnum(const std::initializer_list<ConfigEnumEntry<T>>& entries)
44         : _entries(entries)
45     {
46     }
47 
GetName(T value) const48     std::string GetName(T value) const override
49     {
50         for (const auto& entry : _entries)
51         {
52             if (entry.Value == value)
53             {
54                 return entry.Key;
55             }
56         }
57         return std::string();
58     }
59 
GetValue(const std::string & key,T defaultValue) const60     T GetValue(const std::string& key, T defaultValue) const override
61     {
62         for (const auto& entry : _entries)
63         {
64             if (String::Equals(entry.Key, key, true))
65             {
66                 return entry.Value;
67             }
68         }
69         return defaultValue;
70     }
71 };
72