diff options
author | William S Fulton <wsf@fultondesigns.co.uk> | 2023-03-22 21:05:36 +0000 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2023-03-22 21:05:36 +0000 |
commit | bf122fe985ce89d3ce91df2edae4c29088dfebf0 (patch) | |
tree | 6acd08d87fd12794c066c9f5e10baa966ebf98d0 | |
parent | 0c56f3557eeeee24ce438c78e35439dada5f5001 (diff) | |
parent | 20f7309220a774e1c2b5b0ec3cf94f4d09c192bd (diff) | |
download | swig-bf122fe985ce89d3ce91df2edae4c29088dfebf0.tar.gz |
Merge branch 'bda_standardize_vector_array'
* bda_standardize_vector_array:
std_array.i std_vector.i tweaks
Lib/csharp: Better standardized std_vector.i and std_array.i
-rw-r--r-- | Examples/test-suite/csharp/cpp11_std_array_runme.cs | 15 | ||||
-rw-r--r-- | Lib/csharp/std_array.i | 71 | ||||
-rw-r--r-- | Lib/csharp/std_vector.i | 14 |
3 files changed, 72 insertions, 28 deletions
diff --git a/Examples/test-suite/csharp/cpp11_std_array_runme.cs b/Examples/test-suite/csharp/cpp11_std_array_runme.cs index ee7c5ee24..649e06206 100644 --- a/Examples/test-suite/csharp/cpp11_std_array_runme.cs +++ b/Examples/test-suite/csharp/cpp11_std_array_runme.cs @@ -78,5 +78,20 @@ public class cpp11_std_array_runme catch (ArgumentOutOfRangeException) { } + + // ICollection constructor, differently sized + ai = new ArrayInt6(new int[] {1, 2, 3}); + compareContainers(ai, new int[] { 1, 2, 3, 0, 0, 0}); + ai = new ArrayInt6(new int[] {1, 2, 3, 4, 5, 6, 7, 8}); + compareContainers(ai, new int[] { 1, 2, 3, 4, 5, 6}); + + // ToArray test + int[] aiArray = ai.ToArray(); + for (int i=0; i<ai.Count; i++) { + if (aiArray[i] != ai[i]) + throw new Exception("ToArray failed, index:" + i); + } + if (ai.Count != aiArray.Length) + throw new Exception("ToArray lengths mismatch"); } } diff --git a/Lib/csharp/std_array.i b/Lib/csharp/std_array.i index 6e7fe9eb4..87b1b8956 100644 --- a/Lib/csharp/std_array.i +++ b/Lib/csharp/std_array.i @@ -6,37 +6,37 @@ * The C# wrapper is made to look and feel like a C# System.Collections.Generic.IReadOnlyList<> collection. * ----------------------------------------------------------------------------- */ -%{ -#include <algorithm> -#include <array> -#include <stdexcept> -%} - %include <std_common.i> -%define SWIG_STD_ARRAY_INTERNAL(T, N) -%typemap(csinterfaces) std::array< T, N > "global::System.IDisposable, global::System.Collections.IEnumerable\n , global::System.Collections.Generic.IEnumerable<$typemap(cstype, T)>\n" +%define SWIG_STD_ARRAY_INTERNAL(CTYPE, N) +%typemap(csinterfaces) std::array< CTYPE, N > "global::System.IDisposable, global::System.Collections.IEnumerable\n , global::System.Collections.Generic.IEnumerable<$typemap(cstype, CTYPE)>\n" %proxycode %{ public $csclassname(global::System.Collections.ICollection c) : this() { if (c == null) throw new global::System.ArgumentNullException("c"); - int end = global::System.Math.Min(this.Count, c.Count); + int count = this.Count; int i = 0; - foreach ($typemap(cstype, T) elem in c) { - if (i >= end) + foreach ($typemap(cstype, CTYPE) element in c) { + if (i >= count) break; - this[i++] = elem; + this[i++] = element; } } - public int Count { + public bool IsFixedSize { get { - return (int)size(); + return true; } } - public $typemap(cstype, T) this[int index] { + public bool IsReadOnly { + get { + return false; + } + } + + public $typemap(cstype, CTYPE) this[int index] { get { return getitem(index); } @@ -51,17 +51,29 @@ } } - public void CopyTo($typemap(cstype, T)[] array) + public int Count { + get { + return (int)size(); + } + } + + public bool IsSynchronized { + get { + return false; + } + } + + public void CopyTo($typemap(cstype, CTYPE)[] array) { CopyTo(0, array, 0, this.Count); } - public void CopyTo($typemap(cstype, T)[] array, int arrayIndex) + public void CopyTo($typemap(cstype, CTYPE)[] array, int arrayIndex) { CopyTo(0, array, arrayIndex, this.Count); } - public void CopyTo(int index, $typemap(cstype, T)[] array, int arrayIndex, int count) + public void CopyTo(int index, $typemap(cstype, CTYPE)[] array, int arrayIndex, int count) { if (array == null) throw new global::System.ArgumentNullException("array"); @@ -79,7 +91,13 @@ array.SetValue(getitemcopy(index+i), arrayIndex+i); } - global::System.Collections.Generic.IEnumerator<$typemap(cstype, T)> global::System.Collections.Generic.IEnumerable<$typemap(cstype, T)>.GetEnumerator() { + public $typemap(cstype, CTYPE)[] ToArray() { + $typemap(cstype, CTYPE)[] array = new $typemap(cstype, CTYPE)[this.Count]; + this.CopyTo(array); + return array; + } + + global::System.Collections.Generic.IEnumerator<$typemap(cstype, CTYPE)> global::System.Collections.Generic.IEnumerable<$typemap(cstype, CTYPE)>.GetEnumerator() { return new $csclassnameEnumerator(this); } @@ -97,7 +115,7 @@ /// collection but not when one of the elements of the collection is modified as it is a bit /// tricky to detect unmanaged code that modifies the collection under our feet. public sealed class $csclassnameEnumerator : global::System.Collections.IEnumerator - , global::System.Collections.Generic.IEnumerator<$typemap(cstype, T)> + , global::System.Collections.Generic.IEnumerator<$typemap(cstype, CTYPE)> { private $csclassname collectionRef; private int currentIndex; @@ -112,7 +130,7 @@ } // Type-safe iterator Current - public $typemap(cstype, T) Current { + public $typemap(cstype, CTYPE) Current { get { if (currentIndex == -1) throw new global::System.InvalidOperationException("Enumeration not started."); @@ -120,7 +138,7 @@ throw new global::System.InvalidOperationException("Enumeration finished."); if (currentObject == null) throw new global::System.InvalidOperationException("Collection modified."); - return ($typemap(cstype, T))currentObject; + return ($typemap(cstype, CTYPE))currentObject; } } @@ -161,7 +179,7 @@ public: typedef size_t size_type; typedef ptrdiff_t difference_type; - typedef T value_type; + typedef CTYPE value_type; typedef value_type* pointer; typedef const value_type* const_pointer; typedef value_type& reference; @@ -180,7 +198,7 @@ void swap(array& other); %extend { - T getitemcopy(int index) throw (std::out_of_range) { + CTYPE getitemcopy(int index) throw (std::out_of_range) { if (index>=0 && index<(int)$self->size()) return (*$self)[index]; else @@ -213,6 +231,11 @@ } %enddef +%{ +#include <array> +#include <algorithm> +#include <stdexcept> +%} %csmethodmodifiers std::array::empty "private" %csmethodmodifiers std::array::getitemcopy "private" diff --git a/Lib/csharp/std_vector.i b/Lib/csharp/std_vector.i index a2add584d..8438f6418 100644 --- a/Lib/csharp/std_vector.i +++ b/Lib/csharp/std_vector.i @@ -69,6 +69,12 @@ } } + public bool IsEmpty { + get { + return empty(); + } + } + public int Count { get { return (int)size(); @@ -203,19 +209,20 @@ typedef value_type& reference; typedef CONST_REFERENCE const_reference; + vector(); + vector(const vector &other); + %rename(Clear) clear; void clear(); %rename(Add) push_back; void push_back(CTYPE const& x); size_type size() const; + bool empty() const; size_type capacity() const; void reserve(size_type n); %newobject GetRange(int index, int count); %newobject Repeat(CTYPE const& value, int count); - vector(); - vector(const vector &other); - %extend { vector(int capacity) throw (std::out_of_range) { std::vector< CTYPE >* pv = 0; @@ -415,4 +422,3 @@ SWIG_STD_VECTOR_ENHANCED(float) SWIG_STD_VECTOR_ENHANCED(double) SWIG_STD_VECTOR_ENHANCED(std::string) // also requires a %include <std_string.i> SWIG_STD_VECTOR_ENHANCED(std::wstring) // also requires a %include <std_wstring.i> - |