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_sensitive = 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 if (!found) {
33 if (case_sensitive && boost::iequals(d.name, name)) {
34 found = true;
35 r = d.value;
36 } else if (!case_sensitive && std::strcmp(d.name, name.data())) {
37 found = true;
38 r = d.value;
39 }
40 }
41 });
42
43 if (found) {
44 return r;
45 }
46
47 return std::nullopt;
48}
49
50} // namespace wren::utils
Definition binray_reader.hpp:6
auto string_to_enum(const std::string_view &name, bool case_sensitive=false) -> std::optional< E >
Definition enums.hpp:24
auto enum_to_string(E e) -> std::string
Definition enums.hpp:14