| 1 | // -*- C++ -*- |
| 2 | // SWIG typemaps for std::vector types |
| 3 | // Art Yerkes |
| 4 | // Modified from: Luigi Ballabio |
| 5 | // Apr 8, 2002 |
| 6 | // |
| 7 | // Ocaml implementation |
| 8 | |
| 9 | %include std_common.i |
| 10 | |
| 11 | // ------------------------------------------------------------------------ |
| 12 | // std::vector |
| 13 | // |
| 14 | // The aim of all that follows would be to integrate std::vector with |
| 15 | // Python as much as possible, namely, to allow the user to pass and |
| 16 | // be returned Python tuples or lists. |
| 17 | // const declarations are used to guess the intent of the function being |
| 18 | // exported; therefore, the following rationale is applied: |
| 19 | // |
| 20 | // -- f(std::vector<T>), f(const std::vector<T>&), f(const std::vector<T>*): |
| 21 | // the parameter being read-only, either a Python sequence or a |
| 22 | // previously wrapped std::vector<T> can be passed. |
| 23 | // -- f(std::vector<T>&), f(std::vector<T>*): |
| 24 | // the parameter must be modified; therefore, only a wrapped std::vector |
| 25 | // can be passed. |
| 26 | // -- std::vector<T> f(): |
| 27 | // the vector is returned by copy; therefore, a Python sequence of T:s |
| 28 | // is returned which is most easily used in other Python functions |
| 29 | // -- std::vector<T>& f(), std::vector<T>* f(), const std::vector<T>& f(), |
| 30 | // const std::vector<T>* f(): |
| 31 | // the vector is returned by reference; therefore, a wrapped std::vector |
| 32 | // is returned |
| 33 | // ------------------------------------------------------------------------ |
| 34 | |
| 35 | %{ |
| 36 | #include <vector> |
| 37 | #include <algorithm> |
| 38 | #include <stdexcept> |
| 39 | %} |
| 40 | |
| 41 | // exported class |
| 42 | |
| 43 | namespace std { |
| 44 | template <class T> class vector { |
| 45 | public: |
| 46 | vector(unsigned int size = 0); |
| 47 | vector(unsigned int size, const T& value); |
| 48 | vector(const vector<T>&); |
| 49 | unsigned int size() const; |
| 50 | bool empty() const; |
| 51 | void clear(); |
| 52 | void push_back(const T& x); |
| 53 | T operator [] ( int f ); |
| 54 | vector <T> &operator = ( vector <T> &other ); |
| 55 | %extend { |
| 56 | void set( int i, const T &x ) { |
| 57 | self->resize(i+1); |
| 58 | (*self)[i] = x; |
| 59 | } |
| 60 | }; |
| 61 | %extend { |
| 62 | T *to_array() { |
| 63 | T *array = new T[self->size() + 1]; |
| 64 | for( int i = 0; i < self->size(); i++ ) |
| 65 | array[i] = (*self)[i]; |
| 66 | return array; |
| 67 | } |
| 68 | }; |
| 69 | }; |
| 70 | }; |
| 71 | |
| 72 | %insert(ml) %{ |
| 73 | |
| 74 | let array_to_vector v argcons array = |
| 75 | for i = 0 to (Array.length array) - 1 do |
| 76 | (invoke v) "set" (C_list [ C_int i ; (argcons array.(i)) ]) |
| 77 | done ; |
| 78 | v |
| 79 | |
| 80 | let vector_to_array v argcons array = |
| 81 | for i = 0; to (get_int ((invoke v) "size" C_void)) - 1 do |
| 82 | array.(i) <- argcons ((invoke v) "[]" (C_int i)) |
| 83 | done ; |
| 84 | v |
| 85 | |
| 86 | %} |
| 87 | |
| 88 | %insert(mli) %{ |
| 89 | val array_to_vector : c_obj -> ('a -> c_obj) -> 'a array -> c_obj |
| 90 | val vector_to_array : c_obj -> (c_obj -> 'a) -> 'a array -> c_obj |
| 91 | %} |