forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.hpp
More file actions
76 lines (59 loc) · 1.58 KB
/
python.hpp
File metadata and controls
76 lines (59 loc) · 1.58 KB
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
#ifndef __PYCCASSANDRA_PYTHON
#define __PYCCASSANDRA_PYTHON
#include <vector>
#define PY_SSIZE_T_CLEAN
#include <Python.h>
namespace pyccassandra
{
/// Vectorize Python container.
/// Fills the items in a Python container into a target vector.
///
/// @param container Container.
/// @param target Target vector to contain the items in the container.
/// @returns true if vectorization succeeded, otherwise false, indicating
/// failure with the corresponding Python exception set.
bool VectorizePythonContainer(PyObject* container,
std::vector<PyObject*>& target);
/// Scoped Python reference.
/// RAII-style Python reference, which will be released upon exiting the
/// scope. This behavior can be cancelled by calling Steal on the scoped
/// reference.
class ScopedReference
{
public:
ScopedReference(PyObject* obj = NULL)
: _obj(obj)
{
}
void operator =(PyObject* obj)
{
if (_obj)
Py_DECREF(_obj);
_obj = obj;
}
~ScopedReference()
{
if (_obj)
Py_DECREF(_obj);
}
/// Object.
inline PyObject* Get() const
{
return _obj;
}
/// Steal the reference.
PyObject* Steal()
{
PyObject* obj = _obj;
_obj = NULL;
return obj;
}
operator bool() const
{
return _obj;
}
private:
PyObject* _obj;
};
}
#endif