-
Notifications
You must be signed in to change notification settings - Fork 5
/
PyObjectUtils.hpp
79 lines (67 loc) · 1.9 KB
/
PyObjectUtils.hpp
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
// Copyright (c) 2014-2014 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#pragma once
#include <Python.h>
#include <Pothos/Proxy.hpp>
#include <functional>
#include <iostream>
/***********************************************************************
* Conversion function pointer types
**********************************************************************/
typedef std::function<Pothos::Proxy(Pothos::ProxyEnvironment::Sptr, PyObject*)> PyObjectToProxyFcn;
typedef std::function<PyObject *(const Pothos::Proxy &)> ProxyToPyObjectFcn;
/***********************************************************************
* simple holder of a object ref
**********************************************************************/
#define REF_BORROWED true
#define REF_NEW false
struct PyObjectRef
{
PyObjectRef(void):
obj(nullptr)
{
return;
}
PyObjectRef(const PyObjectRef &ref):
obj(ref.obj)
{
Py_XINCREF(obj);
}
PyObjectRef &operator=(const PyObjectRef &ref)
{
Py_XDECREF(obj);
obj = ref.obj;
Py_XINCREF(obj);
return *this;
}
PyObjectRef(PyObject *obj, const bool borrowed):
obj(obj)
{
if (borrowed) Py_XINCREF(obj);
}
~PyObjectRef(void)
{
Py_XDECREF(obj);
}
PyObject *newRef(void)
{
Py_XINCREF(obj);
return obj;
}
PyObject *obj;
};
/***********************************************************************
* C++ locking structures for calling into and out of the interpreter
**********************************************************************/
struct PyGilStateLock
{
PyGILState_STATE _s;
PyGilStateLock(void):_s(PyGILState_Ensure()){}
~PyGilStateLock(void){PyGILState_Release(_s);}
};
struct PyThreadStateLock
{
PyThreadState *_s;
PyThreadStateLock(void):_s(PyEval_SaveThread()){}
~PyThreadStateLock(void){PyEval_RestoreThread(_s);}
};