dune-common  2.7.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[] (size_type i)
118  {
120  DUNE_ASSERT_BOUNDS(i == 0);
121  return *dataP_;
122  }
123 
125  const K& operator[] (size_type i) const
126  {
128  DUNE_ASSERT_BOUNDS(i == 0);
129  return *dataP_;
130  }
131  }; // class ScalarVectorView
132 
133 } // namespace Impl
134 
135 
136  template< class K>
137  struct DenseMatVecTraits< Impl::ScalarVectorView<K> >
138  {
139  using derived_type = Impl::ScalarVectorView<K>;
140  using value_type = std::remove_const_t<K>;
141  using size_type = std::size_t;
142  };
143 
144  template< class K >
145  struct FieldTraits< Impl::ScalarVectorView<K> > : public FieldTraits<std::remove_const_t<K>> {};
146 
147  template<class K>
148  struct AutonomousValueType<Impl::ScalarVectorView<K>>
149  {
150  using type = FieldVector<std::remove_const_t<K>,1>;
151  };
152 
153 namespace Impl {
154 
166  template<class K>
167  inline std::istream &operator>> ( std::istream &in, ScalarVectorView<K> &v )
168  {
169  K w;
170  if(in >> w)
171  v = w;
172  return in;
173  }
174 
175 
177  template<class T,
178  std::enable_if_t<IsNumber<T>::value, int> = 0>
179  auto asVector(T& t)
180  {
181  return ScalarVectorView<T>{&t};
182  }
183 
185  template<class T,
186  std::enable_if_t<IsNumber<T>::value, int> = 0>
187  auto asVector(const T& t)
188  {
189  return ScalarVectorView<const T>{&t};
190  }
191 
193  template<class T,
194  std::enable_if_t<not IsNumber<T>::value, int> = 0>
195  T& asVector(T& t)
196  {
197  return t;
198  }
199 
201  template<class T,
202  std::enable_if_t<not IsNumber<T>::value, int> = 0>
203  const T& asVector(const T& t)
204  {
205  return t;
206  }
207 
208 } // end namespace Impl
209 
210 } // end namespace Dune
211 
212 #endif // DUNE_COMMON_SCALARVECTORVIEW_HH
Dune::AutonomousValueType::type
T type
Definition: typetraits.hh:582
Dune::DenseVector< ScalarVectorView< K > >::size
size_type size() const
size method
Definition: densevector.hh:337
DUNE_UNUSED_PARAMETER
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentionally unused function parameters with.
Definition: unused.hh:25
Dune::DenseVector< ScalarVectorView< K > >::size_type
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densevector.hh:257
matvectraits.hh
Documentation of the traits classes you need to write for each implementation of DenseVector or Dense...
fvector.hh
Implements a vector constructed from a given type representing a field and a compile-time given size.
typetraits.hh
Traits for type conversions and type information.
densevector.hh
Implements the dense vector interface, with an exchangeable storage class.
Dune::operator>>
Stream & operator>>(Stream &stream, std::tuple< Ts... > &t)
Read a std::tuple.
Definition: streamoperators.hh:41
DUNE_ASSERT_BOUNDS
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition: boundschecking.hh:28
Dune
Dune namespace.
Definition: alignedallocator.hh:13