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