Limited Python/Ruby support for boost::array
Hack to use the std::array support for boost::array. Is limited as it currently exposes some 'using' bugs in SWIG. For example, the type system fails to see that pointers to std::array and pointers to boost::array are the same. This approach saves having to maintain separate boost::array support. The 'using' bug ought to be fixed, otherwise separate boost_array.i files could be easily made from the std_array.i files.
This commit is contained in:
parent
b69719eb5b
commit
93eb7eae0b
10 changed files with 220 additions and 7 deletions
|
|
@ -258,6 +258,7 @@ CPP_TEST_CASES += \
|
|||
langobj \
|
||||
li_attribute \
|
||||
li_attribute_template \
|
||||
li_boost_array \
|
||||
li_boost_shared_ptr \
|
||||
li_boost_shared_ptr_bits \
|
||||
li_boost_shared_ptr_template \
|
||||
|
|
|
|||
|
|
@ -2,6 +2,10 @@
|
|||
|
||||
#if defined(SWIGPYTHON) || defined(SWIGRUBY)
|
||||
|
||||
%{
|
||||
#include <array>
|
||||
%}
|
||||
|
||||
%include <std_array.i>
|
||||
|
||||
%template(ArrayInt6) std::array<int, 6>;
|
||||
|
|
@ -16,6 +20,11 @@ std::array<int, 6> & arrayOutRef() {
|
|||
return a;
|
||||
}
|
||||
|
||||
const std::array<int, 6> & arrayOutConstRef() {
|
||||
static std::array<int, 6> a = { -2, -1, 0, 0, 1, 2 };
|
||||
return a;
|
||||
}
|
||||
|
||||
std::array<int, 6> * arrayOutPtr() {
|
||||
static std::array<int, 6> a = { -2, -1, 0, 0, 1, 2 };
|
||||
return &a;
|
||||
|
|
|
|||
77
Examples/test-suite/li_boost_array.i
Normal file
77
Examples/test-suite/li_boost_array.i
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
%module li_boost_array
|
||||
|
||||
#if defined(SWIGPYTHON) || defined(SWIGRUBY)
|
||||
|
||||
// Hack to use the std::array support for boost::array.
|
||||
// Is limited as it currently exposes some 'using' bugs in SWIG though.
|
||||
// For example, the type system fails to see that pointers to std::array
|
||||
// and pointers to boost::array are the same.
|
||||
|
||||
%{
|
||||
#include <boost/array.hpp>
|
||||
namespace std {
|
||||
using boost::array;
|
||||
}
|
||||
%}
|
||||
namespace boost {
|
||||
using std::array;
|
||||
}
|
||||
|
||||
%include <std_array.i>
|
||||
|
||||
%template(ArrayInt6) std::array<int, 6>;
|
||||
|
||||
%inline %{
|
||||
boost::array<int, 6> arrayOutVal() {
|
||||
const char carray[] = { -2, -1, 0, 0, 1, 2 };
|
||||
boost::array<int, 6> myarray;
|
||||
for (size_t i=0; i<6; ++i) {
|
||||
myarray[i] = carray[i];
|
||||
}
|
||||
return myarray;
|
||||
}
|
||||
|
||||
boost::array<int, 6> & arrayOutRef() {
|
||||
static boost::array<int, 6> a = { -2, -1, 0, 0, 1, 2 };
|
||||
return a;
|
||||
}
|
||||
|
||||
const boost::array<int, 6> & arrayOutConstRef() {
|
||||
static boost::array<int, 6> a = { -2, -1, 0, 0, 1, 2 };
|
||||
return a;
|
||||
}
|
||||
|
||||
boost::array<int, 6> * arrayOutPtr() {
|
||||
static boost::array<int, 6> a = { -2, -1, 0, 0, 1, 2 };
|
||||
return &a;
|
||||
}
|
||||
|
||||
boost::array<int, 6> arrayInVal(boost::array<int, 6> myarray) {
|
||||
for (boost::array<int, 6>::iterator it = myarray.begin(); it!=myarray.end(); ++it) {
|
||||
*it *= 10;
|
||||
}
|
||||
return myarray;
|
||||
}
|
||||
|
||||
const boost::array<int, 6> & arrayInConstRef(const boost::array<int, 6> & myarray) {
|
||||
static boost::array<int, 6> a = myarray;
|
||||
for (boost::array<int, 6>::iterator it = a.begin(); it!=a.end(); ++it) {
|
||||
*it *= 10;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
void arrayInRef(boost::array<int, 6> & myarray) {
|
||||
for (boost::array<int, 6>::iterator it = myarray.begin(); it!=myarray.end(); ++it) {
|
||||
*it *= 10;
|
||||
}
|
||||
}
|
||||
|
||||
void arrayInPtr(boost::array<int, 6> * myarray) {
|
||||
for (boost::array<int, 6>::iterator it = myarray->begin(); it!=myarray->end(); ++it) {
|
||||
*it *= 10;
|
||||
}
|
||||
}
|
||||
%}
|
||||
|
||||
#endif
|
||||
|
|
@ -64,6 +64,8 @@ ps = [0, 1, 2, 3, 4, 5]
|
|||
|
||||
ai = ArrayInt6(ps)
|
||||
|
||||
compare_containers(ps, ai)
|
||||
|
||||
# slices
|
||||
compare_containers(ps[0:6], ai[0:6])
|
||||
compare_containers(ps[0:10], ai[0:10])
|
||||
|
|
@ -137,6 +139,7 @@ setslice_exception(ai, [])
|
|||
|
||||
# Check return
|
||||
compare_containers(arrayOutVal(), [-2, -1, 0, 0, 1, 2])
|
||||
compare_containers(arrayOutConstRef(), [-2, -1, 0, 0, 1, 2])
|
||||
compare_containers(arrayOutRef(), [-2, -1, 0, 0, 1, 2])
|
||||
compare_containers(arrayOutPtr(), [-2, -1, 0, 0, 1, 2])
|
||||
|
||||
|
|
|
|||
55
Examples/test-suite/python/li_boost_array_runme.py
Normal file
55
Examples/test-suite/python/li_boost_array_runme.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
from li_boost_array import *
|
||||
import sys
|
||||
|
||||
|
||||
def failed(a, b, msg):
|
||||
raise RuntimeError, msg + " " + str(list(a)) + " " + str(list(b))
|
||||
|
||||
|
||||
def compare_sequences(a, b):
|
||||
if len(a) != len(b):
|
||||
failed(a, b, "different sizes")
|
||||
for i in range(len(a)):
|
||||
if a[i] != b[i]:
|
||||
failed(a, b, "elements are different")
|
||||
|
||||
def compare_containers(pythonlist, swigarray):
|
||||
compare_sequences(pythonlist, swigarray)
|
||||
|
||||
ps = [0, 1, 2, 3, 4, 5]
|
||||
|
||||
ai = ArrayInt6(ps)
|
||||
|
||||
compare_containers(ps, ai)
|
||||
|
||||
# Modify content
|
||||
for i in range(len(ps)):
|
||||
ps[i] = (ps[i] + 1) * 10
|
||||
ai[i] = (ai[i] + 1) * 10
|
||||
compare_containers(ps, ai)
|
||||
|
||||
# Empty
|
||||
ai = ArrayInt6()
|
||||
compare_containers([0, 0, 0, 0, 0, 0], ai)
|
||||
|
||||
# Check return
|
||||
compare_containers(arrayOutVal(), [-2, -1, 0, 0, 1, 2])
|
||||
compare_containers(arrayOutConstRef(), [-2, -1, 0, 0, 1, 2])
|
||||
#compare_containers(arrayOutRef(), [-2, -1, 0, 0, 1, 2])
|
||||
#compare_containers(arrayOutPtr(), [-2, -1, 0, 0, 1, 2])
|
||||
|
||||
# Check passing arguments
|
||||
ai = arrayInVal([9, 8, 7, 6, 5, 4])
|
||||
compare_containers(ai, [90, 80, 70, 60, 50, 40])
|
||||
|
||||
ai = arrayInConstRef([9, 8, 7, 6, 5, 4])
|
||||
compare_containers(ai, [90, 80, 70, 60, 50, 40])
|
||||
|
||||
#ai = ArrayInt6([9, 8, 7, 6, 5, 4])
|
||||
#arrayInRef(ai)
|
||||
#compare_containers(ai, [90, 80, 70, 60, 50, 40])
|
||||
|
||||
#ai = ArrayInt6([9, 8, 7, 6, 5, 4])
|
||||
#arrayInPtr(ai)
|
||||
#compare_containers(ai, [90, 80, 70, 60, 50, 40])
|
||||
|
||||
|
|
@ -89,6 +89,7 @@ setslice_exception(ai, [])
|
|||
|
||||
# Check return
|
||||
compare_containers(arrayOutVal(), [-2, -1, 0, 0, 1, 2])
|
||||
compare_containers(arrayOutConstRef(), [-2, -1, 0, 0, 1, 2])
|
||||
compare_containers(arrayOutRef(), [-2, -1, 0, 0, 1, 2])
|
||||
compare_containers(arrayOutPtr(), [-2, -1, 0, 0, 1, 2])
|
||||
|
||||
|
|
|
|||
70
Examples/test-suite/ruby/li_boost_array_runme.rb
Normal file
70
Examples/test-suite/ruby/li_boost_array_runme.rb
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#!/usr/bin/env ruby
|
||||
#
|
||||
# Put description here
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
|
||||
require 'swig_assert'
|
||||
|
||||
require 'li_boost_array'
|
||||
|
||||
include Li_boost_array
|
||||
|
||||
|
||||
def failed(a, b, msg)
|
||||
raise RuntimeError, "#{msg} #{a} #{b}"
|
||||
end
|
||||
|
||||
def compare_sequences(a, b)
|
||||
if a.size != b.size
|
||||
failed(a, b, "different sizes")
|
||||
end
|
||||
for i in 0..a.size-1
|
||||
failed(a, b, "elements are different:") if a[i] != b[i]
|
||||
end
|
||||
end
|
||||
|
||||
def compare_containers(rubyarray, swigarray)
|
||||
compare_sequences(rubyarray, swigarray)
|
||||
end
|
||||
|
||||
ps = [0, 1, 2, 3, 4, 5]
|
||||
|
||||
ai = ArrayInt6.new(ps)
|
||||
|
||||
compare_containers(ps, ai)
|
||||
|
||||
# Modify content
|
||||
for i in 0..ps.size-1
|
||||
ps[i] = ps[i] * 10
|
||||
ai[i] = ai[i] * 10
|
||||
end
|
||||
compare_containers(ps, ai)
|
||||
|
||||
# Empty
|
||||
ai = ArrayInt6.new()
|
||||
|
||||
# Check return
|
||||
compare_containers(arrayOutVal(), [-2, -1, 0, 0, 1, 2])
|
||||
compare_containers(arrayOutConstRef(), [-2, -1, 0, 0, 1, 2])
|
||||
#compare_containers(arrayOutRef(), [-2, -1, 0, 0, 1, 2])
|
||||
#compare_containers(arrayOutPtr(), [-2, -1, 0, 0, 1, 2])
|
||||
|
||||
# Check passing arguments
|
||||
ai = arrayInVal([9, 8, 7, 6, 5, 4])
|
||||
compare_containers(ai, [90, 80, 70, 60, 50, 40])
|
||||
|
||||
ai = arrayInConstRef([9, 8, 7, 6, 5, 4])
|
||||
compare_containers(ai, [90, 80, 70, 60, 50, 40])
|
||||
|
||||
#ai = ArrayInt6.new([9, 8, 7, 6, 5, 4])
|
||||
#arrayInRef(ai)
|
||||
#compare_containers(ai, [90, 80, 70, 60, 50, 40])
|
||||
|
||||
#ai = ArrayInt6.new([9, 8, 7, 6, 5, 4])
|
||||
#arrayInPtr(ai)
|
||||
#compare_containers(ai, [90, 80, 70, 60, 50, 40])
|
||||
|
||||
|
|
@ -39,7 +39,7 @@
|
|||
template <class T, size_t N, class Difference>
|
||||
inline std::array<T, N>*
|
||||
getslice(const std::array<T, N>* self, Difference i, Difference j, Py_ssize_t step) {
|
||||
using Sequence = std::array<T, N>;
|
||||
typedef std::array<T, N> Sequence;
|
||||
typename Sequence::size_type size = self->size();
|
||||
Difference ii = 0;
|
||||
Difference jj = 0;
|
||||
|
|
@ -61,7 +61,7 @@
|
|||
template <class T, size_t N, class Difference, class InputSeq>
|
||||
inline void
|
||||
setslice(std::array<T, N>* self, Difference i, Difference j, Py_ssize_t step, const InputSeq& is = InputSeq()) {
|
||||
using Sequence = std::array<T, N>;
|
||||
typedef std::array<T, N> Sequence;
|
||||
typename Sequence::size_type size = self->size();
|
||||
Difference ii = 0;
|
||||
Difference jj = 0;
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@
|
|||
template <class T, size_t N, class Difference>
|
||||
inline std::array<T, N>*
|
||||
getslice(const std::array<T, N>* self, Difference i, Difference j) {
|
||||
using Sequence = std::array<T, N>;
|
||||
typedef std::array<T, N> Sequence;
|
||||
typename Sequence::size_type size = self->size();
|
||||
typename Sequence::size_type ii = swig::check_index(i, size, (i == size && j == size));
|
||||
typename Sequence::size_type jj = swig::slice_index(j, size);
|
||||
|
|
@ -56,7 +56,7 @@
|
|||
template <class T, size_t N, class Difference, class InputSeq>
|
||||
inline void
|
||||
setslice(std::array<T, N>* self, Difference i, Difference j, const InputSeq& v) {
|
||||
using Sequence = std::array<T, N>;
|
||||
typedef std::array<T, N> Sequence;
|
||||
typename Sequence::size_type size = self->size();
|
||||
typename Sequence::size_type ii = swig::check_index(i, size, true);
|
||||
typename Sequence::size_type jj = swig::slice_index(j, size);
|
||||
|
|
|
|||
|
|
@ -40,9 +40,6 @@
|
|||
// for consistency, they expect and return a plain array pointer.
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
%{
|
||||
#include <array>
|
||||
%}
|
||||
|
||||
// exported classes
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue