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