1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | |
16 | * \author Hechong.xyf |
17 | * \date Apr 2018 |
18 | * \brief Interface of AiTheta Index Error |
19 | */ |
20 | |
21 | #ifndef __AITHETA2_INDEX_ERROR_H__ |
22 | #define __AITHETA2_INDEX_ERROR_H__ |
23 | |
24 | #include <map> |
25 | |
26 | namespace aitheta2 { |
27 | |
28 | /*! Index Error |
29 | */ |
30 | class IndexError { |
31 | public: |
32 | /*! Index Error Code |
33 | */ |
34 | class Code { |
35 | public: |
36 | //! Constructor |
37 | Code(int val, const char *str) : value_(-val), desc_(str) { |
38 | IndexError::Instance()->emplace(this); |
39 | } |
40 | |
41 | //! Retrieve the value of code |
42 | operator int() const { |
43 | return (this->value_); |
44 | } |
45 | |
46 | //! Retrieve the value of code |
47 | int value() const { |
48 | return (this->value_); |
49 | } |
50 | |
51 | //! Retrieve the description of code |
52 | const char *desc() const { |
53 | return (this->desc_); |
54 | } |
55 | |
56 | private: |
57 | int value_; |
58 | const char *desc_; |
59 | }; |
60 | |
61 | //! Retrieve the description of code |
62 | static const char *What(int val) { |
63 | return IndexError::Instance()->what(val); |
64 | } |
65 | |
66 | protected: |
67 | //! Constructor |
68 | IndexError(void) : map_() {} |
69 | |
70 | //! Inserts a new code into map |
71 | void emplace(const IndexError::Code *code) { |
72 | map_.emplace(code->value(), code); |
73 | } |
74 | |
75 | //! Retrieve the description of code |
76 | const char *what(int val) const { |
77 | auto iter = map_.find(val); |
78 | if (iter != map_.end()) { |
79 | return iter->second->desc(); |
80 | } |
81 | return "" ; |
82 | } |
83 | |
84 | //! Retrieve the singleton |
85 | static IndexError *Instance(void) { |
86 | static IndexError error; |
87 | return (&error); |
88 | } |
89 | |
90 | private: |
91 | //! Disable them |
92 | IndexError(const IndexError &) = delete; |
93 | IndexError(IndexError &&) = delete; |
94 | IndexError &operator=(const IndexError &) = delete; |
95 | |
96 | //! Error code map |
97 | std::map<int, const IndexError::Code *> map_; |
98 | }; |
99 | |
100 | //! Index Error Code Define |
101 | #define INDEX_ERROR_CODE_DEFINE(__NAME__, __VAL__, __DESC__) \ |
102 | const aitheta2::IndexError::Code IndexError_##__NAME__((__VAL__), \ |
103 | (__DESC__)); \ |
104 | const aitheta2::IndexError::Code &_IndexErrorCode_##__VAL__##_Register( \ |
105 | IndexError_##__NAME__) |
106 | |
107 | //! Index Error Code Declare |
108 | #define INDEX_ERROR_CODE_DECLARE(__NAME__) \ |
109 | extern const aitheta2::IndexError::Code IndexError_##__NAME__ |
110 | |
111 | //! Build-in error code |
112 | INDEX_ERROR_CODE_DECLARE(Success); // Success |
113 | INDEX_ERROR_CODE_DECLARE(Runtime); // Runtime error |
114 | INDEX_ERROR_CODE_DECLARE(Logic); // Logic error |
115 | INDEX_ERROR_CODE_DECLARE(Type); // Type error |
116 | INDEX_ERROR_CODE_DECLARE(System); // System call error |
117 | INDEX_ERROR_CODE_DECLARE(Cast); // Cast error |
118 | INDEX_ERROR_CODE_DECLARE(IO); // IO error |
119 | |
120 | INDEX_ERROR_CODE_DECLARE(NotImplemented); // Not implemented |
121 | INDEX_ERROR_CODE_DECLARE(Unsupported); // Unsupported |
122 | INDEX_ERROR_CODE_DECLARE(Denied); // Permission denied |
123 | INDEX_ERROR_CODE_DECLARE(Canceled); // Operation canceled |
124 | INDEX_ERROR_CODE_DECLARE(Overflow); // Overflow |
125 | INDEX_ERROR_CODE_DECLARE(Underflow); // Underflow |
126 | INDEX_ERROR_CODE_DECLARE(OutOfRange); // Out of range |
127 | INDEX_ERROR_CODE_DECLARE(NoBuffer); // No buffer space available |
128 | INDEX_ERROR_CODE_DECLARE(NoMemory); // Not enough space |
129 | INDEX_ERROR_CODE_DECLARE(NoParamFound); // No parameter found |
130 | INDEX_ERROR_CODE_DECLARE(NoReady); // No ready |
131 | INDEX_ERROR_CODE_DECLARE(NoExist); // No exist |
132 | INDEX_ERROR_CODE_DECLARE(Exist); // Already exist |
133 | INDEX_ERROR_CODE_DECLARE(Mismatch); // Mismatch |
134 | INDEX_ERROR_CODE_DECLARE(Duplicate); // Duplicate |
135 | INDEX_ERROR_CODE_DECLARE(Uninitialized); // Uninitialized |
136 | |
137 | INDEX_ERROR_CODE_DECLARE(InvalidArgument); // Invalid argument |
138 | INDEX_ERROR_CODE_DECLARE(InvalidFormat); // Invalid format |
139 | INDEX_ERROR_CODE_DECLARE(InvalidLength); // Invalid length |
140 | INDEX_ERROR_CODE_DECLARE(InvalidChecksum); // Invalid checksum |
141 | INDEX_ERROR_CODE_DECLARE(InvalidValue); // Invalid value |
142 | |
143 | INDEX_ERROR_CODE_DECLARE(CreateDirectory); // Create directory error |
144 | INDEX_ERROR_CODE_DECLARE(OpenDirectory); // Open directory error |
145 | INDEX_ERROR_CODE_DECLARE(Serialize); // Serialize error |
146 | INDEX_ERROR_CODE_DECLARE(Deserialize); // Deserialize error |
147 | INDEX_ERROR_CODE_DECLARE(CreateFile); // Create file error |
148 | INDEX_ERROR_CODE_DECLARE(OpenFile); // Open file error |
149 | INDEX_ERROR_CODE_DECLARE(SeekFile); // Seek file error |
150 | INDEX_ERROR_CODE_DECLARE(CloseFile); // Close file error |
151 | INDEX_ERROR_CODE_DECLARE(TruncateFile); // TruncateFile file error |
152 | INDEX_ERROR_CODE_DECLARE(MMapFile); // MMap file error |
153 | INDEX_ERROR_CODE_DECLARE(FlushFile); // Flush file error |
154 | INDEX_ERROR_CODE_DECLARE(WriteData); // Write data error |
155 | INDEX_ERROR_CODE_DECLARE(ReadData); // Read data error |
156 | |
157 | INDEX_ERROR_CODE_DECLARE(PackIndex); // Read data error |
158 | INDEX_ERROR_CODE_DECLARE(UnpackIndex); // Read data error |
159 | INDEX_ERROR_CODE_DECLARE(IndexLoaded); // Index loaded |
160 | INDEX_ERROR_CODE_DECLARE(NoIndexLoaded); // No index loaded |
161 | INDEX_ERROR_CODE_DECLARE(NoTrained); // No trained |
162 | INDEX_ERROR_CODE_DECLARE(IndexFull); // Index full |
163 | |
164 | } // namespace aitheta2 |
165 | |
166 | #endif // __AITHETA2_INDEX_ERROR_H__ |
167 | |