File: vector.sip

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (179 lines) | stat: -rw-r--r-- 3,880 bytes parent folder | download | duplicates (4)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// SIP support for std::vector
// by Giovanni Bajo
// Public domain


// ****************************************************
// SIP generic implementation for std::vector<>
// ****************************************************
// ALas, this template-based generic implementation is valid only
// if the element type is a SIP-wrapped type. For basic types (int, double, etc.)
// we are forced to cut & paste to provide a specialization.

template<TYPE>
%MappedType vector<TYPE>
{
%TypeHeaderCode
	#include <vector>
%End

%ConvertFromTypeCode
	PyObject *l;

	// Create the Python list of the correct length.
	if ((l = PyList_New(sipCpp -> size())) == NULL)
		return NULL;

	// Go through each element in the C++ instance and convert it to a
	// wrapped P2d.
	for (int i = 0; i < (int)sipCpp -> size(); ++i)
	{
		TYPE *cpp = new TYPE(sipCpp -> at(i));
		PyObject *pobj;

		// Get the Python wrapper for the Type instance, creating a new
		// one if necessary, and handle any ownership transfer.
		if ((pobj = BALL_CONVERT_FROM_INSTANCE(cpp, TYPE, sipTransferObj)) == NULL)
		{
			// There was an error so garbage collect the Python list.
			Py_DECREF(l);
			return NULL;
		}

		// Add the wrapper to the list.
		PyList_SET_ITEM(l, i, pobj);
	}

	// Return the Python list.
	return l;
%End

%ConvertToTypeCode
	// Check if type is compatible
	if (sipIsErr == NULL)
	{
		// Must be any iterable
		PyObject *i = PyObject_GetIter(sipPy);
		bool iterable = (i != NULL);
		Py_XDECREF(i);
		return iterable;
	}

	// Iterate over the object
	PyObject *iterator = PyObject_GetIter(sipPy);
	PyObject *item;

	std::vector<TYPE> *V = new std::vector<TYPE>();

	while ((item = PyIter_Next(iterator)))
	{
		if (!BALL_CAN_CONVERT_TO_INSTANCE(item, TYPE))
		{
			PyErr_Format(PyExc_TypeError, "object in iterable cannot be converted to TYPE");
			*sipIsErr = 1;
			break;
		}

		int state;
		TYPE* p = BALL_CONVERT_TO_INSTANCE(item, TYPE, state);

		if (!*sipIsErr)
			V->push_back(*p);

		BALL_RELEASE_INSTANCE(p, TYPE, state);
		Py_DECREF(item);
	}

	Py_DECREF(iterator);

	if (*sipIsErr)
	{
		delete V;
		return 0;
	}

	*sipCppPtr = V;
	return sipGetState(sipTransferObj);
%End
};

template<TYPE *>
%MappedType vector<TYPE *>
{
%TypeHeaderCode
	#include <vector>
%End

%ConvertFromTypeCode
	PyObject *l;

	// Create the Python list of the correct length.
	if ((l = PyList_New(sipCpp -> size())) == NULL)
		return NULL;

	// Go through each element in the C++ instance and convert it to a
	// wrapped P2d.
		int i = 0;
	for (std::vector<TYPE *>::const_iterator it = sipCpp->begin(); it != sipCpp -> end(); ++it, ++i)
	{
		TYPE *cpp = *it;
		PyObject *pobj;

		// Get the Python wrapper for the Type instance, creating a new
		// one if necessary, and handle any ownership transfer.
		if ((pobj = BALL_CONVERT_FROM_INSTANCE(cpp, TYPE, sipTransferObj)) == NULL)
		{
			// There was an error so garbage collect the Python list.
			Py_DECREF(l);
			return NULL;
		}

		// Add the wrapper to the list.
		PyList_SET_ITEM(l, i, pobj);
	}

	// Return the Python list.
	return l;
%End

%ConvertToTypeCode
	// Check if type is compatible
	if (sipIsErr == NULL)
	{
		// Must be any iterable
		PyObject *i = PyObject_GetIter(sipPy);
		bool iterable = (i != NULL);
		Py_XDECREF(i);
		return iterable;
	}

	// Iterate over the object
	PyObject *iterator = PyObject_GetIter(sipPy);
	PyObject *item;

	std::vector<TYPE *> *V = new std::vector<TYPE *>();

	while ((item = PyIter_Next(iterator)))
	{
		if (!BALL_CAN_CONVERT_TO_INSTANCE(item, TYPE))
		{
			PyErr_Format(PyExc_TypeError, "object in iterable cannot be converted to TYPE");
			*sipIsErr = 1;
			break;
		}

		int state;
		TYPE* p = reinterpret_cast<TYPE*>(BALL_CONVERT_TO_INSTANCE(item, TYPE, state));

		if (*sipIsErr) {
			delete V;
			return 0;
		} else {
			V->push_back(p);
		}
	}

	*sipCppPtr = V;
	return sipGetState(sipTransferObj);
%End
};