C++ Message Handling without using state machines

L

libster

Hello,

I'm writing code and I have a scenario where I'm waiting to receive a
msg and a timeout message at the same time. For example, I've set a
timer so that
if I don't receive a message in a certain time, then I will receive a
timeout
message. Based on which message I receive first I will perform certain
tasks. Is
there a good way to handle this in C++ without using statemachines. If
I receive the
timeout message first then what do I do when the other message comes in
or vice-versa.
Need to determine a way to find out, once I receive a message, if the
other
message has been received yet. Any advice would be greatly appreciated.



thanks,
Libster
 
R

Robbie Hatley

libster said:
I'm writing code and I have a scenario where I'm waiting to receive
a msg and a timeout message at the same time.

That's a little superfluous, isn't it? I mean, a "timeout message"
already IS a "message". So what you really meant is, "I'm waiting
[callback function?] to receive messages, which may be timeout or
other".
For example, I've set a timer so that if I don't receive a message
in a certain time, then I will receive a timeout message. Based on
which message I receive first I will perform certain tasks. Is
there a good way to handle this in C++ without using state machines?

I'd say a function which processes incoming messages and alters the
state of a system based on what messages it receives already IS a
"state machine". Why are you afraid of that term?
If I receive the timeout message first then what do I do when the
other message comes in or vice-versa? Need to determine a way to
find out, once I receive a message, if the other message has been
received yet. Any advice would be greatly appreciated.

Use static bool variables to keep track of whether certain messages
have been received yet by a function. Sort of like:

int
__stdcall
MyCallbackFunction
(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam
)
{
static bool GotTimeout = false;
static bool GotSleep = false;
static bool GotWakeup = false;
static bool GotExplode = false;
switch (message)
{
case TIMEOUT:
GotTimeout = true;
// do stuff
return 1;
case SLEEP:
GotSleep = true;
// do stuff
return 1;
case WAKEUP:
GotWakeup = true;
// do stuff
return 1;
case EXPLODE:
bomb.explode();
return 666;
}
return 0;
}

You'll have to decide under what conditions you want to reset
your "got-the-message" booleans so that they can be triggered
by the next message of that type. But I think the above scheme
would work.


--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net (put "[ciao]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
 
J

Jorgen Grahn

I'd say a function which processes incoming messages and alters the
state of a system based on what messages it receives already IS a
"state machine".
Yes.

Why are you afraid of that term?

I think he's afraid of the usual, brittle and hard-to-read implementation
with callbacks, switch statements, flags and so on, which you examplify below
(snipped).

I don't know, because I haven't had to do complex state machine stuff ...
but I expect that you can do better than that (more explicitly, more type
safety and other compile-time checks, and so on) with C++.

- Is there something at boost.org, maybe?
- Is there a design involving clever template abuse^Wmetaprogramming?

It wouldn't surprise me if you can make state machines much less tedious and
difficult in C++ without resorting to code generators and stuff.

Not an answer, but I've clarified what the OP was asking for. I hope.

/Jorgen
 
R

Robbie Hatley

Jorgen Grahn said:
I think he's afraid of the usual, brittle and hard-to-read
implementation with callbacks, switch statements, flags and
so on, which you examplify below.

That's all just part of processing messages. Goes with the
territory. Try writing Microsoft Windows window procs.
(Or worse, communications-parsing functions.) (Or worst of
all, a fully-standard-complient C++ compiler! Not that I've
ever done that.) You'll get your fill of state machines in
any of those cases.
I don't know, because I haven't had to do complex state machine
stuff

This is one area where a flow chart helps enormously.
but I expect that you can do better than that (more explicitly,
more type safety and other compile-time checks, and so on) with
C++.

Is there something at boost.org, maybe?

Depends on what the OP wants to do. Sounds like something
very application-specific, rather than something that's going
to be in a library. I mean, come on, what library is going
to have a class called:

GeneralElectricModel4856RadioReceptionFilterStateMachineClass

Not.

Face it, sometimes you just have to roll your own.
Is there a design involving clever template abuse [or]
metaprogramming?

I don't see how metaprogramming is going to help, as that's
purely compile-time, whereas state machines and message
processers are always very real-time by nature.
It wouldn't surprise me if you can make state machines much
less tedious and difficult in C++ without resorting to code
generators and stuff.

The only code generator I've ever used to create state machines
and message processors is my brain. :)

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
 
R

Richard Herring

Robbie said:
:
[...]
Is there something at boost.org, maybe?

Depends on what the OP wants to do. Sounds like something
very application-specific, rather than something that's going
to be in a library. I mean, come on, what library is going
to have a class called:

GeneralElectricModel4856RadioReceptionFilterStateMachineClass

Not.

You're a victim of premature specialisation ;-)

There might well be a library with a StateMachine _framework_ which
defines suitable types for states, messages, transitions etc.
Face it, sometimes you just have to roll your own.
Is there a design involving clever template abuse [or]
metaprogramming?

I don't see how metaprogramming is going to help, as that's
purely compile-time, whereas state machines and message
processers are always very real-time by nature.

But the set of states, the set of possible messages, the table of
transitions etc. are all known at compile time. There might well be
metaprogramming tricks to simplify generating those things.
The only code generator I've ever used to create state machines
and message processors is my brain. :)
Indeed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top