PageRenderTime 41ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Swig | 142 lines | 125 code | 17 blank | 0 comment | 0 complexity | 9d9ac1f98d3db4ba5f9849a7521e06d0 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. //
  2. // SWIG typemaps for std::vector
  3. // Luigi Ballabio
  4. // May 7, 2002
  5. //
  6. // Java 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. // ------------------------------------------------------------------------
  25. // std::vector
  26. //
  27. // The aim of all that follows would be to integrate std::vector with
  28. // Java as much as possible, namely, to allow the user to pass and
  29. // be returned Java (arrays? containers?)
  30. // const declarations are used to guess the intent of the function being
  31. // exported; therefore, the following rationale is applied:
  32. //
  33. // -- f(std::vector<T>), f(const std::vector<T>&), f(const std::vector<T>*):
  34. // the parameter being read-only, either a Java sequence or a
  35. // previously wrapped std::vector<T> can be passed.
  36. // -- f(std::vector<T>&), f(std::vector<T>*):
  37. // the parameter must be modified; therefore, only a wrapped std::vector
  38. // can be passed.
  39. // -- std::vector<T> f():
  40. // the vector is returned by copy; therefore, a Java sequence of T:s
  41. // is returned which is most easily used in other Java functions
  42. // -- std::vector<T>& f(), std::vector<T>* f(), const std::vector<T>& f(),
  43. // const std::vector<T>* f():
  44. // the vector is returned by reference; therefore, a wrapped std::vector
  45. // is returned
  46. // ------------------------------------------------------------------------
  47. %{
  48. #include <vector>
  49. #include <algorithm>
  50. #include <stdexcept>
  51. %}
  52. // exported class
  53. namespace std {
  54. template<class T> class vector {
  55. // add generic typemaps here
  56. public:
  57. vector();
  58. vector(unsigned int);
  59. unsigned int size() const;
  60. %rename(isEmpty) empty;
  61. bool empty() const;
  62. void clear();
  63. %rename(add) push_back;
  64. void push_back(const T& x);
  65. %extend {
  66. T& get(int i) {
  67. int size = int(self->size());
  68. if (i>=0 && i<size)
  69. return (*self)[i];
  70. else
  71. throw std::out_of_range("vector index out of range");
  72. }
  73. void set(int i, const T& x) {
  74. int size = int(self->size());
  75. if (i>=0 && i<size)
  76. (*self)[i] = x;
  77. else
  78. throw std::out_of_range("vector index out of range");
  79. }
  80. }
  81. };
  82. // specializations for built-ins
  83. %define specialize_std_vector(T)
  84. template<> class vector<T> {
  85. // add specialized typemaps here
  86. public:
  87. vector();
  88. vector(unsigned int);
  89. unsigned int size() const;
  90. %rename(isEmpty) empty;
  91. bool empty() const;
  92. void clear();
  93. %rename(add) push_back;
  94. void push_back(T x);
  95. %extend {
  96. T get(int i) {
  97. int size = int(self->size());
  98. if (i>=0 && i<size)
  99. return (*self)[i];
  100. else
  101. throw std::out_of_range("vector index out of range");
  102. }
  103. void set(int i, T x) {
  104. int size = int(self->size());
  105. if (i>=0 && i<size)
  106. (*self)[i] = x;
  107. else
  108. throw std::out_of_range("vector index out of range");
  109. }
  110. }
  111. };
  112. %enddef
  113. specialize_std_vector(bool);
  114. specialize_std_vector(char);
  115. specialize_std_vector(int);
  116. specialize_std_vector(short);
  117. specialize_std_vector(long);
  118. specialize_std_vector(unsigned char);
  119. specialize_std_vector(unsigned int);
  120. specialize_std_vector(unsigned short);
  121. specialize_std_vector(unsigned long);
  122. specialize_std_vector(float);
  123. specialize_std_vector(double);
  124. specialize_std_vector(std::string);
  125. }