NoAlias.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_NOALIAS_H
26 #define EIGEN_NOALIAS_H
27 
28 namespace Eigen {
29 
45 template<typename ExpressionType, template <typename> class StorageBase>
46 class NoAlias
47 {
48  typedef typename ExpressionType::Scalar Scalar;
49  public:
50  NoAlias(ExpressionType& expression) : m_expression(expression) {}
51 
54  template<typename OtherDerived>
55  EIGEN_STRONG_INLINE ExpressionType& operator=(const StorageBase<OtherDerived>& other)
56  { return internal::assign_selector<ExpressionType,OtherDerived,false>::run(m_expression,other.derived()); }
57 
59  template<typename OtherDerived>
60  EIGEN_STRONG_INLINE ExpressionType& operator+=(const StorageBase<OtherDerived>& other)
61  {
62  typedef SelfCwiseBinaryOp<internal::scalar_sum_op<Scalar>, ExpressionType, OtherDerived> SelfAdder;
63  SelfAdder tmp(m_expression);
64  typedef typename internal::nested<OtherDerived>::type OtherDerivedNested;
65  typedef typename internal::remove_all<OtherDerivedNested>::type _OtherDerivedNested;
66  internal::assign_selector<SelfAdder,_OtherDerivedNested,false>::run(tmp,OtherDerivedNested(other.derived()));
67  return m_expression;
68  }
69 
71  template<typename OtherDerived>
72  EIGEN_STRONG_INLINE ExpressionType& operator-=(const StorageBase<OtherDerived>& other)
73  {
74  typedef SelfCwiseBinaryOp<internal::scalar_difference_op<Scalar>, ExpressionType, OtherDerived> SelfAdder;
75  SelfAdder tmp(m_expression);
76  typedef typename internal::nested<OtherDerived>::type OtherDerivedNested;
77  typedef typename internal::remove_all<OtherDerivedNested>::type _OtherDerivedNested;
78  internal::assign_selector<SelfAdder,_OtherDerivedNested,false>::run(tmp,OtherDerivedNested(other.derived()));
79  return m_expression;
80  }
81 
82 #ifndef EIGEN_PARSED_BY_DOXYGEN
83  template<typename ProductDerived, typename Lhs, typename Rhs>
85  { other.derived().addTo(m_expression); return m_expression; }
86 
87  template<typename ProductDerived, typename Lhs, typename Rhs>
88  EIGEN_STRONG_INLINE ExpressionType& operator-=(const ProductBase<ProductDerived, Lhs,Rhs>& other)
89  { other.derived().subTo(m_expression); return m_expression; }
90 
91  template<typename Lhs, typename Rhs, int NestingFlags>
92  EIGEN_STRONG_INLINE ExpressionType& operator+=(const CoeffBasedProduct<Lhs,Rhs,NestingFlags>& other)
93  { return m_expression.derived() += CoeffBasedProduct<Lhs,Rhs,NestByRefBit>(other.lhs(), other.rhs()); }
94 
95  template<typename Lhs, typename Rhs, int NestingFlags>
96  EIGEN_STRONG_INLINE ExpressionType& operator-=(const CoeffBasedProduct<Lhs,Rhs,NestingFlags>& other)
97  { return m_expression.derived() -= CoeffBasedProduct<Lhs,Rhs,NestByRefBit>(other.lhs(), other.rhs()); }
98 #endif
99 
100  protected:
101  ExpressionType& m_expression;
102 };
103 
132 template<typename Derived>
134 {
135  return derived();
136 }
137 
138 } // end namespace Eigen
139 
140 #endif // EIGEN_NOALIAS_H