Boost logo

Boost Users :

Subject: [Boost-users] Copy MPL sequence at runtime?
From: Rhys Ulerich (rhys.ulerich_at_[hidden])
Date: 2010-03-18 03:13:00


Evening all,

I'd like to copy the contents of a boost::mpl::vector_c (or similar)
into an output iterator at runtime. I've not found a clean way to get
an MPL sequence into a runtime-usable form. So far, I've gotten the
included ugliness to work.

0) Is there some simple, idiomatic way to accomplish this goal that I've missed?
1) Any suggestions for how to clean this up? Specifically ways to
reduce the assumptions about the underlying MPL sequence or fixes for
braindead mistakes I've made?
2) Is there some way to have syntax like 'seqcopy<S>(iter)' where the
iterator type is deduced but the sequence type is explicit?

Thanks,
Rhys

#define BOOST_TEST_MODULE $Id: test_mpl.cc 996 2010-03-18 07:04:23Z rhys $

#include <boost/mpl/empty.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/pop_front.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/utility.hpp>
#include <boost/test/included/unit_test.hpp>

using boost::disable_if;
using boost::mpl::empty;
using boost::mpl::front;
using boost::mpl::pop_front;

template<typename S, typename FI, class Enable = void>
struct seqcopy {
    void operator()(FI fi) {}
};

template<typename S, typename FI>
struct seqcopy<S, FI, typename disable_if<typename empty<S>::type>::type> {
    void operator()(FI fi) {
        *fi++ = front<S>::type::value;
        seqcopy<typename pop_front<S>::type, FI>()(fi);
    }
};

BOOST_AUTO_TEST_CASE( testing )
{
    typedef boost::mpl::vector_c<int,2,4,6> values;
    int a[3];

    seqcopy<values, int*>()(a);

    BOOST_CHECK_EQUAL(a[0], 2);
    BOOST_CHECK_EQUAL(a[1], 4);
    BOOST_CHECK_EQUAL(a[2], 6);
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net