Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How can I remove dynamic_cast and if statements from this code snippet?

Reply
Thread Tools

How can I remove dynamic_cast and if statements from this code snippet?

 
 
Bart v Ingen Schenau
Guest
Posts: n/a
 
      11-18-2011
Werner Wrote:

> On Nov 18, 12:09*am, Chris Stankevitz <(E-Mail Removed)>
> wrote:
> > On Nov 17, 11:21*am, <(E-Mail Removed)> wrote:
> >
> > > I think this is a typical usecase for the "visitor"pattern:

> >
> > Tobi, this is exactly what I was looking for. *Case closed.

>
> What happens when new shapes are added to the hierarchy?


Then the ShapeVisitor and derived visitors must be extended to support the
new shape.
This is no different from the original code with an if-else ladder based on
the result from dynamic_cast, except that the compiler is better able to
help warn you if you forgot to update a visitor.

>
> Kind regards,
>
> Werner


Bart v Ingen Schenau

 
Reply With Quote
 
 
 
 
Tobias Müller
Guest
Posts: n/a
 
      11-18-2011
Goran <(E-Mail Removed)> wrote:
> Visitor requires changing Shape and the rest of Lib A, which OP said
> didn't want elsethread.
>
> Goran.


Fair point. However, when reading the rest of the thread I had the
impression that what the OP really wanted is not to leave Lib A unchanged,
but rather not to include any XML specific features.
And a vistior interface is quite abstract and useful for a library like A.

And apparently the OP is happy with that solution.

Tobi.
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      11-18-2011
On Nov 16, 10:37*am, Chris Stankevitz <(E-Mail Removed)>
wrote:
> Hello,
>
> I would like to remove the "dynamic_cast" and "if" statements from the
> code below. *I believe some people would describe this as making the
> code "polymorphic". *Can you recommend a way to do this without
> modifying the original classes in "Library A"?
>
> My intention is to create an "XML Writer" class(es) for shapes without
> putting "XML code" in the base classes. *I plan to use a similar
> pattern for drawing and other tasks my application has to perform on
> Shape objects.


The adapted visitor pattern that people have shown here is one good
way to solve this. Another you could consider though is a multi-
dispatch mechanism separate from the two parts. This would retain the
dynamic_cast stuff, but you can use TMP to build that. Review _Modern
C++ Design_ by Alexandrescu.

Basically, something like so:

template < typename Seq, typename Enable = void >
struct dispatcher
{
typedef typename front<Seq>::type my_type;
typedef typename pop_front<Seq>::type next_seq;

static void write(Shape const& sh, Writer & wr)
{
if (my_type const* csh = dynamic_cast<my_type const*>(&sh))
wr.write(*csh);
else
dispatcher<next_seq>::write(sh);
}
}

template < typename Seq >
struct dispatcher<Seq, typename enable_if<empty<Seq> >::type>
{
static void write(Shape const&, Writer &) { throw
std::runtime_error("Unknown shape type."); }
};

void write_shape(Shape const& sh, Writer & w)
{
typedef vector<Square, Circle> shape_seq;
dispatcher<shape_seq>::write(sh,w);
}
 
Reply With Quote
 
Chris Stankevitz
Guest
Posts: n/a
 
      11-19-2011
Goran <(E-Mail Removed)> wrote:
> Visitor requires changing Shape and the rest of Lib A, which OP said
> didn't want elsethread.


On Nov 18, 8:56*am, Tobias Mller <(E-Mail Removed)> wrote:
> However, when reading the rest of the thread I had the
> impression that what the OP really wanted is not to leave Lib A unchanged,
> but rather not to include any XML specific features.


You are both correct. At first I said "without modifying LibA" to
discourage "just put the XML specific feature in LibA" (turned out
this didn't discourage the first few replies). Later, as it became
clear I did not pose my question well, my question evolved. As Tobi
picked up, I did not want XML specific features in Library A but was
willing to make changes to Library A. However, I can imagine
scenarios in which LibA is not alterable -- in such a case Tobi's
solution would not work. I apologize for leading the group to believe
that.

> And a vistior interface is quite abstract and useful for a library like A..
>
> And apparently the OP is happy with that solution.


Yes! Not only was I happy with it, when I saw it I knew immediately
that this was what I was thinking of -- I just didn't have a name for
it or know how to go about it or even know if it was possible with c+
+.

Thank you,

Chris
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
if statements and case statements questions John Crichton Ruby 6 07-12-2010 06:17 PM
Prepare Statements VS Statements Vince Java 12 01-21-2008 01:18 PM
component statements within architecture statements Neil Zanella VHDL 8 10-20-2006 09:05 AM
if statements with or w/o else statements Harry George Python 6 02-23-2004 06:48 PM



Advertisments