Skip to content
Snippets Groups Projects
Commit 6fcfa07f authored by Oliver Sander's avatar Oliver Sander
Browse files

Implement pseudo-operator[] for MultiTypeBlockVector

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

  MultiTypeBlockVector<A,B,C,D> v;
  std::integral_constant<int,0> _0;
  v[_0] = ...

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

  MultiTypeBlockVector<A,B,C,D> v;
  v[std::integral_constant<int,0>()] = ...
parent 23190cbc
No related branches found
No related tags found
No related merge requests found
......@@ -267,6 +267,45 @@ 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<int,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 writee
* \code
* MultiTypeBlockVector<A,B,C,D> v;
* v[std::integral_constant<int,0>()] = ...
* \endcode
*/
template< int index >
typename mpl::at_c<type,index>::type&
operator[] ( const std::integral_constant< int, index > indexVariable )
{
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< int index >
const typename mpl::at_c<type,index>::type&
operator[] ( const std::integral_constant< int, index > indexVariable ) const
{
DUNE_UNUSED_PARAMETER(indexVariable);
return fusion::at_c<index>(*this);
}
/**
* assignment operator
*/
......
......@@ -26,11 +26,14 @@ int main(int argc, char** argv) try
#else
MultiTypeBlockVector<BlockVector<FieldVector<double,3> >, BlockVector<FieldVector<double,1> > > multiVector;
boost::fusion::at_c<0>(multiVector) = {{1,0,0},
{0,1,0},
{0,0,1}};
std::integral_constant<int, 0> _0;
std::integral_constant<int, 1> _1;
boost::fusion::at_c<1>(multiVector) = {3.14, 42};
multiVector[_0] = {{1,0,0},
{0,1,0},
{0,0,1}};
multiVector[_1] = {3.14, 42};
// test operator<<
std::cout << multiVector << std::endl;
......
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