1 /* ----------------------------------------------------------------------------- 2 * constraints.i 3 * 4 * SWIG constraints library. 5 * 6 * SWIG library file containing typemaps for implementing various kinds of 7 * constraints. Depends upon the SWIG exception library for generating 8 * errors in a language-independent manner. 9 * ----------------------------------------------------------------------------- */ 10 11 #ifdef AUTODOC 12 %text %{ 13 %include <constraints.i> 14 15 This library provides support for applying constraints to function 16 arguments. Using a constraint, you can restrict arguments to be 17 positive numbers, non-NULL pointers, and so on. The following 18 constraints are available : 19 20 Number POSITIVE - Positive number (not zero) 21 Number NEGATIVE - Negative number (not zero) 22 Number NONZERO - Nonzero number 23 Number NONNEGATIVE - Positive number (including zero) 24 Number NONPOSITIVE - Negative number (including zero) 25 Pointer NONNULL - Non-NULL pointer 26 Pointer ALIGN8 - 8-byte aligned pointer 27 Pointer ALIGN4 - 4-byte aligned pointer 28 Pointer ALIGN2 - 2-byte aligned pointer 29 30 To use the constraints, you need to "apply" them to specific 31 function arguments in your code. This is done using the %apply 32 directive. For example : 33 34 %apply Number NONNEGATIVE { double nonneg }; 35 double sqrt(double nonneg); // Name of argument must match 36 37 %apply Pointer NONNULL { void *ptr }; 38 void *malloc(int POSITIVE); // May return a NULL pointer 39 void free(void *ptr); // May not accept a NULL pointer 40 41 Any function argument of the type you specify with the %apply directive 42 will be checked with the appropriate constraint. Multiple types may 43 be specified as follows : 44 45 %apply Pointer NONNULL { void *, Vector *, List *, double *}; 46 47 In this case, all of the types listed would be checked for non-NULL 48 pointers. 49 50 The common datatypes of int, short, long, unsigned int, unsigned long, 51 unsigned short, unsigned char, signed char, float, and double can be 52 checked without using the %apply directive by simply using the 53 constraint name as the parameter name. For example : 54 55 double sqrt(double NONNEGATIVE); 56 double log(double POSITIVE); 57 58 If you have used typedef to change type-names, you can also do this : 59 60 %apply double { Real }; // Make everything defined for doubles 61 // work for Reals. 62 Real sqrt(Real NONNEGATIVE); 63 Real log(Real POSITIVE); 64 65 %} 66 #endif 67 68 %include <exception.i> 69 70 #ifdef SWIGCSHARP 71 // Required attribute for C# exception handling 72 #define SWIGCSHARPCANTHROW , canthrow=1 73 #else 74 #define SWIGCSHARPCANTHROW 75 #endif 76 77 78 // Positive numbers 79 80 %typemap(check SWIGCSHARPCANTHROW) 81 int POSITIVE, 82 short POSITIVE, 83 long POSITIVE, 84 unsigned int POSITIVE, 85 unsigned short POSITIVE, 86 unsigned long POSITIVE, 87 signed char POSITIVE, 88 unsigned char POSITIVE, 89 float POSITIVE, 90 double POSITIVE, 91 Number POSITIVE 92 { 93 if ($1 <= 0) { 94 SWIG_exception(SWIG_ValueError,"Expected a positive value."); 95 } 96 } 97 98 // Negative numbers 99 100 %typemap(check SWIGCSHARPCANTHROW) 101 int NEGATIVE, 102 short NEGATIVE, 103 long NEGATIVE, 104 unsigned int NEGATIVE, 105 unsigned short NEGATIVE, 106 unsigned long NEGATIVE, 107 signed char NEGATIVE, 108 unsigned char NEGATIVE, 109 float NEGATIVE, 110 double NEGATIVE, 111 Number NEGATIVE 112 { 113 if ($1 >= 0) { 114 SWIG_exception(SWIG_ValueError,"Expected a negative value."); 115 } 116 } 117 118 // Nonzero numbers 119 120 %typemap(check SWIGCSHARPCANTHROW) 121 int NONZERO, 122 short NONZERO, 123 long NONZERO, 124 unsigned int NONZERO, 125 unsigned short NONZERO, 126 unsigned long NONZERO, 127 signed char NONZERO, 128 unsigned char NONZERO, 129 float NONZERO, 130 double NONZERO, 131 Number NONZERO 132 { 133 if ($1 == 0) { 134 SWIG_exception(SWIG_ValueError,"Expected a nonzero value."); 135 } 136 } 137 138 // Nonnegative numbers 139 140 %typemap(check SWIGCSHARPCANTHROW) 141 int NONNEGATIVE, 142 short NONNEGATIVE, 143 long NONNEGATIVE, 144 unsigned int NONNEGATIVE, 145 unsigned short NONNEGATIVE, 146 unsigned long NONNEGATIVE, 147 signed char NONNEGATIVE, 148 unsigned char NONNEGATIVE, 149 float NONNEGATIVE, 150 double NONNEGATIVE, 151 Number NONNEGATIVE 152 { 153 if ($1 < 0) { 154 SWIG_exception(SWIG_ValueError,"Expected a non-negative value."); 155 } 156 } 157 158 // Nonpositive numbers 159 160 %typemap(check SWIGCSHARPCANTHROW) 161 int NONPOSITIVE, 162 short NONPOSITIVE, 163 long NONPOSITIVE, 164 unsigned int NONPOSITIVE, 165 unsigned short NONPOSITIVE, 166 unsigned long NONPOSITIVE, 167 signed char NONPOSITIVE, 168 unsigned char NONPOSITIVE, 169 float NONPOSITIVE, 170 double NONPOSITIVE, 171 Number NONPOSITIVE 172 { 173 if ($1 > 0) { 174 SWIG_exception(SWIG_ValueError,"Expected a non-positive value."); 175 } 176 } 177 178 // Non-NULL pointer 179 180 %typemap(check SWIGCSHARPCANTHROW) 181 void * NONNULL, 182 Pointer NONNULL 183 { 184 if (!$1) { 185 SWIG_exception(SWIG_ValueError,"Received a NULL pointer."); 186 } 187 } 188 189 // Aligned pointers 190 191 %typemap(check SWIGCSHARPCANTHROW) 192 void * ALIGN8, 193 Pointer ALIGN8 194 { 195 unsigned long long tmp; 196 tmp = (unsigned long long) $1; 197 if (tmp & 7) { 198 SWIG_exception(SWIG_ValueError,"Pointer must be 8-byte aligned."); 199 } 200 } 201 202 %typemap(check SWIGCSHARPCANTHROW) 203 void * ALIGN4, 204 Pointer ALIGN4 205 { 206 unsigned long long tmp; 207 tmp = (unsigned long long) $1; 208 if (tmp & 3) { 209 SWIG_exception(SWIG_ValueError,"Pointer must be 4-byte aligned."); 210 } 211 } 212 213 %typemap(check SWIGCSHARPCANTHROW) 214 void * ALIGN2, 215 Pointer ALIGN2 216 { 217 unsigned long long tmp; 218 tmp = (unsigned long long) $1; 219 if (tmp & 1) { 220 SWIG_exception(SWIG_ValueError,"Pointer must be 2-byte aligned."); 221 } 222 } 223 224 225