From 519d79c029701f3e4820a960ef156109e6253ed0 Mon Sep 17 00:00:00 2001 From: Aleks Novikov Date: Fri, 18 Oct 2024 14:47:13 +0200 Subject: [PATCH 1/2] Choose access level in PyArrayWrapper based on the constness of given array reference --- src/python/PyArray.hpp | 43 ++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/src/python/PyArray.hpp b/src/python/PyArray.hpp index 2abef303..ef535d3b 100644 --- a/src/python/PyArray.hpp +++ b/src/python/PyArray.hpp @@ -137,15 +137,18 @@ template< typename T, class PyArrayWrapper final : public PyArrayWrapperBase { public: - /** * @brief Constructor. * @param array The array to wrap. + * @param accessLevel The access level (see PyModify). */ - PyArrayWrapper( Array< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE > & array ): - PyArrayWrapperBase( ), + PyArrayWrapper( Array< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE > & array, + int accessLevel = static_cast< int >( LvArray::python::PyModify::READ_ONLY )): + PyArrayWrapperBase(), m_array( array ) - {} + { + setAccessLevel(accessLevel, static_cast(LvArray::MemorySpace::host)); + } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// virtual ~PyArrayWrapper() = default; @@ -240,7 +243,7 @@ PyObject * create( std::unique_ptr< internal::PyArrayWrapperBase > && array ); } // namespace internal /** - * @brief Create a Python object corresponding to @c array. + * @brief Create a MODIFIABLE Python object corresponding to @c array. * @tparam T The type of values in the array. * @tparam NDIM The dimensionality of the array. * @tparam PERM The permutation of the array. @@ -251,16 +254,32 @@ PyObject * create( std::unique_ptr< internal::PyArrayWrapperBase > && array ); * @note The returned Python object holds a reference to @c array, you must ensure * the reference remains valid throughout the lifetime of the object. */ -template< typename T, - int NDIM, - typename PERM, - typename INDEX_TYPE, - template< typename > class BUFFER_TYPE > -std::enable_if_t< internal::canExportToNumpy< T > || ( std::is_same< T, std::string >::value && NDIM == 1 ), PyObject * > +template< typename T, int NDIM, typename PERM, typename INDEX_TYPE, template< typename > class BUFFER_TYPE > +std::enable_if_t< internal::canExportToNumpy< T > || (std::is_same< T, std::string >::value && NDIM == 1 ), PyObject * > create( Array< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE > & array ) { using WrapperType = internal::PyArrayWrapper< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE >; - return internal::create( std::make_unique< WrapperType >( array ) ); + return internal::create( std::make_unique< WrapperType >( array, static_cast(LvArray::python::PyModify::MODIFIABLE) ) ); +} + +/** + * @brief Create a READ_ONLY Python object corresponding to @c array. + * @tparam T The type of values in the array. + * @tparam NDIM The dimensionality of the array. + * @tparam PERM The permutation of the array. + * @tparam INDEX_TYPE The index type of the array. + * @tparam BUFFER_TYPE The buffer type of the array. + * @param array The Array to export to Python. + * @return A Python object corresponding to @c array. + * @note The returned Python object holds a reference to @c array, you must ensure + * the reference remains valid throughout the lifetime of the object. + */ +template< typename T, int NDIM, typename PERM, typename INDEX_TYPE, template< typename > class BUFFER_TYPE > +std::enable_if_t< internal::canExportToNumpy< T > || (std::is_same< T, std::string >::value && NDIM == 1 ), PyObject * > +create( Array< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE > const & array ) +{ + using WrapperType = internal::PyArrayWrapper< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE >; + return internal::create( std::make_unique< WrapperType >( const_cast< Array< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE > & >( array ), static_cast(LvArray::python::PyModify::READ_ONLY) ) ); } /** From 3498a2c236ce23adcb36e3c75a9612a7be0e27eb Mon Sep 17 00:00:00 2001 From: Aleks Novikov Date: Thu, 24 Oct 2024 10:13:43 +0200 Subject: [PATCH 2/2] Uncrustify formatting --- src/python/PyArray.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/python/PyArray.hpp b/src/python/PyArray.hpp index ef535d3b..1a5108f6 100644 --- a/src/python/PyArray.hpp +++ b/src/python/PyArray.hpp @@ -142,12 +142,12 @@ class PyArrayWrapper final : public PyArrayWrapperBase * @param array The array to wrap. * @param accessLevel The access level (see PyModify). */ - PyArrayWrapper( Array< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE > & array, + PyArrayWrapper( Array< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE > & array, int accessLevel = static_cast< int >( LvArray::python::PyModify::READ_ONLY )): PyArrayWrapperBase(), m_array( array ) { - setAccessLevel(accessLevel, static_cast(LvArray::MemorySpace::host)); + setAccessLevel( accessLevel, static_cast< int >(LvArray::MemorySpace::host)); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -259,7 +259,7 @@ std::enable_if_t< internal::canExportToNumpy< T > || (std::is_same< T, std::stri create( Array< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE > & array ) { using WrapperType = internal::PyArrayWrapper< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE >; - return internal::create( std::make_unique< WrapperType >( array, static_cast(LvArray::python::PyModify::MODIFIABLE) ) ); + return internal::create( std::make_unique< WrapperType >( array, static_cast< int >(LvArray::python::PyModify::MODIFIABLE) ) ); } /** @@ -279,7 +279,7 @@ std::enable_if_t< internal::canExportToNumpy< T > || (std::is_same< T, std::stri create( Array< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE > const & array ) { using WrapperType = internal::PyArrayWrapper< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE >; - return internal::create( std::make_unique< WrapperType >( const_cast< Array< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE > & >( array ), static_cast(LvArray::python::PyModify::READ_ONLY) ) ); + return internal::create( std::make_unique< WrapperType >( const_cast< Array< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE > & >( array ), static_cast< int >(LvArray::python::PyModify::READ_ONLY) ) ); } /**