1 #ifndef SimTK_SIMMATRIX_VECTORVIEW_H_
2 #define SimTK_SIMMATRIX_VECTORVIEW_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 
27 /** @file
28 Define the SimTK::VectorView_ class that is part of Simbody's BigMatrix
29 toolset. **/
30 
31 namespace SimTK {
32 
33 //==============================================================================
34 //                                VECTOR VIEW
35 //==============================================================================
36 /** @brief (Advanced) This class is identical to Vector_ except that it has
37 shallow (reference) copy and assignment semantics.
38 
39 Despite the name, this may be an owner if a Vector_ is recast to a %VectorView_.
40 However, there are no owner constructors for %VectorView_.
41 @see Vector_, VectorBase, MatrixView_ **/
42 template <class ELT> class VectorView_ : public VectorBase<ELT> {
43     typedef VectorBase<ELT>                             Base;
44     typedef typename CNT<ELT>::Scalar                   S;
45     typedef typename CNT<ELT>::Number                   Number;
46     typedef typename CNT<ELT>::StdNumber                StdNumber;
47     typedef VectorView_<ELT>                            T;
48     typedef VectorView_< typename CNT<ELT>::TNeg >      TNeg;
49     typedef RowVectorView_< typename CNT<ELT>::THerm >  THerm;
50 public:
51     // Default construction is suppressed.
52     // Uses default destructor.
53 
54     // Create a VectorView_ handle using a given helper rep.
VectorView_(MatrixHelperRep<S> * hrep)55     explicit VectorView_(MatrixHelperRep<S>* hrep) : Base(hrep) {}
56 
57     // Copy constructor is shallow. CAUTION: despite const argument, this preserves writability
58     // if it was present in the source. This is necessary to allow temporary views to be
59     // created and used as lvalues.
VectorView_(const VectorView_ & v)60     VectorView_(const VectorView_& v)
61       : Base(const_cast<MatrixHelper<S>&>(v.getHelper()), typename MatrixHelper<S>::ShallowCopy()) { }
62 
63     // Copy assignment is deep but not reallocating.
64     VectorView_& operator=(const VectorView_& v) {
65         Base::operator=(v); return *this;
66     }
67 
68     // Ask for shallow copy
VectorView_(const MatrixHelper<S> & h)69     explicit VectorView_(const MatrixHelper<S>& h) : Base(h, typename MatrixHelper<S>::ShallowCopy()) { }
VectorView_(MatrixHelper<S> & h)70     explicit VectorView_(MatrixHelper<S>&       h) : Base(h, typename MatrixHelper<S>::ShallowCopy()) { }
71 
72     VectorView_& operator=(const Base& b) { Base::operator=(b); return *this; }
73 
74     VectorView_& operator=(const ELT& v) { Base::operator=(v); return *this; }
75 
76     template <class EE> VectorView_& operator=(const VectorBase<EE>& m)
77       { Base::operator=(m); return*this; }
78     template <class EE> VectorView_& operator+=(const VectorBase<EE>& m)
79       { Base::operator+=(m); return*this; }
80     template <class EE> VectorView_& operator-=(const VectorBase<EE>& m)
81       { Base::operator-=(m); return*this; }
82 
83     VectorView_& operator*=(const StdNumber& t) { Base::operator*=(t); return *this; }
84     VectorView_& operator/=(const StdNumber& t) { Base::operator/=(t); return *this; }
85     VectorView_& operator+=(const ELT& b) { this->elementwiseAddScalarInPlace(b); return *this; }
86     VectorView_& operator-=(const ELT& b) { this->elementwiseSubtractScalarInPlace(b); return *this; }
87 
88 private:
89     // NO DATA MEMBERS ALLOWED
90     VectorView_(); // default construction suppressed (what's it a View of?)
91 };
92 
93 } //namespace SimTK
94 
95 #endif // SimTK_SIMMATRIX_VECTORVIEW_H_
96