Search code examples
c++boost-fusion

Wrapping a Boost.Fusion Sequence


I'm looking for a way to create a Boost.Fusion sequence wrapper that is itself a Fusion sequence and forwards all 'calls' to its wrapped sequence. Something in the lines of

template< typename Sequence >
struct sequence_wrapper
{
    explicit sequence_wrapper( Sequence const& s ) : seq( s ){}

    Sequence seq;
};

where sequence_wrapper< Sequence > is a Fusion sequence as well, and works just as Sequence would. The reason I need this is that I have several functions that operate on Fusion sequences (where all elements satisfy some special requirements), and I would like to add some syntax sugar for which I need a custom type to add overloaded operators to. I do not need the result of operations on a sequence_wrapper to return a sequence_wrapper as well, only the syntax sugar related calls would return a (manually) wrapped sequence. For instance, appending elements to a sequence using the comma operator (somewhat of a Boost.Assign for Fusion sequences):

template< typename Sequence, typename T >
sequence_wrapper<
    typename boost::fusion::result_of::push_back<
        Sequence const&
      , T
    >::type
> operator ,( Sequence const& seq, T const& v )
{
    return
        sequence_wrapper<
            typename boost::fusion::result_of::push_back<
                Sequence const&
              , T
            >::type
        >( boost::fusion::push_back( seq, v ) )
        ;
}

What would be the best way to achieve this (if it is indeed supported by the library)? I'm particularly trying to avoid creating a Fusion sequence from scratch, as I would like to use whatever sequence is returned by Fusion operations. Would inheritance + specialization of tag_of to return the tag of the wrapped sequence just work? Or will I need to define a tag of my own and implement all required functions to just forward the call?


Solution

  • This is what I ended up doing:

    template<
        typename Derived
      , typename Sequence
      , typename TraversalTag = 
            typename boost::fusion::traits::category_of< Sequence >::type
      , typename IsView =
            typename boost::fusion::traits::is_view< Sequence >::type
    >
    class fusion_sequence_wrapper
      : public boost::fusion::sequence_facade< Derived, TraversalTag, IsView >
    {
        typedef Sequence base_sequence_type;
    
    public:
        explicit fusion_sequence_wrapper( base_sequence_type const& sequence )
          : _seq( sequence )
        {}
    
        base_sequence_type const& base() const
        {
            return _seq;
        }
        base_sequence_type& base()
        {
            return _seq;
        }
    
    public:
        template< typename Seq >
        struct begin
        {
            typedef
                typename boost::fusion::result_of::begin<
                    typename boost::mpl::if_<
                        boost::is_const< Seq >
                      , base_sequence_type const
                      , base_sequence_type
                    >::type
                >::type type;
    
            static type call( Seq& s ){ return boost::fusion::begin( s._seq ); }
        };
    
        template< typename Seq >
        struct end
        {
            typedef
                typename boost::fusion::result_of::end<
                    typename boost::mpl::if_<
                        boost::is_const< Seq >
                      , base_sequence_type const
                      , base_sequence_type
                    >::type
                >::type type;
    
            static type call( Seq& s ){ return boost::fusion::end( s._seq ); }
        };
    
        template< typename Seq >
        struct size
        {
            typedef
                typename boost::fusion::result_of::size<
                    typename boost::mpl::if_<
                        boost::is_const< Seq >
                      , base_sequence_type const
                      , base_sequence_type
                    >::type
                >::type type;
    
            static type call( Seq& s ){ return boost::fusion::size( s._seq ); }
        };
    
        template< typename Seq >
        struct empty
        {
            typedef
                typename boost::fusion::result_of::empty<
                    typename boost::mpl::if_<
                        boost::is_const< Seq >
                      , base_sequence_type const
                      , base_sequence_type
                    >::type
                >::type type;
    
            static type call( Seq& s ){ return boost::fusion::empty( s._seq ); }
        };
    
        template< typename Seq, typename N >
        struct at
        {
            typedef
                typename boost::fusion::result_of::at<
                    typename boost::mpl::if_<
                        boost::is_const< Seq >
                      , base_sequence_type const
                      , base_sequence_type
                    >::type
                  , N
                >::type type;
    
            static type call( Seq& s ){ return boost::fusion::at( s._seq ); }
        };
    
        template< typename Seq, typename N >
        struct value_at
        {
            typedef
                typename boost::fusion::result_of::value_at<
                    typename boost::mpl::if_<
                        boost::is_const< Seq >
                      , base_sequence_type const
                      , base_sequence_type
                    >::type
                  , N
                >::type type;
        };
    
    private:
        base_sequence_type _seq;
    };