PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-25/SWIG/Lib/php4/std_vector.i

#
Swig | 158 lines | 141 code | 17 blank | 0 comment | 0 complexity | ae5c217a0b98f8ba23d2512d11ea7271 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. //
  2. // SWIG typemaps for std::vector types
  3. // Luigi Ballabio
  4. // May 7, 2002
  5. //
  6. // PHP implementation
  7. %include exception.i
  8. // containers
  9. // methods which can raise are caused to throw an IndexError
  10. %exception std::vector::get {
  11. try {
  12. $action
  13. } catch (std::out_of_range& e) {
  14. SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
  15. }
  16. }
  17. %exception std::vector::set {
  18. try {
  19. $action
  20. } catch (std::out_of_range& e) {
  21. SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
  22. }
  23. }
  24. %exception std::vector::pop {
  25. try {
  26. $action
  27. } catch (std::out_of_range& e) {
  28. SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
  29. }
  30. }
  31. // ------------------------------------------------------------------------
  32. // std::vector
  33. //
  34. // The aim of all that follows would be to integrate std::vector with
  35. // PHP as much as possible, namely, to allow the user to pass and
  36. // be returned PHP lists.
  37. // const declarations are used to guess the intent of the function being
  38. // exported; therefore, the following rationale is applied:
  39. //
  40. // -- f(std::vector<T>), f(const std::vector<T>&), f(const std::vector<T>*):
  41. // the parameter being read-only, either a PHP sequence or a
  42. // previously wrapped std::vector<T> can be passed.
  43. // -- f(std::vector<T>&), f(std::vector<T>*):
  44. // the parameter must be modified; therefore, only a wrapped std::vector
  45. // can be passed.
  46. // -- std::vector<T> f():
  47. // the vector is returned by copy; therefore, a PHP sequence of T:s
  48. // is returned which is most easily used in other PHP functions
  49. // -- std::vector<T>& f(), std::vector<T>* f(), const std::vector<T>& f(),
  50. // const std::vector<T>* f():
  51. // the vector is returned by reference; therefore, a wrapped std::vector
  52. // is returned
  53. // ------------------------------------------------------------------------
  54. %{
  55. #include <vector>
  56. #include <algorithm>
  57. #include <stdexcept>
  58. %}
  59. // exported class
  60. namespace std {
  61. template<class T> class vector {
  62. // add generic typemaps here
  63. public:
  64. vector(unsigned int size = 0);
  65. unsigned int size() const;
  66. bool empty() const;
  67. void clear();
  68. %rename(push) push_back;
  69. void push_back(const T& x);
  70. %extend {
  71. T pop() {
  72. if (self->size() == 0)
  73. throw std::out_of_range("pop from empty vector");
  74. T x = self->back();
  75. self->pop_back();
  76. return x;
  77. }
  78. T& get(int i) {
  79. int size = int(self->size());
  80. if (i>=0 && i<size)
  81. return (*self)[i];
  82. else
  83. throw std::out_of_range("vector index out of range");
  84. }
  85. void set(int i, const T& x) {
  86. int size = int(self->size());
  87. if (i>=0 && i<size)
  88. (*self)[i] = x;
  89. else
  90. throw std::out_of_range("vector index out of range");
  91. }
  92. }
  93. };
  94. // specializations for built-ins
  95. %define specialize_std_vector(T)
  96. template<> class vector<T> {
  97. // add specialized typemaps here
  98. public:
  99. vector(unsigned int size = 0);
  100. unsigned int size() const;
  101. bool empty() const;
  102. void clear();
  103. %rename(push) push_back;
  104. void push_back(T x);
  105. %extend {
  106. T pop() {
  107. if (self->size() == 0)
  108. throw std::out_of_range("pop from empty vector");
  109. T x = self->back();
  110. self->pop_back();
  111. return x;
  112. }
  113. T get(int i) {
  114. int size = int(self->size());
  115. if (i>=0 && i<size)
  116. return (*self)[i];
  117. else
  118. throw std::out_of_range("vector index out of range");
  119. }
  120. void set(int i, T x) {
  121. int size = int(self->size());
  122. if (i>=0 && i<size)
  123. (*self)[i] = x;
  124. else
  125. throw std::out_of_range("vector index out of range");
  126. }
  127. }
  128. };
  129. %enddef
  130. specialize_std_vector(bool);
  131. specialize_std_vector(char);
  132. specialize_std_vector(int);
  133. specialize_std_vector(short);
  134. specialize_std_vector(long);
  135. specialize_std_vector(unsigned char);
  136. specialize_std_vector(unsigned int);
  137. specialize_std_vector(unsigned short);
  138. specialize_std_vector(unsigned long);
  139. specialize_std_vector(float);
  140. specialize_std_vector(double);
  141. }