@@ -23,14 +23,12 @@ limitations under the License. */
23
23
namespace paddle {
24
24
25
25
/* *
26
- * Status is Paddle error code. It only contain a std::string as error message.
27
- * Although Status inherits the std::exception, but do not throw it except you
28
- * know what you are doing.
26
+ * Error is Paddle error code. It only contain a std::string as error message.
29
27
*
30
28
*
31
- * There are two styles to return status in Paddle.
29
+ * There are two styles to return error in Paddle.
32
30
*
33
- * 1. Return Status
31
+ * 1. Return Error
34
32
* When method return a status, the return must use `__must_check` attribute.
35
33
* Example as below.
36
34
* @code{cpp}
@@ -39,29 +37,29 @@ namespace paddle {
39
37
* Error __must_check bar() {
40
38
* // do something.
41
39
* Status s = foo(); // invoke other method return status.
42
- * if (!s.isOK() ) return s;
40
+ * if (!s) return s;
43
41
* // do something else.
44
42
* return Status();
45
43
* }
46
44
* @endcode{cpp}
47
45
*
48
46
* 2. Return by parameter.
49
- * It is another way to return a status , by using a pointer parameter.
47
+ * It is another way to return an error , by using a pointer parameter.
50
48
* Example as below.
51
49
*
52
50
* @code{cpp}
53
51
* Error bar();
54
52
*
55
- * int foo(Error* status ) {
53
+ * int foo(Error* error ) {
56
54
* // Do something.
57
- * Status s = bar();
58
- * if (!s.isOK() ) {
59
- * *status = s;
55
+ * Error s = bar();
56
+ * if (!s) {
57
+ * *error = s;
60
58
* return 0;
61
59
* }
62
60
* // Do something else.
63
61
* if (someInternalErrorHappend) {
64
- * *status = ErrorF ("Some dimension is too large, %d", dimension);
62
+ * *error = Error ("Some dimension is too large, %d", dimension);
65
63
* return 0;
66
64
* }
67
65
* // End of method.
@@ -72,7 +70,7 @@ namespace paddle {
72
70
* Error s;
73
71
* // do something.
74
72
* foo(&s);
75
- * if (!s.isOK() ) return s;
73
+ * if (!s) return s;
76
74
* }
77
75
* @endcode{cpp}
78
76
*
@@ -81,17 +79,31 @@ namespace paddle {
81
79
* use log(FATAL) or CHECK to make program exit before. When we clean all
82
80
* log(FATAL) and CHECK in Paddle, 'check' method will be removed.
83
81
*/
84
- class Error final : public std::exception {
82
+ class Error final {
85
83
public:
86
84
/* *
87
85
* Default Status. OK
88
86
*/
89
- Error () noexcept {}
87
+ inline Error () {}
90
88
91
89
/* *
92
- * @brief what will return the error message. If status is OK, return nullptr .
90
+ * @brief Create an Error use printf syntax .
93
91
*/
94
- const char * what () const noexcept override {
92
+ inline explicit Error (const char * fmt, ...) {
93
+ va_list ap;
94
+ va_start (ap, fmt);
95
+ constexpr size_t kBufferSize = 1024 ;
96
+ this ->errMsg_ .reset (new std::string (kBufferSize , 0 ));
97
+ auto sz = vsnprintf (&(*errMsg_)[0 ], kBufferSize , fmt, ap);
98
+ this ->errMsg_ ->resize (sz);
99
+ this ->errMsg_ ->shrink_to_fit ();
100
+ va_end (ap);
101
+ }
102
+
103
+ /* *
104
+ * @brief what will return the error message. If no error, return nullptr.
105
+ */
106
+ inline const char * msg () const {
95
107
if (errMsg_) {
96
108
return errMsg_->data ();
97
109
} else {
@@ -100,58 +112,18 @@ class Error final : public std::exception {
100
112
}
101
113
102
114
/* *
103
- * @brief isOK
104
- * @return true if OK.
115
+ * @brief operator bool, return True if there is no error.
105
116
*/
106
- inline bool isOK () const noexcept { return errMsg_ == nullptr ; }
107
-
117
+ inline operator bool () const { return !errMsg_; }
108
118
/* *
109
119
* @brief check this status by glog.
110
120
* @note It is a temp method used during cleaning Paddle code. It will be
111
121
* removed later.
112
122
*/
113
- inline void check () const { CHECK (isOK ()) << what (); }
114
-
115
- /* *
116
- * friend method to create Error.
117
- */
118
- template <typename ... ARGS>
119
- friend Error __must_check ErrorF (const char * fmt, ARGS... args);
123
+ inline void check () const { CHECK (*this ) << msg (); }
120
124
121
125
private:
122
126
std::shared_ptr<std::string> errMsg_;
123
127
};
124
128
125
- /* *
126
- * ErrorF will create an Error by printf syntax.
127
- *
128
- * Specialize this method because clang will give a warning when use printf(fmt)
129
- * without arguments.
130
- */
131
- template <>
132
- inline Error __must_check ErrorF (const char * msg) {
133
- Error e;
134
- e.errMsg_ .reset (new std::string (msg));
135
- return e;
136
- }
137
-
138
- /* *
139
- * ErrorF will create an Error by printf syntax.
140
- *
141
- * Examples:
142
- * @code{cpp}
143
- * auto err = ErrorF("SomeError");
144
- * auto err2 = ErrorF("SomeErrorWithParameter %f %d", real_val, int_val);
145
- * @endcode{cpp}
146
- */
147
- template <typename ... ARGS>
148
- inline Error __must_check ErrorF (const char * fmt, ARGS... args) {
149
- constexpr size_t kBufferSize = 1024 ;
150
- char buffer[kBufferSize ];
151
- snprintf (buffer, kBufferSize , fmt, args...);
152
- Error e;
153
- e.errMsg_ .reset (new std::string (buffer));
154
- return e;
155
- }
156
-
157
129
} // namespace paddle
0 commit comments