-
Notifications
You must be signed in to change notification settings - Fork 0
/
nativelib.cpp
93 lines (73 loc) · 1.95 KB
/
nativelib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <exception>
#include <nativelib.h>
#include <Python.h>
#include <cythoncallbacks.h>
#define MAGIC ((uint32_t)0xFFFF)
struct NativeLib
{
uint32_t magic;
};
NativeResult Init(NativeLibHdl* libHdl)
{
*libHdl = new NativeLib();
(*libHdl)->magic = MAGIC;
return RESULT_OK;
}
NativeResult Destroy(NativeLibHdl* libHdl)
{
if (!libHdl ||
(*libHdl)->magic != MAGIC)
return RESULT_INVALID_ARGS;
delete *libHdl;
*libHdl = nullptr;
return RESULT_OK;
}
NativeResult Get_Python_Callback(NativeLibHdl libHdl, CallbackParams params, NativeCallbacksHdl callbacksHdl)
{
return Getcallbacks_Cython(libHdl, params, callbacksHdl);
}
NativeResult Get_Native_Callback(NativeLibHdl libHdl, CallbackParams params, NativeCallbacksHdl callbacksHdl)
{
return RESULT_NOT_IMPLEMENTED;
}
NativeResult Get_Callback(NativeLibHdl libHdl, CallbackParams params, NativeCallbacksHdl callbacksHdl)
{
if (!libHdl ||
!callbacksHdl ||
libHdl->magic != MAGIC)
return RESULT_INVALID_ARGS;
switch (params.callbackType) {
case PYTHON_CALLBACK:
return Get_Python_Callback(libHdl, params, callbacksHdl);
case NATIVE_CALLBACK:
return Get_Native_Callback(libHdl, params, callbacksHdl);
default:
return RESULT_NOT_IMPLEMENTED;
}
}
NativeResult Call_Callback(NativeLibHdl libHdl, NativeCallbacksHdl callbacksHdl)
{
if (!libHdl ||
!callbacksHdl ||
!callbacksHdl->callback)
return RESULT_INVALID_ARGS;
try
{
return callbacksHdl->callback(libHdl);
}
catch(const std::exception& e)
{
return RESULT_ERROR;
}
}
static PyObject* cythoncallbacks;
__attribute__((constructor)) void libLoad()
{
PyImport_AppendInittab("cythoncallbacks", PyInit_cythoncallbacks);
Py_Initialize();
cythoncallbacks = PyImport_ImportModule("cythoncallbacks");
}
__attribute__((destructor)) void libUnload()
{
Py_Finalize();
}