Instantly share code, notes, and snippets.

Embed
What would you like to do?
TMP mp11 & boost::fusion Exercise...
#include <boost/mp11.hpp>
#include <boost/mp11/mpl.hpp>
#include <boost/fusion/sequence/intrinsic/at_c.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/fusion/tuple/tuple.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/support/deduce_sequence.hpp>
//#include <boost/fusion/adapted.hpp>
#include <tuple>
struct post
{
int title,desc;
};
BOOST_FUSION_ADAPT_STRUCT(post,title, desc)
namespace tagtype{
//tagtypes
struct SingleLine{};
struct MultiLine{};
// a template connecting types and tags
template<class TaggedType, typename Tag1, typename... Tags>
class tag_view
{
TaggedType* value=nullptr;
using myTags = boost::mp11::mp_list<Tag1,Tags...>;
public:
tag_view(){}
tag_view(TaggedType& val):value(&val){}
template<class Tag>
constexpr bool hasTag()const
{
return boost::mp11::mp_contains<myTags, Tag>::value;
}
};
template<class R, class Seq, size_t... I>
R tuple_from_seq( Seq& s, std::index_sequence<I...> ) { return R{ boost::fusion::get<I>(s)... }; }
//create a tuple of tag_views for the member of an fusion adapted struct
template<class seq, class... List >
auto create_tag_views( seq& s, List&&... )
{
std::size_t const N = sizeof...(List);
using taglist =typename boost::mp11::mp_list< List...>;
using sequence = typename boost::fusion::traits::deduce_sequence<seq>::type;
static_assert(N == boost::mp11::mp_size<sequence>::value,"List of tags must be the same size as members in struct");
using R = typename boost::mp11::mp_append<std::tuple<>, boost::mp11::mp_transform< tag_view,sequence,taglist>>;
//R tuple;
//boost::mp11::mp_for_each<boost::mp11::mp_iota_c<N>>( [&]( auto I ){ std::get<I>(tuple) = boost::fusion::get<I>(s); } );
//Assign2Tuple<N-1>::call(s,tuple);// N-1 because its 0 based index N-1 -> 0
return tuple_from_seq<R>(s,std::make_index_sequence<N>());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment