1/* -----------------------------------------------------------------------------
2 * See the LICENSE file for information on copyright, usage and redistribution
3 * of SWIG, and the README file for authors - http://www.swig.org/release.html.
4 *
5 * carrays.swg
6 *
7 * This library file contains macros that can be used to manipulate simple
8 * pointers as arrays.
9 * ----------------------------------------------------------------------------- */
10
11/* -----------------------------------------------------------------------------
12 * %array_functions(TYPE,NAME)
13 *
14 * Generates functions for creating and accessing elements of a C array
15 * (as pointers).  Creates the following functions:
16 *
17 *        TYPE *new_NAME(int nelements)
18 *        void delete_NAME(TYPE *);
19 *        TYPE NAME_getitem(TYPE *, int index);
20 *        void NAME_setitem(TYPE *, int index, TYPE value);
21 *
22 * ----------------------------------------------------------------------------- */
23
24%define %array_functions(TYPE,NAME)
25%{
26  static TYPE *new_##NAME(size_t nelements) {
27    return %new_array(nelements, TYPE);
28  }
29
30  static void delete_##NAME(TYPE *ary) {
31    %delete_array(ary);
32  }
33
34  static TYPE NAME##_getitem(TYPE *ary, size_t index) {
35    return ary[index];
36  }
37  static void NAME##_setitem(TYPE *ary, size_t index, TYPE value) {
38    ary[index] = value;
39  }
40%}
41
42TYPE *new_##NAME(size_t nelements);
43void delete_##NAME(TYPE *ary);
44TYPE NAME##_getitem(TYPE *ary, size_t index);
45void NAME##_setitem(TYPE *ary, size_t index, TYPE value);
46
47%enddef
48
49
50/* -----------------------------------------------------------------------------
51 * %array_class(TYPE,NAME)
52 *
53 * Generates a class wrapper around a C array.  The class has the following
54 * interface:
55 *
56 *          struct NAME {
57 *              NAME(int nelements);
58 *             ~NAME();
59 *              TYPE getitem(int index);
60 *              void setitem(int index, TYPE value);
61 *              TYPE * cast();
62 *              static NAME *frompointer(TYPE *t);
63 *         }
64 *
65 * Use
66 *
67 *    %array_class_wrap(TYPE,NAME,GET,SET)
68 *
69 * if you want  different names for the get/set methods.
70 * ----------------------------------------------------------------------------- */
71
72%define %array_class_wrap(TYPE,NAME,getitem,setitem)
73%{
74typedef TYPE NAME;
75%}
76
77
78typedef struct NAME {
79} NAME;
80
81%extend NAME {
82
83  NAME(size_t nelements) {
84    return %new_array(nelements, TYPE);
85  }
86
87  ~NAME() {
88    %delete_array(self);
89  }
90
91  TYPE getitem(size_t index) {
92    return self[index];
93  }
94
95  void setitem(size_t index, TYPE value) {
96    self[index] = value;
97  }
98
99  TYPE * cast() {
100    return self;
101  }
102
103  static NAME *frompointer(TYPE *t) {
104    return %static_cast(t, NAME *);
105  }
106};
107
108%types(NAME = TYPE);
109
110%enddef
111
112
113#ifndef %array_class
114%define %array_class(TYPE,NAME)
115  %array_class_wrap(TYPE,NAME,getitem,setitem)
116%enddef
117#endif
118