PageRenderTime 23ms CodeModel.GetById 20ms app.highlight 1ms 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
  8
  9%include exception.i
 10
 11// containers
 12
 13// methods which can raise are caused to throw an IndexError
 14%exception std::vector::get {
 15    try {
 16        $action
 17    } catch (std::out_of_range& e) {
 18        SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
 19    }
 20}
 21
 22%exception std::vector::set {
 23    try {
 24        $action
 25    } catch (std::out_of_range& e) {
 26        SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
 27    }
 28}
 29
 30
 31// ------------------------------------------------------------------------
 32// std::vector
 33// 
 34// The aim of all that follows would be to integrate std::vector with 
 35// Java as much as possible, namely, to allow the user to pass and 
 36// be returned Java (arrays? containers?)
 37// const declarations are used to guess the intent of the function being
 38// exported; therefore, the following rationale is applied:
 39// 
 40//   -- f(std::vector<T>), f(const std::vector<T>&), f(const std::vector<T>*):
 41//      the parameter being read-only, either a Java sequence or a
 42//      previously wrapped std::vector<T> can be passed.
 43//   -- f(std::vector<T>&), f(std::vector<T>*):
 44//      the parameter must be modified; therefore, only a wrapped std::vector
 45//      can be passed.
 46//   -- std::vector<T> f():
 47//      the vector is returned by copy; therefore, a Java sequence of T:s 
 48//      is returned which is most easily used in other Java functions
 49//   -- std::vector<T>& f(), std::vector<T>* f(), const std::vector<T>& f(),
 50//      const std::vector<T>* f():
 51//      the vector is returned by reference; therefore, a wrapped std::vector
 52//      is returned
 53// ------------------------------------------------------------------------
 54
 55%{
 56#include <vector>
 57#include <algorithm>
 58#include <stdexcept>
 59%}
 60
 61// exported class
 62
 63namespace std {
 64    
 65    template<class T> class vector {
 66        // add generic typemaps here
 67      public:
 68        vector();
 69        vector(unsigned int);
 70        unsigned int size() const;
 71        %rename(isEmpty) empty;
 72        bool empty() const;
 73        void clear();
 74        %rename(add) push_back;
 75        void push_back(const T& x);
 76        %extend {
 77            T& get(int i) {
 78                int size = int(self->size());
 79                if (i>=0 && i<size)
 80                    return (*self)[i];
 81                else
 82                    throw std::out_of_range("vector index out of range");
 83            }
 84            void set(int i, const T& x) {
 85                int size = int(self->size());
 86                if (i>=0 && i<size)
 87                    (*self)[i] = x;
 88                else
 89                    throw std::out_of_range("vector index out of range");
 90            }
 91        }
 92    };
 93
 94
 95    // specializations for built-ins
 96
 97    %define specialize_std_vector(T)
 98    template<> class vector<T> {
 99        // add specialized typemaps here
100      public:
101        vector();
102        vector(unsigned int);
103        unsigned int size() const;
104        %rename(isEmpty) empty;
105        bool empty() const;
106        void clear();
107        %rename(add) push_back;
108        void push_back(T x);
109        %extend {
110            T get(int i) {
111                int size = int(self->size());
112                if (i>=0 && i<size)
113                    return (*self)[i];
114                else
115                    throw std::out_of_range("vector index out of range");
116            }
117            void set(int i, T x) {
118                int size = int(self->size());
119                if (i>=0 && i<size)
120                    (*self)[i] = x;
121                else
122                    throw std::out_of_range("vector index out of range");
123            }
124        }
125    };
126    %enddef
127
128    specialize_std_vector(bool);
129    specialize_std_vector(char);
130    specialize_std_vector(int);
131    specialize_std_vector(short);
132    specialize_std_vector(long);
133    specialize_std_vector(unsigned char);
134    specialize_std_vector(unsigned int);
135    specialize_std_vector(unsigned short);
136    specialize_std_vector(unsigned long);
137    specialize_std_vector(float);
138    specialize_std_vector(double);
139    specialize_std_vector(std::string);
140
141}
142