Mantid
Loading...
Searching...
No Matches
VectorToNumpy.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 <boost/mpl/and.hpp>
15#include <boost/mpl/if.hpp>
16#include <type_traits>
17#include <vector>
18
19namespace Mantid {
20namespace PythonInterface {
21namespace Policies {
22
23namespace // anonymous
24{
25//-----------------------------------------------------------------------
26// MPL helper structs
27//-----------------------------------------------------------------------
30template <typename T> struct is_std_vector : std::false_type {};
31
34template <typename T> struct is_std_vector<std::vector<T>> : std::true_type {};
35
36//-----------------------------------------------------------------------
37// VectorRefToNumpyImpl - Policy for reference returns
38//-----------------------------------------------------------------------
42template <typename VectorType, typename ConversionPolicy> struct VectorRefToNumpyImpl {
43 inline PyObject *operator()(const VectorType &cvector) const {
45 }
46
47 inline PyTypeObject const *get_pytype() const { return ndarrayType(); }
48};
49
50template <typename T> struct VectorRefToNumpy_Requires_Reference_To_StdVector_Return_Type {};
51} // namespace
52
63template <typename ConversionPolicy> struct VectorRefToNumpy {
64 // The boost::python framework calls return_value_policy::apply<T>::type
65 template <class T> struct apply {
66 // Typedef that removes and const or reference qualifiers from the type
67 using non_const_type = typename std::remove_const<typename std::remove_reference<T>::type>::type;
68 // MPL compile-time check that T is a reference to a std::vector
69 using type = typename boost::mpl::if_c<is_std_vector<non_const_type>::value,
70 VectorRefToNumpyImpl<non_const_type, ConversionPolicy>,
71 VectorRefToNumpy_Requires_Reference_To_StdVector_Return_Type<T>>::type;
72 };
73};
74
75//-----------------------------------------------------------------------
76// VectorToNumpy return_value_policy
77//-----------------------------------------------------------------------
78namespace {
83template <typename VectorType> struct VectorToNumpyImpl {
84 inline PyObject *operator()(const VectorType &cvector) const {
86 }
87
88 inline PyTypeObject const *get_pytype() const { return ndarrayType(); }
89};
90
91template <typename T> struct VectorToNumpy_Requires_StdVector_Return {};
92} // namespace
93
101 // The boost::python framework calls return_value_policy::apply<T>::type
102 template <class T> struct apply {
103 // Typedef that removes any const from the type
104 using non_const_type = typename std::remove_const<typename std::remove_reference<T>::type>::type;
105 // MPL compile-time check that T is a std::vector
106 using type = typename boost::mpl::if_c<is_std_vector<non_const_type>::value,
107 VectorRefToNumpyImpl<non_const_type, Converters::Clone>,
108 VectorToNumpy_Requires_StdVector_Return<T>>::type;
109 };
110};
111} // namespace Policies
112} // namespace PythonInterface
113} // namespace Mantid
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.
STL namespace.
Converter that takes a std::vector and converts it into a flat numpy array.
typename std::remove_const< typename std::remove_reference< T >::type >::type non_const_type
Definition: VectorToNumpy.h:67
typename boost::mpl::if_c< is_std_vector< non_const_type >::value, VectorRefToNumpyImpl< non_const_type, ConversionPolicy >, VectorRefToNumpy_Requires_Reference_To_StdVector_Return_Type< T > >::type type
Definition: VectorToNumpy.h:71
Implements a return value policy that returns a numpy array from a rerence to a std::vector.
Definition: VectorToNumpy.h:63
typename boost::mpl::if_c< is_std_vector< non_const_type >::value, VectorRefToNumpyImpl< non_const_type, Converters::Clone >, VectorToNumpy_Requires_StdVector_Return< T > >::type type
typename std::remove_const< typename std::remove_reference< T >::type >::type non_const_type
Implements a return value policy that returns a numpy array from a function returning a std::vector b...