Skip to content
Snippets Groups Projects
Commit dc69a72c authored by Marco Agnese's avatar Marco Agnese
Browse files

deprecate ForLoop, ForEachValue and ForEachValuePair since they are only a...

deprecate ForLoop, ForEachValue and ForEachValuePair since they are only a useless wrapper around the generic Hybrid::forEach
parent 40752c75
No related branches found
No related tags found
2 merge requests!212Fix link to build system doc,!169Deprecate ForLoop, ForEachValue and ForEachValuePair
Pipeline #
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#warning The header dune/common/forloop.hh is deprecated. Use directly "Hybrid::forEach" and include dune/common/hybridutilities.hh.
#ifndef DUNE_COMMON_FORLOOP_HH
#define DUNE_COMMON_FORLOOP_HH
#include <utility>
#include <dune/common/deprecated.hh>
#include <dune/common/hybridutilities.hh>
#include <dune/common/std/utility.hh>
/** \file
* \brief A static for loop for template meta-programming
*/
namespace Dune
{
/** \class ForLoop
* @brief A static loop using TMP
*
* The ForLoop takes a
* \code template<int i> class Operation \endcode
* template argument with a static apply method
* which is called for i=first...last (first<=last are int template arguments).
* A specialization for class template class Operation for i=first
* or i=last is not required. The class Operation must provide a
* static void function apply(...). Arguments are perfectly forwarded
* through the ForLoop to this function.
*
* It is possible to pass a subclass to the ForLoop
* (since no specialization is needed).
*
* Example of usage:
* \code
* template<class Tuple>
* struct PrintTupleTypes
* {
* template <int i>
* struct Operation
* {
* template<class Stream>
* static void apply(Stream &stream, const std::string &prefix)
* {
* stream << prefix << i << ": "
* << className<typename tuple_element<i, Tuple>::type>()
* << std::endl;
* }
* };
* template<class Stream>
* static void print(Stream &stream, const std::string &prefix)
* {
* // cannot attach on-the-fly in the argument to ForLoop<..>::apply() since
* // that would yield an rvalue
* std::string extended_prefix = prefix+" ";
*
* stream << prefix << "tuple<" << std::endl;
* ForLoop<Operation, 0, tuple_size<Tuple>::value-1>::
* apply(stream, extended_prefix);
* stream << prefix << ">" << std::endl;
* }
* };
* \endcode
*/
template< template< int > class Operation, int first, int last >
struct ForLoop
struct DUNE_DEPRECATED_MSG("Use Hybrid::forEach instead!") ForLoop
{
static_assert( (first <= last), "ForLoop: first > last" );
......
......@@ -17,17 +17,6 @@ struct Eval
typedef void* Type;
};
struct Counter {
Counter() : result_(0) {}
template <class T1>
void visit(T1) { ++result_; }
template <class T1, class T2>
void visit(T1, T2) { ++(++result_); }
int result_;
};
int main(int, char**)
{
typedef std::tuple<int*,double*,long*,char*> PointerTuple;
......@@ -82,22 +71,6 @@ int main(int, char**)
}
#endif
PointerTuple1 pointers1 = Dune::NullPointerInitialiser<PointerTuple1>::apply();
if(static_cast<size_t>(std::tuple_size<PointerTuple>::value) != static_cast<size_t>(std::tuple_size<PointerTuple>::value)) {
std::cerr<<"Length and size do not match!"<<std::endl;
}
Counter count;
Dune::ForEachValue<PointerTuple> forEach(pointers);
forEach.apply(count);
std::cout << "Number of elements is: " << count.result_ << std::endl;
Dune::ForEachValuePair<PointerTuple,PointerTuple1> foreach1(pointers, pointers1);
foreach1.apply(count);
if(Dune::At<2>::get(pointers)!=std::get<1>(pointers)) {
ret+=10;
std::cerr<<"at inconsistent!"<<std::endl;
......
......@@ -8,6 +8,7 @@
#include <tuple>
#include <type_traits>
#include <dune/common/deprecated.hh>
#include <dune/common/hybridutilities.hh>
#include <dune/common/std/type_traits.hh>
#include <dune/common/std/utility.hh>
......@@ -316,105 +317,38 @@ namespace Dune {
}
};
/**
* @brief Helper template which implements iteration over all storage
* elements in a std::tuple.
*
* Compile-time constructs that allows one to process all elements in a std::tuple.
* The exact operation performed on an element is defined by a function
* object, which needs to implement a visit method which is applicable to
* all storage elements of a std::tuple. Each std::tuple element is visited once, and
* the iteration is done in ascending order.
*
* The following example implements a function object which counts the
* elements in a std::tuple
*
* \code
* template <class T>
* struct Counter
* {
* Counter() :
* result_(0)
* {}
*
* template <class T>
* void visit(T& elem)
* {
* ++result_;
* }
*
* int result_;
* };
* \endcode
*
* The number of elements in the std::tuple are stored in the member variable
* result_. The Counter can be used as follows, assuming a std::tuple t of type
* MyTuple is given:
*
* \code
* Counter c;
* ForEachValue<MyTuple> forEach(t);
*
* forEach.apply(c);
* std::cout << "Number of elements is: " << c.result_ << std::endl;
* \endcode
*/
template<class Tuple>
class ForEachValue
struct DUNE_DEPRECATED_MSG("Use Hybrid::forEach instead!") ForEachValue
{
public:
//! \brief Constructor
//! \param t The std::tuple which we want to process.
ForEachValue(Tuple& t) :
t_(t)
{}
//! \brief Applies a function object to each storage element of the std::tuple.
//! \param f Function object.
template<class Functor>
void apply(Functor& f) const
{
Hybrid::forEach(Std::make_index_sequence<std::tuple_size<Tuple>::value>{},
[&](auto i){f.visit(std::get<i>(t_));});
}
private:
Tuple& t_;
};
/**
* @brief Extension of ForEachValue to two std::tuple's.
*
* This class provides the framework to process two std::tuple's at once. It works
* the same as ForEachValue, just that the corresponding function object
* takes one argument from the first std::tuple and one argument from the second.
*
* \note You have to ensure that the two std::tuple's you provide are compatible
* in the sense that they have the same length and that the objects passed
* to the function objects are related in meaningful way. The best way to
* enforce it is to build the second std::tuple from the existing first std::tuple
* using ForEachType.
*/
template<class Tuple1, class Tuple2>
class ForEachValuePair
struct DUNE_DEPRECATED_MSG("Use Hybrid::forEach instead!") ForEachValuePair
{
public:
//! Constructor
//! \param t1 First std::tuple.
//! \param t2 Second std::tuple.
ForEachValuePair(Tuple1& t1, Tuple2& t2) :
t1_(t1),
t2_(t2)
{}
//! Applies the function object f to the pair of std::tuple's.
//! \param f The function object to apply on the pair of std::tuple's.
template<class Functor>
void apply(Functor& f)
{
Hybrid::forEach(Std::make_index_sequence<std::tuple_size<Tuple1>::value>{},
[&](auto i){f.visit(std::get<i>(t1_), std::get<i>(t2_));});
}
private:
Tuple1& t1_;
Tuple2& t2_;
};
......
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