enforce.h 3.0 KB
Newer Older
L
liuruilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once

W
wangliu 已提交
17
#ifdef ENABLE_EXCEPTION
L
liuruilong 已提交
18
#include <stdio.h>
19
#include <stdlib.h>
L
liuruilong 已提交
20 21 22 23 24 25
#include <exception>
#include <string>
#endif

namespace paddle_mobile {

W
wangliu 已提交
26
#ifdef ENABLE_EXCEPTION
L
liuruilong 已提交
27 28 29 30 31 32
struct PaddleMobileException : public std::exception {
  const std::string exception_prefix = "paddle mobile C++ Exception: \n";
  std::string message;

  PaddleMobileException(const char *header, const char *detail,
                        const char *file, const int line) {
L
liuruilong 已提交
33
    char buffer[1500];
L
liuruilong 已提交
34
    snprintf(buffer, sizeof(buffer),
L
liuruilong 已提交
35 36 37
             "%s| %s \n| [in file] : %s\n| [on line] : %d\n| [detail]  : %s\n",
             exception_prefix.c_str(), header, file, line, detail);
    message = std::string(buffer);
L
liuruilong 已提交
38 39 40 41 42 43 44 45 46 47 48
  }
  const char *what() const noexcept { return message.c_str(); }
};

#define PADDLE_MOBILE_THROW_EXCEPTION(...)                                 \
  {                                                                        \
    char buffer[1000];                                                     \
    snprintf(buffer, sizeof(buffer), __VA_ARGS__);                         \
    std::string detail(buffer);                                            \
    throw paddle_mobile::PaddleMobileException("Custom Exception", buffer, \
                                               __FILE__, __LINE__);        \
49 50
  }                                                                        \
  exit(0);
L
liuruilong 已提交
51

L
liuruilong 已提交
52
#define PADDLE_MOBILE_ENFORCE(stat, ...)                                      \
L
liuruilong 已提交
53 54 55 56 57 58
  {                                                                           \
    if (stat) {                                                               \
    } else {                                                                  \
      char buffer[1000];                                                      \
      snprintf(buffer, sizeof(buffer), __VA_ARGS__);                          \
      std::string detail(buffer);                                             \
L
liuruilong 已提交
59
      throw paddle_mobile::PaddleMobileException("paddle-mobile enforce",     \
L
liuruilong 已提交
60 61 62 63 64
                                                 buffer, __FILE__, __LINE__); \
    }                                                                         \
  }
#else
#define PADDLE_MOBILE_THROW_EXCEPTION(...)
Z
zhaojiaying01 已提交
65 66 67 68 69 70 71 72

#define PADDLE_MOBILE_ENFORCE(stat, ...) \
  {                                      \
    if (stat) {                          \
    } else {                             \
    }                                    \
  }

L
liuruilong 已提交
73 74
#endif

L
liuruilong 已提交
75
}  // namespace paddle_mobile