dune-common 2.9.0
Loading...
Searching...
No Matches
scalarvectorview.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// SPDX-FileCopyrightInfo: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_COMMON_SCALARVECTORVIEW_HH
6#define DUNE_COMMON_SCALARVECTORVIEW_HH
7
8#include <cstddef>
9#include <type_traits>
10#include <istream>
11
16
17namespace Dune {
18
19namespace Impl {
20
35 template<class K>
36 class ScalarVectorView :
37 public DenseVector<ScalarVectorView<K>>
38 {
39 K* dataP_;
40 using Base = DenseVector<ScalarVectorView<K>>;
41
42 template <class>
43 friend class ScalarVectorView;
44 public:
45
47 constexpr static int dimension = 1;
48
50 using size_type = typename Base::size_type;
51
53 using reference = std::decay_t<K>&;
54
56 using const_reference = const K&;
57
58 //===== construction
59
61 constexpr ScalarVectorView ()
62 : dataP_(nullptr)
63 {}
64
66 ScalarVectorView (K* p) :
67 dataP_(p)
68 {}
69
71 ScalarVectorView (const ScalarVectorView &other) :
72 Base(),
73 dataP_(other.dataP_)
74 {}
75
77 ScalarVectorView (ScalarVectorView &&other) :
78 Base(),
79 dataP_( other.dataP_ )
80 {}
81
83 ScalarVectorView& operator= (const ScalarVectorView& other)
84 {
85 assert(dataP_);
86 assert(other.dataP_);
87 *dataP_ = *(other.dataP_);
88 return *this;
89 }
90
91 template<class KK>
92 ScalarVectorView& operator= (const ScalarVectorView<KK>& other)
93 {
94 assert(dataP_);
95 assert(other.dataP_);
96 *dataP_ = *(other.dataP_);
97 return *this;
98 }
99
101 template<typename T,
102 std::enable_if_t<std::is_convertible<T, K>::value, int> = 0>
103 inline ScalarVectorView& operator= (const T& k)
104 {
105 *dataP_ = k;
106 return *this;
107 }
108
110 static constexpr size_type size ()
111 {
112 return 1;
113 }
114
116 K& operator[] ([[maybe_unused]] size_type i)
117 {
118 DUNE_ASSERT_BOUNDS(i == 0);
119 return *dataP_;
120 }
121
123 const K& operator[] ([[maybe_unused]] size_type i) const
124 {
125 DUNE_ASSERT_BOUNDS(i == 0);
126 return *dataP_;
127 }
128 }; // class ScalarVectorView
129
130} // namespace Impl
131
132
133 template< class K>
134 struct DenseMatVecTraits< Impl::ScalarVectorView<K> >
135 {
136 using derived_type = Impl::ScalarVectorView<K>;
137 using value_type = std::remove_const_t<K>;
138 using size_type = std::size_t;
139 };
140
141 template< class K >
142 struct FieldTraits< Impl::ScalarVectorView<K> > : public FieldTraits<std::remove_const_t<K>> {};
143
144 template<class K>
145 struct AutonomousValueType<Impl::ScalarVectorView<K>>
146 {
147 using type = FieldVector<std::remove_const_t<K>,1>;
148 };
149
150namespace Impl {
151
163 template<class K>
164 inline std::istream &operator>> ( std::istream &in, ScalarVectorView<K> &v )
165 {
166 K w;
167 if(in >> w)
168 v = w;
169 return in;
170 }
171
172
174 template<class T,
175 std::enable_if_t<IsNumber<T>::value, int> = 0>
176 auto asVector(T& t)
177 {
178 return ScalarVectorView<T>{&t};
179 }
180
182 template<class T,
183 std::enable_if_t<IsNumber<T>::value, int> = 0>
184 auto asVector(const T& t)
185 {
186 return ScalarVectorView<const T>{&t};
187 }
188
190 template<class T,
191 std::enable_if_t<not IsNumber<T>::value, int> = 0>
192 T& asVector(T& t)
193 {
194 return t;
195 }
196
198 template<class T,
199 std::enable_if_t<not IsNumber<T>::value, int> = 0>
200 const T& asVector(const T& t)
201 {
202 return t;
203 }
204
205} // end namespace Impl
206
207} // end namespace Dune
208
209#endif // DUNE_COMMON_SCALARVECTORVIEW_HH
Implements a vector constructed from a given type representing a field and a compile-time given size.
Traits for type conversions and type information.
Documentation of the traits classes you need to write for each implementation of DenseVector or Dense...
Implements the dense vector interface, with an exchangeable storage class.
Stream & operator>>(Stream &stream, std::tuple< Ts... > &t)
Read a std::tuple.
Definition streamoperators.hh:43
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition boundschecking.hh:30
Dune namespace.
Definition alignedallocator.hh:13
size_type size() const
size method
Definition densevector.hh:336
Traits::size_type size_type
The type used for the index access and size operation.
Definition densevector.hh:259
T type
Definition typetraits.hh:501