Boost logo

Boost :

Subject: Re: [boost] fusion concepts
From: Robert Ramey (ramey_at_[hidden])
Date: 2011-06-14 00:38:24


Joel de Guzman wrote:
> On 6/14/2011 5:41 AM, Robert Ramey wrote:
>>
> Robert, I can't read the code. Encrypted?

///////////////////////////////////////////
// fusion/concepts.hpp

#ifndef _FUSION_CONCEPTS_
#define _FUSION_CONCEPTS_

// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif

#include <boost/concept/assert.hpp>
#include <boost/concept_check.hpp>
#include <boost/fusion/iterator.hpp>
#include <boost/fusion/sequence.hpp>
#include <boost/mpl/print.hpp>

namespace boost {
namespace fusion {

template <typename I>
struct ForwardIterator :
    boost::EqualityComparable<I>,
    boost::CopyConstructible<I>
{
public:
    BOOST_CONCEPT_USAGE(ForwardIterator)
    {
        typedef I J;
        // verify the the following exist
        typedef typename boost::fusion::result_of::next<I>::type t1;
// BOOST_CONCEPT_ASSERT(( ForwardIterator<t1> ));
        typedef typename boost::fusion::result_of::equal_to<I, J>::type t2;
        typedef typename boost::fusion::result_of::advance_c<I,0>::type t3;
        typedef typename boost::fusion::result_of::advance<I ,M>::type t4;
        typedef typename boost::fusion::result_of::distance<I ,J>::type t5;
        // distance should be convertable to a signed int
        int x = boost::fusion::distance(i, j);
        typedef typename boost::fusion::result_of::deref<I>::type t6;
        typedef typename boost::fusion::result_of::value_of<I>::type t7;
        same_type(i == j, b);
        same_type(i != j, b);
        same_type(deref(i), v);;
        same_type(*i, v);
    }

private:
    I i, j;
    bool b;
    typename boost::fusion::result_of::value_of<I>::type v;
    typedef boost::mpl::int_<0>::type M;

    // Type deduction will fail unless the arguments have the same type.
    template <typename T>
    void same_type(T const&, T const&);
};

template <typename S>
struct ForwardSequence {
public:
    BOOST_CONCEPT_USAGE(ForwardSequence)
    {
        // verify the the following exist
        typedef typename result_of::begin<S>::type t1;
        BOOST_CONCEPT_ASSERT(( ForwardIterator<t1> ));
        t1 x1 = begin(s);
        typedef typename result_of::end<S>::type t2;
        BOOST_CONCEPT_ASSERT(( ForwardIterator<t2> ));
        t2 x2 = end(s);
        typedef typename result_of::size<S>::type t3;
        t3 x3 = size(s);
        typedef typename result_of::empty<S>::type t4;
        t4 x4 = empty(s);
        typedef typename result_of::front<S>::type t5;
        t5 x5 = front(s);
    }

private:
    S s;

    // Type deduction will fail unless the arguments have the same type.
    template <typename T>
    void same_type(T const&, T const&);
};

} // fusion
} // boost

#include <boost/fusion/iterator/key_of.hpp>
#include <boost/fusion/iterator/value_of_data.hpp>
#include <boost/fusion/iterator/deref_data.hpp>

namespace boost {
namespace fusion {

template <typename I>
struct AssociativeIterator :
    ForwardIterator<I>
{
public:
    BOOST_CONCEPT_USAGE(AssociativeIterator)
    {
        // verify the the following exist
        typedef typename boost::fusion::result_of::key_of<I>::type t1;
        typedef typename boost::fusion::result_of::value_of_data<I>::type
t2;
        typedef typename boost::fusion::result_of::deref_data<I>::type t3;
        same_type(deref_data(i),
boost::fusion::result_of::deref_data<I>::type);
    }

 private:
    I i;
    // Type deduction will fail unless the arguments have the same type.
    template <typename T>
    void same_type(T const&, T const&);
};

} // fusion
} // boost

