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 : #ifndef TRANSPORT_STATUS_H
11 : #define TRANSPORT_STATUS_H
12 :
13 : #include <iostream>
14 : #include <vector>
15 : #include <string>
16 : #include <cstdint>
17 :
18 : namespace Hccl {
19 :
20 : // MAKE_ENUM(TransportStatus, INIT, SOCKET_OK, SOCKET_TIMEOUT, READY)
21 :
22 : class TransportStatus {
23 : public:
24 : // 对应宏里的: enum Value : uint8_t { __VA_ARGS__, __COUNT__, INVALID };
25 : enum Value : uint8_t {
26 : INIT,
27 : SOCKET_OK,
28 : SOCKET_TIMEOUT,
29 : CONNECT_FAILED,
30 : READY,
31 : __COUNT__, // 宏自动生成的计数器
32 : INVALID // 宏自动生成的无效值
33 : };
34 :
35 : // 默认构造函数
36 : TransportStatus() {
37 : }
38 :
39 : // 允许通过 TransportStatus::INIT 等方式构造
40 285 : constexpr TransportStatus(Value v) : value(v) {
41 285 : }
42 :
43 : // 类型转换操作符,允许将对象隐式转换为内部枚举值 (用于 switch 等场景)
44 3 : constexpr operator Value() const {
45 3 : return value;
46 : }
47 :
48 : // ================== 运算符重载 (原样保留) ==================
49 :
50 : constexpr bool operator==(TransportStatus a) const {
51 : return value == a.value;
52 : }
53 :
54 : constexpr bool operator!=(TransportStatus a) const {
55 : return value != a.value;
56 : }
57 :
58 : constexpr bool operator<(TransportStatus a) const {
59 : return value < a.value;
60 : }
61 :
62 : // 针对内部 enum Value 的比较重载
63 103 : constexpr bool operator==(Value v) const {
64 103 : return value == v;
65 : }
66 :
67 70 : constexpr bool operator!=(Value v) const {
68 70 : return value != v;
69 : }
70 :
71 : constexpr bool operator<(Value v) const {
72 : return value < v;
73 : }
74 :
75 : // ================== 描述功能 ==================
76 :
77 : // 返回枚举的字符串描述
78 : // 原宏通过解析字符串实现,这里直接静态定义,性能更好且更直观
79 1 : std::string Describe() const {
80 : static const std::vector<std::string> m = {
81 : "TransportStatus::INIT",
82 : "TransportStatus::SOCKET_OK",
83 : "TransportStatus::SOCKET_TIMEOUT",
84 : "TransportStatus::CONNECT_FAILED",
85 : "TransportStatus::READY"
86 24 : };
87 :
88 : // 边界检查:如果值超出了定义的字符串范围(例如是 __COUNT__ 或 INVALID)
89 : // 原宏中的逻辑是 if (value > m.size()),这里修正为 >= 以防止越界崩溃
90 1 : if (value >= m.size()) {
91 0 : return "TransportStatus::Invalid";
92 : }
93 1 : return m[value];
94 : }
95 :
96 : // 重载输出流操作符,支持 std::cout << status
97 0 : friend std::ostream &operator<<(std::ostream &stream, const TransportStatus &v) {
98 0 : return stream << v.Describe();
99 : }
100 :
101 : private:
102 : // 对应宏里的 private: Value value{INVALID};
103 : Value value{INVALID};
104 : };
105 :
106 : } // namespace Hccl
107 :
108 : #endif // TRANSPORT_STATUS_H
|