Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2001-12-29 15:27:16


On Saturday, December 29, 2001, at 02:18 PM, Dan Nuffer wrote:

> Recently I faced the same sort of problem when making the multi_pass
> iterator of Spirit into a policy-based class. multi_pass is similar in
> a lot of way to Loki's SmartPtr. I ran into the problem where one
> policy needed to acess member functions of another policy. I solved the
> problem by changing this:
>
> ValueT dereference()
> {
> if (queuePosition == queuedElements->end())
> {
> return get_input(); // here's the problem! This policy can't
> access get_input b/c it's part of the InputPolicy
> }
> else
> {
> return *queuePosition;
> }
> }
>
> to this:
>
> template <typename MultiPassT>
> static ValueT dereference(MultiPassT const& mp)
> {
> if (mp.queuePosition == mp.queuedElements->end())
> {
> return mp.get_input();
> }
> else
> {
> return *mp.queuePosition;
> }
> }
>
> So, I basically switched the functions that needed to acess other
> policies to static template member functions that were passed a
> reference to the main class.

This is exactly the approach taken by the Boost Iterator Adaptors
Library (shouldn't spirit be using that library for its iterators?).
However, the problem Beman is dealing with has to do, in part, with the
fact that there isn't a well-defined scope for the interface of a smart
pointer as there is for an iterator. We should keep in mind the domains
are different in this regard.

If an analogy between smart pointers and iterators /does/ apply, I think
the Policy Adaptor pattern described in the paper Jeremy and I wrote at
http://www.boost.org/libs/utility/iterator_adaptors.pdf may be helpful
in simplifying the design. By collecting all of the policies in one
place, managing their interactions is simpler. However, it depends on
being able to identify the core behaviors of the domain. If we think
that we'll keep discovering new elements to the interface of smart
pointers, some sort of policy inheritance will be neccessary.

-Dave


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