Euphoria
enum.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include <map>
5 
6 
7 namespace eu::io
8 {
9  struct FileSystem;
10  struct FilePath;
11 }
12 
13 
14 namespace eu::core
15 {
16  struct EnumValue;
17 
18  // todo(Gustav): as a compile option, use a hash instead of the string enum
19  // todo(Gustav): rename to something like DynEnum to not be confused with regular enums
20 
27  struct EnumType
28  {
29  using ValueToNameMap = std::map<size_t, std::string>;
30  using NameToValueMap = std::map<std::string, size_t>;
31 
32  std::string type_name;
35  bool is_adding;
36  size_t next_index;
37 
38  EnumType(std::string name);
39  ~EnumType();
40 
41  EnumType(const EnumType&) = delete;
42  EnumType(EnumType&&) = delete;
43  void operator=(const EnumType&) = delete;
44  void operator=(EnumType&&) = delete;
45 
46  [[nodiscard]] EnumValue to_enum(const std::string& name);
47  void add_enums(const std::vector<std::string>& names);
48  void add_enum(const std::string& name);
49 
50  [[nodiscard]] std::string to_string(size_t v) const;
51  };
52 
53  struct EnumValue
54  {
55  // todo(Gustav): only have the type in debug/test builds
57  size_t value;
58 
59  EnumValue(EnumType* t, size_t v);
60 
61  // todo(Gustav): add enum_type to the parameter to verify against stored member
62  [[nodiscard]] std::string to_string() const;
63  [[nodiscard]] size_t to_value() const;
64 
65  bool operator==(const EnumValue& other) const;
66  bool operator!=(const EnumValue& other) const;
67  bool operator<(const EnumValue& other) const;
68  };
69 
70 
71  std::string to_string(const EnumValue& v);
72 
73 
74  void
76  (
77  EnumType* type,
78  io::FileSystem* fs,
79  const io::FilePath& path
80  );
81 
82 
83 #define DECLARE_ENUM_TYPE(NAME) EnumType& NAME##_EnumType();
84 #define IMPLEMENT_ENUM_TYPE(NAME) \
85  EnumType& NAME##_EnumType() \
86  { \
87  static EnumType type {#NAME}; \
88  return type; \
89  }
90 
91 
92 // std::string constructor may throw
93 // todo(Gustav): provide compile time option to use hashes instead
94 // http://aras-p.info/blog/2016/08/09/More-Hash-Function-Tests/
95 #define DEFINE_ENUM_VALUE(TYPE, NAME, STRING) \
96  const ::eu::core::EnumValue NAME = TYPE##_EnumType().to_enum(STRING)
97 #define SET_ENUM_FROM_FILE(FS, PATH, TYPE) \
98  load_enum_type(&TYPE##_EnumType(), FS, PATH)
99 
100 }
ADD_DEFAULT_FORMATTER(eu::core::EnumValue, std::string, eu::core::to_string)
std::string to_string(const EnumValue &v)
Definition: enum.cc:175
void load_enum_type(EnumType *type, io::FileSystem *fs, const io::FilePath &path)
Definition: enum.cc:183
Definition: enum.h:8
Represents a dynamically loaded "enum type".
Definition: enum.h:28
EnumType(EnumType &&)=delete
std::map< size_t, std::string > ValueToNameMap
Definition: enum.h:29
NameToValueMap name_to_value
Definition: enum.h:34
EnumType(const EnumType &)=delete
EnumValue to_enum(const std::string &name)
Definition: enum.cc:61
void add_enum(const std::string &name)
Definition: enum.cc:112
void add_enums(const std::vector< std::string > &names)
Definition: enum.cc:81
void operator=(const EnumType &)=delete
std::map< std::string, size_t > NameToValueMap
Definition: enum.h:30
void operator=(EnumType &&)=delete
std::string type_name
Definition: enum.h:32
EnumType(std::string name)
Definition: enum.cc:26
bool is_adding
Definition: enum.h:35
std::string to_string(size_t v) const
Definition: enum.cc:41
ValueToNameMap value_to_name
Definition: enum.h:33
size_t next_index
Definition: enum.h:36
EnumType * type
Definition: enum.h:56
size_t to_value() const
Definition: enum.cc:146
bool operator<(const EnumValue &other) const
Definition: enum.cc:168
bool operator==(const EnumValue &other) const
Definition: enum.cc:153
bool operator!=(const EnumValue &other) const
Definition: enum.cc:161
EnumValue(EnumType *t, size_t v)
Definition: enum.cc:131
std::string to_string() const
Definition: enum.cc:139
size_t value
Definition: enum.h:57