Boost logo

Boost Users :

Subject: Re: [Boost-users] [fusion] Wrapping a Fusion sequence
From: Agustín K-ballo Bergé (kaballo86_at_[hidden])
Date: 2012-09-04 17:11:18


On 31/08/2012 11:02 p.m., Agustín K-ballo Bergé wrote:
> On 31/08/2012 12:05 a.m., Joel de Guzman wrote:
>> On 8/29/2012 7:10 AM, Agustín K-ballo Bergé wrote:
>>> I'm looking for a way to create a Fusion sequence wrapper that is
>>> itself a Fusion sequence
>>> and forwards all 'calls' to its wrapped sequence.
>>>
>>> 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?
>>
>> I do not know an easy way other than using the Sequence Facade:
>> http://tinyurl.com/8k5wq5p
>>
>> Regards,
>
> Thank you for your answer. I was hoping there was a simpler way given
> that, as far as I understand, most (all?) Fusion containers are
> implemented as wrappers over vector. Also, its still a bit unclear to me
> how does sequence_facade relate to the full extension mechanism.
>
> Here is my first attempt at a sequence wrapper:
>

Here is my second attempt, using references where appropriate and
propagating constness:

     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;
     };

Agustín K-ballo Bergé.-
http://fusionfenix.com


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