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 "vector"
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 : constexpr enumClass(Value v) : value(v) {} \
27 : \
28 : constexpr operator Value() const { return value; } \
29 : \
30 : constexpr bool operator==(enumClass a) const { return value == a.value; } \
31 : \
32 : constexpr bool operator!=(enumClass a) const { return value != a.value; } \
33 : \
34 : constexpr bool operator<(enumClass a) const { return value < a.value; } \
35 : \
36 : constexpr bool operator==(Value v) const { return value == v; } \
37 : \
38 : constexpr bool operator!=(Value v) const { return value != v; } \
39 : \
40 : constexpr bool operator<(Value v) const { return value < v; } \
41 : \
42 : std::string Describe() const \
43 : { \
44 : static std::vector<std::string> m = InitStrs(); \
45 : if (value > m.size()) \
46 : return std::string(#enumClass) + "::Invalid"; \
47 : return m[value]; \
48 : } \
49 : \
50 : friend std::ostream& operator<<(std::ostream& stream, const enumClass& v) { return stream << v.Describe(); } \
51 : \
52 : private: \
53 : Value value{INVALID}; \
54 : \
55 : static std::vector<std::string> InitStrs() \
56 : { \
57 : std::vector<std::string> strings; \
58 : std::string s = #__VA_ARGS__; \
59 : std::string token; \
60 : for (char c : s) { \
61 : if (c == ' ' || c == ',') { \
62 : if (!token.empty()) { \
63 : strings.push_back({std::string(#enumClass) + "::" + token}); \
64 : token.clear(); \
65 : } \
66 : } else { \
67 : token += c; \
68 : } \
69 : } \
70 : if (!token.empty()) \
71 : strings.push_back({std::string(#enumClass) + "::" + token}); \
72 : return strings; \
73 : } \
74 : };
75 :
76 : namespace std {
77 : struct EnumClassHash {
78 : template <typename T>
79 35516 : std::size_t operator()(T t) const
80 : {
81 35516 : return static_cast<std::size_t>(t);
82 : }
83 : };
84 : } // namespace std
85 :
86 : #endif // HCCLV2_ENUM_FACTORY_H
|