/*
Bidirectional Iterator
Random Access Iterator
Associative Iterator

Forward Sequence
Bidirectional Sequence
Random Access Sequence
Associative Sequence

Callable Object
Regular Callable Object
Deferred Callable Object
Polymorphic Function Object
*/

#endif // _FUSION_CONCEPTS_

///////////////////////////////////////////////
// test_concepts.cpp

#include <boost/concept/assert.hpp>
#include "concepts.hpp"

namespace boost {
namespace fusion{

struct ForwardIteratorArchtype {
    bool operator==(const ForwardIteratorArchtype &) const;
    bool operator!=(const ForwardIteratorArchtype &) const;
    int operator*();
};

namespace traits {
    template <>
    struct category_of<ForwardIteratorArchtype>
    {
        typedef forward_traversal_tag type;
    };
}

namespace result_of {
    template<>
    struct next<ForwardIteratorArchtype> {
        typedef ForwardIteratorArchtype type;
    };
    template<typename I>
    struct equal_to<ForwardIteratorArchtype, I> {
        typedef typename boost::is_same<I, ForwardIteratorArchtype>::type
type;
    };
    template<int N>
    struct advance_c<ForwardIteratorArchtype, N> {
        typedef ForwardIteratorArchtype type;
    };
    template<typename N>
    struct advance<ForwardIteratorArchtype, N> {
        typedef ForwardIteratorArchtype type;
    };
    template<>
    struct distance<ForwardIteratorArchtype, ForwardIteratorArchtype> {
        typedef int type;
    };
    template<>
    struct deref<ForwardIteratorArchtype> {
        typedef int type;
    };
    template<>
    struct value_of<ForwardIteratorArchtype> {
        typedef int type;
    };
} // result_of

result_of::next<ForwardIteratorArchtype>::type
next(ForwardIteratorArchtype &);

template <int N>
typename result_of::advance_c<ForwardIteratorArchtype, N>::type const
advance_c(ForwardIteratorArchtype const& i);
void f(){
    ForwardIteratorArchtype i;
    advance_c<1>(i);
}

template <typename M>
typename result_of::advance<ForwardIteratorArchtype, M>::type const
advance(ForwardIteratorArchtype const & i);
void g(){
    ForwardIteratorArchtype i;
    advance<boost::mpl::int_<1> >(i);
}

result_of::deref<ForwardIteratorArchtype>::type
deref(ForwardIteratorArchtype const& i);

result_of::deref<ForwardIteratorArchtype>::type
value_of(ForwardIteratorArchtype const& i);

result_of::distance<ForwardIteratorArchtype, ForwardIteratorArchtype>::type
distance(ForwardIteratorArchtype const& i, ForwardIteratorArchtype const&
j);

} // fusion
} // boost

BOOST_CONCEPT_ASSERT((
    boost::fusion::ForwardIterator<
        boost::fusion::ForwardIteratorArchtype
>
));

namespace boost {
namespace fusion{

struct AssociativeIteratorArchtype :
    ForwardIteratorArchtype
{
    //int operator*();
};

namespace traits {
    template <>
    struct category_of<AssociativeIteratorArchtype>
    {
        typedef associative_tag type;
    };
}

namespace result_of {
    template<>
    struct boost::fusion::result_of::key_of<AssociativeIteratorArchtype> {
        typedef AssociativeIteratorArchtype type;
    };
    template<>
    struct value_of_data<AssociativeIteratorArchtype> {
        typedef int type;
    };
    template<>
    struct deref_data<AssociativeIteratorArchtype> {
        typedef int type;
    };
} // result_of

result_of::deref_data<AssociativeIteratorArchtype>::type
deref_data(AssociativeIteratorArchtype &);

} // fusion
} // boost

BOOST_CONCEPT_ASSERT((
    boost::fusion::AssociativeIterator<
        boost::fusion::AssociativeIteratorArchtype
>
));

>
> Regards,


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk