Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | // |
2 | // SWIG typemaps for std::vector types | |
3 | // Luigi Ballabio | |
4 | // May 7, 2002 | |
5 | // | |
6 | // PHP 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 | // 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 | %{ | |
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(unsigned int size = 0); | |
48 | unsigned int size() const; | |
49 | bool empty() const; | |
50 | void clear(); | |
51 | %rename(push) push_back; | |
52 | void push_back(const T& x); | |
53 | %extend { | |
54 | T pop() throw (std::out_of_range) { | |
55 | if (self->size() == 0) | |
56 | throw std::out_of_range("pop from empty vector"); | |
57 | T x = self->back(); | |
58 | self->pop_back(); | |
59 | return x; | |
60 | } | |
61 | T& get(int i) throw (std::out_of_range) { | |
62 | int size = int(self->size()); | |
63 | if (i>=0 && i<size) | |
64 | return (*self)[i]; | |
65 | else | |
66 | throw std::out_of_range("vector index out of range"); | |
67 | } | |
68 | void set(int i, const T& x) throw (std::out_of_range) { | |
69 | int size = int(self->size()); | |
70 | if (i>=0 && i<size) | |
71 | (*self)[i] = x; | |
72 | else | |
73 | throw std::out_of_range("vector index out of range"); | |
74 | } | |
75 | } | |
76 | }; | |
77 | ||
78 | ||
79 | // specializations for built-ins | |
80 | ||
81 | %define specialize_std_vector(T) | |
82 | template<> class vector<T> { | |
83 | // add specialized typemaps here | |
84 | public: | |
85 | vector(unsigned int size = 0); | |
86 | unsigned int size() const; | |
87 | bool empty() const; | |
88 | void clear(); | |
89 | %rename(push) push_back; | |
90 | void push_back(T x); | |
91 | %extend { | |
92 | T pop() throw (std::out_of_range) { | |
93 | if (self->size() == 0) | |
94 | throw std::out_of_range("pop from empty vector"); | |
95 | T x = self->back(); | |
96 | self->pop_back(); | |
97 | return x; | |
98 | } | |
99 | T get(int i) throw (std::out_of_range) { | |
100 | int size = int(self->size()); | |
101 | if (i>=0 && i<size) | |
102 | return (*self)[i]; | |
103 | else | |
104 | throw std::out_of_range("vector index out of range"); | |
105 | } | |
106 | void set(int i, T x) throw (std::out_of_range) { | |
107 | int size = int(self->size()); | |
108 | if (i>=0 && i<size) | |
109 | (*self)[i] = x; | |
110 | else | |
111 | throw std::out_of_range("vector index out of range"); | |
112 | } | |
113 | } | |
114 | }; | |
115 | %enddef | |
116 | ||
117 | specialize_std_vector(bool); | |
118 | specialize_std_vector(char); | |
119 | specialize_std_vector(int); | |
120 | specialize_std_vector(short); | |
121 | specialize_std_vector(long); | |
122 | specialize_std_vector(unsigned char); | |
123 | specialize_std_vector(unsigned int); | |
124 | specialize_std_vector(unsigned short); | |
125 | specialize_std_vector(unsigned long); | |
126 | specialize_std_vector(float); | |
127 | specialize_std_vector(double); | |
128 | ||
129 | } | |
130 |