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 : // 允许通过 TransportStatus::INIT 等方式构造
39 295 : constexpr TransportStatus(Value v) : value(v) {}
40 :
41 : // 类型转换操作符,允许将对象隐式转换为内部枚举值 (用于 switch 等场景)
42 3 : constexpr operator Value() const { return value; }
43 :
44 : // ================== 运算符重载 (原样保留) ==================
45 :
46 : constexpr bool operator==(TransportStatus a) const { return value == a.value; }
47 :
48 : constexpr bool operator!=(TransportStatus a) const { return value != a.value; }
49 :
50 : constexpr bool operator<(TransportStatus a) const { return value < a.value; }
51 :
52 : // 针对内部 enum Value 的比较重载
53 103 : constexpr bool operator==(Value v) const { return value == v; }
54 :
55 74 : constexpr bool operator!=(Value v) const { return value != v; }
56 :
57 : constexpr bool operator<(Value v) const { return value < v; }
58 :
59 : // ================== 描述功能 ==================
60 :
61 : // 返回枚举的字符串描述
62 : // 原宏通过解析字符串实现,这里直接静态定义,性能更好且更直观
63 1 : std::string Describe() const
64 : {
65 : static const std::vector<std::string> m
66 : = {"TransportStatus::INIT", "TransportStatus::SOCKET_OK", "TransportStatus::SOCKET_TIMEOUT",
67 25 : "TransportStatus::CONNECT_FAILED", "TransportStatus::READY"};
68 :
69 : // 边界检查:如果值超出了定义的字符串范围(例如是 __COUNT__ 或 INVALID)
70 : // 原宏中的逻辑是 if (value > m.size()),这里修正为 >= 以防止越界崩溃
71 1 : if (value >= m.size()) {
72 0 : return "TransportStatus::Invalid";
73 : }
74 1 : return m[value];
75 : }
76 :
77 : // 重载输出流操作符,支持 std::cout << status
78 0 : friend std::ostream& operator<<(std::ostream& stream, const TransportStatus& v) { return stream << v.Describe(); }
79 :
80 : private:
81 : // 对应宏里的 private: Value value{INVALID};
82 : Value value{INVALID};
83 : };
84 :
85 : } // namespace Hccl
86 :
87 : #endif // TRANSPORT_STATUS_H
|