diff --git a/dune/istl/multitypeblockmatrix.hh b/dune/istl/multitypeblockmatrix.hh index e63fb4c4cf6c6430adba4ed4ecf04c9d4d852670..646aa72579492107f7469509616bfdf2e3ac9ce3 100644 --- a/dune/istl/multitypeblockmatrix.hh +++ b/dune/istl/multitypeblockmatrix.hh @@ -225,6 +225,44 @@ namespace Dune { typedef typename mpl::at_c<T1,0>::type field_type; + /** \brief Random-access operator + * + * This method mimicks the behavior of normal vector access with square brackets like, e.g., m[5] = .... + * The problem is that the return type is different for each value of the argument in the brackets. + * Therefore we implement a trick using std::integral_constant. To access the first row of + * a MultiTypeBlockMatrix named m write + * \code + * std::integral_constant<std::size_t,0> _0; + * m[_0] = ... + * \endcode + * The name '_0' used here as a static replacement of the integer number zero is arbitrary. + * Any other variable name can be used. If you don't like the separate variable, you can writee + * \code + * m[std::integral_constant<std::size_t,0>()] = ... + * \endcode + */ + template< std::size_t index > + auto + operator[] ( const std::integral_constant< std::size_t, index > indexVariable ) -> decltype(fusion::at_c<index>(*this)) + { + DUNE_UNUSED_PARAMETER(indexVariable); + return fusion::at_c<index>(*this); + } + + /** \brief Const random-access operator + * + * This is the const version of the random-access operator. See the non-const version for a full + * explanation of how to use it. + */ + template< std::size_t index > + auto + operator[] ( const std::integral_constant< std::size_t, index > indexVariable ) const -> decltype(fusion::at_c<index>(*this)) + { + DUNE_UNUSED_PARAMETER(indexVariable); + return fusion::at_c<index>(*this); + } + + /** * assignment operator */ diff --git a/dune/istl/multitypeblockvector.hh b/dune/istl/multitypeblockvector.hh index e93d66a192bfdcef3417032ad56bb3e6a9c777a4..9267b8695762b44fd4e9b9cc1709dbee889f77d1 100644 --- a/dune/istl/multitypeblockvector.hh +++ b/dune/istl/multitypeblockvector.hh @@ -261,6 +261,47 @@ namespace Dune { */ int count() {return mpl::size<type>::value;} + /** \brief Random-access operator + * + * This method mimicks the behavior of normal vector access with square brackets like, e.g., v[5] = 1. + * The problem is that the return type is different for each value of the argument in the brackets. + * Therefore we implement a trick using std::integral_constant. To access the first entry of + * a MultiTypeBlockVector named v write + * \code + * MultiTypeBlockVector<A,B,C,D> v; + * std::integral_constant<std::size_t,0> _0; + * v[_0] = ... + * \endcode + * The name '_0' used here as a static replacement of the integer number zero is arbitrary. + * Any other variable name can be used. If you don't like the separate variable, you can write + * \code + * MultiTypeBlockVector<A,B,C,D> v; + * v[std::integral_constant<std::size_t,0>()] = ... + * \endcode + */ + template< std::size_t index > + auto + operator[] ( const std::integral_constant< std::size_t, index > indexVariable ) + -> decltype(fusion::at_c<index>(*this)) + { + DUNE_UNUSED_PARAMETER(indexVariable); + return fusion::at_c<index>(*this); + } + + /** \brief Const random-access operator + * + * This is the const version of the random-access operator. See the non-const version for a full + * explanation of how to use it. + */ + template< std::size_t index > + auto + operator[] ( const std::integral_constant< std::size_t, index > indexVariable ) const + -> decltype(fusion::at_c<index>(*this)) + { + DUNE_UNUSED_PARAMETER(indexVariable); + return fusion::at_c<index>(*this); + } + /** * assignment operator */