Loading...
Searching...
No Matches
scalarvectorview.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_COMMON_SCALARVECTORVIEW_HH
#define DUNE_COMMON_SCALARVECTORVIEW_HH
#include <cstddef>
#include <type_traits>
#include <istream>
#include <dune/common/densevector.hh>
#include <dune/common/fvector.hh>
#include <dune/common/typetraits.hh>
#include <dune/common/matvectraits.hh>
namespace Dune {
namespace Impl {
template<class K>
class ScalarVectorView :
public DenseVector<ScalarVectorView<K>>
{
K* dataP_;
using Base = DenseVector<ScalarVectorView<K>>;
template <class>
friend class ScalarVectorView;
public:
constexpr static int dimension = 1;
using size_type = typename Base::size_type;
using reference = std::decay_t<K>&;
using const_reference = const K&;
//===== construction
constexpr ScalarVectorView ()
: dataP_(nullptr)
{}
ScalarVectorView (K* p) :
dataP_(p)
{}
ScalarVectorView (const ScalarVectorView &other) :
Base(),
dataP_(other.dataP_)
{}
ScalarVectorView (ScalarVectorView &&other) :
Base(),
dataP_( other.dataP_ )
{}
ScalarVectorView& operator= (const ScalarVectorView& other)
{
assert(dataP_);
assert(other.dataP_);
*dataP_ = *(other.dataP_);
return *this;
}
template<class KK>
ScalarVectorView& operator= (const ScalarVectorView<KK>& other)
{
assert(dataP_);
assert(other.dataP_);
*dataP_ = *(other.dataP_);
return *this;
}
template<typename T,
std::enable_if_t<std::is_convertible<T, K>::value, int> = 0>
inline ScalarVectorView& operator= (const T& k)
{
*dataP_ = k;
return *this;
}
static constexpr size_type size ()
{
return 1;
}
K& operator[] ([[maybe_unused]] size_type i)
{
DUNE_ASSERT_BOUNDS(i == 0);
return *dataP_;
}
const K& operator[] ([[maybe_unused]] size_type i) const
{
DUNE_ASSERT_BOUNDS(i == 0);
return *dataP_;
}
}; // class ScalarVectorView
} // namespace Impl
template< class K>
struct DenseMatVecTraits< Impl::ScalarVectorView<K> >
{
using derived_type = Impl::ScalarVectorView<K>;
using value_type = std::remove_const_t<K>;
using size_type = std::size_t;
};
template< class K >
struct FieldTraits< Impl::ScalarVectorView<K> > : public FieldTraits<std::remove_const_t<K>> {};
template<class K>
struct AutonomousValueType<Impl::ScalarVectorView<K>>
{
using type = FieldVector<std::remove_const_t<K>,1>;
};
namespace Impl {
template<class K>
inline std::istream &operator>> ( std::istream &in, ScalarVectorView<K> &v )
{
K w;
if(in >> w)
v = w;
return in;
}
template<class T,
std::enable_if_t<IsNumber<T>::value, int> = 0>
auto asVector(T& t)
{
return ScalarVectorView<T>{&t};
}
template<class T,
std::enable_if_t<IsNumber<T>::value, int> = 0>
auto asVector(const T& t)
{
return ScalarVectorView<const T>{&t};
}
template<class T,
std::enable_if_t<not IsNumber<T>::value, int> = 0>
T& asVector(T& t)
{
return t;
}
template<class T,
std::enable_if_t<not IsNumber<T>::value, int> = 0>
const T& asVector(const T& t)
{
return t;
}
} // end namespace Impl
} // end namespace Dune
#endif // DUNE_COMMON_SCALARVECTORVIEW_HH
typetraits.hh
Traits for type conversions and type information.
matvectraits.hh
Documentation of the traits classes you need to write for each implementation of DenseVector or Dense...
densevector.hh
Implements the dense vector interface, with an exchangeable storage class.
fvector.hh
Implements a vector constructed from a given type representing a field and a compile-time given size.
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:30
Dune
Dune namespace
Definition alignedallocator.hh:13
Dune::DenseVector< ScalarVectorView< K > >::size_type
Traits::size_type size_type
Definition densevector.hh:274
Dune::DenseVector< ScalarVectorView< K > >::DenseVector
constexpr DenseVector()=default
Dune::DenseVector< ScalarVectorView< K > >::size
constexpr size_type size() const
Definition densevector.hh:351
Dune::FieldTraits
Definition ftraits.hh:26
Dune::DenseMatVecTraits
Definition matvectraits.hh:31
Dune::AutonomousValueType
Type free of internal references that T can be converted to.
Definition typetraits.hh:509
Dune::AutonomousValueType::type
T type
Definition typetraits.hh:509