"Victor Bazarov" <v.Abazarov@comAcast.net> a ?crit dans le message de news:
fdei4e$d60$1@news.datemas.de...
pcrepieux wrote:
I recently meet a problem while "playing" with the state pattern. I
was wondering if each of the member function dedicated to handle
events open(), close(), ack() could be change to something like
process(openEv& ev), process(closeEv& ev), ... no problem with this
point. Going further in this way, i thought that the process member
function would be a great candidate for a template. Hum ... it is
not. The process function have to be virtual in the base state class.
Trying to approach something similar, here is what i did :
I used a variant type (boost::variant) that holds any of my event:
typedef boost::variant<pdu1,pdu2,pdu3> pdu;
In the base state class I defined a virtual process(pdu p) and then a
process(pdu p) in each concrete state class that uses a visitor
(inherited from boost::static_visitor) to call the process(xxxEv).
I'd like to know if this is a pertinent solution or if this simply
highlight that if i need to do something like that, something is
wrong in the design.
I would probably make all events inherit from one base Event class
and then make 'process' accept a reference to that, instead of the
'boost::variant'. Every derived class then could check if he has
received the "correct" event by using 'dynamic_cast' or some other
RTTI (or pseudo-RTTI) way. It just seems to me that RT polymorphism
is much more flexible than CT one.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
In fact as i can know the type at compile time, i just wanted to avoid
checking it at runtime. I think this would involve something like a "switch
case" in the process function and as a single state can handle many
different event type i just thought this could be bypassed.
What do you mean by "pseudo RTTI" ? Something i could implement myself to
know what derived class is referenced by the base class pointer ?
I often read that using RTTI could be costly ... Not that i try to optimize
code first, but in my case i believe i don't need RTTI (maybe i'm wrong). A
code like this would have make me really happy :
class State{
public:
...
template <class Ev> virtual int process(Ev& ev);