Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef HCCLV2_ENUM_FACTORY_H
12 : #define HCCLV2_ENUM_FACTORY_H
13 :
14 : #include "string_util.h"
15 :
16 : #include <string>
17 : #include <sstream>
18 :
19 : #define MAKE_ENUM(enumClass, ...) \
20 : class enumClass { \
21 : public: \
22 : enum Value : uint8_t { __VA_ARGS__, __COUNT__, INVALID }; \
23 : \
24 : enumClass() \
25 : { \
26 : } \
27 : \
28 : constexpr enumClass(Value v) : value(v) \
29 : { \
30 : } \
31 : \
32 : constexpr operator Value() const \
33 : { \
34 : return value; \
35 : } \
36 : \
37 : constexpr bool operator==(enumClass a) const \
38 : { \
39 : return value == a.value; \
40 : } \
41 : \
42 : constexpr bool operator!=(enumClass a) const \
43 : { \
44 : return value != a.value; \
45 : } \
46 : \
47 : constexpr bool operator<(enumClass a) const \
48 : { \
49 : return value < a.value; \
50 : } \
51 : \
52 : constexpr bool operator==(Value v) const \
53 : { \
54 : return value == v; \
55 : } \
56 : \
57 : constexpr bool operator!=(Value v) const \
58 : { \
59 : return value != v; \
60 : } \
61 : \
62 : constexpr bool operator<(Value v) const \
63 : { \
64 : return value < v; \
65 : } \
66 : \
67 : std::string Describe() const \
68 : { \
69 : static std::vector<std::string> m = InitStrs(); \
70 : if (value >= m.size()) \
71 : return std::string(#enumClass) + "::Invalid"; \
72 : return m[value]; \
73 : } \
74 : \
75 : friend std::ostream &operator<<(std::ostream &stream, const enumClass &v) \
76 : { \
77 : return stream << v.Describe(); \
78 : } \
79 : \
80 : private: \
81 : Value value{INVALID}; \
82 : \
83 : static std::vector<std::string> InitStrs() \
84 : { \
85 : std::vector<std::string> strings; \
86 : std::string s = #__VA_ARGS__; \
87 : std::string token; \
88 : for (char c : s) { \
89 : if (c == ' ' || c == ',') { \
90 : if (!token.empty()) { \
91 : strings.push_back({std::string(#enumClass) + "::" + token}); \
92 : token.clear(); \
93 : } \
94 : } else { \
95 : token += c; \
96 : } \
97 : } \
98 : if (!token.empty()) \
99 : strings.push_back({std::string(#enumClass) + "::" + token}); \
100 : return strings; \
101 : } \
102 : };
103 :
104 : namespace std {
105 : struct EnumClassHash {
106 629626 : template <typename T> std::size_t operator()(T t) const
107 : {
108 629626 : return static_cast<std::size_t>(t);
109 : }
110 : };
111 : } // namespace std
112 :
113 : #endif // HCCLV2_ENUM_FACTORY_H
|