Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | // |
2 | // SWIG typemaps for std::vector | |
3 | // Luigi Ballabio | |
4 | // May 7, 2002 | |
5 | // | |
6 | // Java implementation | |
7 | ||
8 | %include <std_common.i> | |
9 | ||
10 | // ------------------------------------------------------------------------ | |
11 | // std::vector | |
12 | // | |
13 | // The aim of all that follows would be to integrate std::vector with | |
14 | // Java as much as possible, namely, to allow the user to pass and | |
15 | // be returned Java (arrays? containers?) | |
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 Java 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 Java sequence of T:s | |
27 | // is returned which is most easily used in other Java 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 | %{ | |
35 | #include <vector> | |
36 | #include <algorithm> | |
37 | #include <stdexcept> | |
38 | %} | |
39 | ||
40 | // exported class | |
41 | ||
42 | namespace std { | |
43 | ||
44 | template<class T> class vector { | |
45 | // add generic typemaps here | |
46 | public: | |
47 | vector(); | |
48 | vector(unsigned int); | |
49 | unsigned int size() const; | |
50 | %rename(isEmpty) empty; | |
51 | bool empty() const; | |
52 | void clear(); | |
53 | %rename(add) push_back; | |
54 | void push_back(const T& x); | |
55 | %extend { | |
56 | T& get(int i) throw (std::out_of_range) { | |
57 | int size = int(self->size()); | |
58 | if (i>=0 && i<size) | |
59 | return (*self)[i]; | |
60 | else | |
61 | throw std::out_of_range("vector index out of range"); | |
62 | } | |
63 | void set(int i, const T& x) throw (std::out_of_range) { | |
64 | int size = int(self->size()); | |
65 | if (i>=0 && i<size) | |
66 | (*self)[i] = x; | |
67 | else | |
68 | throw std::out_of_range("vector index out of range"); | |
69 | } | |
70 | } | |
71 | }; | |
72 | ||
73 | ||
74 | // specializations for built-ins | |
75 | ||
76 | %define specialize_std_vector(T) | |
77 | template<> class vector<T> { | |
78 | // add specialized typemaps here | |
79 | public: | |
80 | vector(); | |
81 | vector(unsigned int); | |
82 | unsigned int size() const; | |
83 | %rename(isEmpty) empty; | |
84 | bool empty() const; | |
85 | void clear(); | |
86 | %rename(add) push_back; | |
87 | void push_back(T x); | |
88 | %extend { | |
89 | T get(int i) throw (std::out_of_range) { | |
90 | int size = int(self->size()); | |
91 | if (i>=0 && i<size) | |
92 | return (*self)[i]; | |
93 | else | |
94 | throw std::out_of_range("vector index out of range"); | |
95 | } | |
96 | void set(int i, T x) throw (std::out_of_range) { | |
97 | int size = int(self->size()); | |
98 | if (i>=0 && i<size) | |
99 | (*self)[i] = x; | |
100 | else | |
101 | throw std::out_of_range("vector index out of range"); | |
102 | } | |
103 | } | |
104 | }; | |
105 | %enddef | |
106 | ||
107 | specialize_std_vector(bool); | |
108 | specialize_std_vector(char); | |
109 | specialize_std_vector(int); | |
110 | specialize_std_vector(short); | |
111 | specialize_std_vector(long); | |
112 | specialize_std_vector(unsigned char); | |
113 | specialize_std_vector(unsigned int); | |
114 | specialize_std_vector(unsigned short); | |
115 | specialize_std_vector(unsigned long); | |
116 | specialize_std_vector(float); | |
117 | specialize_std_vector(double); | |
118 | specialize_std_vector(std::string); | |
119 | ||
120 | } | |
121 |