diff --git a/dune/xt/common/test/vector.cc b/dune/xt/common/test/vector.cc new file mode 100644 index 0000000000000000000000000000000000000000..673327b3f96eaa1d2b64d640214736cd197863e0 --- /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 510922c93c4d7a4dd013c62600809e2634ea82e5..186e1e59ea2dba2c028aaf429903c6f9f7d4d4ce 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;