2 // SWIG constraint library
6 // This library file contains typemaps for implementing various kinds of
7 // constraints. Depends upon the SWIG exception library for generating
8 // errors in a language-independent manner.
11 %section "Constraint Library",info,after,pre,nosort,skip=1,chop_left=3,chop_right=0,chop_top=0,chop_bottom=0
14 %include constraints.i
16 This library provides support for applying constraints to function
17 arguments. Using a constraint, you can restrict arguments to be
18 positive numbers, non-NULL pointers, and so on. The following
19 constraints are available :
21 Number POSITIVE - Positive number (not zero)
22 Number NEGATIVE - Negative number (not zero)
23 Number NONZERO - Nonzero number
24 Number NONNEGATIVE - Positive number (including zero)
25 Number NONPOSITIVE - Negative number (including zero)
26 Pointer NONNULL - Non-NULL pointer
27 Pointer ALIGN8 - 8-byte aligned pointer
28 Pointer ALIGN4 - 4-byte aligned pointer
29 Pointer ALIGN2 - 2-byte aligned pointer
31 To use the constraints, you need to "apply" them to specific
32 function arguments in your code. This is done using the %apply
33 directive. For example :
35 %apply Number NONNEGATIVE { double nonneg };
36 double sqrt(double nonneg); // Name of argument must match
38 %apply Pointer NONNULL { void *ptr };
39 void *malloc(int POSITIVE); // May return a NULL pointer
40 void free(void *ptr); // May not accept a NULL pointer
42 Any function argument of the type you specify with the %apply directive
43 will be checked with the appropriate constraint. Multiple types may
44 be specified as follows :
46 %apply Pointer NONNULL { void *, Vector *, List *, double *};
48 In this case, all of the types listed would be checked for non-NULL
51 The common datatypes of int, short, long, unsigned int, unsigned long,
52 unsigned short, unsigned char, signed char, float, and double can be
53 checked without using the %apply directive by simply using the
54 constraint name as the parameter name. For example :
56 double sqrt(double NONNEGATIVE);
57 double log(double POSITIVE);
59 If you have used typedef to change type-names, you can also do this :
61 %apply double { Real }; // Make everything defined for doubles
63 Real sqrt(Real NONNEGATIVE);
64 Real log(Real POSITIVE);
73 %typemap(check) int POSITIVE,
76 unsigned int POSITIVE,
77 unsigned short POSITIVE,
78 unsigned long POSITIVE,
80 unsigned char POSITIVE,
86 SWIG_exception(SWIG_ValueError,"Expected a positive value.");
92 %typemap(check) int NEGATIVE,
95 unsigned int NEGATIVE,
96 unsigned short NEGATIVE,
97 unsigned long NEGATIVE,
99 unsigned char NEGATIVE,
105 SWIG_exception(SWIG_ValueError,"Expected a negative value.");
111 %typemap(check) int NONZERO,
114 unsigned int NONZERO,
115 unsigned short NONZERO,
116 unsigned long NONZERO,
118 unsigned char NONZERO,
124 SWIG_exception(SWIG_ValueError,"Expected a nonzero value.");
128 // Nonnegative numbers
130 %typemap(check) int NONNEGATIVE,
133 unsigned int NONNEGATIVE,
134 unsigned short NONNEGATIVE,
135 unsigned long NONNEGATIVE,
136 signed char NONNEGATIVE,
137 unsigned char NONNEGATIVE,
143 SWIG_exception(SWIG_ValueError,"Expected a non-negative value.");
147 // Nonpositive numbers
149 %typemap(check) int NONPOSITIVE,
152 unsigned int NONPOSITIVE,
153 unsigned short NONPOSITIVE,
154 unsigned long NONPOSITIVE,
155 signed char NONPOSITIVE,
156 unsigned char NONPOSITIVE,
162 SWIG_exception(SWIG_ValueError,"Expected a non-positive value.");
168 %typemap(check) void * NONNULL,
172 SWIG_exception(SWIG_ValueError,"Received a NULL pointer.");
178 %typemap(check) void * ALIGN8,
182 tmp = (long) $target;
184 SWIG_exception(SWIG_ValueError,"Pointer must be 8-byte aligned.");
188 %typemap(check) void * ALIGN4,
192 tmp = (long) $target;
194 SWIG_exception(SWIG_ValueError,"Pointer must be 4-byte aligned.");
198 %typemap(check) void * ALIGN2,
202 tmp = (long) $target;
204 SWIG_exception(SWIG_ValueError,"Pointer must be 2-byte aligned.");