forked from mkleehammer/pyodbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper.h
More file actions
120 lines (93 loc) · 1.99 KB
/
wrapper.h
File metadata and controls
120 lines (93 loc) · 1.99 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifndef _WRAPPER_H_
#define _WRAPPER_H_
class Object
{
// This is a simple wrapper around PyObject pointers to release them when this object goes
// out of scope. Note that it does *not* increment the reference count on acquisition but
// it *does* decrement the count if you don't use Detach.
//
// It also does not have a copy constructor and doesn't try to manage passing pointers
// around. This is simply used to simplify functions by allowing early exits.
Object(const Object& illegal) { }
void operator=(const Object& illegal) { }
protected:
PyObject* p;
public:
Object(PyObject* _p = 0)
{
p = _p;
}
~Object()
{
Py_XDECREF(p);
}
Object& operator=(PyObject* pNew)
{
Py_XDECREF(p);
p = pNew;
return *this;
}
bool IsValid() const { return p != 0; }
bool Attach(PyObject* _p)
{
// Returns true if the new pointer is non-zero.
Py_XDECREF(p);
p = _p;
return (_p != 0);
}
PyObject* Detach()
{
PyObject* pT = p;
p = 0;
return pT;
}
operator PyObject*()
{
return p;
}
operator PyVarObject*() { return (PyVarObject*)p; }
operator const bool() { return p != 0; }
PyObject* Get()
{
return p;
}
};
class Tuple
: public Object
{
private:
Tuple(const Tuple& other) {}
void operator=(const Tuple& other) {}
public:
Tuple(PyObject* _p = 0)
: Object(_p)
{
}
operator PyTupleObject*()
{
return (PyTupleObject*)p;
}
PyObject*& operator[](int i)
{
I(p != 0);
return PyTuple_GET_ITEM(p, i);
}
Py_ssize_t size() { return p ? PyTuple_GET_SIZE(p) : 0; }
};
#ifdef WINVER
struct RegKey
{
HKEY hkey;
RegKey()
{
hkey = 0;
}
~RegKey()
{
if (hkey != 0)
RegCloseKey(hkey);
}
operator HKEY() { return hkey; }
};
#endif
#endif // _WRAPPER_H_