@@ -23,14 +23,12 @@ limitations under the License. */
2323namespace paddle {
2424
2525/* *
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.
2927 *
3028 *
31- * There are two styles to return status in Paddle.
29+ * There are two styles to return error in Paddle.
3230 *
33- * 1. Return Status
31+ * 1. Return Error
3432 * When method return a status, the return must use `__must_check` attribute.
3533 * Example as below.
3634 * @code{cpp}
@@ -39,29 +37,29 @@ namespace paddle {
3937 * Error __must_check bar() {
4038 * // do something.
4139 * Status s = foo(); // invoke other method return status.
42- * if (!s.isOK() ) return s;
40+ * if (!s) return s;
4341 * // do something else.
4442 * return Status();
4543 * }
4644 * @endcode{cpp}
4745 *
4846 * 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.
5048 * Example as below.
5149 *
5250 * @code{cpp}
5351 * Error bar();
5452 *
55- * int foo(Error* status ) {
53+ * int foo(Error* error ) {
5654 * // Do something.
57- * Status s = bar();
58- * if (!s.isOK() ) {
59- * *status = s;
55+ * Error s = bar();
56+ * if (!s) {
57+ * *error = s;
6058 * return 0;
6159 * }
6260 * // Do something else.
6361 * if (someInternalErrorHappend) {
64- * *status = ErrorF ("Some dimension is too large, %d", dimension);
62+ * *error = Error ("Some dimension is too large, %d", dimension);
6563 * return 0;
6664 * }
6765 * // End of method.
@@ -72,7 +70,7 @@ namespace paddle {
7270 * Error s;
7371 * // do something.
7472 * foo(&s);
75- * if (!s.isOK() ) return s;
73+ * if (!s) return s;
7674 * }
7775 * @endcode{cpp}
7876 *
@@ -81,17 +79,31 @@ namespace paddle {
8179 * use log(FATAL) or CHECK to make program exit before. When we clean all
8280 * log(FATAL) and CHECK in Paddle, 'check' method will be removed.
8381 */
84- class Error final : public std::exception {
82+ class Error final {
8583public:
8684 /* *
8785 * Default Status. OK
8886 */
89- Error () noexcept {}
87+ inline Error () {}
9088
9189 /* *
92- * @brief what will return the error message. If status is OK, return nullptr .
90+ * @brief Create an Error use printf syntax .
9391 */
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 {
95107 if (errMsg_) {
96108 return errMsg_->data ();
97109 } else {
@@ -100,58 +112,18 @@ class Error final : public std::exception {
100112 }
101113
102114 /* *
103- * @brief isOK
104- * @return true if OK.
115+ * @brief operator bool, return True if there is no error.
105116 */
106- inline bool isOK () const noexcept { return errMsg_ == nullptr ; }
107-
117+ inline operator bool () const { return !errMsg_; }
108118 /* *
109119 * @brief check this status by glog.
110120 * @note It is a temp method used during cleaning Paddle code. It will be
111121 * removed later.
112122 */
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 (); }
120124
121125private:
122126 std::shared_ptr<std::string> errMsg_;
123127};
124128
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-
157129} // namespace paddle
0 commit comments