By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,781 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

C++ Message Handling without using state machines

P: n/a
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

Jun 30 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"libster" <oh********@aol.com> wrote:
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/

Jun 30 '06 #2

P: n/a
On Fri, 30 Jun 2006 09:37:42 GMT, Robbie Hatley <bo***********@no.spamwrote:
"libster" <oh********@aol.comwrote:
....
>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".
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

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Jul 2 '06 #3

P: n/a
"Jorgen Grahn" wrote:
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:

GeneralElectricModel4856RadioReceptionFilterStateM achineClass

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/
Jul 3 '06 #4

P: n/a
In message <tX********************@newssvr13.news.prodigy.com >, Robbie
Hatley <bo***********@no.spamwrites
>"Jorgen Grahn" wrote:
[...]
>>
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:

GeneralElectricModel4856RadioReceptionFilterState MachineClass

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.
>
>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. :-)
Indeed.

--
Richard Herring
Jul 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.