PageRenderTime 16ms CodeModel.GetById 11ms app.highlight 1ms RepoModel.GetById 2ms app.codeStats 0ms

/trunk/Lib/python/carrays.i

#
Swig | 53 lines | 38 code | 15 blank | 0 comment | 0 complexity | fb907954506446a48a82754d3e58be9f MD5 | raw file
 1%define %array_class(TYPE,NAME)
 2#if defined(SWIGPYTHON_BUILTIN)
 3  %feature("python:slot", "sq_item", functype="ssizeargfunc") NAME::__getitem__;
 4  %feature("python:slot", "sq_ass_item", functype="ssizeobjargproc") NAME::__setitem__;
 5
 6%inline %{
 7typedef struct {
 8    TYPE *el;
 9} NAME;
10%}
11
12%extend NAME {
13
14  NAME(size_t nelements) {
15      NAME *arr = %new_instance(NAME);
16      arr->el = %new_array(nelements, TYPE);
17      return arr;
18  }
19
20  ~NAME() {
21      %delete_array(self->el);
22      %delete(self);
23  }
24  
25  TYPE __getitem__(size_t index) {
26      return self->el[index];
27  }
28
29  void __setitem__(size_t index, TYPE value) {
30      self->el[index] = value;
31  }
32
33  TYPE * cast() {
34      return self->el;
35  }
36
37  static NAME *frompointer(TYPE *t) {
38      return %reinterpret_cast(t, NAME *);
39  }
40};
41
42%types(NAME = TYPE);
43
44#else
45  %array_class_wrap(TYPE,NAME,__getitem__,__setitem__)
46#endif
47%enddef
48
49%include <typemaps/carrays.swg>
50
51
52
53