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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
///////////////////////////////////////////////////////////////////////////////////////////////
// handling for making C++ types act as python lists/tuples, to allow a limited amount of
// by-reference access from python
//
// We do this mostly by adding named methods and slots that modify or access the list in place.
%header %{
// hacky macro to check if two variables are the same by looking at their length & second letter.
// Used to identify if we're dealing with 'self' in a typemap or not
#define STR_EQ(a, b) (sizeof(#a) == sizeof(#b) && (#a[1] == #b[1]))
%}
// a typemap that allows us to modify 'self' in place, but convert any inputs by value.
%define LIST_MODIFY_IN_PLACE_TYPEMAP(ContainerType)
%typemap(in) const ContainerType & (unsigned char tempmem[32], bool wasSelf = false) {
using array_type = std::remove_pointer<decltype($1)>::type;
{
// convert the sequence by value using ConvertFromPy
static_assert(sizeof(tempmem) >= sizeof(array_type), "not enough temp space for $1_basetype");
tempalloc($1, tempmem);
int failIdx = 0;
int res = TypeConversion<array_type>::ConvertFromPy($input, indirect($1), &failIdx);
if(!SWIG_IsOK(res))
{
if(res == SWIG_TypeError)
{
SWIG_exception_fail(SWIG_ArgError(res), "in method '$symname' argument $argnum of type '$1_basetype'");
}
else
{
snprintf(convert_error, sizeof(convert_error)-1, "in method '$symname' argument $argnum of type '$1_basetype', decoding element %d", failIdx);
SWIG_exception_fail(SWIG_ArgError(res), convert_error);
}
}
}
}
%typemap(in) ContainerType * (unsigned char tempmem[32], bool wasSelf = false) {
using array_type = std::remove_pointer<decltype($1)>::type;
// don't convert 'self', leave as-is so we can modify by reference. Everything else needs to be
// converted/copied to allow passing lists (or any sequence) python objects to a function
// expecting a particular C++ array type
constexpr bool isSelf = STR_EQ($input, self);
wasSelf = isSelf;
if(isSelf)
{
// we use an indirect dispatch class here (see self_dispatch) to avoid the need to instantiate
// the conversion template for types we don't care about
$1 = self_dispatch<isSelf>::getthis<array_type>(self);
}
else
{
// convert the sequence by value using ConvertFromPy
static_assert(sizeof(tempmem) >= sizeof(array_type), "not enough temp space for $1_basetype");
tempalloc($1, tempmem);
int failIdx = 0;
int res = TypeConversion<array_type>::ConvertFromPy($input, indirect($1), &failIdx);
if(!SWIG_IsOK(res))
{
if(res == SWIG_TypeError)
{
SWIG_exception_fail(SWIG_ArgError(res), "in method '$symname' argument $argnum of type '$1_basetype'");
}
else
{
snprintf(convert_error, sizeof(convert_error)-1, "in method '$symname' argument $argnum of type '$1_basetype', decoding element %d", failIdx);
SWIG_exception_fail(SWIG_ArgError(res), convert_error);
}
}
}
}
%typemap(freearg) ContainerType * {
// if we converted the sequence by-value, then destroy it again
if(!wasSelf$argnum)
tempdealloc($1);
}
%enddef // %define LIST_MODIFY_IN_PLACE_TYPEMAP
// this macro defines the list array class named methods, forwarding to templated implementations
%define EXTEND_ARRAY_CLASS_METHODS(Container)
%extend Container {
// functions with optional parameters need to use kwargs otherwise SWIG wraps them in multiple
// overloads
%feature("kwargs") pop;
%feature("kwargs") sort;
%feature("kwargs") index;
PyObject *append(PyObject *value)
{
return array_append($self, value);
}
PyObject *clear()
{
return array_clear($self);
}
PyObject *insert(PyObject *index, PyObject *value)
{
return array_insert($self, index, value);
}
PyObject *pop(PyObject *index = NULL)
{
return array_pop($self, index);
}
PyObject *sort(PyObject *key = NULL, bool reverse = false)
{
return array_sort($self, key, reverse);
}
PyObject *copy()
{
return array_copy($self);
}
PyObject *reverse()
{
return array_reverse($self);
}
PyObject *index(PyObject *item, PyObject *start = NULL, PyObject *end = NULL)
{
return array_indexOf($self, item, start, end);
}
PyObject *count(PyObject *item)
{
return array_countOf($self, item);
}
PyObject *extend(PyObject *items)
{
return array_selfconcat($self, items);
}
PyObject *remove(PyObject *item)
{
return array_removeOne($self, item);
}
} // %extend Container
%enddef // define EXTEND_ARRAY_CLASS_METHODS(Container)
// Declare the slots that we're going to add - see ARRAY_DEFINE_SLOTS for the definitions
%define ARRAY_ADD_SLOTS(array_type, unique_name)
// define slots
%feature("python:tp_repr") array_type STRINGIZE(repr_##unique_name);
%feature("python:tp_str") array_type STRINGIZE(repr_##unique_name);
%feature("python:sq_item") array_type STRINGIZE(getitem_##unique_name);
%feature("python:sq_ass_item") array_type STRINGIZE(setitem_##unique_name);
%feature("python:sq_length") array_type STRINGIZE(length_##unique_name);
%feature("python:sq_concat") array_type STRINGIZE(concat_##unique_name);
%feature("python:sq_repeat") array_type STRINGIZE(repeat_##unique_name);
%feature("python:sq_inplace_concat") array_type STRINGIZE(selfconcat_##unique_name);
%feature("python:sq_inplace_repeat") array_type STRINGIZE(selfrepeat_##unique_name);
%feature("python:mp_subscript") array_type STRINGIZE(getsubscript_##unique_name);
%feature("python:mp_ass_subscript") array_type STRINGIZE(setsubscript_##unique_name);
// https://docs.python.org/3/library/collections.abc.html
// https://docs.python.org/3/library/stdtypes.html#typesseq-common
// https://docs.python.org/3/library/stdtypes.html#typesseq-mutable
// https://docs.python.org/3/library/stdtypes.html#list
%enddef
// define C exported wrappers to bind to the slots that forward to templated implementations
%define ARRAY_DEFINE_SLOTS(array_type, unique_name)
// define C-exported wrappers that forward to templated implementations
%wrapper %{
PyObject *repr_##unique_name(PyObject *self)
{
array_type *thisptr = array_thisptr<array_type>(self);
if(!thisptr)
return NULL;
return array_repr(thisptr);
}
PyObject *getitem_##unique_name(PyObject *self, Py_ssize_t idx)
{
array_type *thisptr = array_thisptr<array_type>(self);
if(!thisptr)
return NULL;
return array_getitem(thisptr, idx);
}
int setitem_##unique_name(PyObject *self, Py_ssize_t idx, PyObject *val)
{
array_type *thisptr = array_thisptr<array_type>(self);
if(!thisptr)
return -1;
return array_setitem(thisptr, idx, val);
}
Py_ssize_t length_##unique_name(PyObject *self, Py_ssize_t idx, PyObject *val)
{
array_type *thisptr = array_thisptr<array_type>(self);
if(!thisptr)
return -1;
return array_len(thisptr);
}
PyObject *getsubscript_##unique_name(PyObject *self, PyObject *idx)
{
array_type *thisptr = array_thisptr<array_type>(self);
if(!thisptr)
return NULL;
return array_getsubscript(thisptr, idx);
}
int setsubscript_##unique_name(PyObject *self, PyObject *idx, PyObject *val)
{
array_type *thisptr = array_thisptr<array_type>(self);
if(!thisptr)
return -1;
return array_setsubscript(thisptr, idx, val);
}
PyObject *concat_##unique_name(PyObject *self, PyObject *vals)
{
array_type *thisptr = array_thisptr<array_type>(self);
if(!thisptr)
return NULL;
return array_concat(thisptr, vals);
}
PyObject *repeat_##unique_name(PyObject *self, Py_ssize_t count)
{
array_type *thisptr = array_thisptr<array_type>(self);
if(!thisptr)
return NULL;
return array_repeat(thisptr, count);
}
PyObject *selfconcat_##unique_name(PyObject *self, PyObject *vals)
{
array_type *thisptr = array_thisptr<array_type>(self);
if(!thisptr)
return NULL;
PyObject *ret = array_selfconcat(thisptr, vals);
if(ret)
{
Py_DECREF(ret);
Py_INCREF(self);
return self;
}
return NULL;
}
PyObject *selfrepeat_##unique_name(PyObject *self, Py_ssize_t count)
{
array_type *thisptr = array_thisptr<array_type>(self);
if(!thisptr)
return NULL;
PyObject *ret = array_selfrepeat(thisptr, count);
if(ret)
{
Py_DECREF(ret);
Py_INCREF(self);
return self;
}
return NULL;
}
%}
%enddef
%define ARRAY_INSTANTIATION_CHECK_NAME(typeName) add_your_use_of_##typeName##_to_swig_interface %enddef
// these pair of macros (TEMPLATE_ARRAY_DECLARE / NON_TEMPLATE_ARRAY_INSTANTIATE) are defined before
// including the header that defines a class, to set up typemaps and other things that must be
// available before the definition is parsed.
//
// Afterwards, you must call EXTEND_ARRAY_CLASS_METHODS to add the necessary named methods.
%define TEMPLATE_ARRAY_DECLARE(typeName)
LIST_MODIFY_IN_PLACE_TYPEMAP(typeName)
// add a check to make sure that we explicitly instantiate all uses of this template (as a reference
// type, not as a purely in parameter to a function - those are converted by value to C++ to allow
// passing pure lists that aren't C++ side at all).
%header %{
template<typename innerType>
void ARRAY_INSTANTIATION_CHECK_NAME(typeName)(typeName<innerType> *);
%}
// override these typemaps to instantiate a checking template.
%typemap(check) typeName * { ARRAY_INSTANTIATION_CHECK_NAME(typeName)($1); }
%typemap(check) typeName & { ARRAY_INSTANTIATION_CHECK_NAME(typeName)($1); }
%typemap(check) typeName { ARRAY_INSTANTIATION_CHECK_NAME(typeName)($1); }
%typemap(ret) typeName * { ARRAY_INSTANTIATION_CHECK_NAME(typeName)($1); }
%typemap(ret) typeName & { ARRAY_INSTANTIATION_CHECK_NAME(typeName)($1); }
%typemap(ret) typeName { ARRAY_INSTANTIATION_CHECK_NAME(typeName)(&$1); }
%enddef
%define NON_TEMPLATE_ARRAY_INSTANTIATE(typeName)
ARRAY_ADD_SLOTS(typeName, typeName)
ARRAY_DEFINE_SLOTS(typeName, typeName)
LIST_MODIFY_IN_PLACE_TYPEMAP(typeName)
%enddef
// this instantiates a templated array for a particular type and sets it up to act like a python
// sequence.
%define TEMPLATE_ARRAY_INSTANTIATE(arrayType, innerType)
ARRAY_ADD_SLOTS(arrayType<innerType>, arrayType##_of_##innerType)
// instantiate template
%rename(arrayType##_of_##innerType) arrayType<innerType>;
%template(arrayType##_of_##innerType) arrayType<innerType>;
ARRAY_DEFINE_SLOTS(arrayType<innerType>, arrayType##_of_##innerType)
%header %{
template<>
void ARRAY_INSTANTIATION_CHECK_NAME(arrayType)(arrayType<innerType> *)
{
}
%}
%enddef
// variation of the above to handle pointer'd inner type
%define TEMPLATE_ARRAY_INSTANTIATE_PTR(arrayType, innerType)
ARRAY_ADD_SLOTS(arrayType<innerType *>, arrayType##_of_ptr_##innerType)
// instantiate template
%rename(arrayType##_of_ptr_##innerType) arrayType<innerType *>;
%template(arrayType##_of_ptr_##innerType) arrayType<innerType *>;
ARRAY_DEFINE_SLOTS(arrayType<innerType *>, arrayType##_of_ptr_##innerType)
%header %{
template<>
void ARRAY_INSTANTIATION_CHECK_NAME(arrayType)(arrayType<innerType *> *)
{
}
%}
%enddef
%define TEMPLATE_NAMESPACE_ARRAY_INSTANTIATE(arrayType, nspace, innerType)
ARRAY_ADD_SLOTS(arrayType<nspace::innerType>, arrayType##_of_##nspace##_##innerType)
// instantiate template
%rename(arrayType##_of_##nspace##_##innerType) arrayType<nspace::innerType>;
%template(arrayType##_of_##nspace##_##innerType) arrayType<nspace::innerType>;
ARRAY_DEFINE_SLOTS(arrayType<nspace::innerType>, arrayType##_of_##nspace##_##innerType)
%header %{
template<>
void ARRAY_INSTANTIATION_CHECK_NAME(arrayType)(arrayType<nspace::innerType> *)
{
}
%}
%enddef
|