From 59be5c31242a333502a76b53122f69306382cd6f Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Wed, 19 Dec 2007 08:14:00 +0000 Subject: enforce constness in map(), do only one const_cast, and improve API --- src/Core/Map.h | 34 ++++++++++++++++++++++++++++------ src/Core/MatrixBase.h | 9 ++++++--- 2 files changed, 34 insertions(+), 9 deletions(-) (limited to 'src/Core') diff --git a/src/Core/Map.h b/src/Core/Map.h index 02852a9f6..e8300a7c3 100644 --- a/src/Core/Map.h +++ b/src/Core/Map.h @@ -36,7 +36,7 @@ template class Map static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime, ColsAtCompileTime = MatrixType::ColsAtCompileTime; - Map(Scalar* data, int rows, int cols) : m_data(data), m_rows(rows), m_cols(cols) + Map(const Scalar* data, int rows, int cols) : m_data(data), m_rows(rows), m_cols(cols) { assert(rows > 0 && cols > 0); } @@ -55,22 +55,22 @@ template class Map Scalar& _coeffRef(int row, int col) { - return m_data[row + col * m_rows]; + return const_cast(m_data)[row + col * m_rows]; } protected: - Scalar* m_data; + const Scalar* m_data; int m_rows, m_cols; }; template -Map MatrixBase::map(const Scalar* data, int rows, int cols) +const Map MatrixBase::map(const Scalar* data, int rows, int cols) { return Map(const_cast(data), rows, cols); } template -Map MatrixBase::map(const Scalar* data, int size) +const Map MatrixBase::map(const Scalar* data, int size) { assert(IsVector); if(ColsAtCompileTime == 1) @@ -80,11 +80,33 @@ Map MatrixBase::map(const Scalar* data, int size) } template -Map MatrixBase::map(const Scalar* data) +const Map MatrixBase::map(const Scalar* data) { return Map(const_cast(data), RowsAtCompileTime, ColsAtCompileTime); } +template +Map MatrixBase::map(Scalar* data, int rows, int cols) +{ + return Map(data, rows, cols); +} + +template +Map MatrixBase::map(Scalar* data, int size) +{ + assert(IsVector); + if(ColsAtCompileTime == 1) + return Map(data, size, 1); + else + return Map(data, 1, size); +} + +template +Map MatrixBase::map(Scalar* data) +{ + return Map(data, RowsAtCompileTime, ColsAtCompileTime); +} + template Matrix<_Scalar, _Rows, _Cols> ::Matrix(const Scalar *data, int rows, int cols) diff --git a/src/Core/MatrixBase.h b/src/Core/MatrixBase.h index c37b098f8..83b6fa169 100644 --- a/src/Core/MatrixBase.h +++ b/src/Core/MatrixBase.h @@ -95,9 +95,12 @@ template class MatrixBase diagonal(const OtherDerived& coeffs); DiagonalCoeffs diagonal() const; - static Map map(const Scalar* array, int rows, int cols); - static Map map(const Scalar* array, int size); - static Map map(const Scalar* array); + static const Map map(const Scalar* array, int rows, int cols); + static const Map map(const Scalar* array, int size); + static const Map map(const Scalar* array); + static Map map(Scalar* array, int rows, int cols); + static Map map(Scalar* array, int size); + static Map map(Scalar* array); template bool isApprox( -- cgit v1.2.3