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.
7 * SWIG constraints library.
9 * SWIG library file containing typemaps for implementing various kinds of
10 * constraints. Depends upon the SWIG exception library for generating
11 * errors in a language-independent manner.
12 * ----------------------------------------------------------------------------- */
16 %include <constraints.i>
18 This library provides support for applying constraints to function
19 arguments. Using a constraint, you can restrict arguments to be
20 positive numbers, non-NULL pointers, and so on. The following
21 constraints are available :
23 Number POSITIVE - Positive number (not zero)
24 Number NEGATIVE - Negative number (not zero)
25 Number NONZERO - Nonzero number
26 Number NONNEGATIVE - Positive number (including zero)
27 Number NONPOSITIVE - Negative number (including zero)
28 Pointer NONNULL - Non-NULL pointer
29 Pointer ALIGN8 - 8-byte aligned pointer
30 Pointer ALIGN4 - 4-byte aligned pointer
31 Pointer ALIGN2 - 2-byte aligned pointer
33 To use the constraints, you need to "apply" them to specific
34 function arguments in your code. This is done using the %apply
35 directive. For example :
37 %apply Number NONNEGATIVE { double nonneg };
38 double sqrt(double nonneg); // Name of argument must match
40 %apply Pointer NONNULL { void *ptr };
41 void *malloc(int POSITIVE); // May return a NULL pointer
42 void free(void *ptr); // May not accept a NULL pointer
44 Any function argument of the type you specify with the %apply directive
45 will be checked with the appropriate constraint. Multiple types may
46 be specified as follows :
48 %apply Pointer NONNULL { void *, Vector *, List *, double *};
50 In this case, all of the types listed would be checked for non-NULL
53 The common datatypes of int, short, long, unsigned int, unsigned long,
54 unsigned short, unsigned char, signed char, float, and double can be
55 checked without using the %apply directive by simply using the
56 constraint name as the parameter name. For example :
58 double sqrt(double NONNEGATIVE);
59 double log(double POSITIVE);
61 If you have used typedef to change type-names, you can also do this :
63 %apply double { Real }; // Make everything defined for doubles
65 Real sqrt(Real NONNEGATIVE);
66 Real log(Real POSITIVE);
71 %include <exception.i>
74 // Required attribute for C# exception handling
75 #define SWIGCSHARPCANTHROW , canthrow=1
77 #define SWIGCSHARPCANTHROW
83 %typemap(check SWIGCSHARPCANTHROW)
87 unsigned int POSITIVE,
88 unsigned short POSITIVE,
89 unsigned long POSITIVE,
91 unsigned char POSITIVE,
97 SWIG_exception(SWIG_ValueError,"Expected a positive value.");
103 %typemap(check SWIGCSHARPCANTHROW)
107 unsigned int NEGATIVE,
108 unsigned short NEGATIVE,
109 unsigned long NEGATIVE,
110 signed char NEGATIVE,
111 unsigned char NEGATIVE,
117 SWIG_exception(SWIG_ValueError,"Expected a negative value.");
123 %typemap(check SWIGCSHARPCANTHROW)
127 unsigned int NONZERO,
128 unsigned short NONZERO,
129 unsigned long NONZERO,
131 unsigned char NONZERO,
137 SWIG_exception(SWIG_ValueError,"Expected a nonzero value.");
141 // Nonnegative numbers
143 %typemap(check SWIGCSHARPCANTHROW)
147 unsigned int NONNEGATIVE,
148 unsigned short NONNEGATIVE,
149 unsigned long NONNEGATIVE,
150 signed char NONNEGATIVE,
151 unsigned char NONNEGATIVE,
157 SWIG_exception(SWIG_ValueError,"Expected a non-negative value.");
161 // Nonpositive numbers
163 %typemap(check SWIGCSHARPCANTHROW)
167 unsigned int NONPOSITIVE,
168 unsigned short NONPOSITIVE,
169 unsigned long NONPOSITIVE,
170 signed char NONPOSITIVE,
171 unsigned char NONPOSITIVE,
177 SWIG_exception(SWIG_ValueError,"Expected a non-positive value.");
183 %typemap(check SWIGCSHARPCANTHROW)
188 SWIG_exception(SWIG_ValueError,"Received a NULL pointer.");
194 %typemap(check SWIGCSHARPCANTHROW)
198 unsigned long long tmp;
199 tmp = (unsigned long long) $1;
201 SWIG_exception(SWIG_ValueError,"Pointer must be 8-byte aligned.");
205 %typemap(check SWIGCSHARPCANTHROW)
209 unsigned long long tmp;
210 tmp = (unsigned long long) $1;
212 SWIG_exception(SWIG_ValueError,"Pointer must be 4-byte aligned.");
216 %typemap(check SWIGCSHARPCANTHROW)
220 unsigned long long tmp;
221 tmp = (unsigned long long) $1;
223 SWIG_exception(SWIG_ValueError,"Pointer must be 2-byte aligned.");