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.i 6 * 7 * SWIG library file containing 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%{ 26static TYPE *new_##NAME(int nelements) { %} 27#ifdef __cplusplus 28%{ return new TYPE[nelements]; %} 29#else 30%{ return (TYPE *) calloc(nelements,sizeof(TYPE)); %} 31#endif 32%{} 33 34static void delete_##NAME(TYPE *ary) { %} 35#ifdef __cplusplus 36%{ delete [] ary; %} 37#else 38%{ free(ary); %} 39#endif 40%{} 41 42static TYPE NAME##_getitem(TYPE *ary, int index) { 43 return ary[index]; 44} 45static void NAME##_setitem(TYPE *ary, int index, TYPE value) { 46 ary[index] = value; 47} 48%} 49 50TYPE *new_##NAME(int nelements); 51void delete_##NAME(TYPE *ary); 52TYPE NAME##_getitem(TYPE *ary, int index); 53void NAME##_setitem(TYPE *ary, int index, TYPE value); 54 55%enddef 56 57 58/* ----------------------------------------------------------------------------- 59 * %array_class(TYPE,NAME) 60 * 61 * Generates a class wrapper around a C array. The class has the following 62 * interface: 63 * 64 * struct NAME { 65 * NAME(int nelements); 66 * ~NAME(); 67 * TYPE getitem(int index); 68 * void setitem(int index, TYPE value); 69 * TYPE * cast(); 70 * static NAME *frompointer(TYPE *t); 71 * } 72 * 73 * ----------------------------------------------------------------------------- */ 74 75%define %array_class(TYPE,NAME) 76%{ 77typedef TYPE NAME; 78%} 79typedef struct NAME { 80 /* Put language specific enhancements here */ 81 82} NAME; 83 84%extend NAME { 85 86#ifdef __cplusplus 87NAME(int nelements) { 88 return new TYPE[nelements]; 89} 90~NAME() { 91 delete [] self; 92} 93#else 94NAME(int nelements) { 95 return (TYPE *) calloc(nelements,sizeof(TYPE)); 96} 97~NAME() { 98 free(self); 99} 100#endif 101 102TYPE getitem(int index) { 103 return self[index]; 104} 105void setitem(int index, TYPE value) { 106 self[index] = value; 107} 108TYPE * cast() { 109 return self; 110} 111static NAME *frompointer(TYPE *t) { 112 return (NAME *) t; 113} 114 115}; 116 117%types(NAME = TYPE); 118 119%enddef 120 121