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