From 0350f11d6ac8541984b4736dc9d0d6baa5d9f743 Mon Sep 17 00:00:00 2001 From: Rene Milk <rene.milk@wwu.de> Date: Mon, 28 Aug 2017 14:02:19 +0200 Subject: [PATCH] [vector] adds a a mechanism to get vectors with repalced scalar imp --- dune/xt/common/test/vector.cc | 31 +++++++++++++++++++++++++++++++ dune/xt/common/vector.hh | 33 ++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 dune/xt/common/test/vector.cc diff --git a/dune/xt/common/test/vector.cc b/dune/xt/common/test/vector.cc new file mode 100644 index 000000000..673327b3f --- /dev/null +++ b/dune/xt/common/test/vector.cc @@ -0,0 +1,31 @@ +// This file is part of the dune-xt-common project: +// https://github.com/dune-community/dune-xt-common +// Copyright 2009-2017 dune-xt-common developers and contributors. All rights reserved. +// License: Dual licensed as BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause) +// or GPL-2.0+ (http://opensource.org/licenses/gpl-license) +// with "runtime exception" (http://www.dune-project.org/license.html) +// Authors: +// Rene Milk (2017) + +#include <dune/xt/common/test/main.hxx> + +#include <iostream> + +#include <dune/xt/common/type_traits.hh> + +using namespace Dune::XT::Common; +using namespace std; + +GTEST_TEST(VectorAbstraction, Transfer) +{ + { + using Vec = std::vector<int>; + using Abstract = VectorAbstraction<Vec>; + using Transferred = Abstract::Transfer<std::string>; + } + { + using Vec = std::array<int, 2>; + using Abstract = VectorAbstraction<Vec>; + using Transferred = Abstract::Transfer<std::string>; + } +} diff --git a/dune/xt/common/vector.hh b/dune/xt/common/vector.hh index 510922c93..186e1e59e 100644 --- a/dune/xt/common/vector.hh +++ b/dune/xt/common/vector.hh @@ -81,6 +81,25 @@ struct VectorAbstraction } }; +template <class SizeType> +struct SizeTransfer +{ + template <template <class, SizeType...> class VectorTemplate, SizeType... values> + struct Base + { + template <class NewScalarType> + using Transfer = VectorTemplate<NewScalarType, values...>; + }; +}; + +template <template <class, typename...> class VectorTemplate, typename... Ts> +struct TypeTransferBase +{ + template <class NewScalarType> + using Transfer = VectorTemplate<NewScalarType, Ts...>; +}; + + template <class VectorType, class ScalarType> struct SubscriptOperatorGetAndSet { @@ -99,7 +118,8 @@ struct SubscriptOperatorGetAndSet template <class T> struct VectorAbstraction<std::vector<T>> - : public SubscriptOperatorGetAndSet<std::vector<T>, typename Dune::FieldTraits<T>::field_type> + : public SubscriptOperatorGetAndSet<std::vector<T>, typename Dune::FieldTraits<T>::field_type>, + public TypeTransferBase<std::vector> { typedef std::vector<T> VectorType; typedef typename Dune::FieldTraits<T>::field_type ScalarType; @@ -126,7 +146,8 @@ struct VectorAbstraction<std::vector<T>> template <class K, size_t SIZE> struct VectorAbstraction<std::array<K, SIZE>> - : public SubscriptOperatorGetAndSet<std::array<K, SIZE>, typename Dune::FieldTraits<K>::field_type> + : public SubscriptOperatorGetAndSet<std::array<K, SIZE>, typename Dune::FieldTraits<K>::field_type>, + public SizeTransfer<size_t>::Base<std::array, SIZE> { typedef std::array<K, SIZE> VectorType; typedef typename Dune::FieldTraits<K>::field_type ScalarType; @@ -159,7 +180,8 @@ struct VectorAbstraction<std::array<K, SIZE>> template <class K> struct VectorAbstraction<Dune::DynamicVector<K>> - : public SubscriptOperatorGetAndSet<Dune::DynamicVector<K>, typename Dune::FieldTraits<K>::field_type> + : public SubscriptOperatorGetAndSet<Dune::DynamicVector<K>, typename Dune::FieldTraits<K>::field_type>, + public TypeTransferBase<Dune::DynamicVector> { typedef Dune::DynamicVector<K> VectorType; typedef typename Dune::FieldTraits<K>::field_type ScalarType; @@ -186,7 +208,8 @@ struct VectorAbstraction<Dune::DynamicVector<K>> template <class K, int SIZE> struct VectorAbstraction<Dune::FieldVector<K, SIZE>> - : public SubscriptOperatorGetAndSet<Dune::FieldVector<K, SIZE>, typename Dune::FieldTraits<K>::field_type> + : public SubscriptOperatorGetAndSet<Dune::FieldVector<K, SIZE>, typename Dune::FieldTraits<K>::field_type>, + public SizeTransfer<int>::Base<Dune::FieldVector, SIZE> { typedef Dune::FieldVector<K, SIZE> VectorType; typedef typename Dune::FieldTraits<K>::field_type ScalarType; @@ -216,7 +239,7 @@ struct VectorAbstraction<Dune::FieldVector<K, SIZE>> }; template <class T> -struct VectorAbstraction<std::complex<T>> +struct VectorAbstraction<std::complex<T>> : public TypeTransferBase<std::complex> { typedef std::complex<T> VectorType; typedef std::complex<T> ScalarType; -- GitLab