Skip to content
Snippets Groups Projects
Commit 9ca63448 authored by Timo Koch's avatar Timo Koch
Browse files

Merge branch 'feature/partial-const' into 'master'

[common][partial] Add MTBV containing const references

See merge request !2215
parents cef12744 feddbafa
No related branches found
No related tags found
1 merge request!2215[common][partial] Add MTBV containing const references
......@@ -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
......
......@@ -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)
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment