wren
Vulkan-based game engine
Loading...
Searching...
No Matches
enums.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <boost/algorithm/string.hpp>
4#include <boost/describe.hpp>
5#include <string>
6
7#define DESCRIBED_ENUM(E, ...) \
8 enum class E { __VA_ARGS__ }; \
9 BOOST_DESCRIBE_ENUM(E, __VA_ARGS__)
10
11namespace wren::utils {
12
13template <class E>
14auto enum_to_string(E e) -> std::string {
15 std::string r = "(unamed)";
16 boost::mp11::mp_for_each<boost::describe::describe_enumerators<E>>(
17 [&](auto d) {
18 if (e == d.value) r = std::string(d.name);
19 });
20 return r;
21}
22
23template <typename E>
24auto string_to_enum(const std::string_view& name, bool case_insensitive = false)
25 -> std::optional<E> {
26 bool found = false;
27
28 E r = {};
29
30 boost::mp11::mp_for_each<boost::describe::describe_enumerators<E>>(
31 [&](auto d) {
32 const std::string described = d.name;
33 if (!found) {
34 if (case_insensitive && boost::iequals(name, described)) {
35 found = true;
36 r = d.value;
37 } else if (!case_insensitive && name == described) {
38 found = true;
39 r = d.value;
40 }
41 }
42 });
43
44 if (found) {
45 return r;
46 }
47
48 return std::nullopt;
49}
50
51} // namespace wren::utils
Definition binray_reader.hpp:6
auto string_to_enum(const std::string_view &name, bool case_insensitive=false) -> std::optional< E >
Definition enums.hpp:24
auto enum_to_string(E e) -> std::string
Definition enums.hpp:14