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 REMORA_EXPRESSION_TYPE_HPP
29 #define REMORA_EXPRESSION_TYPE_HPP
30 
31 namespace remora{
32 
33 struct cpu_tag{};
34 struct gpu_tag{};
35 
36 
37 /// \brief Base class for Vector Expression models
38 ///
39 /// it does not model the Vector Expression concept but all derived types should.
40 /// The class defines a common base type and some common interface for all
41 /// statically derived Vector Expression classes.
42 /// We implement the casts to the statically derived type.
43 template<class E, class Device>
44 struct vector_expression {
45  typedef E expression_type;
46  typedef Device device_type;
47  const expression_type &operator()() const {
48  return *static_cast<const expression_type *>(this);
49  }
50 
51  expression_type &operator()() {
52  return *static_cast<expression_type *>(this);
53  }
54 };
55 
56 /// \brief Base class for Vector container models
57 ///
58 /// it does not model the Vector concept but all derived types should.
59 /// The class defines a common base type and some common interface for all
60 /// statically derived Vector classes
61 /// We implement the casts to the statically derived type.
62 template<class C, class Device>
63 struct vector_container:public vector_expression<C, Device> {
64  typedef C container_type;
65 
66  const container_type &operator()() const {
67  return *static_cast<const container_type *>(this);
68  }
69 
70  container_type &operator()() {
71  return *static_cast<container_type *>(this);
72  }
73 };
74 
75 
76 /// \brief Base class for Matrix Expression models
77 ///
78 /// it does not model the Matrix Expression concept but all derived types should.
79 /// The class defines a common base type and some common interface for all
80 /// statically derived Matrix Expression classes
81 /// We implement the casts to the statically derived type.
82 template<class E, class Device>
83 struct matrix_expression {
84  typedef E expression_type;
85  typedef Device device_type;
86 
87  const expression_type &operator()() const {
88  return *static_cast<const expression_type *>(this);
89  }
90 
91  expression_type &operator()() {
92  return *static_cast<expression_type *>(this);
93  }
94 };
95 
96 /// \brief Base class for expressions of vector sets
97 ///
98 /// The vector set expression type is similar to a matrix type. However it behaves
99 /// like a vector of vectors with elements of the vector being vectors. Moreover
100 /// all usual vector-space operations can be used . There is no distinction to the sizes of the elements
101 /// and all vectors may have different dimensionalities.
102 ///
103 /// it does not model the Matrix Expression concept but all derived types should.
104 /// The class defines a common base type and some common interface for all
105 /// statically derived Matrix Expression classes
106 /// We implement the casts to the statically derived type.
107 template<class E>
108 struct vector_set_expression {
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 Matrix container models
121 ///
122 /// it does not model the Matrix concept but all derived types should.
123 /// The class defines a common base type and some common interface for all
124 /// statically derived Matrix classes
125 /// We implement the casts to the statically derived type.
126 template<class C, class Device>
127 struct matrix_container: public matrix_expression<C, Device> {
128  typedef C container_type;
129 
130  const container_type &operator()() const {
131  return *static_cast<const container_type *>(this);
132  }
133 
134  container_type &operator()() {
135  return *static_cast<container_type *>(this);
136  }
137 };
138 
139 template<class P>
140 struct temporary_proxy:public P{
141  temporary_proxy(P const& p):P(p){}
142 
143  template<class E>
144  P& operator=(E const& e){
145  return static_cast<P&>(*this) = e;
146  }
147 
148  P& operator=(temporary_proxy<P> const& e){
149  return static_cast<P&>(*this) = e;
150  }
151 };
152 
153 }
154 
155 #endif