Boost logo

Boost :

Subject: [boost] [range] MFC/ATL select_first / select_second specializations for map_keys and map_values adaptors
From: Adam Walling (adam.walling_at_[hidden])
Date: 2012-03-10 20:47:24


I enjoy using the range library, and also foreach. When dealing with legacy
code, the MFC / ATL range adaptors are wonderful. Unfortunately, the CMap
and CMapStringToString iterators return CPair objects rather than
std::pair, hence causing the select_first / select_second_mutable /
select_second_const metafunctions to fail.

I added these specializations to allow them to work with the map_keys and
map_values adaptors. Hopefully they will be of use to someone in the future
or adapted to be included with the other MFC / ATL code.

Would it be worthwhile to format this as a patch to the library?

namespace boost
{
    namespace range_detail
    {
        // CMap and CMapStringToString range iterators return CPair,
        // which has a key and value member. Other MFC range iterators
        // already return adapted std::pair objects. This allows usage
        // of the map_keys and map_values range adaptors with CMap
        // and CMapStringToString

        // specialization for CMap
        template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
        struct select_first<CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>>
        {
            typedef BOOST_DEDUCED_TYPENAME CMap<KEY, ARG_KEY, VALUE,
ARG_VALUE> map_type;
            typedef BOOST_DEDUCED_TYPENAME range_reference<const
map_type>::type argument_type;
            typedef BOOST_DEDUCED_TYPENAME const KEY& result_type;

            result_type operator()( argument_type r ) const
            {
                return r.key;
            }
        };

        template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
        struct select_second_mutable<CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>>
        {
            typedef BOOST_DEDUCED_TYPENAME CMap<KEY, ARG_KEY, VALUE,
ARG_VALUE> map_type;
            typedef BOOST_DEDUCED_TYPENAME range_reference<map_type>::type
argument_type;
            typedef BOOST_DEDUCED_TYPENAME VALUE& result_type;

            result_type operator()( argument_type r ) const
            {
                return r.value;
            }
        };

        template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
        struct select_second_const<CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>>
        {
            typedef BOOST_DEDUCED_TYPENAME CMap<KEY, ARG_KEY, VALUE,
ARG_VALUE> map_type;
            typedef BOOST_DEDUCED_TYPENAME range_reference<const
map_type>::type argument_type;
            typedef BOOST_DEDUCED_TYPENAME const VALUE& result_type;

            result_type operator()( argument_type r ) const
            {
                return r.value;
            }
        };

        // specialization for CMapStringToString
        template<>
        struct select_first<CMapStringToString>
        {
            typedef range_reference<const CMapStringToString>::type
argument_type;
            typedef const CString& result_type;

            result_type operator()( argument_type r ) const
            {
                return r.key;
            }
        };

        template<>
        struct select_second_mutable<CMapStringToString>
        {
            typedef range_reference<CMapStringToString>::type argument_type;
            typedef CString& result_type;

            result_type operator()( argument_type r ) const
            {
                return r.value;
            }
        };

        template<>
        struct select_second_const<CMapStringToString>
        {
            typedef range_reference<const CMapStringToString>::type
argument_type;
            typedef const CString& result_type;

            result_type operator()( argument_type r ) const
            {
                return r.value;
            }
        };
    }
}

-- 
- Adam D. Walling



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