summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorERROR <siwa.boonpun@gmail.com>2022-01-04 06:12:56 +0700
committerGitHub <noreply@github.com>2022-01-04 12:12:56 +1300
commitd73f933c4bb5ca1f327870554f75a87372964162 (patch)
treea2035f87b972a8fcee1243cb108ee685e1f41784
parent48eba82ac8847f8422710fac677875baf88566df (diff)
downloadswig-d73f933c4bb5ca1f327870554f75a87372964162.tar.gz
Support std::array in Golang (#2045)
Support std::array in Golang Closes #2045
-rw-r--r--Examples/test-suite/cpp11_std_array.i2
-rw-r--r--Examples/test-suite/go/cpp11_std_array_runme.go68
-rw-r--r--Lib/go/std_array.i43
3 files changed, 112 insertions, 1 deletions
diff --git a/Examples/test-suite/cpp11_std_array.i b/Examples/test-suite/cpp11_std_array.i
index 3d4771551..9dc11ce9e 100644
--- a/Examples/test-suite/cpp11_std_array.i
+++ b/Examples/test-suite/cpp11_std_array.i
@@ -1,6 +1,6 @@
%module cpp11_std_array
-#if defined(SWIGPYTHON) || defined(SWIGRUBY) || defined(SWIGJAVA) || defined(SWIGCSHARP)
+#if defined(SWIGPYTHON) || defined(SWIGRUBY) || defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGGO)
%{
#include <array>
diff --git a/Examples/test-suite/go/cpp11_std_array_runme.go b/Examples/test-suite/go/cpp11_std_array_runme.go
new file mode 100644
index 000000000..97b5df2ec
--- /dev/null
+++ b/Examples/test-suite/go/cpp11_std_array_runme.go
@@ -0,0 +1,68 @@
+package main
+
+import (
+ "fmt"
+ "swigtests/cpp11_std_array"
+)
+
+func CompareContainers(actual cpp11_std_array.ArrayInt6, expected [6]int) error {
+ if int(actual.Size()) != len(expected) {
+ return fmt.Errorf("Sizes are different: %d %d", actual.Size(), len(expected))
+ }
+ for i := 0; i < int(actual.Size()); i++ {
+ actualValue := actual.Get(i)
+ expectedValue := expected[i]
+ if actualValue != expectedValue {
+ return fmt.Errorf("Value is wrong for element %d. Expected %d got: %d", i, expectedValue, actualValue)
+ }
+ }
+ if actual.IsEmpty() {
+ return fmt.Errorf("ai should not be empty")
+ }
+ return nil
+}
+
+func main() {
+ ai := cpp11_std_array.NewArrayInt6()
+ ps := [6]int{0, 0, 0, 0, 0, 0}
+ CompareContainers(ai, ps)
+
+ vals := [6]int{10, 20, 30, 40, 50, 60}
+ for i := 0; i < len(vals); i++ {
+ ai.Set(i, vals[i])
+ }
+ CompareContainers(ai, vals);
+
+ // Check return
+ vals = [6]int{-2, -1, 0, 0, 1, 2}
+ CompareContainers(cpp11_std_array.ArrayOutVal(), vals);
+ CompareContainers(cpp11_std_array.ArrayOutConstRef(), vals);
+ CompareContainers(cpp11_std_array.ArrayOutRef(), vals);
+ CompareContainers(cpp11_std_array.ArrayOutPtr(), vals);
+
+ // Check passing arguments
+ vals = [6]int{9, 8, 7, 6, 5, 4}
+ valsArrayInt6 := cpp11_std_array.NewArrayInt6()
+ for i := 0; i < len(vals); i++ {
+ valsArrayInt6.Set(i, vals[i])
+ }
+
+ ai = cpp11_std_array.ArrayInVal(valsArrayInt6);
+ CompareContainers(ai, vals);
+
+ ai = cpp11_std_array.ArrayInConstRef(valsArrayInt6);
+ CompareContainers(ai, vals);
+
+ ai = cpp11_std_array.NewArrayInt6(valsArrayInt6);
+ cpp11_std_array.ArrayInRef(ai);
+ CompareContainers(ai, vals);
+
+ ai = cpp11_std_array.NewArrayInt6(valsArrayInt6);
+ cpp11_std_array.ArrayInPtr(ai);
+ CompareContainers(ai, vals);
+
+ // Fill
+ ai.Fill(111)
+ vals = [6]int{111, 111, 111, 111, 111, 111}
+ CompareContainers(ai, vals);
+}
diff --git a/Lib/go/std_array.i b/Lib/go/std_array.i
new file mode 100644
index 000000000..36c790e3c
--- /dev/null
+++ b/Lib/go/std_array.i
@@ -0,0 +1,43 @@
+/* -----------------------------------------------------------------------------
+ * std_array.i
+ * ----------------------------------------------------------------------------- */
+
+%include <std_common.i>
+
+namespace std {
+
+ template<class T, size_t N> class array {
+ public:
+ typedef size_t size_type;
+ typedef ptrdiff_t difference_type;
+ typedef T value_type;
+ typedef value_type* pointer;
+ typedef const value_type* const_pointer;
+ typedef value_type& reference;
+ typedef const value_type& const_reference;
+
+ array();
+ array(const array& other);
+
+ size_type size() const;
+ %rename(isEmpty) empty;
+ bool empty() const;
+ void fill(const T& u);
+ %extend {
+ const_reference get(int i) throw (std::out_of_range) {
+ int size = int(self->size());
+ if (i>=0 && i<size)
+ return (*self)[i];
+ else
+ throw std::out_of_range("array index out of range");
+ }
+ void set(int i, const value_type& val) throw (std::out_of_range) {
+ int size = int(self->size());
+ if (i>=0 && i<size)
+ (*self)[i] = val;
+ else
+ throw std::out_of_range("array index out of range");
+ }
+ }
+ };
+}