3

I have a map where the value is a weak pointer. This works: While I can write this:

for_each( IFoo::foo_wptr obj, objects | range::map_values ) {
    IFoo::foo_ptr myObj = obj.lock();
    if( myObj ) myObj->notify();
}

I'd much rather have a new range that transforms to a locked shared pointer. Something like this:

for_each( IFoo::foo_ptr obj, objects | range::map_values | range::locked ) {
    if( obj ) obj->notify();
}

however, I've been unable to figure out what that transform should look like; or if it should even be a transform.

Does anyone have an idea? I'm convinced this pattern may quite common.

4

1 回答 1

0

这是一个我刚刚拼凑起来的例子:

#include <boost/iterator/transform_iterator.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/range/adaptor/map.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/foreach.hpp>
#include <map>
#include <vector>

struct weak_ptr_locker
{
    template <typename Sig>
    struct result;

    template <typename F, typename T>
    struct result<F(boost::weak_ptr<T>&)>
    {
        typedef boost::shared_ptr<T> type;
    };

    template <typename T>
    boost::shared_ptr<T> operator()(const boost::weak_ptr<T>& pWeak) const
    {
        return pWeak.lock();
    }
};

template<typename R>
struct lock_transform_range :
    boost::iterator_range< boost::transform_iterator< weak_ptr_locker, typename         boost::range_iterator<R>::type> >
{
private:
    typedef boost::iterator_range< boost::transform_iterator< weak_ptr_locker, typename     boost::range_iterator<R>::type> > base;

public:
    typedef boost::transform_iterator< weak_ptr_locker, typename     boost::range_iterator<R>::type > iterator;

    lock_transform_range(R& r)
        : base(iterator(boost::begin(r)), iterator(boost::end(r)))
    {}
};

namespace detail
{
    struct lock_transform_forwarder {};
}

template< class BidirectionalRng >
inline lock_transform_range<BidirectionalRng> operator | (BidirectionalRng& r,     detail::lock_transform_forwarder)
{
    return lock_transform_range<BidirectionalRng>(r);
}

template< class BidirectionalRng >
inline lock_transform_range<const BidirectionalRng> operator | (const BidirectionalRng&     r, detail::lock_transform_forwarder)
{
    return lock_transform_range<const BidirectionalRng>(r);
}

namespace
{
    const detail::lock_transform_forwarder locked = detail::lock_transform_forwarder();
}

struct Foo
{
    Foo(int i)
        : i(i)
    {}

    int i;
    void notify()
    {
        std::cout << i << std::endl;
    }
};

int main(int argc, char* argv[])
{
    typedef std::map<int, boost::weak_ptr<Foo> > Map;
    typedef boost::shared_ptr<Foo> FooPtr;
    Map objects;

    std::vector<FooPtr> storage;
    for (int i = 0; i < 10; ++i)
    {
        storage.push_back(boost::make_shared<Foo>(i));
        objects[i] = storage.back();
    }

    using namespace boost::adaptors;
    for_each(FooPtr obj, objects | map_values | locked) 
    {
        if (obj) 
            obj->notify();
    }
    return 0;
}
于 2014-07-15T19:28:52.923 回答