|
1 | 1 | //
|
2 |
| -// Created by wangyz38535 on 2022/6/29. |
| 2 | +// Created by wangyzhou on 2022/6/29. |
3 | 3 | //
|
4 | 4 |
|
5 | 5 | #ifndef DLMS_STATUS_H
|
6 | 6 | #define DLMS_STATUS_H
|
7 | 7 | #include <string>
|
8 | 8 |
|
9 |
| - |
10 | 9 | class Status {
|
11 |
| - public: |
12 |
| - // Create a success status. |
13 |
| - Status() noexcept: state_(nullptr) {} |
| 10 | +public: |
| 11 | + // Create a success status. |
| 12 | + Status() noexcept: state_(nullptr) {} |
14 | 13 |
|
15 |
| - ~Status() { delete[] state_; } |
| 14 | + ~Status() { delete[] state_; } |
16 | 15 |
|
17 |
| - Status(const Status &rhs); |
| 16 | + Status(const Status &rhs); |
18 | 17 |
|
19 |
| - Status &operator=(const Status &rhs); |
| 18 | + Status &operator=(const Status &rhs); |
20 | 19 |
|
21 |
| - Status(Status &&rhs) noexcept: state_(rhs.state_) { rhs.state_ = nullptr; } |
| 20 | + Status(Status &&rhs) noexcept: state_(rhs.state_) { rhs.state_ = nullptr; } |
22 | 21 |
|
23 |
| - Status &operator=(Status &&rhs) noexcept; |
| 22 | + Status &operator=(Status &&rhs) noexcept; |
24 | 23 |
|
25 |
| - // Return a success status. |
26 |
| - static Status OK() { return Status(); } |
| 24 | + // Return a success status. |
| 25 | + static Status OK() { return Status(); } |
27 | 26 |
|
28 |
| - // Return error status of an appropriate type. |
29 |
| - static Status NotFound(const std::string &msg, const std::string &msg2 = std::string()) { |
30 |
| - return Status(kNotFound, msg, msg2); |
31 |
| - } |
| 27 | + // Return error status of an appropriate type. |
| 28 | + static Status NotFound(const std::string &msg, const std::string &msg2 = std::string()) { |
| 29 | + return Status(kNotFound, msg, msg2); |
| 30 | + } |
| 31 | + // something is broken, IE.json format invlaide |
| 32 | + static Status Corruption(const std::string &msg, const std::string &msg2 = std::string()) { |
| 33 | + return Status(kCorruption, msg, msg2); |
| 34 | + } |
32 | 35 |
|
33 |
| - static Status Corruption(const std::string &msg, const std::string &msg2 = std::string()) { |
34 |
| - return Status(kCorruption, msg, msg2); |
35 |
| - } |
| 36 | + static Status NotSupported(const std::string &msg, const std::string &msg2 = std::string()) { |
| 37 | + return Status(kNotSupported, msg, msg2); |
| 38 | + } |
36 | 39 |
|
37 |
| - static Status NotSupported(const std::string &msg, const std::string &msg2 = std::string()) { |
38 |
| - return Status(kNotSupported, msg, msg2); |
39 |
| - } |
| 40 | + static Status InvalidArgument(const std::string &msg, const std::string &msg2 = std::string()) { |
| 41 | + return Status(kInvalidArgument, msg, msg2); |
| 42 | + } |
40 | 43 |
|
41 |
| - static Status InvalidArgument(const std::string &msg, const std::string &msg2 = std::string()) { |
42 |
| - return Status(kInvalidArgument, msg, msg2); |
43 |
| - } |
| 44 | + static Status IOError(const std::string &msg, const std::string &msg2 = std::string()) { |
| 45 | + return Status(kIOError, msg, msg2); |
| 46 | + } |
44 | 47 |
|
45 |
| - static Status IOError(const std::string &msg, const std::string &msg2 = std::string()) { |
46 |
| - return Status(kIOError, msg, msg2); |
47 |
| - } |
| 48 | + static Status OutOfMemory(const std::string &msg, const std::string &msg2 = std::string()) { |
| 49 | + return Status(kOutOfMemory, msg, msg2); |
| 50 | + } |
48 | 51 |
|
49 |
| - // Returns true iff the status indicates success. |
50 |
| - bool ok() const { return (state_ == nullptr); } |
51 |
| - |
52 |
| - // Returns true iff the status indicates a NotFound error. |
53 |
| - bool IsNotFound() const { return code() == kNotFound; } |
54 |
| - |
55 |
| - // Returns true iff the status indicates a Corruption error. |
56 |
| - bool IsCorruption() const { return code() == kCorruption; } |
57 |
| - |
58 |
| - // Returns true iff the status indicates an IOError. |
59 |
| - bool IsIOError() const { return code() == kIOError; } |
60 |
| - |
61 |
| - // Returns true iff the status indicates a NotSupportedError. |
62 |
| - bool IsNotSupportedError() const { return code() == kNotSupported; } |
63 |
| - |
64 |
| - // Returns true iff the status indicates an InvalidArgument. |
65 |
| - bool IsInvalidArgument() const { return code() == kInvalidArgument; } |
66 |
| - |
67 |
| - // Return a string representation of this status suitable for printing. |
68 |
| - // Returns the string "OK" for success. |
69 |
| - std::string ToString() const { |
70 |
| - if (state_ == nullptr) { |
71 |
| - return "OK"; |
72 |
| - } else { |
73 |
| - char tmp[30]; |
74 |
| - const char *type; |
75 |
| - switch (code()) { |
76 |
| - case kOk: |
77 |
| - type = "OK"; |
78 |
| - break; |
79 |
| - case kNotFound: |
80 |
| - type = "NotFound: "; |
81 |
| - break; |
82 |
| - case kCorruption: |
83 |
| - type = "Corruption: "; |
84 |
| - break; |
85 |
| - case kNotSupported: |
86 |
| - type = "Not implemented: "; |
87 |
| - break; |
88 |
| - case kInvalidArgument: |
89 |
| - type = "Invalid argument: "; |
90 |
| - break; |
91 |
| - case kIOError: |
92 |
| - type = "IO error: "; |
93 |
| - break; |
94 |
| - default: |
95 |
| - std::snprintf(tmp, sizeof(tmp), |
96 |
| - "Unknown code(%d): ", static_cast<int>(code())); |
97 |
| - type = tmp; |
98 |
| - break; |
99 |
| - } |
100 |
| - std::string result(type); |
101 |
| - uint32_t length; |
102 |
| - std::memcpy(&length, state_, sizeof(length)); |
103 |
| - result.append(state_ + 5, length); |
104 |
| - return result; |
| 52 | + // Returns true iff the status indicates success. |
| 53 | + bool ok() const { return (state_ == nullptr); } |
| 54 | + |
| 55 | + // Returns true iff the status indicates a NotFound error. |
| 56 | + bool IsNotFound() const { return code() == kNotFound; } |
| 57 | + |
| 58 | + // Returns true iff the status indicates a Corruption error. |
| 59 | + bool IsCorruption() const { return code() == kCorruption; } |
| 60 | + |
| 61 | + // Returns true iff the status indicates an IOError. |
| 62 | + bool IsIOError() const { return code() == kIOError; } |
| 63 | + |
| 64 | + // Returns true iff the status indicates a NotSupportedError. |
| 65 | + bool IsNotSupportedError() const { return code() == kNotSupported; } |
| 66 | + |
| 67 | + // Returns true iff the status indicates an InvalidArgument. |
| 68 | + bool IsInvalidArgument() const { return code() == kInvalidArgument; } |
| 69 | + |
| 70 | + // Returns true iff the status indicates an InvalidArgument. |
| 71 | + bool IsOutOfMemory() const { return code() == kOutOfMemory; } |
| 72 | + |
| 73 | + // Return a string representation of this status suitable for printing. |
| 74 | + // Returns the string "OK" for success. |
| 75 | + std::string ToString() const { |
| 76 | + if (state_ == nullptr) { |
| 77 | + return "OK"; |
| 78 | + } else { |
| 79 | + char tmp[30]; |
| 80 | + const char *type; |
| 81 | + switch (code()) { |
| 82 | + case kOk: |
| 83 | + type = "OK"; |
| 84 | + break; |
| 85 | + case kNotFound: |
| 86 | + type = "NotFound: "; |
| 87 | + break; |
| 88 | + case kCorruption: |
| 89 | + type = "Corruption: "; |
| 90 | + break; |
| 91 | + case kNotSupported: |
| 92 | + type = "Not implemented: "; |
| 93 | + break; |
| 94 | + case kInvalidArgument: |
| 95 | + type = "Invalid argument: "; |
| 96 | + break; |
| 97 | + case kIOError: |
| 98 | + type = "IO error: "; |
| 99 | + break; |
| 100 | + case kOutOfMemory: |
| 101 | + type = "Out of memory: "; |
| 102 | + break; |
| 103 | + default: |
| 104 | + std::snprintf(tmp, sizeof(tmp), |
| 105 | + "Unknown code(%d): ", static_cast<int>(code())); |
| 106 | + type = tmp; |
| 107 | + break; |
105 | 108 | }
|
| 109 | + std::string result(type); |
| 110 | + uint32_t length; |
| 111 | + std::memcpy(&length, state_, sizeof(length)); |
| 112 | + result.append(state_ + 5, length); |
| 113 | + return result; |
106 | 114 | }
|
| 115 | + } |
107 | 116 |
|
108 |
| - private: |
109 |
| - enum Code { |
110 |
| - kOk = 0, |
111 |
| - kNotFound = 1, |
112 |
| - kCorruption = 2, |
113 |
| - kNotSupported = 3, |
114 |
| - kInvalidArgument = 4, |
115 |
| - kIOError = 5 |
116 |
| - }; |
117 |
| - |
118 |
| - Code code() const { |
119 |
| - return (state_ == nullptr) ? kOk : static_cast<Code>(state_[4]); |
120 |
| - } |
| 117 | + private: |
| 118 | + enum Code { |
| 119 | + kOk = 0, |
| 120 | + kNotFound = 1, |
| 121 | + kCorruption = 2, |
| 122 | + kNotSupported = 3, |
| 123 | + kInvalidArgument = 4, |
| 124 | + kIOError = 5, |
| 125 | + kOutOfMemory = 6, |
| 126 | + }; |
| 127 | + |
| 128 | + Code code() const { |
| 129 | + return (state_ == nullptr) ? kOk : static_cast<Code>(state_[4]); |
| 130 | + } |
121 | 131 |
|
122 |
| - Status(Code code, const std::string &msg, const std::string &msg2) { |
123 |
| - assert(code != kOk); |
124 |
| - const auto len1 = static_cast<uint32_t>(msg.size()); |
125 |
| - const auto len2 = static_cast<uint32_t>(msg2.size()); |
126 |
| - const uint32_t size = len1 + (len2 ? (2 + len2) : 0); |
127 |
| - char *result = new char[size + 5]; |
128 |
| - std::memcpy(result, &size, sizeof(size)); |
129 |
| - result[4] = static_cast<char>(code); |
130 |
| - std::memcpy(result + 5, msg.data(), len1); |
131 |
| - if (len2) { |
132 |
| - result[5 + len1] = ':'; |
133 |
| - result[6 + len1] = ' '; |
134 |
| - std::memcpy(result + 7 + len1, msg2.data(), len2); |
135 |
| - } |
136 |
| - state_ = result; |
| 132 | + Status(Code code, const std::string &msg, const std::string &msg2) { |
| 133 | + assert(code != kOk); |
| 134 | + const auto len1 = static_cast<uint32_t>(msg.size()); |
| 135 | + const auto len2 = static_cast<uint32_t>(msg2.size()); |
| 136 | + const uint32_t size = len1 + (len2 ? (2 + len2) : 0); |
| 137 | + char *result = new char[size + 5]; |
| 138 | + std::memcpy(result, &size, sizeof(size)); |
| 139 | + result[4] = static_cast<char>(code); |
| 140 | + std::memcpy(result + 5, msg.data(), len1); |
| 141 | + if (len2) { |
| 142 | + result[5 + len1] = ':'; |
| 143 | + result[6 + len1] = ' '; |
| 144 | + std::memcpy(result + 7 + len1, msg2.data(), len2); |
137 | 145 | }
|
| 146 | + state_ = result; |
| 147 | + } |
138 | 148 |
|
139 |
| - static const char *CopyState(const char *s) { |
140 |
| - uint32_t size; |
141 |
| - std::memcpy(&size, state, sizeof(size)); |
142 |
| - char *result = new char[size + 5]; |
143 |
| - std::memcpy(result, state, size + 5); |
144 |
| - return result; |
145 |
| - } |
| 149 | + static const char *CopyState(const char *s) { |
| 150 | + uint32_t size; |
| 151 | + std::memcpy(&size, state, sizeof(size)); |
| 152 | + char *result = new char[size + 5]; |
| 153 | + std::memcpy(result, state, size + 5); |
| 154 | + return result; |
| 155 | + } |
146 | 156 |
|
147 |
| - // OK status has a null state_. Otherwise, state_ is a new[] array |
148 |
| - // of the following form: |
149 |
| - // state_[0..3] == length of message |
150 |
| - // state_[4] == code |
151 |
| - // state_[5..] == message |
152 |
| - const char *state_; |
| 157 | + // OK status has a null state_. Otherwise, state_ is a new[] array |
| 158 | + // of the following form: |
| 159 | + // state_[0..3] == length of message |
| 160 | + // state_[4] == code |
| 161 | + // state_[5..] == message |
| 162 | + const char *state_; |
153 | 163 | };
|
154 | 164 |
|
155 | 165 | inline Status::Status(const Status &rhs) {
|
|
0 commit comments