Support std::array in Golang (#2045)

Support std::array in Golang

Closes #2045
This commit is contained in:
ERROR 2022-01-04 06:12:56 +07:00 committed by GitHub
commit d73f933c4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 112 additions and 1 deletions

View file

@ -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>

View file

@ -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);
}

43
Lib/go/std_array.i Normal file
View file

@ -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");
}
}
};
}