paperjam
paperjam

Reputation: 8518

Boost Fusion/MPL: convert type from sequence to sequence of equivalent any_range's

I want to use Boost's any_range to handle multiple heterogeneous data ranges. The type of my data ranges is known as a Fusion vector, for example:

typedef vector<double, int, char> TypeSequence

Given such a type, I want to write a template to derive a further type like this:

vector<AnyRange<double>::value, AnyRange<int>::value, AnyRange<char>::value>

where AnyRange is defined as:

using namespace boost;
template <typename T>
struct AnyRange
{
    typedef typename any_range<typename T, forward_pass_traversal_tag, int, std::ptrdiff_t> value;
};

I've tried and failed. Is this even possible with Fusion? MPL? Or perhaps I'm heading down the wrong path with any_range.

Upvotes: 3

Views: 2374

Answers (1)

Luc Touraille
Luc Touraille

Reputation: 82061

You can do this easily using boost::mpl::transform, which you can use with Fusion sequences (as long as you include the appropriate headers to make Fusion sequences behave as confirming MPL sequences):

#include <boost/range/any_range.hpp>

#include <boost/fusion/include/mpl.hpp> // Required to adapt Fusion to MPL
#include <boost/fusion/include/vector.hpp>

#include <boost/mpl/transform.hpp>


template < typename T >
struct EmbedInAnyRange
{
    typedef boost::any_range< // no need for typename here
        T,                    // no need for typename here
        forward_pass_traversal_tag, 
        int,                  // not sure what this parameter is, I leave int...
        std::ptrdiff_t
    > type;
};

int main()
{
    typedef boost::fusion::vector< double, int, char > Tuple;

    typedef boost::mpl::transform<
        Tuple,
        EmbedInAnyRange< boost::mpl::_ >
    >::type AnyRangeTuple;

    AnyRangeTuple myTuple( 
        std::vector< double >(), 
        std::list< int >(), 
        std::vector< char >() );
}

If you want, you can put the transformation into its own metafunction:

template < typename Seq >
struct EmbedAllInAnyRange
{
    typedef typename boost::mpl::transform< // typename needed
        Seq,
        EmbedInAnyRange< boost::mpl::_ >
    >::type type;
};

...

typedef EmbedAllInRange< Tuple >::type AnyRangeTuple;

Upvotes: 7

Related Questions