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