make shameless use of const_cast to reduce code redundancy. This means Eigen2

gives up enforcing constness. I really tried to enforce it, but it really was
much hassle because our expression templates can be lvalues (not only rvalues)
and so much code had to be written twice.
This commit is contained in:
Benoit Jacob 2007-10-15 05:56:21 +00:00
parent f355ef2df0
commit 884a718b0a
20 changed files with 34 additions and 83 deletions

View File

@ -54,8 +54,7 @@ template<typename MatrixType> class Block
EI_INHERIT_ASSIGNMENT_OPERATORS(Block)
private:
Block& _ref() { return *this; }
const Block& _constRef() const { return *this; }
const Block& _ref() const { return *this; }
int _rows() const { return m_endRow - m_startRow + 1; }
int _cols() const { return m_endCol - m_startCol + 1; }

View File

@ -49,8 +49,7 @@ template<typename MatrixType> class Column
EI_INHERIT_ASSIGNMENT_OPERATORS(Column)
private:
Column& _ref() { return *this; }
const Column& _constRef() const { return *this; }
const Column& _ref() const { return *this; }
int _rows() const { return m_matrix.rows(); }
int _cols() const { return 1; }

View File

@ -31,7 +31,7 @@ template<typename MatrixType> class Conjugate
{
public:
typedef typename MatrixType::Scalar Scalar;
typedef typename MatrixType::ConstRef MatRef;
typedef typename MatrixType::Ref MatRef;
friend class Object<Scalar, Conjugate<MatrixType> >;
static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime,
@ -45,8 +45,7 @@ template<typename MatrixType> class Conjugate
EI_INHERIT_ASSIGNMENT_OPERATORS(Conjugate)
private:
Conjugate& _ref() { return *this; }
const Conjugate& _constRef() const { return *this; }
const Conjugate& _ref() const { return *this; }
int _rows() const { return m_matrix.rows(); }
int _cols() const { return m_matrix.cols(); }
@ -63,7 +62,7 @@ template<typename Scalar, typename Derived>
Conjugate<Derived>
Object<Scalar, Derived>::conjugate() const
{
return Conjugate<Derived>(static_cast<const Derived*>(this)->constRef());
return Conjugate<Derived>(static_cast<const Derived*>(this)->ref());
}
#endif // EI_CONJUGATE_H

View File

@ -31,8 +31,8 @@ template<typename Lhs, typename Rhs> class Difference
{
public:
typedef typename Lhs::Scalar Scalar;
typedef typename Lhs::ConstRef LhsRef;
typedef typename Rhs::ConstRef RhsRef;
typedef typename Lhs::Ref LhsRef;
typedef typename Rhs::Ref RhsRef;
friend class Object<Scalar, Difference>;
static const int RowsAtCompileTime = Lhs::RowsAtCompileTime,
@ -51,7 +51,6 @@ template<typename Lhs, typename Rhs> class Difference
private:
const Difference& _ref() const { return *this; }
const Difference& _constRef() const { return *this; }
int _rows() const { return m_lhs.rows(); }
int _cols() const { return m_lhs.cols(); }
@ -69,7 +68,7 @@ template<typename Scalar, typename Derived1, typename Derived2>
Difference<Derived1, Derived2>
operator-(const Object<Scalar, Derived1> &mat1, const Object<Scalar, Derived2> &mat2)
{
return Difference<Derived1, Derived2>(mat1.constRef(), mat2.constRef());
return Difference<Derived1, Derived2>(mat1.ref(), mat2.ref());
}
template<typename Scalar, typename Derived>

View File

@ -77,7 +77,6 @@ Scalar Object<Scalar, Derived>::dot(const OtherDerived& other) const
template<typename Scalar, typename Derived>
typename NumTraits<Scalar>::Real Object<Scalar, Derived>::norm2() const
{
assert(IsVector);
return NumTraits<Scalar>::real(dot(*this));
}

View File

@ -45,7 +45,7 @@ template<typename MatrixType> class FromArray
private:
FromArray& _ref() { return *this; }
const FromArray& _constRef() const { return *this; }
const FromArray& _ref() const { return *this; }
int _rows() const { return m_rows; }
int _cols() const { return m_cols; }

View File

@ -44,7 +44,7 @@ template<typename MatrixType> class Identity
private:
Identity& _ref() { return *this; }
const Identity& _constRef() const { return *this; }
const Identity& _ref() const { return *this; }
int _rows() const { return m_rows; }
int _cols() const { return m_rows; }

View File

@ -36,9 +36,7 @@ class Matrix : public Object<_Scalar, Matrix<_Scalar, _Rows, _Cols> >,
typedef MatrixStorage<_Scalar, _Rows, _Cols> Storage;
typedef _Scalar Scalar;
typedef MatrixRef<Matrix> Ref;
typedef MatrixConstRef<Matrix> ConstRef;
friend class MatrixRef<Matrix>;
friend class MatrixConstRef<Matrix>;
static const int RowsAtCompileTime = _Rows, ColsAtCompileTime = _Cols;
@ -49,8 +47,7 @@ class Matrix : public Object<_Scalar, Matrix<_Scalar, _Rows, _Cols> >,
{ return Storage::m_array; }
private:
Ref _ref() { return Ref(*this); }
ConstRef _constRef() const { return ConstRef(*this); }
Ref _ref() const { return Ref(*this); }
const Scalar& _read(int row, int col) const
{

View File

@ -26,31 +26,6 @@
#ifndef EI_MATRIXREF_H
#define EI_MATRIXREF_H
template<typename MatrixType> class MatrixConstRef
: public Object<typename MatrixType::Scalar, MatrixConstRef<MatrixType> >
{
public:
typedef typename MatrixType::Scalar Scalar;
friend class Object<Scalar, MatrixConstRef>;
MatrixConstRef(const MatrixType& matrix) : m_matrix(matrix) {}
MatrixConstRef(const MatrixConstRef& other) : m_matrix(other.m_matrix) {}
~MatrixConstRef() {}
EI_INHERIT_ASSIGNMENT_OPERATORS(MatrixConstRef)
private:
int _rows() const { return m_matrix.rows(); }
int _cols() const { return m_matrix.cols(); }
const Scalar& _read(int row, int col) const
{
return m_matrix._read(row, col);
}
const MatrixType& m_matrix;
};
template<typename MatrixType> class MatrixRef
: public Object<typename MatrixType::Scalar, MatrixRef<MatrixType> >
{
@ -58,7 +33,7 @@ template<typename MatrixType> class MatrixRef
typedef typename MatrixType::Scalar Scalar;
friend class Object<Scalar, MatrixRef>;
MatrixRef(MatrixType& matrix) : m_matrix(matrix) {}
MatrixRef(const MatrixType& matrix) : m_matrix(*const_cast<MatrixType*>(&matrix)) {}
MatrixRef(const MatrixRef& other) : m_matrix(other.m_matrix) {}
~MatrixRef() {}

View File

@ -54,7 +54,7 @@ template<typename MatrixType> class Minor
private:
Minor& _ref() { return *this; }
const Minor& _constRef() const { return *this; }
const Minor& _ref() const { return *this; }
int _rows() const { return m_matrix.rows() - 1; }
int _cols() const { return m_matrix.cols() - 1; }

View File

@ -55,18 +55,14 @@ template<typename Scalar, typename Derived> class Object
static const bool IsVector = RowsAtCompileTime == 1 || ColsAtCompileTime == 1;
typedef typename ForwardDecl<Derived>::Ref Ref;
typedef typename ForwardDecl<Derived>::ConstRef ConstRef;
typedef typename NumTraits<Scalar>::Real RealScalar;
int rows() const { return static_cast<const Derived *>(this)->_rows(); }
int cols() const { return static_cast<const Derived *>(this)->_cols(); }
int size() const { return rows() * cols(); }
Ref ref()
{ return static_cast<Derived *>(this)->_ref(); }
ConstRef constRef() const
{ return static_cast<const Derived *>(this)->_constRef(); }
Ref ref() const
{ return static_cast<const Derived *>(this)->_ref(); }
Scalar& write(int row, int col)
{

View File

@ -31,7 +31,7 @@ template<typename MatrixType> class Opposite
{
public:
typedef typename MatrixType::Scalar Scalar;
typedef typename MatrixType::ConstRef MatRef;
typedef typename MatrixType::Ref MatRef;
friend class Object<Scalar, Opposite<MatrixType> >;
static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime,
@ -45,8 +45,7 @@ template<typename MatrixType> class Opposite
EI_INHERIT_ASSIGNMENT_OPERATORS(Opposite)
private:
Opposite& _ref() { return *this; }
const Opposite& _constRef() const { return *this; }
const Opposite& _ref() const { return *this; }
int _rows() const { return m_matrix.rows(); }
int _cols() const { return m_matrix.cols(); }
@ -63,7 +62,7 @@ template<typename Scalar, typename Derived>
Opposite<Derived>
Object<Scalar, Derived>::operator-() const
{
return Opposite<Derived>(static_cast<const Derived*>(this)->constRef());
return Opposite<Derived>(static_cast<const Derived*>(this)->ref());
}
#endif // EI_OPPOSITE_H

View File

@ -66,8 +66,8 @@ template<typename Lhs, typename Rhs> class Product
{
public:
typedef typename Lhs::Scalar Scalar;
typedef typename Lhs::ConstRef LhsRef;
typedef typename Rhs::ConstRef RhsRef;
typedef typename Lhs::Ref LhsRef;
typedef typename Rhs::Ref RhsRef;
friend class Object<Scalar, Product>;
static const int RowsAtCompileTime = Lhs::RowsAtCompileTime,
@ -86,7 +86,6 @@ template<typename Lhs, typename Rhs> class Product
private:
const Product& _ref() const { return *this; }
const Product& _constRef() const { return *this; }
int _rows() const { return m_lhs.rows(); }
int _cols() const { return m_rhs.cols(); }
@ -115,7 +114,7 @@ template<typename OtherDerived>
Product<Derived, OtherDerived>
Object<Scalar, Derived>::lazyProduct(const Object<Scalar, OtherDerived> &other) const
{
return Product<Derived, OtherDerived>(constRef(), other.constRef());
return Product<Derived, OtherDerived>(ref(), other.ref());
}
template<typename Scalar, typename Derived1, typename Derived2>

View File

@ -42,8 +42,7 @@ template<typename MatrixType> class Random
}
private:
Random& _ref() { return *this; }
const Random& _constRef() const { return *this; }
const Random& _ref() const { return *this; }
int _rows() const { return m_rows; }
int _cols() const { return m_cols; }

View File

@ -55,8 +55,7 @@ template<typename MatrixType> class Row
EI_INHERIT_ASSIGNMENT_OPERATORS(Row)
private:
Row& _ref() { return *this; }
const Row& _constRef() const { return *this; }
const Row& _ref() const { return *this; }
int _rows() const { return 1; }
int _cols() const { return m_matrix.cols(); }

View File

@ -31,7 +31,7 @@ template<typename MatrixType> class ScalarMultiple
{
public:
typedef typename MatrixType::Scalar Scalar;
typedef typename MatrixType::ConstRef MatRef;
typedef typename MatrixType::Ref MatRef;
friend class Object<typename MatrixType::Scalar, ScalarMultiple<MatrixType> >;
static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime,
@ -47,7 +47,6 @@ template<typename MatrixType> class ScalarMultiple
private:
const ScalarMultiple& _ref() const { return *this; }
const ScalarMultiple& _constRef() const { return *this; }
int _rows() const { return m_matrix.rows(); }
int _cols() const { return m_matrix.cols(); }
@ -67,7 +66,7 @@ ScalarMultiple<Derived> \
operator*(const Object<Scalar, Derived>& matrix, \
OtherScalar scalar) \
{ \
return ScalarMultiple<Derived>(matrix.constRef(), scalar); \
return ScalarMultiple<Derived>(matrix.ref(), scalar); \
} \
\
template<typename Scalar, typename Derived> \
@ -75,7 +74,7 @@ ScalarMultiple<Derived> \
operator*(OtherScalar scalar, \
const Object<Scalar, Derived>& matrix) \
{ \
return ScalarMultiple<Derived>(matrix.constRef(), scalar); \
return ScalarMultiple<Derived>(matrix.ref(), scalar); \
} \
\
template<typename Scalar, typename Derived> \

View File

@ -31,8 +31,8 @@ template<typename Lhs, typename Rhs> class Sum
{
public:
typedef typename Lhs::Scalar Scalar;
typedef typename Lhs::ConstRef LhsRef;
typedef typename Rhs::ConstRef RhsRef;
typedef typename Lhs::Ref LhsRef;
typedef typename Rhs::Ref RhsRef;
friend class Object<Scalar, Sum>;
static const int RowsAtCompileTime = Lhs::RowsAtCompileTime,
@ -50,9 +50,7 @@ template<typename Lhs, typename Rhs> class Sum
EI_INHERIT_ASSIGNMENT_OPERATORS(Sum)
private:
const Sum& _ref() const { return *this; }
const Sum& _constRef() const { return *this; }
int _rows() const { return m_lhs.rows(); }
int _cols() const { return m_lhs.cols(); }
@ -70,7 +68,7 @@ template<typename Scalar, typename Derived1, typename Derived2>
Sum<Derived1, Derived2>
operator+(const Object<Scalar, Derived1> &mat1, const Object<Scalar, Derived2> &mat2)
{
return Sum<Derived1, Derived2>(mat1.constRef(), mat2.constRef());
return Sum<Derived1, Derived2>(mat1.ref(), mat2.ref());
}
template<typename Scalar, typename Derived>

View File

@ -45,8 +45,7 @@ template<typename MatrixType> class Transpose
EI_INHERIT_ASSIGNMENT_OPERATORS(Transpose)
private:
Transpose& _ref() { return *this; }
const Transpose& _constRef() const { return *this; }
const Transpose& _ref() const { return *this; }
int _rows() const { return m_matrix.cols(); }
int _cols() const { return m_matrix.rows(); }

View File

@ -43,7 +43,6 @@ using Eigen::Matrix;
//forward declarations
template<typename _Scalar, int _Rows, int _Cols> class Matrix;
template<typename MatrixType> class MatrixRef;
template<typename MatrixType> class MatrixConstRef;
template<typename MatrixType> class Row;
template<typename MatrixType> class Column;
template<typename MatrixType> class Minor;
@ -64,14 +63,12 @@ template<typename MatrixType> class FromArray;
template<typename T> struct ForwardDecl
{
typedef T Ref;
typedef T ConstRef;
};
template<typename _Scalar, int _Rows, int _Cols>
struct ForwardDecl<Matrix<_Scalar, _Rows, _Cols> >
{
typedef MatrixRef<Matrix<_Scalar, _Rows, _Cols> > Ref;
typedef MatrixConstRef<Matrix<_Scalar, _Rows, _Cols> > ConstRef;
};
const int Dynamic = -1;

View File

@ -42,8 +42,7 @@ template<typename MatrixType> class Zero
}
private:
Zero& _ref() { return *this; }
const Zero& _constRef() const { return *this; }
const Zero& _ref() const { return *this; }
int _rows() const { return m_rows; }
int _cols() const { return m_cols; }