Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
7.0 KiB

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