-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum.hpp
More file actions
105 lines (79 loc) · 2.67 KB
/
Copy pathenum.hpp
File metadata and controls
105 lines (79 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* File: /lib/utils/enum.hpp
*
* This file is a part of cpplib project which is distributed under MIT License.
* See file LICENSE for full license details.
*
* Copyright (c) 2020-present Nikita Zuev (V.Slavski!) <[email protected]>
*/
#ifndef CPPLIB__lib__utils__enum__hpp
#define CPPLIB__lib__utils__enum__hpp
#include <type_traits>
#include <functional>
#include <cpp/lib_concepts>
namespace lib::utils {
// DECLARATION LIB_UTILS_ENUM
/// @note 'count' value includes, 'invalid' is always zero.
#define LIB_UTILS_ENUM( EnumSpec, ... ) \
enum class EnumSpec { \
invalid = 0, \
__VA_ARGS__, \
count \
};
// DECLARATION lib::utils::EnumId<>
template< ::cpp::Enumeration E >
using EnumId = ::std::underlying_type_t< E >;
// DECLARATION lib::utils::EnumValue<>()
template< ::cpp::Enumeration E >
inline constexpr EnumId<E> EnumValue( E e ) noexcept { return (EnumId<E>)e; }
// DECLARATION lib::utils::NAMES<>[]
template< ::cpp::Enumeration E >
inline constexpr const char * NAMES[ (EnumId<E>) E::count ] = { "<LIB_UTILS_ENUM_NAMES missed>" };
// DECLARATION LIB_UTILS_ENUM_NAMES
#define LIB_UTILS_ENUM_NAMES( Enum, ... ) \
namespace lib::utils { \
template<> \
inline constexpr const char * NAMES<Enum>[ (EnumId<Enum>) Enum::count ] = { \
"<invalid>", \
__VA_ARGS__ \
}; \
} /* namespace lib::utils */
// DECLARATION lib::utils: is_valid<>, foreach<>
template< ::cpp::Enumeration E, ::cpp::Integral I >
inline constexpr bool is_valid( I i ) noexcept {
return i > (EnumId<E>)E::invalid and i < (EnumId<E>)E::count;
}
template< ::cpp::Enumeration E >
inline constexpr bool is_valid( E e ) noexcept {
return is_valid<E>( (EnumId<E>)e );
}
template< ::cpp::Enumeration E >
inline constexpr bool foreach( ::std::function<bool(E, EnumId<E>)> statement ) noexcept {
auto id = (EnumId<E>)E::invalid;
while ( ++id < (EnumId<E>)E::count )
if ( not statement( (E)id, id ) )
return false;
return true;
}
} // namespace lib::utils
#include <string>
#include "../../lib/cstring.hpp"
// DECLARATION lib::str<>, lib::unstr<>
namespace lib {
template< ::cpp::Enumeration E >
inline /*constexpr*/ ::std::string str( E e ) noexcept {
using namespace utils;
if ( is_valid( e ) )
return NAMES<E>[ (EnumId<E>)e ];
return NAMES<E>[ (EnumId<E>)E::invalid ];
}
template< ::cpp::Enumeration E >
inline constexpr E unstr( const cstring & field_name ) noexcept {
using namespace utils;
auto id = (EnumId<E>)E::invalid;
while ( ++id < (EnumId<E>)E::count )
if ( field_name == NAMES<E>[id] )
return (E)id;
return E::invalid;
}
} // namespace lib::utils
#endif // CPPLIB__lib__utils__enum__hpp