PageRenderTime 84ms CodeModel.GetById 35ms app.highlight 10ms RepoModel.GetById 30ms app.codeStats 0ms

/tags/rel-1-3-29/SWIG/Lib/perl5/std_map.i

#
Swig | 175 lines | 149 code | 18 blank | 8 comment | 0 complexity | bf3d67a7b52cf9413be17123f6d2c652 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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_map.i
  6 *
  7 * SWIG typemaps for std::map
  8 * ----------------------------------------------------------------------------- */
  9
 10%include <std_common.i>
 11
 12// ------------------------------------------------------------------------
 13// std::map
 14// ------------------------------------------------------------------------
 15
 16%{
 17#include <map>
 18#include <algorithm>
 19#include <stdexcept>
 20%}
 21
 22// exported class
 23
 24namespace std {
 25
 26    template<class K, class T> class map {
 27        // add typemaps here
 28      public:
 29        map();
 30        map(const map<K,T> &);
 31        
 32        unsigned int size() const;
 33        bool empty() const;
 34        void clear();
 35        %extend {
 36            T& get(const K& key) throw (std::out_of_range) {
 37                std::map<K,T >::iterator i = self->find(key);
 38                if (i != self->end())
 39                    return i->second;
 40                else
 41                    throw std::out_of_range("key not found");
 42            }
 43            void set(const K& key, const T& x) {
 44                (*self)[key] = x;
 45            }
 46            void del(const K& key) throw (std::out_of_range) {
 47                std::map<K,T >::iterator i = self->find(key);
 48                if (i != self->end())
 49                    self->erase(i);
 50                else
 51                    throw std::out_of_range("key not found");
 52            }
 53            bool has_key(const K& key) {
 54                std::map<K,T >::iterator i = self->find(key);
 55                return i != self->end();
 56            }
 57        }
 58    };
 59
 60
 61    // specializations for built-ins
 62
 63    %define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO)
 64
 65    template<class T> class map<K,T> {
 66        // add typemaps here
 67      public:
 68        map();
 69        map(const map<K,T> &);
 70        
 71        unsigned int size() const;
 72        bool empty() const;
 73        void clear();
 74        %extend {
 75            T& get(K key) throw (std::out_of_range) {
 76                std::map<K,T >::iterator i = self->find(key);
 77                if (i != self->end())
 78                    return i->second;
 79                else
 80                    throw std::out_of_range("key not found");
 81            }
 82            void set(K key, const T& x) {
 83                (*self)[key] = x;
 84            }
 85            void del(K key) throw (std::out_of_range) {
 86                std::map<K,T >::iterator i = self->find(key);
 87                if (i != self->end())
 88                    self->erase(i);
 89                else
 90                    throw std::out_of_range("key not found");
 91            }
 92            bool has_key(K key) {
 93                std::map<K,T >::iterator i = self->find(key);
 94                return i != self->end();
 95            }
 96        }
 97    };
 98    %enddef
 99
100    %define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO)
101    template<class K> class map<K,T> {
102        // add typemaps here
103      public:
104        map();
105        map(const map<K,T> &);
106        
107        unsigned int size() const;
108        bool empty() const;
109        void clear();
110        %extend {
111            T get(const K& key) throw (std::out_of_range) {
112                std::map<K,T >::iterator i = self->find(key);
113                if (i != self->end())
114                    return i->second;
115                else
116                    throw std::out_of_range("key not found");
117            }
118            void set(const K& key, T x) {
119                (*self)[key] = x;
120            }
121            void del(const K& key) throw (std::out_of_range) {
122                std::map<K,T >::iterator i = self->find(key);
123                if (i != self->end())
124                    self->erase(i);
125                else
126                    throw std::out_of_range("key not found");
127            }
128            bool has_key(const K& key) {
129                std::map<K,T >::iterator i = self->find(key);
130                return i != self->end();
131            }
132        }
133    };
134    %enddef
135
136    %define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO,
137                                       T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO)
138    template<> class map<K,T> {
139        // add typemaps here
140      public:
141        map();
142        map(const map<K,T> &);
143        
144        unsigned int size() const;
145        bool empty() const;
146        void clear();
147        %extend {
148            T get(K key) throw (std::out_of_range) {
149                std::map<K,T >::iterator i = self->find(key);
150                if (i != self->end())
151                    return i->second;
152                else
153                    throw std::out_of_range("key not found");
154            }
155            void set(K key, T x) {
156                (*self)[key] = x;
157            }
158            void del(K key) throw (std::out_of_range) {
159                std::map<K,T >::iterator i = self->find(key);
160                if (i != self->end())
161                    self->erase(i);
162                else
163                    throw std::out_of_range("key not found");
164            }
165            bool has_key(K key) {
166                std::map<K,T >::iterator i = self->find(key);
167                return i != self->end();
168            }
169        }
170    };
171    %enddef
172
173    // add specializations here
174
175}