diff --git a/dumux/common/partial.hh b/dumux/common/partial.hh index 1dc4dbeabd0f57e9c01006425bc3ed3594c011ee..eeb65a35a820bc08f3df2ad870f60e6b2dc7c2e6 100644 --- a/dumux/common/partial.hh +++ b/dumux/common/partial.hh @@ -42,6 +42,17 @@ auto partial(Dune::MultiTypeBlockVector<Args...>& v, Dune::index_constant<i>... return Dune::MultiTypeBlockVector<std::add_lvalue_reference_t<std::decay_t<std::tuple_element_t<indices, std::tuple<Args...>>>>...>(v[indices]...); } +/*! + * \brief a function to get a MultiTypeBlockVector with const references to some entries of another MultiTypeBlockVector + * \param v a MultiTypeBlockVector + * \param indices the indices of the entries that should be referenced + */ +template<class ...Args, std::size_t ...i> +auto partial(const Dune::MultiTypeBlockVector<Args...>& v, Dune::index_constant<i>... indices) +{ + return Dune::MultiTypeBlockVector<std::add_lvalue_reference_t<const std::decay_t<std::tuple_element_t<indices, std::tuple<Args...>>>>...>(v[indices]...); +} + /*! * \brief a function to get a tuple with references to some entries of another tuple * \param v a tuple @@ -53,6 +64,17 @@ auto partial(std::tuple<Args...>& v, Dune::index_constant<i>... indices) return std::tuple<std::add_lvalue_reference_t<std::decay_t<std::tuple_element_t<indices, std::tuple<Args...>>>>...>(std::get<indices>(v)...); } +/*! + * \brief a function to get a tuple with const references to some entries of another tuple + * \param v a tuple + * \param indices a tuple of indices of the entries that should be referenced + */ +template<class ...Args, std::size_t ...i> +auto partial(const std::tuple<Args...>& v, Dune::index_constant<i>... indices) +{ + return std::tuple<std::add_lvalue_reference_t<const std::decay_t<std::tuple_element_t<indices, std::tuple<Args...>>>>...>(std::get<indices>(v)...); +} + /*! * \brief a function to get a MultiTypeBlockVector with references to some entries of another MultiTypeBlockVector * \param t an std::tuple or Dune::MultiTypeBlockVector diff --git a/test/common/test_partial.cc b/test/common/test_partial.cc index f420efc90a4c3bc1c4ded62753cab3bee07d2b8b..177aa96515174644db927429d91422f0f035012b 100644 --- a/test/common/test_partial.cc +++ b/test/common/test_partial.cc @@ -2,6 +2,7 @@ #include <iostream> #include <tuple> +#include <type_traits> #include <dune/common/indices.hh> #include <dune/common/classname.hh> @@ -48,6 +49,24 @@ void runTest() if (!Dune::FloatCmp::eq(std::get<2>(m)[0][0], 5.0)) DUNE_THROW(Dune::Exception, "Modifying referenced partial vector failed! (m = " << std::get<2>(m)[0][0] << ", p = " << std::get<1>(p)[0][0] << ")"); + + const auto mConst = m; + auto pConst = partial(mConst, _0, _2); + static_assert(std::is_const_v<std::remove_reference_t<decltype(std::get<0>(pConst))>>); + static_assert(std::is_const_v<std::remove_reference_t<decltype(std::get<1>(pConst))>>); + + if (!Dune::FloatCmp::eq(std::get<0>(mConst)[0][0], std::get<0>(pConst)[0][0])) + DUNE_THROW(Dune::Exception, "Values differ! (mConst = " << std::get<0>(mConst)[0][0] << ", pConst = " << std::get<0>(pConst)[0][0] << ")"); + + const auto& mConstRef = m; + auto pConstRef = partial(mConstRef, _0, _2); + static_assert(std::is_const_v<std::remove_reference_t<decltype(std::get<0>(pConstRef))>>); + static_assert(std::is_const_v<std::remove_reference_t<decltype(std::get<1>(pConstRef))>>); + + std::get<2>(m)[0][0] = 10.0; + + if (!Dune::FloatCmp::eq(std::get<1>(pConstRef)[0][0], 10.0)) + DUNE_THROW(Dune::Exception, "Modifying referenced partial vector failed! (m = " << std::get<2>(m)[0][0] << ", pConstRef = " << std::get<1>(pConstRef)[0][0] << ")"); } } // end namespace Dumux @@ -60,7 +79,6 @@ int main(int argc, char* argv[]) try runTest<std::tuple>(); return 0; - } catch (const Dune::Exception& e) {