dune-common  2.8.0
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 #ifndef DUNE_COMMON_SCALARVECTORVIEW_HH
4 #define DUNE_COMMON_SCALARVECTORVIEW_HH
5 
6 #include <cstddef>
7 #include <type_traits>
8 #include <istream>
9 
11 #include <dune/common/fvector.hh>
14 
15 namespace Dune {
16 
17 namespace Impl {
18 
33  template<class K>
34  class ScalarVectorView :
35  public DenseVector<ScalarVectorView<K>>
36  {
37  K* dataP_;
38  using Base = DenseVector<ScalarVectorView<K>>;
39 
40  template <class>
41  friend class ScalarVectorView;
42  public:
43 
45  enum {
47  dimension = 1
48  };
49 
51  using size_type = typename Base::size_type;
52 
54  using reference = std::decay_t<K>&;
55 
57  using const_reference = const K&;
58 
59  //===== construction
60 
62  constexpr ScalarVectorView ()
63  : dataP_(nullptr)
64  {}
65 
67  ScalarVectorView (K* p) :
68  dataP_(p)
69  {}
70 
72  ScalarVectorView (const ScalarVectorView &other) :
73  Base(),
74  dataP_(other.dataP_)
75  {}
76 
78  ScalarVectorView (ScalarVectorView &&other) :
79  Base(),
80  dataP_( other.dataP_ )
81  {}
82 
84  ScalarVectorView& operator= (const ScalarVectorView& other)
85  {
86  assert(dataP_);
87  assert(other.dataP_);
88  *dataP_ = *(other.dataP_);
89  return *this;
90  }
91 
92  template<class KK>
93  ScalarVectorView& operator= (const ScalarVectorView<KK>& other)
94  {
95  assert(dataP_);
96  assert(other.dataP_);
97  *dataP_ = *(other.dataP_);
98  return *this;
99  }
100 
102  template<typename T,
103  std::enable_if_t<std::is_convertible<T, K>::value, int> = 0>
104  inline ScalarVectorView& operator= (const T& k)
105  {
106  *dataP_ = k;
107  return *this;
108  }
109 
111  static constexpr size_type size ()
112  {
113  return 1;
114  }
115 
117  K& operator[] ([[maybe_unused]] size_type i)
118  {
119  DUNE_ASSERT_BOUNDS(i == 0);
120  return *dataP_;
121  }
122 
124  const K& operator[] ([[maybe_unused]] size_type i) const
125  {
126  DUNE_ASSERT_BOUNDS(i == 0);
127  return *dataP_;
128  }
129  }; // class ScalarVectorView
130 
131 } // namespace Impl
132 
133 
134  template< class K>
135  struct DenseMatVecTraits< Impl::ScalarVectorView<K> >
136  {
137  using derived_type = Impl::ScalarVectorView<K>;
138  using value_type = std::remove_const_t<K>;
139  using size_type = std::size_t;
140  };
141 
142  template< class K >
143  struct FieldTraits< Impl::ScalarVectorView<K> > : public FieldTraits<std::remove_const_t<K>> {};
144 
145  template<class K>
146  struct AutonomousValueType<Impl::ScalarVectorView<K>>
147  {
148  using type = FieldVector<std::remove_const_t<K>,1>;
149  };
150 
151 namespace Impl {
152 
164  template<class K>
165  inline std::istream &operator>> ( std::istream &in, ScalarVectorView<K> &v )
166  {
167  K w;
168  if(in >> w)
169  v = w;
170  return in;
171  }
172 
173 
175  template<class T,
176  std::enable_if_t<IsNumber<T>::value, int> = 0>
177  auto asVector(T& t)
178  {
179  return ScalarVectorView<T>{&t};
180  }
181 
183  template<class T,
184  std::enable_if_t<IsNumber<T>::value, int> = 0>
185  auto asVector(const T& t)
186  {
187  return ScalarVectorView<const T>{&t};
188  }
189 
191  template<class T,
192  std::enable_if_t<not IsNumber<T>::value, int> = 0>
193  T& asVector(T& t)
194  {
195  return t;
196  }
197 
199  template<class T,
200  std::enable_if_t<not IsNumber<T>::value, int> = 0>
201  const T& asVector(const T& t)
202  {
203  return t;
204  }
205 
206 } // end namespace Impl
207 
208 } // end namespace Dune
209 
210 #endif // DUNE_COMMON_SCALARVECTORVIEW_HH
Implements the dense vector interface, with an exchangeable storage class.
Implements a vector constructed from a given type representing a field and a compile-time given size.
Documentation of the traits classes you need to write for each implementation of DenseVector or Dense...
Traits for type conversions and type information.
Stream & operator>>(Stream &stream, std::tuple< Ts... > &t)
Read a std::tuple.
Definition: streamoperators.hh:41
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition: boundschecking.hh:28
Dune namespace.
Definition: alignedallocator.hh:11
size_type size() const
size method
Definition: densevector.hh:337
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densevector.hh:257
T type
Definition: typetraits.hh:501