The Sparta Modeling Framework
Loading...
Searching...
No Matches
Enum.hpp
1// <Enum> -*- C++ -*-
2#pragma once
3
4#include <cinttypes>
5#include <string>
6#include <memory>
7#include <vector>
10
11namespace sparta
12{
13namespace utils
14{
20 template<class EnumType>
21 class Enum
22 {
23 friend class Value;
24 static const std::unique_ptr<std::string[]> names_;
25
26
27 public:
28 typedef EnumType value_type;
29
31 // enum constants of the class enum template argument.
32 static void populateNames(std::vector<std::string>& names){
33 for(std::size_t i = 0; i < static_cast<uint32_t>(value_type::__LAST); ++i){
34 names.emplace_back(names_[i]);
35 }
36 }
37
39 {
40 public:
43 {}
44
45 UnknownNameException(const std::string& reason) :
46 SpartaException(reason)
47 {}
48 };
49
50 public:
51 class Value
52 {
53 public:
54 Value(const EnumType& val = EnumType::__FIRST):
55 val_(val)
56 {}
57
58 Value(const Value& other):
59 val_(other.val_)
60 {}
61
62 explicit operator EnumType() const
63 {
64 return val_;
65 }
66
67 operator uint32_t() const
68 {
69 return static_cast<uint32_t>(val_);
70 }
71
72 operator std::string() const
73 {
74 return Enum<EnumType>::names_[static_cast<uint32_t>(val_)];
75 }
76
77 Value& operator=(const Value& other)
78 {
79 val_ = other.val_;
80 return *this;
81 }
82
83 bool operator==(const Value& other) const
84 {
85 return (val_ == other.val_);
86 }
87
88 bool operator!=(const Value& other) const
89 {
90 return (val_ != other.val_);
91 }
92
93 const EnumType & getEnum() const {
94 return val_;
95 }
96
97 private:
98 EnumType val_;
99 };
100
101 public:
103 {
104 public:
105 iterator(const EnumType& val):
106 val_(val)
107 {}
108
109 iterator(const iterator& other):
110 val_(other.val_)
111 {}
112
113 void operator++()
114 {
115 val_ = static_cast<EnumType>(static_cast<uint32_t>(val_) + 1);
116 }
117
118 Value operator*() const
119 {
120 return Value(val_);
121 }
122
123 bool operator==(const iterator& other) const
124 {
125 return (val_ == other.val_);
126 }
127
128 bool operator!=(const iterator& other) const
129 {
130 return (val_ != other.val_);
131 }
132
133 private:
134 EnumType val_;
135 };
136
137 public:
138
139 Enum() {}
140
141 template<class...Args>
142 Enum(const Args&...args)
143 {
144 static_assert(std::is_enum<EnumType>::value,
145 "ERROR: EnumType is not an enum");
146 setName_(args...);
147 }
148
149 Enum(const Enum& other)
150 {
151 (void) other;
152 }
153
154 const Value operator()(const EnumType& val) const
155 {
156 return Value(val);
157 }
158
159 const Value operator()(const uint32_t i) const
160 {
161 return Value(static_cast<EnumType>(i));
162 }
163
164 const Value operator()(const std::string& s) const
165 {
166 // This is OK for small enums when this method is not used in
167 // critical path code. Use a real string matching algorithm
168 // if needed eventually
169 for (uint32_t i = 0; i < static_cast<uint32_t>(EnumType::__LAST); ++i) {
170 if (names_[i] == s) {
171 return this->operator()(i);
172 }
173 }
174 //throw UnknownEnumException("Undeclared Enum value: ") << s;
175 throw UnknownNameException();
176 return Value(EnumType::__LAST);
177 }
178
179 iterator begin() const
180 {
181 return iterator(EnumType::__FIRST);
182 }
183
184 iterator end() const
185 {
186 return iterator(EnumType::__LAST);
187 }
188
189 size_t size() const {
190 return static_cast<uint32_t>(EnumType::__LAST);
191 }
192
193 private:
194 void setName_()
195 {
196 names_[static_cast<uint32_t>(EnumType::__LAST)] = "<<LAST>>";
197 }
198
199 template<class...Args>
200 void setName_(const EnumType& id, const std::string& name, const Args&...args)
201 {
202 names_[static_cast<uint32_t>(id)] = name;
203 setName_(args...);
204 }
205 };
206
207} // utils
208} // sparta
Set of macros for Sparta assertions. Caught by the framework.
Exception class for all of Sparta.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
SpartaException()
Construct a SpartaException object with empty reason.
Class that wraps a C++ class enum and allows conversion between POD and the enum type....
Definition Enum.hpp:22
static void populateNames(std::vector< std::string > &names)
Method which populates an argument vector with string-names of.
Definition Enum.hpp:32
Macros for handling exponential backoff.