Simbody  3.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MatrixView_.h
Go to the documentation of this file.
1 #ifndef SimTK_SIMMATRIX_MATRIXVIEW_H_
2 #define SimTK_SIMMATRIX_MATRIXVIEW_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKcommon *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2005-13 Stanford University and the Authors. *
13  * Authors: Michael Sherman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
31 namespace SimTK {
32 
33 //==============================================================================
34 // MATRIX VIEW
35 //==============================================================================
43 template <class ELT> class MatrixView_ : public MatrixBase<ELT> {
44  typedef MatrixBase<ELT> Base;
45  typedef typename CNT<ELT>::Scalar S;
46  typedef typename CNT<ELT>::StdNumber StdNumber;
47 public:
48  // Default construction is suppressed.
49  // Uses default destructor.
50 
51  // Create a MatrixView_ handle using a given helper rep.
52  explicit MatrixView_(MatrixHelperRep<S>* hrep) : Base(hrep) {}
53 
54  // Copy constructor is shallow. CAUTION: despite const argument, this preserves writability
55  // if it was present in the source. This is necessary to allow temporary views to be
56  // created and used as lvalues.
59  const_cast<MatrixHelper<S>&>(m.getHelper()),
60  typename MatrixHelper<S>::ShallowCopy()) {}
61 
62  // Copy assignment is deep but not reallocating.
64  Base::operator=(m); return *this;
65  }
66 
67  // Ask for shallow copy
68  MatrixView_(const MatrixHelper<S>& h) : Base(MatrixCommitment(), h, typename MatrixHelper<S>::ShallowCopy()) { }
69  MatrixView_(MatrixHelper<S>& h) : Base(MatrixCommitment(), h, typename MatrixHelper<S>::ShallowCopy()) { }
70 
71  MatrixView_& operator=(const Matrix_<ELT>& v) { Base::operator=(v); return *this; }
72  MatrixView_& operator=(const ELT& e) { Base::operator=(e); return *this; }
73 
74  template <class EE> MatrixView_& operator=(const MatrixBase<EE>& m)
75  { Base::operator=(m); return *this; }
76  template <class EE> MatrixView_& operator+=(const MatrixBase<EE>& m)
77  { Base::operator+=(m); return *this; }
78  template <class EE> MatrixView_& operator-=(const MatrixBase<EE>& m)
79  { Base::operator-=(m); return *this; }
80 
81  MatrixView_& operator*=(const StdNumber& t) { Base::operator*=(t); return *this; }
82  MatrixView_& operator/=(const StdNumber& t) { Base::operator/=(t); return *this; }
83  MatrixView_& operator+=(const ELT& r) { this->updDiag() += r; return *this; }
84  MatrixView_& operator-=(const ELT& r) { this->updDiag() -= r; return *this; }
85 
86  operator const Matrix_<ELT>&() const { return *reinterpret_cast<const Matrix_<ELT>*>(this); }
87  operator Matrix_<ELT>&() { return *reinterpret_cast<Matrix_<ELT>*>(this); }
88 
89 private:
90  // NO DATA MEMBERS ALLOWED
91  MatrixView_(); // default constructor suppressed (what's it a view of?)
92 };
93 
94 
95 
96 } //namespace SimTK
97 
98 #endif // SimTK_SIMMATRIX_MATRIXVIEW_H_