4

I basically have this problem: right now, we have a system where it gets a string as input, and it basically says ACTION:.

For each of the actions there is an automatically generated function(Rational Rose GRRR), such as

bouncer_comm.chatMessage("data goes here").sendAt(msg->sapIndex0());
bouncer_comm.askforname().sendAt(msg->sapindex0());

bouncer_comm returns an RTOutSignal, I can't create them manually because of the bizarre structure rose uses.

Right now, my only option is to create a hundred or so if statements, where I do:

if(action == "CHAT")  bouncer_comm.chatMessage("data goes here").sendAt(msg->sapIndex0());

Which is realllllyy annoying.

What would be the best way to avoid this? I've looked at / tried countless things, this is a really old version of rational rose (pre 2k) and yeah.

If anyone has any ideas that would be amazing.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
UberJumper
  • 20,245
  • 19
  • 69
  • 87

5 Answers5

12

I like @cobbal's idea of the function pointer hash above, but you could replace this conditional logic with polymorphism.

see: http://c2.com/cgi/wiki?ReplaceConditionalWithPolymorphism

killingmichael
  • 186
  • 2
  • 6
6

A hash storing function pointers could work well here

cobbal
  • 69,903
  • 20
  • 143
  • 156
  • Could you perhaps care to elaborate? I tried making a map of function pointers but since they take different values it causes problems. – UberJumper Mar 21 '09 at 21:29
  • I'd agree if this were C and not C++, but since it's the latter, would have to favor the polymorphic approach – Dexygen Mar 21 '09 at 21:51
  • If each method has different signatures, avoiding function pointers is probably in your interest. – Robert P Mar 21 '09 at 21:53
  • To be honest, I know very little about boost. Sounds like these other suggestions will work better. – cobbal Mar 21 '09 at 22:01
2

I used polymorphism combined with the factory pattern. I reduced a lot of if's to this :


MyAbstractClass *ac = Factory::getHandlerFor(data);
ac->perform(parameters);
Geo
  • 93,257
  • 117
  • 344
  • 520
1

I think the easiest is a map of boost::functions.

rlbond
  • 65,341
  • 56
  • 178
  • 228
0

You could use boost::bind or boost::function and a map. This would allow you to call the correct function, even know each function has a different amount of parameters.

If you don't want any extra code you could use function objects and inheritance.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636