-
Notifications
You must be signed in to change notification settings - Fork 1
/
hamr_python_deleter.h
47 lines (39 loc) · 1.34 KB
/
hamr_python_deleter.h
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
#ifndef hamr_python_deleter_h
#define hamr_python_deleter_h
#include "hamr_config.h"
#include <Python.h>
namespace hamr
{
/// a deleter for memory managed from within Python
/** This class manages an array allocated by a Python code. In the functor's
* constructor a refrence to a user provdied Python object is stolen. When the
* functor is invoked, a reference to this Python object is released. It is up
* to the Python object to free the memory. One may use a PyCapsule to
* implement custom delete methods if they are needed.
*/
template <typename T>
class HAMR_EXPORT python_deleter
{
public:
/** constructs the deleter. A reference to obj is stolen by this constructor.
* @param[in] ptr a pointer to shared data
* @param[in] n_elem the number of elements of type T shared
* @param[in] obj a PyObject who's reference count will be decremented when
* the data shared from Python is no longer needed.
*/
python_deleter(T *ptr, size_t n_elem, PyObject *obj);
/** deletes the array
* @param[in] ptr the pointer to the array to delete. must be the same as
* that passed during construction.
*/
void operator()(T *ptr);
private:
T *m_ptr;
size_t m_elem;
PyObject *m_object;
};
}
#if !defined(HAMR_SEPARATE_IMPL)
#include "hamr_python_deleter_impl.h"
#endif
#endif