]>
Commit | Line | Data |
---|---|---|
c90f71dd RD |
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 | %section "Constraint Library",info,after,pre,nosort,skip=1,chop_left=3,chop_right=0,chop_top=0,chop_bottom=0 | |
12 | ||
13 | %text %{ | |
14 | %include constraints.i | |
15 | ||
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 : | |
20 | ||
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 | |
30 | ||
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 : | |
34 | ||
35 | %apply Number NONNEGATIVE { double nonneg }; | |
36 | double sqrt(double nonneg); // Name of argument must match | |
37 | ||
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 | |
41 | ||
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 : | |
45 | ||
46 | %apply Pointer NONNULL { void *, Vector *, List *, double *}; | |
47 | ||
48 | In this case, all of the types listed would be checked for non-NULL | |
49 | pointers. | |
50 | ||
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 : | |
55 | ||
56 | double sqrt(double NONNEGATIVE); | |
57 | double log(double POSITIVE); | |
58 | ||
59 | If you have used typedef to change type-names, you can also do this : | |
60 | ||
61 | %apply double { Real }; // Make everything defined for doubles | |
62 | // work for Reals. | |
63 | Real sqrt(Real NONNEGATIVE); | |
64 | Real log(Real POSITIVE); | |
65 | ||
66 | %} | |
67 | #endif | |
68 | ||
69 | %include exception.i | |
70 | ||
71 | // Positive numbers | |
72 | ||
73 | %typemap(check) int POSITIVE, | |
74 | short POSITIVE, | |
75 | long POSITIVE, | |
76 | unsigned int POSITIVE, | |
77 | unsigned short POSITIVE, | |
78 | unsigned long POSITIVE, | |
79 | signed char POSITIVE, | |
80 | unsigned char POSITIVE, | |
81 | float POSITIVE, | |
82 | double POSITIVE, | |
83 | Number POSITIVE | |
84 | { | |
85 | if ($target <= 0) { | |
86 | SWIG_exception(SWIG_ValueError,"Expected a positive value."); | |
87 | } | |
88 | } | |
89 | ||
90 | // Negative numbers | |
91 | ||
92 | %typemap(check) int NEGATIVE, | |
93 | short NEGATIVE, | |
94 | long NEGATIVE, | |
95 | unsigned int NEGATIVE, | |
96 | unsigned short NEGATIVE, | |
97 | unsigned long NEGATIVE, | |
98 | signed char NEGATIVE, | |
99 | unsigned char NEGATIVE, | |
100 | float NEGATIVE, | |
101 | double NEGATIVE, | |
102 | Number NEGATIVE | |
103 | { | |
104 | if ($target >= 0) { | |
105 | SWIG_exception(SWIG_ValueError,"Expected a negative value."); | |
106 | } | |
107 | } | |
108 | ||
109 | // Nonzero numbers | |
110 | ||
111 | %typemap(check) int NONZERO, | |
112 | short NONZERO, | |
113 | long NONZERO, | |
114 | unsigned int NONZERO, | |
115 | unsigned short NONZERO, | |
116 | unsigned long NONZERO, | |
117 | signed char NONZERO, | |
118 | unsigned char NONZERO, | |
119 | float NONZERO, | |
120 | double NONZERO, | |
121 | Number NONZERO | |
122 | { | |
123 | if ($target == 0) { | |
124 | SWIG_exception(SWIG_ValueError,"Expected a nonzero value."); | |
125 | } | |
126 | } | |
127 | ||
128 | // Nonnegative numbers | |
129 | ||
130 | %typemap(check) int NONNEGATIVE, | |
131 | short NONNEGATIVE, | |
132 | long NONNEGATIVE, | |
133 | unsigned int NONNEGATIVE, | |
134 | unsigned short NONNEGATIVE, | |
135 | unsigned long NONNEGATIVE, | |
136 | signed char NONNEGATIVE, | |
137 | unsigned char NONNEGATIVE, | |
138 | float NONNEGATIVE, | |
139 | double NONNEGATIVE, | |
140 | Number NONNEGATIVE | |
141 | { | |
142 | if ($target < 0) { | |
143 | SWIG_exception(SWIG_ValueError,"Expected a non-negative value."); | |
144 | } | |
145 | } | |
146 | ||
147 | // Nonpositive numbers | |
148 | ||
149 | %typemap(check) int NONPOSITIVE, | |
150 | short NONPOSITIVE, | |
151 | long NONPOSITIVE, | |
152 | unsigned int NONPOSITIVE, | |
153 | unsigned short NONPOSITIVE, | |
154 | unsigned long NONPOSITIVE, | |
155 | signed char NONPOSITIVE, | |
156 | unsigned char NONPOSITIVE, | |
157 | float NONPOSITIVE, | |
158 | double NONPOSITIVE, | |
159 | Number NONPOSITIVE | |
160 | { | |
161 | if ($target < 0) { | |
162 | SWIG_exception(SWIG_ValueError,"Expected a non-positive value."); | |
163 | } | |
164 | } | |
165 | ||
166 | // Non-NULL pointer | |
167 | ||
168 | %typemap(check) void * NONNULL, | |
169 | Pointer NONNULL | |
170 | { | |
171 | if (!$target) { | |
172 | SWIG_exception(SWIG_ValueError,"Received a NULL pointer."); | |
173 | } | |
174 | } | |
175 | ||
176 | // Aligned pointers | |
177 | ||
178 | %typemap(check) void * ALIGN8, | |
179 | Pointer ALIGN8 | |
180 | { | |
181 | long tmp; | |
182 | tmp = (long) $target; | |
183 | if (tmp & 7) { | |
184 | SWIG_exception(SWIG_ValueError,"Pointer must be 8-byte aligned."); | |
185 | } | |
186 | } | |
187 | ||
188 | %typemap(check) void * ALIGN4, | |
189 | Pointer ALIGN4 | |
190 | { | |
191 | long tmp; | |
192 | tmp = (long) $target; | |
193 | if (tmp & 3) { | |
194 | SWIG_exception(SWIG_ValueError,"Pointer must be 4-byte aligned."); | |
195 | } | |
196 | } | |
197 | ||
198 | %typemap(check) void * ALIGN2, | |
199 | Pointer ALIGN2 | |
200 | { | |
201 | long tmp; | |
202 | tmp = (long) $target; | |
203 | if (tmp & 1) { | |
204 | SWIG_exception(SWIG_ValueError,"Pointer must be 2-byte aligned."); | |
205 | } | |
206 | } | |
207 | ||
208 |