|
| 1 | +/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <iostream> |
| 18 | +#include <sstream> |
| 19 | +#include <string> |
| 20 | + |
| 21 | +namespace paddle { |
| 22 | + |
| 23 | +//////////////// Exception handling and Error Message ///////////////// |
| 24 | +#if !defined(_WIN32) |
| 25 | +#define PD_UNLIKELY(expr) (__builtin_expect(static_cast<bool>(expr), 0)) |
| 26 | +#define PD_LIKELY(expr) (__builtin_expect(static_cast<bool>(expr), 1)) |
| 27 | +#else |
| 28 | +#define PD_UNLIKELY(expr) (expr) |
| 29 | +#define PD_LIKELY(expr) (expr) |
| 30 | +#endif |
| 31 | + |
| 32 | +struct PD_Exception : public std::exception { |
| 33 | + public: |
| 34 | + template <typename... Args> |
| 35 | + explicit PD_Exception(const std::string& msg, const char* file, int line, |
| 36 | + const char* default_msg) { |
| 37 | + std::ostringstream sout; |
| 38 | + if (msg.empty()) { |
| 39 | + sout << default_msg << "\n [" << file << ":" << line << "]"; |
| 40 | + } else { |
| 41 | + sout << msg << "\n [" << file << ":" << line << "]"; |
| 42 | + } |
| 43 | + err_msg_ = sout.str(); |
| 44 | + } |
| 45 | + |
| 46 | + const char* what() const noexcept override { return err_msg_.c_str(); } |
| 47 | + |
| 48 | + private: |
| 49 | + std::string err_msg_; |
| 50 | +}; |
| 51 | + |
| 52 | +class ErrorMessage { |
| 53 | + public: |
| 54 | + template <typename... Args> |
| 55 | + explicit ErrorMessage(const Args&... args) { |
| 56 | + build_string(args...); |
| 57 | + } |
| 58 | + |
| 59 | + void build_string() { oss << ""; } |
| 60 | + |
| 61 | + template <typename T> |
| 62 | + void build_string(const T& t) { |
| 63 | + oss << t; |
| 64 | + } |
| 65 | + |
| 66 | + template <typename T, typename... Args> |
| 67 | + void build_string(const T& t, const Args&... args) { |
| 68 | + build_string(t); |
| 69 | + build_string(args...); |
| 70 | + } |
| 71 | + |
| 72 | + std::string to_string() { return oss.str(); } |
| 73 | + |
| 74 | + private: |
| 75 | + std::ostringstream oss; |
| 76 | +}; |
| 77 | + |
| 78 | +#if defined _WIN32 |
| 79 | +#define HANDLE_THE_ERROR try { |
| 80 | +#define END_HANDLE_THE_ERROR \ |
| 81 | + } \ |
| 82 | + catch (const std::exception& e) { \ |
| 83 | + std::cerr << e.what() << std::endl; \ |
| 84 | + throw e; \ |
| 85 | + } |
| 86 | +#else |
| 87 | +#define HANDLE_THE_ERROR |
| 88 | +#define END_HANDLE_THE_ERROR |
| 89 | +#endif |
| 90 | + |
| 91 | +#define PD_CHECK(COND, ...) \ |
| 92 | + do { \ |
| 93 | + if (PD_UNLIKELY(!(COND))) { \ |
| 94 | + auto __message__ = ::paddle::ErrorMessage(__VA_ARGS__).to_string(); \ |
| 95 | + throw ::paddle::PD_Exception(__message__, __FILE__, __LINE__, \ |
| 96 | + "Expected " #COND \ |
| 97 | + ", but it's not satisfied."); \ |
| 98 | + } \ |
| 99 | + } while (0) |
| 100 | + |
| 101 | +#define PD_THROW(...) \ |
| 102 | + do { \ |
| 103 | + auto __message__ = ::paddle::ErrorMessage(__VA_ARGS__).to_string(); \ |
| 104 | + throw ::paddle::PD_Exception(__message__, __FILE__, __LINE__, \ |
| 105 | + "An error occured."); \ |
| 106 | + } while (0) |
| 107 | + |
| 108 | +} // namespace paddle |
0 commit comments