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.

132 lines
4.4 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. * std_vector.i
  6. *
  7. * SWIG typemaps for std::vector types
  8. * ----------------------------------------------------------------------------- */
  9. %include <std_common.i>
  10. // ------------------------------------------------------------------------
  11. // std::vector
  12. //
  13. // The aim of all that follows would be to integrate std::vector with
  14. // PHP as much as possible, namely, to allow the user to pass and
  15. // be returned PHP lists.
  16. // const declarations are used to guess the intent of the function being
  17. // exported; therefore, the following rationale is applied:
  18. //
  19. // -- f(std::vector<T>), f(const std::vector<T>&), f(const std::vector<T>*):
  20. // the parameter being read-only, either a PHP sequence or a
  21. // previously wrapped std::vector<T> can be passed.
  22. // -- f(std::vector<T>&), f(std::vector<T>*):
  23. // the parameter must be modified; therefore, only a wrapped std::vector
  24. // can be passed.
  25. // -- std::vector<T> f():
  26. // the vector is returned by copy; therefore, a PHP sequence of T:s
  27. // is returned which is most easily used in other PHP functions
  28. // -- std::vector<T>& f(), std::vector<T>* f(), const std::vector<T>& f(),
  29. // const std::vector<T>* f():
  30. // the vector is returned by reference; therefore, a wrapped std::vector
  31. // is returned
  32. // ------------------------------------------------------------------------
  33. %{
  34. #include <vector>
  35. #include <algorithm>
  36. #include <stdexcept>
  37. %}
  38. // exported class
  39. namespace std {
  40. template<class T> class vector {
  41. // add generic typemaps here
  42. public:
  43. vector(unsigned int size = 0);
  44. unsigned int size() const;
  45. bool empty() const;
  46. void clear();
  47. %rename(push) push_back;
  48. void push_back(const T& x);
  49. %extend {
  50. T pop() throw (std::out_of_range) {
  51. if (self->size() == 0)
  52. throw std::out_of_range("pop from empty vector");
  53. T x = self->back();
  54. self->pop_back();
  55. return x;
  56. }
  57. T& get(int i) throw (std::out_of_range) {
  58. int size = int(self->size());
  59. if (i>=0 && i<size)
  60. return (*self)[i];
  61. else
  62. throw std::out_of_range("vector index out of range");
  63. }
  64. void set(int i, const T& x) throw (std::out_of_range) {
  65. int size = int(self->size());
  66. if (i>=0 && i<size)
  67. (*self)[i] = x;
  68. else
  69. throw std::out_of_range("vector index out of range");
  70. }
  71. }
  72. };
  73. // specializations for built-ins
  74. %define specialize_std_vector(T)
  75. template<> class vector<T> {
  76. // add specialized typemaps here
  77. public:
  78. vector(unsigned int size = 0);
  79. unsigned int size() const;
  80. bool empty() const;
  81. void clear();
  82. %rename(push) push_back;
  83. void push_back(T x);
  84. %extend {
  85. T pop() throw (std::out_of_range) {
  86. if (self->size() == 0)
  87. throw std::out_of_range("pop from empty vector");
  88. T x = self->back();
  89. self->pop_back();
  90. return x;
  91. }
  92. T get(int i) throw (std::out_of_range) {
  93. int size = int(self->size());
  94. if (i>=0 && i<size)
  95. return (*self)[i];
  96. else
  97. throw std::out_of_range("vector index out of range");
  98. }
  99. void set(int i, T x) throw (std::out_of_range) {
  100. int size = int(self->size());
  101. if (i>=0 && i<size)
  102. (*self)[i] = x;
  103. else
  104. throw std::out_of_range("vector index out of range");
  105. }
  106. }
  107. };
  108. %enddef
  109. specialize_std_vector(bool);
  110. specialize_std_vector(char);
  111. specialize_std_vector(int);
  112. specialize_std_vector(short);
  113. specialize_std_vector(long);
  114. specialize_std_vector(unsigned char);
  115. specialize_std_vector(unsigned int);
  116. specialize_std_vector(unsigned short);
  117. specialize_std_vector(unsigned long);
  118. specialize_std_vector(float);
  119. specialize_std_vector(double);
  120. }