Mantid
Loading...
Searching...
No Matches
MatrixToNumpy.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
13
14#include <type_traits>
15
16#include <boost/mpl/and.hpp>
17#include <boost/mpl/if.hpp>
18
19namespace Mantid {
20namespace PythonInterface {
21namespace Policies {
22
23namespace // anonymous
24{
25//-----------------------------------------------------------------------
26// MPL helper structs
27//-----------------------------------------------------------------------
30template <typename T> struct is_matrix : boost::false_type {};
31
34template <typename T> struct is_matrix<Kernel::Matrix<T>> : boost::true_type {};
35
36//-----------------------------------------------------------------------
37// MatrixRefToNumpyImpl - Policy for reference returns
38//-----------------------------------------------------------------------
43template <typename MatrixType, typename ConversionPolicy> struct MatrixRefToNumpyImpl {
44 inline PyObject *operator()(const MatrixType &cmatrix) const {
46 }
47
48 inline PyTypeObject const *get_pytype() const { return ndarrayType(); }
49};
50
51template <typename T> struct MatrixRefToNumpy_Requires_Reference_To_Matrix_Return_Type {};
52} // namespace
53
64template <typename ConversionPolicy> struct MatrixRefToNumpy {
65 // The boost::python framework calls return_value_policy::apply<T>::type
66 template <class T> struct apply {
67 // Typedef that removes and const or reference qualifiers from the return
68 // type
69 using non_const_type = typename std::remove_const<typename std::remove_reference<T>::type>::type;
70 // MPL compile-time check that T is a reference to a Kernel::Matrix
71 using type = typename boost::mpl::if_c<boost::mpl::and_<std::is_reference<T>, is_matrix<non_const_type>>::value,
72 MatrixRefToNumpyImpl<non_const_type, ConversionPolicy>,
73 MatrixRefToNumpy_Requires_Reference_To_Matrix_Return_Type<T>>::type;
74 };
75};
76
77//-----------------------------------------------------------------------
78// MatrixToNumpy return_value_policy
79//-----------------------------------------------------------------------
80namespace {
85template <typename MatrixType> struct MatrixToNumpyImpl {
86 inline PyObject *operator()(const MatrixType &cvector) const {
88 }
89
90 inline PyTypeObject const *get_pytype() const { return ndarrayType(); }
91};
92
93template <typename T> struct MatrixToNumpy_Requires_Matrix_Return_By_Value {};
94} // namespace
95
103 // The boost::python framework calls return_value_policy::apply<T>::type
104 template <class T> struct apply {
105 // Typedef that removes any const from the type
106 using non_const_type = typename std::remove_const<T>::type;
107 // MPL compile-time check that T is a std::vector
108 using type = typename boost::mpl::if_c<is_matrix<non_const_type>::value,
109 MatrixRefToNumpyImpl<non_const_type, Converters::Clone>,
110 MatrixToNumpy_Requires_Matrix_Return_By_Value<T>>::type;
111 };
112};
113} // namespace Policies
114} // namespace PythonInterface
115} // namespace Mantid
double value
The value of the point.
Definition FitMW.cpp:51
MANTID_PYTHONINTERFACE_CORE_DLL PyTypeObject * ndarrayType()
Return the type object for a numpy.NDArray.
Definition NDArray.cpp:38
Helper class which provides the Collimation Length for SANS instruments.
Converter that takes a Mantid Matrix and converts it into a numpy array.
typename std::remove_const< typename std::remove_reference< T >::type >::type non_const_type
typename boost::mpl::if_c< boost::mpl::and_< std::is_reference< T >, is_matrix< non_const_type > >::value, MatrixRefToNumpyImpl< non_const_type, ConversionPolicy >, MatrixRefToNumpy_Requires_Reference_To_Matrix_Return_Type< T > >::type type
Implements a return value policy that returns a numpy array from a Matrix.
typename std::remove_const< T >::type non_const_type
typename boost::mpl::if_c< is_matrix< non_const_type >::value, MatrixRefToNumpyImpl< non_const_type, Converters::Clone >, MatrixToNumpy_Requires_Matrix_Return_By_Value< T > >::type type
Implements a return value policy that returns a numpy array from a function returning a std::vector b...