Mantid
Loading...
Searching...
No Matches
WrapWithNDArray.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2012 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
10#include <boost/python/detail/prefix.hpp>
11#include <vector>
12
13namespace Mantid {
14namespace PythonInterface {
15namespace Converters {
20
21namespace Impl {
22// Forward declare a conversion function. This should be specialized for each
23// container type that is to be wrapped
24template <typename ElementType>
25PyObject *wrapWithNDArray(const ElementType *, const int ndims, Py_intptr_t *dims, const NumpyWrapMode mode,
26 const OwnershipMode oMode = OwnershipMode::Cpp);
27} // namespace Impl
28
35
36 template <typename ElementType> struct apply {
43 static PyObject *create1D(const std::vector<ElementType> &cdata) {
44 Py_intptr_t dims[1] = {static_cast<int>(cdata.size())};
45 return createFromArray(cdata.data(), 1, dims);
46 }
57 static PyObject *createFromArray(const ElementType *cdata, const int ndims, Py_intptr_t *dims) {
58 return Impl::wrapWithNDArray(cdata, ndims, dims, ReadOnly);
59 }
60 };
61};
62
69
70 template <typename ElementType> struct apply {
71
77 static PyObject *create1D(const std::vector<ElementType> &cdata) {
78 Py_intptr_t dims[1] = {static_cast<int>(cdata.size())};
79 return createFromArray(cdata.data(), 1, dims);
80 }
91 static PyObject *createFromArray(const ElementType *cdata, const int ndims, Py_intptr_t *dims) {
92 return Impl::wrapWithNDArray(cdata, ndims, dims, ReadWrite);
93 }
94 };
95};
96} // namespace Converters
97} // namespace PythonInterface
98} // namespace Mantid
PyObject * wrapWithNDArray(const ElementType *, const int ndims, Py_intptr_t *dims, const NumpyWrapMode mode, const OwnershipMode oMode=OwnershipMode::Cpp)
Defines the wrapWithNDArray specialization for C array types.
NumpyWrapMode
Enum defining wrapping type for conversion to numpy.
OwnershipMode
Enum defining transfer of ownership when converting to numpy array.
Helper class which provides the Collimation Length for SANS instruments.
static PyObject * create1D(const std::vector< ElementType > &cdata)
Returns a read-only 1D Numpy array wrapped around an existing container that knows its size.
static PyObject * createFromArray(const ElementType *cdata, const int ndims, Py_intptr_t *dims)
Returns a read-only Numpy array wrapped around an existing array.
WrapReadOnly is a policy for VectorToNDArray to wrap the vector in a read-only numpy array that looks...
static PyObject * create1D(const std::vector< ElementType > &cdata)
Returns a read-write Numpy array wrapped around an existing vector.
static PyObject * createFromArray(const ElementType *cdata, const int ndims, Py_intptr_t *dims)
Returns a read-only Numpy array wrapped around an existing array.
WrapReadWrite is a policy for VectorToNDArray to wrap the vector in a read-write numpy array that loo...