views:

107

answers:

2

While making a state system that follows the state design pattern (which is working quite well so far) and I am now wondering if there is a way to send arbitrary data to this system. I was thinking that this might be possible using a Stimulus class.

The system itself is composited into another object that can respond to the stimuli, and both the state machine and the states themselves can have stimuli as well, and they will be passed from the outer to the inner levels via function calls. The problem being that the stimuli need to carry arbitrary data to these different levels, and I can't think of a simple way to get it out.

I was thinking that it might be possible using a dynamic_cast, but I was wondering if there might be a better way.

+1  A: 

If it's truly arbitrary, dynamic_cast is your best bet. If you intend all parts of the state machine to work on a finite number of methods of the data, it would be more elegant to have all of the data classes you pass to the state machine inherit from a pure virtual class that describes those methods, then do all passing via that class.

Jekke
If the data classes all implement a particular interface ("pure virtual class"), then why bother with dynamic_cast at all? Why not simply call that interface?
Max Lybbert
+2  A: 

boost::any might be of use to you depending on what you are doing.

Keith Nicholas