|
Boost Users : |
Subject: Re: [Boost-users] variant: bounded types derive from same base class
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2009-08-03 11:33:59
Hi!
The only solution I can come up with is the declaration of the visitor in
your ABCBase as virtual function and visitor has a function template to
dispatch different types:
class ABCVisitor
{
public:
template<class T>
void visit(T& t)
{
// no visit implemented ...
}
};
class ABCBase
{
//... dtor, copy ctor, assignment operator etc.
public:
void accept_visitor(ABCVisitor& v)
{
do_accept_visitor(v);
}
private:
virtual void accept_visitor(ABCVisitor& v)=0;
};
class ABCDerived1
{
//... dtor, copy ctor, assignment operator etc.
void accept_visitor(ABCVisitor& v)
{
v.visit(*this);
}
};
template<>
void ABCVisitor::visit<ABCDerived1>(ABCDerived1& derived)
{
// handle derived here...
}
Hope that helps,
Ovanes
On Mon, Aug 3, 2009 at 4:22 PM, Hicham Mouline <hicham_at_[hidden]> wrote:
> > -----Original Message-----
> > From: boost-users-bounces_at_[hidden] [mailto:boost-users-
> > bounces_at_[hidden]] On Behalf Of Igor R
> > Sent: 03 August 2009 14:50
> > To: boost-users_at_[hidden]
> > Subject: Re: [Boost-users] variant: bounded types derive from same base
> class
> >
> > > My restriction is that f takes a ABC const-ref as an input.
> > > Can I construct the variant from the const ABC& abc
> > > Maybe like
> > > variant<D1, D2, ... Dn> v(abc);
> > >
> > > without deep copying. The Di objects are big.
> > >
> > > Then I would apply the visitation.
> > >
> > > Maybe storing variant<D1*, D2*, Dn*>...
> >
> > Yes, you can store ptrs in the variant.
> >
> > > So f would look like
> > >
> > > void f( const ABC& abc )
> > > {
> > > const VariantTypeDefProvidedByLib v(&abc);
> > >
> > > v.apply_visitor( YourVisitor );
> > > }
> >
> > You cannot construct the variant in f(), because you already lost the
> > compile-time type of the object - it's ABC now.
> > If you'd like to work with ABC's and virtual functions, then why do
> > you need variant? Variant is about preserving the compile-time type.
>
> Then that's not what I need.
> I need a runtime visitor pattern.
>
> Parts of the user code deal with ABCs, and the user also writes functions
> for specific derived types,
> that use non common public functions of the derived types.
>
> The f function still needs to dispatch to the appropriate piece of code
> depending on the ABC type.
>
> void f( const ABC& abc )
> {
>
> Switch (abc.getTag())
> {
> case D1tag: // do something
> ...
> Case Dntag:
> }
> } // I get the warning from g++ if I miss 1 case
>
>
> Or
>
> void f( const ABC& abc )
> {
> if (dynamic_cast<D1*>(&abc))
> // do something
> ...
> if (dynamic_cast<Dn*>(&abc))
> // do something
> } // no warnings here If I miss a case
>
>
> Maybe what I want is just not possible,
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
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