Boost logo

Boost Users :

From: Tobias Schwinger (tschwinger_at_[hidden])
Date: 2007-05-24 17:50:37


Rereading my quick post from yesterday I notice:

The visit facility could be generalized further to deduce the base type,
so 'visit' is not limited to 'ast::node' arguments.

We can also change the interface of 'visit' to hide all the pointers
from the user (taking a reference instead and applying boost::addressof
in case operator& is overloaded):

   namespace detail
   {
       template <class Last, class T, class Visitor>
       bool visit(Last, Last, T *, Visitor &)
       {
           throw "bummer";
       }
       template <class First, class Last, class T, class Visitor>
       bool visit(First, Last, T * object, Visitor & v)
       {
           typedef typename mpl::deref<First>::type type;
           type * typed_object = dynamic_cast<type *>(object);

           if (! typed_object)
               detail::visit(
                   typename mpl::next<First>::type()
                 , Last()
                 , object
                 , v
               );
           else
               v(*typed_object);
       }
   }

   template<class T, class Visitor>
   bool visit(T & object, Visitor & with_visitor)
   {
       typedef typename Visitor::accepted_types seq;

       return detail::visit(
           typename mpl::begin<seq>::type()
         , typename mpl::end<seq>::type()
         , boost::addressof(object)
         , with_visitor
       );
   }

Regards,
Tobias


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