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
9#include <boost/python/detail/prefix.hpp>
10#include <vector>
11
12namespace Mantid {
13namespace PythonInterface {
14namespace Converters {
19
20namespace Impl {
21// Forward declare a conversion function. This should be specialized for each
22// container type that is to be wrapped
23template <typename ElementType>
24PyObject *wrapWithNDArray(const ElementType *, const int ndims, Py_intptr_t *dims, const NumpyWrapMode mode,
25 const OwnershipMode oMode = OwnershipMode::Cpp);
26} // namespace Impl
27
34
35 template <typename ElementType> struct apply {
42 static PyObject *create1D(const std::vector<ElementType> &cdata) {
43 Py_intptr_t dims[1] = {static_cast<int>(cdata.size())};
44 return createFromArray(cdata.data(), 1, dims);
45 }
56 static PyObject *createFromArray(const ElementType *cdata, const int ndims, Py_intptr_t *dims) {
57 return Impl::wrapWithNDArray(cdata, ndims, dims, ReadOnly);
58 }
59 };
60};
61
68
69 template <typename ElementType> struct apply {
70
76 static PyObject *create1D(const std::vector<ElementType> &cdata) {
77 Py_intptr_t dims[1] = {static_cast<int>(cdata.size())};
78 return createFromArray(cdata.data(), 1, dims);
79 }
90 static PyObject *createFromArray(const ElementType *cdata, const int ndims, Py_intptr_t *dims) {
91 return Impl::wrapWithNDArray(cdata, ndims, dims, ReadWrite);
92 }
93 };
94};
95} // namespace Converters
96} // namespace PythonInterface
97} // 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...