-
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. Overhaul Python->C++ property setters.
This aligns each handler with the C type rather than the python type which makes conversions into the C type easier.
- Loading branch information
1 parent
68b54e2
commit a9ebcae
Showing
31 changed files
with
1,094 additions
and
512 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
54 changes: 54 additions & 0 deletions
54
...rk/PythonInterface/inc/MantidPythonInterface/kernel/Converters/NDArrayToVectorConverter.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,54 @@ | ||
#ifndef MANTID_PYTHONINTERFACE_NDARRAYTOVECTORCONVERTER_H_ | ||
#define MANTID_PYTHONINTERFACE_NDARRAYTOVECTORCONVERTER_H_ | ||
/* | ||
Copyright © 2011 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 <boost/python/object.hpp> | ||
#include <vector> | ||
|
||
namespace Mantid | ||
{ | ||
namespace PythonInterface | ||
{ | ||
|
||
/** | ||
* Converts a Python sequence type to a C++ std::vector, where the vector element | ||
* type is defined by the template type | ||
*/ | ||
template <typename DestElementType> | ||
struct NDArrayToVectorConverter | ||
{ | ||
/// Constructor | ||
NDArrayToVectorConverter(const boost::python::object & value); | ||
/// Do the conversion | ||
const std::vector<DestElementType> operator()(); | ||
private: | ||
/// Check the array is of the correct type and coerce it if not | ||
void typeCheck(); | ||
/// Pointer to ndarray object | ||
boost::python::object m_arr; | ||
}; | ||
|
||
} | ||
} | ||
|
||
#endif /* MANTID_PYTHONINTERFACE_NDARRAYTOVECTORCONVERTER_H_ */ |
76 changes: 76 additions & 0 deletions
76
.../Framework/PythonInterface/inc/MantidPythonInterface/kernel/Converters/NDArrayTypeIndex.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,76 @@ | ||
#ifndef MANTID_PYTHONINTERFACE_NDARRAYTYPEINDEX_H_ | ||
#define MANTID_PYTHONINTERFACE_NDARRAYTYPEINDEX_H_ | ||
/** | ||
Copyright © 2011 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 <boost/python/detail/prefix.hpp> // Safe include of Python.h | ||
|
||
// See http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PY_ARRAY_UNIQUE_SYMBOL | ||
#define PY_ARRAY_UNIQUE_SYMBOL KERNEL_ARRAY_API | ||
#define NO_IMPORT_ARRAY | ||
#include <numpy/ndarrayobject.h> | ||
|
||
namespace Mantid | ||
{ | ||
namespace PythonInterface | ||
{ | ||
namespace Numpy | ||
{ | ||
|
||
/** | ||
* Defines a mapping between C++ type given by | ||
* the template parameter and numpy type enum | ||
* NPY_TYPES. | ||
* | ||
* There is no general definition, only specialized | ||
* versions are defined. Each specialization should | ||
* contain a static const NPY_TYPES definition giving | ||
* the result of the mapping | ||
*/ | ||
template<typename T> | ||
struct NDArrayTypeIndex | ||
{}; | ||
|
||
/// Macro to define mappings between the CType and Numpy enum | ||
#define DEFINE_TYPE_MAPPING(CType, NDTypeNum) \ | ||
template<>\ | ||
struct NDArrayTypeIndex<CType>\ | ||
{\ | ||
static int typenum() { return NDTypeNum; }\ | ||
}; | ||
|
||
DEFINE_TYPE_MAPPING(int16_t, NPY_INT16); | ||
DEFINE_TYPE_MAPPING(uint16_t, NPY_UINT16); | ||
DEFINE_TYPE_MAPPING(int32_t, NPY_INT32); | ||
DEFINE_TYPE_MAPPING(uint32_t, NPY_UINT32); | ||
DEFINE_TYPE_MAPPING(int64_t, NPY_INT64); | ||
DEFINE_TYPE_MAPPING(uint64_t, NPY_UINT64); | ||
DEFINE_TYPE_MAPPING(double, NPY_DOUBLE); | ||
// Not needed outside here | ||
#undef DEFINE_TYPE_MAPPING | ||
|
||
} | ||
} | ||
} | ||
|
||
|
||
#endif /* MANTID_PYTHONINTERFACE_NDARRAYTYPEINDEX_H_*/ |
121 changes: 121 additions & 0 deletions
121
...PythonInterface/inc/MantidPythonInterface/kernel/Converters/PySequenceToVectorConverter.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,121 @@ | ||
#ifndef MANTID_PYTHONINTERFACE_PYSEQUENCETOVECTORCONVERTER_H_ | ||
#define MANTID_PYTHONINTERFACE_PYSEQUENCETOVECTORCONVERTER_H_ | ||
/* | ||
Copyright © 2011 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 <boost/python/object.hpp> | ||
#include <boost/python/extract.hpp> | ||
#include <vector> | ||
|
||
namespace Mantid | ||
{ | ||
namespace PythonInterface | ||
{ | ||
namespace // <anonymous> | ||
{ | ||
/** | ||
* Extract a C type from a Python object. | ||
*/ | ||
template<typename CType> | ||
struct ExtractCType | ||
{ | ||
/** | ||
* Calls extract on the Python object using the template type | ||
* @param value A pointer to the Python object | ||
* @return The value as a C type | ||
*/ | ||
inline CType operator()(PyObject* value) | ||
{ | ||
return boost::python::extract<CType>(value); | ||
} | ||
}; | ||
|
||
/** | ||
* Template specialization to convert a Python object to a C++ std::string | ||
*/ | ||
template<> | ||
struct ExtractCType<std::string> | ||
{ | ||
/** | ||
* Uses boost lexical cast to convert the type to a string | ||
* @param value A pointer to the Python object | ||
* @return The value as a C type | ||
*/ | ||
inline std::string operator()(PyObject *value) | ||
{ | ||
return boost::python::extract<std::string>(PyObject_Str(value)); | ||
} | ||
}; | ||
|
||
} //end <anonymous> | ||
|
||
/** | ||
* Converts a Python sequence type to a C++ std::vector, where the element | ||
* type is defined by the template type | ||
*/ | ||
template <typename DestElementType> | ||
struct PySequenceToVectorConverter | ||
{ | ||
PySequenceToVectorConverter(const boost::python::object & value) | ||
: m_obj(value.ptr()) | ||
{ | ||
check(value); | ||
} | ||
|
||
/** | ||
* Converts the Python object to a C++ vector | ||
* @return A std::vector<ElementType> containing the values | ||
* from the Python sequence | ||
*/ | ||
inline const std::vector<DestElementType> operator()() | ||
{ | ||
Py_ssize_t length = PySequence_Size(m_obj); | ||
std::vector<DestElementType> cvector(length); | ||
if(length == 0) return cvector; | ||
ExtractCType<DestElementType> elementConverter; | ||
for( Py_ssize_t i = 0; i < length; ++i ) | ||
{ | ||
PyObject *item = PySequence_Fast_GET_ITEM(m_obj, i); | ||
DestElementType element = elementConverter(item); | ||
cvector[i] = element; | ||
} | ||
return cvector; | ||
} | ||
|
||
private: | ||
inline void check(const boost::python::object & obj) | ||
{ | ||
if( !PySequence_Check(obj.ptr()) ) | ||
{ | ||
throw std::invalid_argument(std::string("PySequenceToVectorConverter expects Python sequence type, found ") | ||
+ obj.ptr()->ob_type->tp_name); | ||
} | ||
} | ||
/// Python object to convert | ||
PyObject *m_obj; | ||
|
||
}; | ||
|
||
} | ||
} | ||
|
||
|
||
#endif /* MANTID_PYTHONINTERFACE_PYSEQUENCETOVECTORCONVERTER_H_ */ |
61 changes: 0 additions & 61 deletions
61
Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/NumpyTypeHandler.h
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.