expression_types.hpp
Go to the documentation of this file.
1 /*!
2  * \brief Defines the basic types of CRTP base-classes
3  *
4  * \author O. Krause
5  * \date 2013
6  *
7  *
8  * \par Copyright 1995-2015 Shark Development Team
9  *
10  * <BR><HR>
11  * This file is part of Shark.
12  * <http://image.diku.dk/shark/>
13  *
14  * Shark is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU Lesser General Public License as published
16  * by the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * Shark is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
26  *
27  */
28 #ifndef SHARK_LINALG_BLAS_EXPRESSION_TYPE_HPP
29 #define SHARK_LINALG_BLAS_EXPRESSION_TYPE_HPP
30 
31 namespace shark {
32 namespace blas {
33 
34 /** \brief Base class for Vector Expression models
35  *
36  * it does not model the Vector Expression concept but all derived types should.
37  * The class defines a common base type and some common interface for all
38  * statically derived Vector Expression classes.
39  * We implement the casts to the statically derived type.
40  */
41 template<class E>
43  typedef E expression_type;
44 
45  const expression_type &operator()() const {
46  return *static_cast<const expression_type *>(this);
47  }
48 
49  expression_type &operator()() {
50  return *static_cast<expression_type *>(this);
51  }
52 };
53 
54 /** \brief Base class for Vector container models
55  *
56  * it does not model the Vector concept but all derived types should.
57  * The class defines a common base type and some common interface for all
58  * statically derived Vector classes
59  * We implement the casts to the statically derived type.
60  */
61 template<class C>
63  typedef C container_type;
64 
65  const container_type &operator()() const {
66  return *static_cast<const container_type *>(this);
67  }
68 
69  container_type &operator()() {
70  return *static_cast<container_type *>(this);
71  }
72 };
73 
74 
75 /** \brief Base class for Matrix Expression models
76  *
77  * it does not model the Matrix Expression concept but all derived types should.
78  * The class defines a common base type and some common interface for all
79  * statically derived Matrix Expression classes
80  * We iboost::mplement the casts to the statically derived type.
81  */
82 template<class E>
84  typedef E expression_type;
85 
86  const expression_type &operator()() const {
87  return *static_cast<const expression_type *>(this);
88  }
89 
90  expression_type &operator()() {
91  return *static_cast<expression_type *>(this);
92  }
93 };
94 
95 /** \brief Base class for expressions of matrix sets
96  *
97  * The matrix set expression type is similar to a tensor type. However it behaves
98  * like a vector of matrices with elements of the vector being matrices. Moreover
99  * all usual operations can be used. There is no distinction to the sizes of the matrices
100  * and all matrices may have different dimensionalities.
101  *
102  * it does not model the Matrix Expression concept but all derived types should.
103  * The class defines a common base type and some common interface for all
104  * statically derived Matrix Expression classes
105  * We iboost::mplement the casts to the statically derived type.
106  */
107 template<class E>
109  typedef E expression_type;
110 
111  const expression_type &operator()() const {
112  return *static_cast<const expression_type *>(this);
113  }
114 
115  expression_type &operator()() {
116  return *static_cast<expression_type *>(this);
117  }
118 };
119 
120 /** \brief Base class for expressions of vector sets
121  *
122  * The vector set expression type is similar to a matrix type. However it behaves
123  * like a vector of vectors with elements of the vector being vectors. Moreover
124  * all usual vector-space operations can be used . There is no distinction to the sizes of the elements
125  * and all vectors may have different dimensionalities.
126  *
127  * it does not model the Matrix Expression concept but all derived types should.
128  * The class defines a common base type and some common interface for all
129  * statically derived Matrix Expression classes
130  * We iboost::mplement the casts to the statically derived type.
131  */
132 template<class E>
134  typedef E expression_type;
135 
136  const expression_type &operator()() const {
137  return *static_cast<const expression_type *>(this);
138  }
139 
140  expression_type &operator()() {
141  return *static_cast<expression_type *>(this);
142  }
143 };
144 
145 /** \brief Base class for Matrix container models
146  *
147  * it does not model the Matrix concept but all derived types should.
148  * The class defines a common base type and some common interface for all
149  * statically derived Matrix classes
150  * We implement the casts to the statically derived type.
151  */
152 template<class C>
154  typedef C container_type;
155 
156  const container_type &operator()() const {
157  return *static_cast<const container_type *>(this);
158  }
159 
160  container_type &operator()() {
161  return *static_cast<container_type *>(this);
162  }
163 };
164 
165 template<class P>
166 struct temporary_proxy:public P{
167  temporary_proxy(P const& p):P(p){}
168 
169  template<class E>
170  P& operator=(E const& e){
171  return static_cast<P&>(*this) = e;
172  }
173 
175  return static_cast<P&>(*this) = e;
176  }
177 };
178 
179 }
180 }
181 
182 #endif