runtime_base.h 1.46 KB
Newer Older
Minjie Wang's avatar
Minjie Wang committed
1
2
/*!
 *  Copyright (c) 2016 by Contributors
3
4
 * @file runtime_base.h
 * @brief Base of all C APIs
Minjie Wang's avatar
Minjie Wang committed
5
 */
6
7
#ifndef DGL_RUNTIME_RUNTIME_BASE_H_
#define DGL_RUNTIME_RUNTIME_BASE_H_
Minjie Wang's avatar
Minjie Wang committed
8
9

#include <dgl/runtime/c_runtime_api.h>
10

Minjie Wang's avatar
Minjie Wang committed
11
12
#include <stdexcept>

13
/*! @brief  macro to guard beginning and end section of all functions */
Minjie Wang's avatar
Minjie Wang committed
14
#define API_BEGIN() try {
15
/*! @brief every function starts with API_BEGIN();
Minjie Wang's avatar
Minjie Wang committed
16
     and finishes with API_END() or API_END_HANDLE_ERROR */
17
18
19
20
21
22
#define API_END()                           \
  }                                         \
  catch (std::runtime_error & _except_) {   \
    return DGLAPIHandleException(_except_); \
  }                                         \
  return 0;  // NOLINT(*)
Minjie Wang's avatar
Minjie Wang committed
23
/*!
24
 * @brief every function starts with API_BEGIN();
Minjie Wang's avatar
Minjie Wang committed
25
 *   and finishes with API_END() or API_END_HANDLE_ERROR
26
27
 *   The finally clause contains procedure to cleanup states when an error
 * happens.
Minjie Wang's avatar
Minjie Wang committed
28
 */
29
30
31
32
33
34
35
#define API_END_HANDLE_ERROR(Finalize)      \
  }                                         \
  catch (std::runtime_error & _except_) {   \
    Finalize;                               \
    return DGLAPIHandleException(_except_); \
  }                                         \
  return 0;  // NOLINT(*)
Minjie Wang's avatar
Minjie Wang committed
36
37

/*!
38
39
40
 * @brief handle exception throwed out
 * @param e the exception
 * @return the return value of API after exception is handled
Minjie Wang's avatar
Minjie Wang committed
41
 */
42
43
inline int DGLAPIHandleException(const std::runtime_error &e) {
  DGLAPISetLastError(e.what());
Minjie Wang's avatar
Minjie Wang committed
44
45
46
  return -1;
}

47
#endif  // DGL_RUNTIME_RUNTIME_BASE_H_