PageRenderTime 18ms CodeModel.GetById 13ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/java/std_vector.i

#
Swig | 85 lines | 75 code | 7 blank | 3 comment | 0 complexity | f9f5bfb0d42fef712631b33f3623ca95 MD5 | raw file
 1/* -----------------------------------------------------------------------------
 2 * std_vector.i
 3 * ----------------------------------------------------------------------------- */
 4
 5%include <std_common.i>
 6
 7%{
 8#include <vector>
 9#include <stdexcept>
10%}
11
12namespace std {
13    
14    template<class T> class vector {
15      public:
16        typedef size_t size_type;
17        typedef T value_type;
18        typedef const value_type& const_reference;
19        vector();
20        vector(size_type n);
21        size_type size() const;
22        size_type capacity() const;
23        void reserve(size_type n);
24        %rename(isEmpty) empty;
25        bool empty() const;
26        void clear();
27        %rename(add) push_back;
28        void push_back(const value_type& x);
29        %extend {
30            const_reference get(int i) throw (std::out_of_range) {
31                int size = int(self->size());
32                if (i>=0 && i<size)
33                    return (*self)[i];
34                else
35                    throw std::out_of_range("vector index out of range");
36            }
37            void set(int i, const value_type& val) throw (std::out_of_range) {
38                int size = int(self->size());
39                if (i>=0 && i<size)
40                    (*self)[i] = val;
41                else
42                    throw std::out_of_range("vector index out of range");
43            }
44        }
45    };
46
47    // bool specialization
48    template<> class vector<bool> {
49      public:
50        typedef size_t size_type;
51        typedef bool value_type;
52        typedef bool const_reference;
53        vector();
54        vector(size_type n);
55        size_type size() const;
56        size_type capacity() const;
57        void reserve(size_type n);
58        %rename(isEmpty) empty;
59        bool empty() const;
60        void clear();
61        %rename(add) push_back;
62        void push_back(const value_type& x);
63        %extend {
64            const_reference get(int i) throw (std::out_of_range) {
65                int size = int(self->size());
66                if (i>=0 && i<size)
67                    return (*self)[i];
68                else
69                    throw std::out_of_range("vector index out of range");
70            }
71            void set(int i, const value_type& val) throw (std::out_of_range) {
72                int size = int(self->size());
73                if (i>=0 && i<size)
74                    (*self)[i] = val;
75                else
76                    throw std::out_of_range("vector index out of range");
77            }
78        }
79    };
80}
81
82%define specialize_std_vector(T)
83#warning "specialize_std_vector - specialization for type T no longer needed"
84%enddef
85