Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | /* ----------------------------------------------------------------------------- |
2 | * carrays.i | |
3 | * | |
4 | * Author(s): David Beazley (beazley@cs.uchicago.edu) | |
5 | * | |
6 | * This library file contains macros that can be used to manipulate simple | |
7 | * pointers as arrays. | |
8 | * | |
9 | * $Header: /cvsroot/swig/SWIG/Lib/carrays.i,v 1.5 2004/10/06 16:51:19 marcelomatus Exp $ | |
10 | * ----------------------------------------------------------------------------- */ | |
11 | ||
12 | /* ----------------------------------------------------------------------------- | |
13 | * %array_functions(TYPE,NAME) | |
14 | * | |
15 | * Generates functions for creating and accessing elements of a C array | |
16 | * (as pointers). Creates the following functions: | |
17 | * | |
18 | * TYPE *new_NAME(int nelements) | |
19 | * void delete_NAME(TYPE *); | |
20 | * TYPE NAME_getitem(TYPE *, int index); | |
21 | * void NAME_setitem(TYPE *, int index, TYPE value); | |
22 | * | |
23 | * ----------------------------------------------------------------------------- */ | |
24 | ||
25 | %define %array_functions(TYPE,NAME) | |
26 | %{ | |
27 | static TYPE *new_##NAME(int nelements) { %} | |
28 | #ifdef __cplusplus | |
29 | %{ return new TYPE[nelements]; %} | |
30 | #else | |
31 | %{ return (TYPE *) calloc(nelements,sizeof(TYPE)); %} | |
32 | #endif | |
33 | %{} | |
34 | ||
35 | static void delete_##NAME(TYPE *ary) { %} | |
36 | #ifdef __cplusplus | |
37 | %{ delete [] ary; %} | |
38 | #else | |
39 | %{ free(ary); %} | |
40 | #endif | |
41 | %{} | |
42 | ||
43 | static TYPE NAME##_getitem(TYPE *ary, int index) { | |
44 | return ary[index]; | |
45 | } | |
46 | static void NAME##_setitem(TYPE *ary, int index, TYPE value) { | |
47 | ary[index] = value; | |
48 | } | |
49 | %} | |
50 | ||
51 | TYPE *new_##NAME(int nelements); | |
52 | void delete_##NAME(TYPE *ary); | |
53 | TYPE NAME##_getitem(TYPE *ary, int index); | |
54 | void NAME##_setitem(TYPE *ary, int index, TYPE value); | |
55 | ||
56 | %enddef | |
57 | ||
58 | ||
59 | /* ----------------------------------------------------------------------------- | |
60 | * %array_class(TYPE,NAME) | |
61 | * | |
62 | * Generates a class wrapper around a C array. The class has the following | |
63 | * interface: | |
64 | * | |
65 | * struct NAME { | |
66 | * NAME(int nelements); | |
67 | * ~NAME(); | |
68 | * TYPE getitem(int index); | |
69 | * void setitem(int index, TYPE value); | |
70 | * TYPE * cast(); | |
71 | * static NAME *frompointer(TYPE *t); | |
72 | * } | |
73 | * | |
74 | * ----------------------------------------------------------------------------- */ | |
75 | ||
76 | %define %array_class(TYPE,NAME) | |
77 | %{ | |
78 | typedef TYPE NAME; | |
79 | %} | |
80 | typedef struct NAME { | |
81 | /* Put language specific enhancements here */ | |
82 | ||
83 | #if defined(SWIGPYTHON) || defined(SWIGRUBY) | |
84 | %rename(__getitem__) getitem; | |
85 | %rename(__setitem__) setitem; | |
86 | #endif | |
87 | } NAME; | |
88 | ||
89 | %extend NAME { | |
90 | ||
91 | #ifdef __cplusplus | |
92 | NAME(int nelements) { | |
93 | return new TYPE[nelements]; | |
94 | } | |
95 | ~NAME() { | |
96 | delete [] self; | |
97 | } | |
98 | #else | |
99 | NAME(int nelements) { | |
100 | return (TYPE *) calloc(nelements,sizeof(TYPE)); | |
101 | } | |
102 | ~NAME() { | |
103 | free(self); | |
104 | } | |
105 | #endif | |
106 | ||
107 | TYPE getitem(int index) { | |
108 | return self[index]; | |
109 | } | |
110 | void setitem(int index, TYPE value) { | |
111 | self[index] = value; | |
112 | } | |
113 | TYPE * cast() { | |
114 | return self; | |
115 | } | |
116 | static NAME *frompointer(TYPE *t) { | |
117 | return (NAME *) t; | |
118 | } | |
119 | ||
120 | }; | |
121 | ||
122 | %types(NAME = TYPE); | |
123 | ||
124 | %enddef | |
125 |