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