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 * cpointer.swg 6 * 7 * This library file contains macros that can be used to manipulate simple 8 * pointer objects. 9 * 10 * ----------------------------------------------------------------------------- */ 11 12/* ----------------------------------------------------------------------------- 13 * %pointer_class(type,name) 14 * 15 * Places a simple proxy around a simple type like 'int', 'float', or whatever. 16 * The proxy provides this interface: 17 * 18 * class type { 19 * public: 20 * type(); 21 * ~type(); 22 * type value(); 23 * void assign(type value); 24 * }; 25 * 26 * Example: 27 * 28 * %pointer_class(int, intp); 29 * 30 * int add(int *x, int *y) { return *x + *y; } 31 * 32 * In python (with proxies) 33 * 34 * >>> a = intp() 35 * >>> a.assign(10) 36 * >>> a.value() 37 * 10 38 * >>> b = intp() 39 * >>> b.assign(20) 40 * >>> print add(a,b) 41 * 30 42 * 43 * As a general rule, this macro should not be used on class/structures that 44 * are already defined in the interface. 45 * ----------------------------------------------------------------------------- */ 46 47 48%define %pointer_class(TYPE, NAME) 49%{ 50typedef TYPE NAME; 51%} 52 53typedef struct { 54} NAME; 55 56%extend NAME { 57 NAME() { 58 return %new_instance(TYPE); 59 } 60 ~NAME() { 61 if (self) %delete(self); 62 } 63} 64 65%extend NAME { 66 67 void assign(TYPE value) { 68 *self = value; 69 } 70 TYPE value() { 71 return *self; 72 } 73 TYPE * cast() { 74 return self; 75 } 76 static NAME * frompointer(TYPE *t) { 77 return (NAME *) t; 78 } 79} 80 81%types(NAME = TYPE); 82 83%enddef 84 85/* ----------------------------------------------------------------------------- 86 * %pointer_functions(type,name) 87 * 88 * Create functions for allocating/deallocating pointers. This can be used 89 * if you don't want to create a proxy class or if the pointer is complex. 90 * 91 * %pointer_functions(int, intp) 92 * 93 * int add(int *x, int *y) { return *x + *y; } 94 * 95 * In python (with proxies) 96 * 97 * >>> a = copy_intp(10) 98 * >>> intp_value(a) 99 * 10 100 * >>> b = new_intp() 101 * >>> intp_assign(b,20) 102 * >>> print add(a,b) 103 * 30 104 * >>> delete_intp(a) 105 * >>> delete_intp(b) 106 * 107 * ----------------------------------------------------------------------------- */ 108 109%define %pointer_functions(TYPE,NAME) 110%{ 111 static TYPE *new_##NAME() { 112 return %new_instance(TYPE); 113 } 114 115 static TYPE *copy_##NAME(TYPE value) { 116 return %new_copy(value, TYPE); 117 } 118 119 static void delete_##NAME(TYPE *self) { 120 if (self) %delete(self); 121 } 122 123 static void NAME ##_assign(TYPE *self, TYPE value) { 124 *self = value; 125 } 126 127 static TYPE NAME ##_value(TYPE *self) { 128 return *self; 129 } 130%} 131 132TYPE *new_##NAME(); 133TYPE *copy_##NAME(TYPE value); 134void delete_##NAME(TYPE *self); 135void NAME##_assign(TYPE *self, TYPE value); 136TYPE NAME##_value(TYPE *self); 137 138%enddef 139 140/* ----------------------------------------------------------------------------- 141 * %pointer_cast(type1,type2,name) 142 * 143 * Generates a pointer casting function. 144 * ----------------------------------------------------------------------------- */ 145 146%define %pointer_cast(TYPE1,TYPE2,NAME) 147%inline %{ 148TYPE2 NAME(TYPE1 x) { 149 return %static_cast(x, TYPE2); 150} 151%} 152%enddef 153 154 155 156 157 158 159 160 161