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
26namespace aitheta2 {
27
28/*! Index Error
29 */
30class 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
112INDEX_ERROR_CODE_DECLARE(Success); // Success
113INDEX_ERROR_CODE_DECLARE(Runtime); // Runtime error
114INDEX_ERROR_CODE_DECLARE(Logic); // Logic error
115INDEX_ERROR_CODE_DECLARE(Type); // Type error
116INDEX_ERROR_CODE_DECLARE(System); // System call error
117INDEX_ERROR_CODE_DECLARE(Cast); // Cast error
118INDEX_ERROR_CODE_DECLARE(IO); // IO error
119
120INDEX_ERROR_CODE_DECLARE(NotImplemented); // Not implemented
121INDEX_ERROR_CODE_DECLARE(Unsupported); // Unsupported
122INDEX_ERROR_CODE_DECLARE(Denied); // Permission denied
123INDEX_ERROR_CODE_DECLARE(Canceled); // Operation canceled
124INDEX_ERROR_CODE_DECLARE(Overflow); // Overflow
125INDEX_ERROR_CODE_DECLARE(Underflow); // Underflow
126INDEX_ERROR_CODE_DECLARE(OutOfRange); // Out of range
127INDEX_ERROR_CODE_DECLARE(NoBuffer); // No buffer space available
128INDEX_ERROR_CODE_DECLARE(NoMemory); // Not enough space
129INDEX_ERROR_CODE_DECLARE(NoParamFound); // No parameter found
130INDEX_ERROR_CODE_DECLARE(NoReady); // No ready
131INDEX_ERROR_CODE_DECLARE(NoExist); // No exist
132INDEX_ERROR_CODE_DECLARE(Exist); // Already exist
133INDEX_ERROR_CODE_DECLARE(Mismatch); // Mismatch
134INDEX_ERROR_CODE_DECLARE(Duplicate); // Duplicate
135INDEX_ERROR_CODE_DECLARE(Uninitialized); // Uninitialized
136
137INDEX_ERROR_CODE_DECLARE(InvalidArgument); // Invalid argument
138INDEX_ERROR_CODE_DECLARE(InvalidFormat); // Invalid format
139INDEX_ERROR_CODE_DECLARE(InvalidLength); // Invalid length
140INDEX_ERROR_CODE_DECLARE(InvalidChecksum); // Invalid checksum
141INDEX_ERROR_CODE_DECLARE(InvalidValue); // Invalid value
142
143INDEX_ERROR_CODE_DECLARE(CreateDirectory); // Create directory error
144INDEX_ERROR_CODE_DECLARE(OpenDirectory); // Open directory error
145INDEX_ERROR_CODE_DECLARE(Serialize); // Serialize error
146INDEX_ERROR_CODE_DECLARE(Deserialize); // Deserialize error
147INDEX_ERROR_CODE_DECLARE(CreateFile); // Create file error
148INDEX_ERROR_CODE_DECLARE(OpenFile); // Open file error
149INDEX_ERROR_CODE_DECLARE(SeekFile); // Seek file error
150INDEX_ERROR_CODE_DECLARE(CloseFile); // Close file error
151INDEX_ERROR_CODE_DECLARE(TruncateFile); // TruncateFile file error
152INDEX_ERROR_CODE_DECLARE(MMapFile); // MMap file error
153INDEX_ERROR_CODE_DECLARE(FlushFile); // Flush file error
154INDEX_ERROR_CODE_DECLARE(WriteData); // Write data error
155INDEX_ERROR_CODE_DECLARE(ReadData); // Read data error
156
157INDEX_ERROR_CODE_DECLARE(PackIndex); // Read data error
158INDEX_ERROR_CODE_DECLARE(UnpackIndex); // Read data error
159INDEX_ERROR_CODE_DECLARE(IndexLoaded); // Index loaded
160INDEX_ERROR_CODE_DECLARE(NoIndexLoaded); // No index loaded
161INDEX_ERROR_CODE_DECLARE(NoTrained); // No trained
162INDEX_ERROR_CODE_DECLARE(IndexFull); // Index full
163
164} // namespace aitheta2
165
166#endif // __AITHETA2_INDEX_ERROR_H__
167