dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
EnumString.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <format>
13#include <optional>
14#include <string>
15#include <string_view>
16#include <unordered_map>
17#include <vector>
18
19// Project includes
20#include "CompilerSupport.hpp"
21#include "Macro.hpp"
22
74
75#ifndef DOXYGEN_ONLY
76#define _BUILDER_ENUM_TO_STRING_MAPPING(v, l) case v: return l;
77#define ENUM_TO_STRING_GENERATE_MAPPING(...) GENERATOR_2_X(_BUILDER_ENUM_TO_STRING_MAPPING, __VA_ARGS__)
78
79#define _BUILDER_ENUM_FROM_STRING_MAPPING(v, l) { v, l },
80#define ENUM_FROM_STRING_GENERATE_MAPPING(...) GENERATOR_2_X(_BUILDER_ENUM_FROM_STRING_MAPPING, __VA_ARGS__)
81
82#define _BUILDER_ENUM_STRING_ONE_OUT_OF_TWO_1(v, l) v,
83#define ENUM_STRING_ONE_OUT_OF_TWO_1(...) GENERATOR_2_X(_BUILDER_ENUM_STRING_ONE_OUT_OF_TWO_1, __VA_ARGS__)
84
85#define _BUILDER_ENUM_STRING_ONE_OUT_OF_TWO_2(v, l) l,
86#define ENUM_STRING_ONE_OUT_OF_TWO_2(...) GENERATOR_2_X(_BUILDER_ENUM_STRING_ONE_OUT_OF_TWO_2, __VA_ARGS__)
87#endif
88
103#define ENUM_TO_STRING_IMPL(E, ...) \
104 namespace dfx::Enum \
105 { \
106 [[nodiscard]] std::string_view toStringView(E e) noexcept \
107 { \
108 switch (e) \
109 { \
110 ENUM_TO_STRING_GENERATE_MAPPING(__VA_ARGS__) \
111 } \
112 UNREACHABLE_IMPL(); \
113 } \
114 [[nodiscard]] std::string toString(E e) noexcept \
115 { return std::string(toStringView(e)); } \
116 } // !namespace dfx::Enum
117
118#ifdef DOXYGEN_ONLY
119namespace dfx::Enum
120{
122 template<typename E>
123 [[nodiscard]] std::string_view toStringView(E e) noexcept;
125 template<typename E>
126 [[nodiscard]] std::string toString(E e) noexcept;
127}
128#endif
129
137namespace dfx::Enum
138{
143 template<typename E>
144 [[nodiscard]] std::optional<E> fromStringView(std::string_view) noexcept = delete;
145
150 template<typename E>
151 [[nodiscard]] std::optional<E> fromString(std::string const &) noexcept = delete;
152} // !namespace dfx::Enum
153
163#define ENUM_FROM_STRING_IMPL(E) \
164 namespace dfx::Enum \
165 { \
166 template<> \
167 [[nodiscard]] std::optional<E> fromStringView(std::string_view str) noexcept \
168 { \
169 auto const & map = allValuesMapView<E>(); \
170 auto const itr = map.find(str); \
171 if (itr == map.end()) \
172 return std::nullopt; \
173 return itr->second; \
174 } \
175 template<> \
176 [[nodiscard]] std::optional<E> fromString(std::string const & str) noexcept \
177 { return fromStringView<E>(str); } \
178 } // !namespace dfx::Enum
179
180/* Get all enum mapping values function */
181namespace dfx::Enum
182{
187 template<typename E>
188 [[nodiscard]] std::unordered_map<std::string, E> const & allValuesMap() noexcept = delete;
189
195 template<typename E>
196 [[nodiscard]] std::unordered_map<std::string_view, E> const & allValuesMapView() noexcept = delete;
197} // !namespace dfx::Enum
198
208#define ENUM_ALL_VALUES_MAP_IMPL(E, ...) \
209 namespace dfx::Enum \
210 { \
211 template<> \
212 [[nodiscard]] std::unordered_map<std::string, E> const & allValuesMap() noexcept \
213 { \
214 static std::unordered_map<std::string, E> const map = { \
215 ENUM_FROM_STRING_GENERATE_MAPPING(__VA_ARGS__) \
216 }; \
217 return map; \
218 } \
219 template<> \
220 [[nodiscard]] std::unordered_map<std::string_view, E> const & allValuesMapView() noexcept \
221 { \
222 static std::unordered_map<std::string_view, E> const map = { \
223 ENUM_FROM_STRING_GENERATE_MAPPING(__VA_ARGS__) \
224 }; \
225 return map; \
226 } \
227 } // !namespace dfx::Enum
228
229/* Get all enum values function */
230namespace dfx::Enum
231{
234 template<typename E>
235 [[nodiscard]] std::vector<E> const & allValues() noexcept = delete;
236
239 template<typename E>
240 [[nodiscard]] std::vector<std::string_view> const & allValuesStringView() noexcept = delete;
241} // !namespace dfx::Enum
242
252#define ENUM_ALL_VALUES_IMPL(E, ...) \
253 namespace dfx::Enum \
254 { \
255 template<> \
256 [[nodiscard]] std::vector<E> const & allValues() noexcept \
257 { \
258 static std::vector<E> const vec = { \
259 ENUM_STRING_ONE_OUT_OF_TWO_1(__VA_ARGS__) \
260 }; \
261 return vec; \
262 } \
263 template<> \
264 [[nodiscard]] std::vector<std::string_view> const & allValuesStringView<E>() noexcept \
265 { \
266 static std::vector<std::string_view> const vec = { \
267 ENUM_STRING_ONE_OUT_OF_TWO_2(__VA_ARGS__) \
268 }; \
269 return vec; \
270 } \
271 } // !namespace dfx::Enum
272
284#define ENUM_STD_FMT_FORMATTER(E) \
285template<> \
286struct std::formatter<E> : public formatter<std::string_view> \
287{ \
288 auto format(E const & e, format_context & ctx) const \
289 { return formatter<std::string_view>::format(dfx::Enum::toStringView(e), ctx); } \
290}
291
307#define DEFINE_ENUM_STRING(Enum, ...) \
308 ENUM_TO_STRING_IMPL(Enum, __VA_ARGS__) \
309 ENUM_FROM_STRING_IMPL(Enum) \
310 ENUM_ALL_VALUES_MAP_IMPL(Enum, SWAP_POSITION(__VA_ARGS__)) \
311 ENUM_ALL_VALUES_IMPL(Enum, __VA_ARGS__)
312
327#define DECLARE_ENUM_STRING_FUNCTIONS(E) \
328 namespace dfx::Enum \
329 { \
330 [[nodiscard]] std::string_view toStringView(E e) noexcept; \
331 [[nodiscard]] std::string toString(E e) noexcept; \
332 template<> [[nodiscard]] std::optional<E> fromStringView(std::string_view str) noexcept; \
333 template<> [[nodiscard]] std::optional<E> fromString(std::string const & str) noexcept; \
334 template<> [[nodiscard]] std::unordered_map<std::string_view, E> const & allValuesMapView() noexcept; \
335 template<> [[nodiscard]] std::unordered_map<std::string, E> const & allValuesMap() noexcept; \
336 template<> [[nodiscard]] std::vector<E> const & allValues() noexcept; \
337 template<> [[nodiscard]] std::vector<std::string_view> const & allValuesStringView<E>() noexcept; \
338 } \
339 ENUM_STD_FMT_FORMATTER(E)
Enum string conversion and enumeration utilities.
Definition EnumString.hpp:120
std::string toString(E e) noexcept
Convert an enum value into a string.
std::unordered_map< std::string, E > const & allValuesMap() noexcept=delete
Map from label (owned string) to enum value for type E.
std::vector< std::string_view > const & allValuesStringView() noexcept=delete
Return all labels registered for E, in the order provided to the macro. Deleted by default; provided ...
std::unordered_map< std::string_view, E > const & allValuesMapView() noexcept=delete
Map from label (string_view) to enum value for type E.
std::optional< E > fromString(std::string const &) noexcept=delete
Convert a string to an enum value.
std::vector< E > const & allValues() noexcept=delete
Return all enum values registered for E, in the order provided to the macro. Deleted by default; prov...
std::string_view toStringView(E e) noexcept
Convert an enum value into a string view.
std::optional< E > fromStringView(std::string_view) noexcept=delete
Convert a string view to an enum value.
STL namespace.