From 24f8fdeb4635d0f81bfb87a7eb049e8ce72346f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20S=C3=A1nchez?= Date: Thu, 14 Mar 2024 21:04:49 +0000 Subject: [PATCH] Fix CwiseUnaryView const access (Attempt 2). --- Eigen/src/Core/CwiseUnaryView.h | 86 +++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 35 deletions(-) diff --git a/Eigen/src/Core/CwiseUnaryView.h b/Eigen/src/Core/CwiseUnaryView.h index f1653c87a..6bd838b4d 100644 --- a/Eigen/src/Core/CwiseUnaryView.h +++ b/Eigen/src/Core/CwiseUnaryView.h @@ -20,9 +20,7 @@ template struct traits > : traits { typedef typename result_of::Scalar&)>::type1 ScalarRef; static_assert(std::is_reference::value, "Views must return a reference type."); - typedef remove_all_t MutableScalar; - // Ensure const matrices stay const. - typedef std::conditional_t::value, const MutableScalar, MutableScalar> Scalar; + typedef remove_all_t Scalar; typedef typename MatrixType::Nested MatrixTypeNested; typedef remove_all_t MatrixTypeNested_; enum { @@ -48,11 +46,13 @@ struct traits > : traits +template ::value> class CwiseUnaryViewImpl; +} // namespace internal + /** \class CwiseUnaryView * \ingroup Core_Module * @@ -67,11 +67,11 @@ class CwiseUnaryViewImpl; * \sa MatrixBase::unaryViewExpr(const CustomUnaryOp &) const, class CwiseUnaryOp */ template -class CwiseUnaryView - : public CwiseUnaryViewImpl::StorageKind> { +class CwiseUnaryView : public internal::CwiseUnaryViewImpl::StorageKind> { public: - typedef typename CwiseUnaryViewImpl::StorageKind>::Base Base; + typedef typename internal::CwiseUnaryViewImpl::StorageKind>::Base Base; EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseUnaryView) typedef typename internal::ref_selector::non_const_type MatrixTypeNested; typedef internal::remove_all_t NestedExpression; @@ -98,40 +98,63 @@ class CwiseUnaryView ViewOp m_functor; }; +namespace internal { + // Generic API dispatcher -template -class CwiseUnaryViewImpl : public internal::generic_xpr_base >::type { +template +class CwiseUnaryViewImpl : public generic_xpr_base >::type { public: - typedef typename internal::generic_xpr_base >::type Base; + typedef typename generic_xpr_base >::type Base; }; template -class CwiseUnaryViewImpl - : public internal::dense_xpr_base >::type { +class CwiseUnaryViewImpl + : public dense_xpr_base >::type { public: typedef CwiseUnaryView Derived; - typedef typename internal::dense_xpr_base >::type Base; - + typedef typename dense_xpr_base >::type Base; EIGEN_DENSE_PUBLIC_INTERFACE(Derived) EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryViewImpl) - EIGEN_DEVICE_FUNC inline Scalar* data() { return &(this->coeffRef(0)); } EIGEN_DEVICE_FUNC inline const Scalar* data() const { return &(this->coeffRef(0)); } EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index innerStride() const { - return StrideType::InnerStrideAtCompileTime != 0 - ? int(StrideType::InnerStrideAtCompileTime) - : derived().nestedExpression().innerStride() * sizeof(typename internal::traits::Scalar) / - sizeof(Scalar); + return StrideType::InnerStrideAtCompileTime != 0 ? int(StrideType::InnerStrideAtCompileTime) + : derived().nestedExpression().innerStride() * + sizeof(typename traits::Scalar) / sizeof(Scalar); } EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index outerStride() const { - return StrideType::OuterStrideAtCompileTime != 0 - ? int(StrideType::OuterStrideAtCompileTime) - : derived().nestedExpression().outerStride() * sizeof(typename internal::traits::Scalar) / - sizeof(Scalar); + return StrideType::OuterStrideAtCompileTime != 0 ? int(StrideType::OuterStrideAtCompileTime) + : derived().nestedExpression().outerStride() * + sizeof(typename traits::Scalar) / sizeof(Scalar); } + protected: + EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(CwiseUnaryViewImpl) + + // Allow const access to coeffRef for the case of direct access being enabled. + EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index index) const { + return internal::evaluator(derived()).coeffRef(index); + } + + EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index row, Index col) const { + return internal::evaluator(derived()).coeffRef(row, col); + } +}; + +template +class CwiseUnaryViewImpl + : public CwiseUnaryViewImpl { + public: + typedef CwiseUnaryViewImpl Base; + typedef CwiseUnaryView Derived; + EIGEN_DENSE_PUBLIC_INTERFACE(Derived) + EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryViewImpl) + + using Base::data; + EIGEN_DEVICE_FUNC inline Scalar* data() { return &(this->coeffRef(0)); } + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index row, Index col) { return internal::evaluator(derived()).coeffRef(row, col); } @@ -142,17 +165,10 @@ class CwiseUnaryViewImpl protected: EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(CwiseUnaryViewImpl) - - // Allow const access to coeffRef for the case of direct access being enabled. - EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index index) const { - return const_cast(this)->coeffRef(index); - } - - EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index row, Index col) const { - return const_cast(this)->coeffRef(row, col); - } }; -} // end namespace Eigen +} // namespace internal + +} // namespace Eigen #endif // EIGEN_CWISE_UNARY_VIEW_H