| 1 | // Warning: Use the typemaps here in the expectation that the macros they are in will change name. |
| 2 | |
| 3 | /* |
| 4 | * SWIG typemaps for std::vector |
| 5 | * C# implementation |
| 6 | * The C# wrapper is made to look and feel like a typesafe C# System.Collections.ArrayList |
| 7 | * All the methods in IList are defined, but we don't derive from IList as this is a typesafe collection. |
| 8 | * Warning: heavy macro usage in this file. Use swig -E to get a sane view on the real file contents! |
| 9 | */ |
| 10 | |
| 11 | %include <std_common.i> |
| 12 | |
| 13 | // MACRO for use within the std::vector class body |
| 14 | // CSTYPE and CTYPE respectively correspond to the types in the cstype and ctype typemaps |
| 15 | %define SWIG_STD_VECTOR_MINIMUM(CSTYPE, CTYPE...) |
| 16 | %typemap(csinterfaces) std::vector<CTYPE > "IDisposable, System.Collections.IEnumerable"; |
| 17 | %typemap(cscode) std::vector<CTYPE > %{ |
| 18 | public $csclassname(System.Collections.ICollection c) : this() { |
| 19 | if (c == null) |
| 20 | throw new ArgumentNullException("c"); |
| 21 | foreach (CSTYPE element in c) { |
| 22 | this.Add(element); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | public bool IsFixedSize { |
| 27 | get { |
| 28 | return false; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | public bool IsReadOnly { |
| 33 | get { |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public CSTYPE this[int index] { |
| 39 | get { |
| 40 | return getitem(index); |
| 41 | } |
| 42 | set { |
| 43 | setitem(index, value); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public int Capacity { |
| 48 | get { |
| 49 | return (int)capacity(); |
| 50 | } |
| 51 | set { |
| 52 | if (value < size()) |
| 53 | throw new ArgumentOutOfRangeException("Capacity"); |
| 54 | reserve((uint)value); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public int Count { |
| 59 | get { |
| 60 | return (int)size(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public bool IsSynchronized { |
| 65 | get { |
| 66 | return false; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public void CopyTo(System.Array array) { |
| 71 | CopyTo(0, array, 0, this.Count); |
| 72 | } |
| 73 | |
| 74 | public void CopyTo(System.Array array, int arrayIndex) { |
| 75 | CopyTo(0, array, arrayIndex, this.Count); |
| 76 | } |
| 77 | |
| 78 | public void CopyTo(int index, System.Array array, int arrayIndex, int count) { |
| 79 | if (array == null) |
| 80 | throw new ArgumentNullException("array"); |
| 81 | if (index < 0) |
| 82 | throw new ArgumentOutOfRangeException("index", "Value is less than zero"); |
| 83 | if (arrayIndex < 0) |
| 84 | throw new ArgumentOutOfRangeException("arrayIndex", "Value is less than zero"); |
| 85 | if (count < 0) |
| 86 | throw new ArgumentOutOfRangeException("count", "Value is less than zero"); |
| 87 | if (array.Rank > 1) |
| 88 | throw new ArgumentException("Multi dimensional array."); |
| 89 | if (index+count > this.Count || arrayIndex+count > array.Length) |
| 90 | throw new ArgumentException("Number of elements to copy is too large."); |
| 91 | for (int i=0; i<count; i++) |
| 92 | array.SetValue(getitemcopy(index+i), arrayIndex+i); |
| 93 | } |
| 94 | |
| 95 | // Type-safe version of IEnumerable.GetEnumerator |
| 96 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { |
| 97 | return new $csclassnameEnumerator(this); |
| 98 | } |
| 99 | |
| 100 | public $csclassnameEnumerator GetEnumerator() { |
| 101 | return new $csclassnameEnumerator(this); |
| 102 | } |
| 103 | |
| 104 | // Type-safe enumerator |
| 105 | /// Note that the IEnumerator documentation requires an InvalidOperationException to be thrown |
| 106 | /// whenever the collection is modified. This has been done for changes in the size of the |
| 107 | /// collection but not when one of the elements of the collection is modified as it is a bit |
| 108 | /// tricky to detect unmanaged code that modifies the collection under our feet. |
| 109 | public sealed class $csclassnameEnumerator : System.Collections.IEnumerator { |
| 110 | private $csclassname collectionRef; |
| 111 | private int currentIndex; |
| 112 | private object currentObject; |
| 113 | private int currentSize; |
| 114 | |
| 115 | public $csclassnameEnumerator($csclassname collection) { |
| 116 | collectionRef = collection; |
| 117 | currentIndex = -1; |
| 118 | currentObject = null; |
| 119 | currentSize = collectionRef.Count; |
| 120 | } |
| 121 | |
| 122 | // Type-safe iterator Current |
| 123 | public CSTYPE Current { |
| 124 | get { |
| 125 | if (currentIndex == -1) |
| 126 | throw new InvalidOperationException("Enumeration not started."); |
| 127 | if (currentIndex > currentSize - 1) |
| 128 | throw new InvalidOperationException("Enumeration finished."); |
| 129 | if (currentObject == null) |
| 130 | throw new InvalidOperationException("Collection modified."); |
| 131 | return (CSTYPE)currentObject; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Type-unsafe IEnumerator.Current |
| 136 | object System.Collections.IEnumerator.Current { |
| 137 | get { |
| 138 | return Current; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | public bool MoveNext() { |
| 143 | int size = collectionRef.Count; |
| 144 | bool moveOkay = (currentIndex+1 < size) && (size == currentSize); |
| 145 | if (moveOkay) { |
| 146 | currentIndex++; |
| 147 | currentObject = collectionRef[currentIndex]; |
| 148 | } else { |
| 149 | currentObject = null; |
| 150 | } |
| 151 | return moveOkay; |
| 152 | } |
| 153 | |
| 154 | public void Reset() { |
| 155 | currentIndex = -1; |
| 156 | currentObject = null; |
| 157 | if (collectionRef.Count != currentSize) { |
| 158 | throw new InvalidOperationException("Collection modified."); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | %} |
| 163 | |
| 164 | public: |
| 165 | typedef size_t size_type; |
| 166 | %rename(Clear) clear; |
| 167 | void clear(); |
| 168 | %rename(Add) push_back; |
| 169 | void push_back(const CTYPE& value); |
| 170 | size_type size() const; |
| 171 | size_type capacity() const; |
| 172 | void reserve(size_type n); |
| 173 | %newobject GetRange(int index, int count); |
| 174 | %newobject Repeat(const CTYPE& value, int count); |
| 175 | vector(); |
| 176 | %extend { |
| 177 | vector(int capacity) throw (std::out_of_range) { |
| 178 | std::vector<CTYPE >* pv = 0; |
| 179 | if (capacity >= 0) { |
| 180 | pv = new std::vector<CTYPE >(); |
| 181 | pv->reserve(capacity); |
| 182 | } else { |
| 183 | throw std::out_of_range("capacity"); |
| 184 | } |
| 185 | return pv; |
| 186 | } |
| 187 | CTYPE getitemcopy(int index) throw (std::out_of_range) { |
| 188 | if (index>=0 && index<(int)self->size()) |
| 189 | return (*self)[index]; |
| 190 | else |
| 191 | throw std::out_of_range("index"); |
| 192 | } |
| 193 | const CTYPE& getitem(int index) throw (std::out_of_range) { |
| 194 | if (index>=0 && index<(int)self->size()) |
| 195 | return (*self)[index]; |
| 196 | else |
| 197 | throw std::out_of_range("index"); |
| 198 | } |
| 199 | void setitem(int index, const CTYPE& value) throw (std::out_of_range) { |
| 200 | if (index>=0 && index<(int)self->size()) |
| 201 | (*self)[index] = value; |
| 202 | else |
| 203 | throw std::out_of_range("index"); |
| 204 | } |
| 205 | // Takes a deep copy of the elements unlike ArrayList.AddRange |
| 206 | void AddRange(const std::vector<CTYPE >& values) { |
| 207 | self->insert(self->end(), values.begin(), values.end()); |
| 208 | } |
| 209 | // Takes a deep copy of the elements unlike ArrayList.GetRange |
| 210 | std::vector<CTYPE > *GetRange(int index, int count) throw (std::out_of_range, std::invalid_argument) { |
| 211 | if (index < 0) |
| 212 | throw std::out_of_range("index"); |
| 213 | if (count < 0) |
| 214 | throw std::out_of_range("count"); |
| 215 | if (index >= (int)self->size()+1 || index+count > (int)self->size()) |
| 216 | throw std::invalid_argument("invalid range"); |
| 217 | return new std::vector<CTYPE >(self->begin()+index, self->begin()+index+count); |
| 218 | } |
| 219 | void Insert(int index, const CTYPE& value) throw (std::out_of_range) { |
| 220 | if (index>=0 && index<(int)self->size()+1) |
| 221 | self->insert(self->begin()+index, value); |
| 222 | else |
| 223 | throw std::out_of_range("index"); |
| 224 | } |
| 225 | // Takes a deep copy of the elements unlike ArrayList.InsertRange |
| 226 | void InsertRange(int index, const std::vector<CTYPE >& values) throw (std::out_of_range) { |
| 227 | if (index>=0 && index<(int)self->size()+1) |
| 228 | self->insert(self->begin()+index, values.begin(), values.end()); |
| 229 | else |
| 230 | throw std::out_of_range("index"); |
| 231 | } |
| 232 | void RemoveAt(int index) throw (std::out_of_range) { |
| 233 | if (index>=0 && index<(int)self->size()) |
| 234 | self->erase(self->begin() + index); |
| 235 | else |
| 236 | throw std::out_of_range("index"); |
| 237 | } |
| 238 | void RemoveRange(int index, int count) throw (std::out_of_range, std::invalid_argument) { |
| 239 | if (index < 0) |
| 240 | throw std::out_of_range("index"); |
| 241 | if (count < 0) |
| 242 | throw std::out_of_range("count"); |
| 243 | if (index >= (int)self->size()+1 || index+count > (int)self->size()) |
| 244 | throw std::invalid_argument("invalid range"); |
| 245 | self->erase(self->begin()+index, self->begin()+index+count); |
| 246 | } |
| 247 | static std::vector<CTYPE > *Repeat(const CTYPE& value, int count) throw (std::out_of_range) { |
| 248 | if (count < 0) |
| 249 | throw std::out_of_range("count"); |
| 250 | return new std::vector<CTYPE >(count, value); |
| 251 | } |
| 252 | void Reverse() { |
| 253 | std::reverse(self->begin(), self->end()); |
| 254 | } |
| 255 | void Reverse(int index, int count) throw (std::out_of_range, std::invalid_argument) { |
| 256 | if (index < 0) |
| 257 | throw std::out_of_range("index"); |
| 258 | if (count < 0) |
| 259 | throw std::out_of_range("count"); |
| 260 | if (index >= (int)self->size()+1 || index+count > (int)self->size()) |
| 261 | throw std::invalid_argument("invalid range"); |
| 262 | std::reverse(self->begin()+index, self->begin()+index+count); |
| 263 | } |
| 264 | // Takes a deep copy of the elements unlike ArrayList.SetRange |
| 265 | void SetRange(int index, const std::vector<CTYPE >& values) throw (std::out_of_range) { |
| 266 | if (index < 0) |
| 267 | throw std::out_of_range("index"); |
| 268 | if (index+values.size() > self->size()) |
| 269 | throw std::out_of_range("index"); |
| 270 | std::copy(values.begin(), values.end(), self->begin()+index); |
| 271 | } |
| 272 | } |
| 273 | %enddef |
| 274 | |
| 275 | // Extra methods added to the collection class if operator== is defined for the class being wrapped |
| 276 | // CSTYPE and CTYPE respectively correspond to the types in the cstype and ctype typemaps |
| 277 | %define SWIG_STD_VECTOR_EXTRA_OP_EQUALS_EQUALS(CSTYPE, CTYPE...) |
| 278 | %extend { |
| 279 | bool Contains(const CTYPE& value) { |
| 280 | return std::find(self->begin(), self->end(), value) != self->end(); |
| 281 | } |
| 282 | int IndexOf(const CTYPE& value) { |
| 283 | int index = -1; |
| 284 | std::vector<CTYPE >::iterator it = std::find(self->begin(), self->end(), value); |
| 285 | if (it != self->end()) |
| 286 | index = it - self->begin(); |
| 287 | return index; |
| 288 | } |
| 289 | int LastIndexOf(const CTYPE& value) { |
| 290 | int index = -1; |
| 291 | std::vector<CTYPE >::reverse_iterator rit = std::find(self->rbegin(), self->rend(), value); |
| 292 | if (rit != self->rend()) |
| 293 | index = self->rend() - 1 - rit; |
| 294 | return index; |
| 295 | } |
| 296 | void Remove(const CTYPE& value) { |
| 297 | std::vector<CTYPE >::iterator it = std::find(self->begin(), self->end(), value); |
| 298 | if (it != self->end()) |
| 299 | self->erase(it); |
| 300 | } |
| 301 | } |
| 302 | %enddef |
| 303 | |
| 304 | // Macros for std::vector class specializations |
| 305 | // CSTYPE and CTYPE respectively correspond to the types in the cstype and ctype typemaps |
| 306 | %define SWIG_STD_VECTOR_SPECIALIZE(CSTYPE, CTYPE...) |
| 307 | namespace std { |
| 308 | template<> class vector<CTYPE > { |
| 309 | SWIG_STD_VECTOR_MINIMUM(CSTYPE, CTYPE) |
| 310 | SWIG_STD_VECTOR_EXTRA_OP_EQUALS_EQUALS(CSTYPE, CTYPE) |
| 311 | }; |
| 312 | } |
| 313 | %enddef |
| 314 | |
| 315 | %define SWIG_STD_VECTOR_SPECIALIZE_MINIMUM(CSTYPE, CTYPE...) |
| 316 | namespace std { |
| 317 | template<> class vector<CTYPE > { |
| 318 | SWIG_STD_VECTOR_MINIMUM(CSTYPE, CTYPE) |
| 319 | }; |
| 320 | } |
| 321 | %enddef |
| 322 | |
| 323 | |
| 324 | %{ |
| 325 | #include <vector> |
| 326 | #include <algorithm> |
| 327 | #include <stdexcept> |
| 328 | %} |
| 329 | |
| 330 | %csmethodmodifiers std::vector::getitemcopy "private" |
| 331 | %csmethodmodifiers std::vector::getitem "private" |
| 332 | %csmethodmodifiers std::vector::setitem "private" |
| 333 | %csmethodmodifiers std::vector::size "private" |
| 334 | %csmethodmodifiers std::vector::capacity "private" |
| 335 | %csmethodmodifiers std::vector::reserve "private" |
| 336 | |
| 337 | namespace std { |
| 338 | // primary (unspecialized) class template for std::vector |
| 339 | // does not require operator== to be defined |
| 340 | template<class T> class vector { |
| 341 | SWIG_STD_VECTOR_MINIMUM(T, T) |
| 342 | }; |
| 343 | } |
| 344 | |
| 345 | // template specializations for std::vector |
| 346 | // these provide extra collections methods as operator== is defined |
| 347 | SWIG_STD_VECTOR_SPECIALIZE(bool, bool) |
| 348 | SWIG_STD_VECTOR_SPECIALIZE(char, char) |
| 349 | SWIG_STD_VECTOR_SPECIALIZE(sbyte, signed char) |
| 350 | SWIG_STD_VECTOR_SPECIALIZE(byte, unsigned char) |
| 351 | SWIG_STD_VECTOR_SPECIALIZE(short, short) |
| 352 | SWIG_STD_VECTOR_SPECIALIZE(ushort, unsigned short) |
| 353 | SWIG_STD_VECTOR_SPECIALIZE(int, int) |
| 354 | SWIG_STD_VECTOR_SPECIALIZE(uint, unsigned int) |
| 355 | SWIG_STD_VECTOR_SPECIALIZE(int, long) |
| 356 | SWIG_STD_VECTOR_SPECIALIZE(uint, unsigned long) |
| 357 | SWIG_STD_VECTOR_SPECIALIZE(long, long long) |
| 358 | SWIG_STD_VECTOR_SPECIALIZE(ulong, unsigned long long) |
| 359 | SWIG_STD_VECTOR_SPECIALIZE(float, float) |
| 360 | SWIG_STD_VECTOR_SPECIALIZE(double, double) |
| 361 | SWIG_STD_VECTOR_SPECIALIZE(string, std::string) // also requires a %include "std_string.i" |
| 362 | |
| 363 | |