PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/constraints.i

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