Skip to content
Snippets Groups Projects
Commit faadb87b authored by Carsten Gräser's avatar Carsten Gräser
Browse files

[python] Add python cnversion for Dune::TupleVector

parent c9b8ac08
No related branches found
No related tags found
1 merge request!82[python] Add python cnversion for Dune::TupleVector
Pipeline #31499 passed
......@@ -20,6 +20,10 @@
#include <dune/common/fvector.hh>
#include <dune/common/parametertree.hh>
#include <dune/common/typetraits.hh>
#include <dune/common/hybridutilities.hh>
#include <dune/common/tuplevector.hh>
#include <dune/common/indices.hh>
#include <dune/common/rangeutilities.hh>
#include <dune/geometry/utility/typefromvertexcount.hh>
......@@ -373,6 +377,45 @@ struct Conversion<Dune::BlockVector<T> >
};
// conversion of FieldVector
template<class... T>
struct Conversion<Dune::TupleVector<T...> >
{
enum {useDefaultConstructorConversion=true};
static void toC(PyObject* list, Dune::TupleVector<T...>& v)
{
static constexpr auto n = Dune::index_constant<sizeof...(T)>{};
if (not PySequence_Check(list))
DUNE_THROW(Dune::Exception, "cannot convert a non-sequence to a Dune::TupleVector<T...>");
else
{
if (PySequence_Size(list)!=v.size())
DUNE_THROW(Dune::Exception, "cannot convert a sequence of size" << PySequence_Size(list) << " to a Dune::TupleVector of size " << v.size());
Dune::Hybrid::forEach(Dune::range(n), [&](auto i) {
Reference(PySequence_GetItem(list, i)).toC(v[i]);
});
// The above is OK since PySequence_GetItem returns a new reference.
// With PyTuple_GetItem we would need one of the following since it
// returns a borrowed reference:
// Conversion<T>::toC(PySequence_GetItem(list, i), v[i]);
// Reference(Imp::inc(PySequence_GetItem(list, i))).toC(v[i]);
}
}
static PyObject* toPy(const Dune::TupleVector<T...>& v)
{
static constexpr auto n = Dune::index_constant<sizeof...(T)>{};
PyObject* tuple = PyTuple_New(n);
Dune::Hybrid::forEach(Dune::range(n), [&](auto i) {
PyTuple_SetItem(tuple, i, Imp::inc(makeObject(v[i])));
});
// We have to use inc() since PyTuple_SetItem STEALS a reference!
return tuple;
}
};
// conversion to dict to ParameterTree
template<>
struct Conversion<Dune::ParameterTree>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment