Boost logo

Boost Users :

From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2007-08-21 07:42:20


On 8/21/07, Martin Apel <martin.apel_at_[hidden]> wrote:
>
> Hi James,
> >> the easiest way is something like the following:
> >>
> >> #include <boost/type_traits.hpp>
> >> #include <boost/static_assert.hpp>
> >>
> >> BOOST_STATIC_ASSERT (boost::is_base_of<Base, Derived>::value);
> >>
> >> Hope this helps,
> >>
> >> Martin
> >>
> >
> > Thanks Martin - I knew I had seen it somewhere.
> >
> > That covers the first point, but I've just realised I need to be a bit
> > cleverer. The incoming type can either be the type itself or a pointer
> > to the type, or a boost::shared_ptr to the type, and the type must be
> > derived as the original question. Depending on which it is I need to do
> > slightly different things, so I need to, effectively, conditionally
> > compile a bit of code depending on the results of the tests, or assert
> > if the type (after getting rid of the pointer/shared point bit) isn't
> > derived from my base class.
> >
> > Basically I'm iterating through a vector of the incoming type and
> > calling a function in the type stored. That function comes from the base
> > class, but in order to be really generic I need to handle the shared_ptr
> > and pointer cases.
> >
> > I think I need mpl for that, but I only get to chapter three of the book
> > before my head starts to hurt. Any ideas/hints/places I can look?
> >
> >
> I'm not sure if you really need mpl to do this. To me it looks as if you
> need one template
> and multiple partial specialization, something like the following:
>
> template<typename T>
> class Handler
> {
> public:
> void doIt (T val)
> {
> .... // default implementation
> }
> };
>
> template<typename T>
> class Handler<boost::shared_ptr<T> >
> {
> public:
> void doIt (boost::shared_ptr<T> val)
> {
> ... // Impl for shared_ptr
> }
>
> template <typename T>
> class Handler<T*>
> ....
>
>
> Hope this helps,
>
> Martin
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

Actually you can use only one Handler class and some templated function
overloads:

template<class Base>
class Hanlder
{
    template<class Derived>
    void do_it(T const& t, typename boost::enable_if<
boost::is_base_of<Base, T> >::type*=0)
    {
      //do smth.
    }

   template<class Derived>
   void do_it(boost::shared_ptr<T> t, typename boost::enable_if<
boost::is_base_of<Base, T> >::type*=0)
   {
     //do smth.
   }

   ....

};



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