-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #4399. return_value_policy for creation of numpy wrappers
The policy avoids have to have a useless intermediary wrapping function and creates code that is much more resuable
- Loading branch information
1 parent
12174a4
commit c94b713
Showing
7 changed files
with
171 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
...antid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Policies/VectorToNumpy.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#ifndef MANTID_PYTHONINTERFACE_VECTORTONUMPY_H_ | ||
#define MANTID_PYTHONINTERFACE_VECTORTONUMPY_H_ | ||
/** | ||
Copyright © 2012 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory | ||
This file is part of Mantid. | ||
Mantid is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. | ||
Mantid is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid> | ||
Code Documentation is available at: <http://doxygen.mantidproject.org> | ||
*/ | ||
#include "MantidKernel/System.h" | ||
#include "MantidPythonInterface/kernel/NumpyConverters.h" | ||
//#include <boost/mpl/if.hpp> | ||
//#include <boost/mpl/or.hpp> | ||
//#include <boost/type_traits/is_convertible.hpp> | ||
|
||
#include <vector> | ||
|
||
namespace Mantid | ||
{ | ||
namespace PythonInterface | ||
{ | ||
namespace Policies | ||
{ | ||
//----------------------------------------------------------------------- | ||
// Conversion Policies | ||
//----------------------------------------------------------------------- | ||
|
||
/** | ||
* WrapReadOnly is a policy for VectorToPython | ||
* to wrap the vector in a read-only numpy array | ||
* that looks at the original data. No copy is performed | ||
*/ | ||
|
||
struct WrapReadOnly | ||
{ | ||
template<typename VectorType> | ||
struct apply | ||
{ | ||
/** | ||
* Returns a read-only Numpy array wrapped around an existing vector | ||
* @param cvector | ||
* @return | ||
*/ | ||
static PyObject * create(const VectorType & cvector) | ||
{ | ||
return Numpy::wrapWithReadOnlyNumpy(cvector); | ||
} | ||
}; | ||
}; | ||
|
||
/** | ||
* WrapReadWrite is a policy for VectorToPython | ||
* to wrap the vector in a read-write numpy array | ||
* that looks at the original data. No copy is performed | ||
*/ | ||
struct WrapReadWrite | ||
{ | ||
template<typename VectorType> | ||
struct apply | ||
{ | ||
/** | ||
* Returns a read-write Numpy array wrapped around an existing vector | ||
* @param cvector | ||
* @return | ||
*/ | ||
static PyObject * create(const VectorType & cvector) | ||
{ | ||
return Numpy::wrapWithNumpy(cvector); | ||
} | ||
}; | ||
}; | ||
|
||
namespace // anonymous | ||
{ | ||
/** | ||
* Helper struct that implements the conversion | ||
* policy. | ||
*/ | ||
template<typename VectorType, typename ConversionPolicy> | ||
struct ConvertVectorToNDArray | ||
{ | ||
inline PyObject * operator()(const VectorType & cvector) const | ||
{ | ||
typedef typename ConversionPolicy::template apply<VectorType> policy; | ||
return policy::create(cvector); | ||
} | ||
|
||
inline PyTypeObject const* get_pytype() const | ||
{ | ||
return Numpy::getNDArrayType(); | ||
} | ||
}; | ||
} | ||
|
||
//----------------------------------------------------------------------- | ||
// return_value_policy | ||
//----------------------------------------------------------------------- | ||
/** | ||
* Implements a return value policy that | ||
* returns a numpy array from a std::vector | ||
* | ||
* The type of conversion is specified by a policy: | ||
* (1) WrapReadOnly - Creates a read-only array around the original data (no copy is performed) | ||
* (2) WrapReadWrite - Creates a read-write array around the original data (no copy is performed) | ||
*/ | ||
template<typename ConversionPolicy> | ||
struct VectorToNumpy | ||
{ | ||
template <class T> | ||
struct apply | ||
{ | ||
typedef ConvertVectorToNDArray<T, ConversionPolicy> type; | ||
}; | ||
}; | ||
|
||
} | ||
} | ||
} | ||
|
||
#endif // MANTID_PYTHONINTERFACE_VECTORTONUMPY_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters