Discussion:
code question...
yair24
2005-02-09 16:27:03 UTC
Permalink
hello,

I have a little problem:

I have this code:

==============================
void operate statemachine()
{
switch(state)
{
case STBY:
OperateStbyState();
break;
case READY:
OperateReadyState();
break;
.
.
.
case FIRE:
OperateFireState();
break;
}
}


void OperateStbyState()
{
/// implementation of state operations
.
.
.
}
void OperateReadyState()
{
/// implementation of state operations
.
.
.
}
void OperateFireState()
{
/// implementation of state operations
.
.
.
}


==============================

the states (STBY READY FIRE)are #defined in the begining of the file.
now, sometimes I want to add more states ao I go to the begining of
the file and define a new state for example:

#define START 10

and then I add the function

void OperateStartState();

and the implementation of this function:

void OperateStartState()
{
/// implementation of state operations
.
.
.
}

but I always forget to add this lines:

case START:
OperateStartState();
break;

and when I start to debug the program I see that when I goto START
state nothing happends...

my question is this:


is there anyway to make the compiler give me a warning in case I add
more defines but forget to add the lines in the switch statement?

(I mean something like the RS232.lib that if you dont define the
buffer size it gives you a compiler warant and defaults the buffer to
31. )









Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/rabbit-semi/

<*> To unsubscribe from this group, send an email to:
rabbit-semi-unsubscribe-***@public.gmane.org

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Don Starr
2005-02-09 17:03:13 UTC
Permalink
<snip state machine code>
Post by yair24
the states (STBY READY FIRE)are #defined in the begining of the
file. now, sometimes I want to add more states ao I go to the
#define START 10
and then I add the function
void OperateStartState();
void OperateStartState()
{
/// implementation of state operations
}
OperateStartState();
break;
and when I start to debug the program I see that when I goto
START state nothing happends...
is there anyway to make the compiler give me a warning in case
I add more defines but forget to add the lines in the switch
statement?
Not with a switch(). There is no compile-time checking of the
expression, apart from any optimizations the compiler might make.

You could add something like the following, to give you a warning
in the debugger at run-time:
default:
printf( "Warning: case %d not defined\n", state );
break;

(Replace the printf with whatever is appropriate for your debug
environment).

This will do two things: First, it will give you a run-time
indication that you forgot to add a case for a new state.
Second, it will flag "bad" state values, in case your state
variable was somehow corrupted, even if all "real" states were
implemented.

-Don




Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/rabbit-semi/

<*> To unsubscribe from this group, send an email to:
rabbit-semi-unsubscribe-***@public.gmane.org

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Tom Collins
2005-02-09 18:07:42 UTC
Permalink
Post by yair24
is there anyway to make the compiler give me a warning in case I add
more defines but forget to add the lines in the switch statement?
You should follow Don's advice, as it will help you catch missing
states.

You could build a table of the states and their corresponding
functions. If you had the table in your code near the defines of each
state, it would be easier to remember to add it. Of coure, you'll need
to include the function declarations before the state table, and
they'll all have to be of the same type (number and type of parameters,
return value).

This is off the top of my head, and hasn't been tested, but here are
some snippets:

typedef struct {
int state;
int (*operate)();
} STATE_ENTRY

#define STBY 1
int OperateStbyState();
#define READY 2
int OperateReadyState();
#define FIRE 3
int OperateFireState();

const STATE_ENTRY state_table[] = {
{ STBY, &OperateStbyState },
{ READY, &OperateReadyState },
{ FIRE, &OperateFireState }
};

(later in code)

foundstate = 0;
for (i = 0; i < (sizeof(state_table)/sizeof(state_table[1])); i++) {
if (newstate == state_table[i].state) {
state_table[i].operate();
foundstate = 1;
break;
}
}
if (!foundstate) {
printf ("Can't find state table entry for state %d.\n", newstate);
}

--
Tom Collins - tom-lnEA/wrDJtNWk0Htik3J/***@public.gmane.org
QmailAdmin: http://qmailadmin.sf.net/ Vpopmail: http://vpopmail.sf.net/
Info on the Sniffter hand-held Network Tester: http://sniffter.com/




Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/rabbit-semi/

<*> To unsubscribe from this group, send an email to:
rabbit-semi-unsubscribe-***@public.gmane.org

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
yair24
2005-02-09 20:59:22 UTC
Permalink
thank you both Don and Tom,

this is a very nice code here in this example I will try to see if I
can iomplement it with my program.

thanks again.
Post by Tom Collins
Post by yair24
is there anyway to make the compiler give me a warning in case I add
more defines but forget to add the lines in the switch
statement?
Post by Tom Collins
You should follow Don's advice, as it will help you catch missing
states.
You could build a table of the states and their corresponding
functions. If you had the table in your code near the defines of each
state, it would be easier to remember to add it. Of coure, you'll need
to include the function declarations before the state table, and
they'll all have to be of the same type (number and type of
parameters,
Post by Tom Collins
return value).
This is off the top of my head, and hasn't been tested, but here are
typedef struct {
int state;
int (*operate)();
} STATE_ENTRY
#define STBY 1
int OperateStbyState();
#define READY 2
int OperateReadyState();
#define FIRE 3
int OperateFireState();
const STATE_ENTRY state_table[] = {
{ STBY, &OperateStbyState },
{ READY, &OperateReadyState },
{ FIRE, &OperateFireState }
};
(later in code)
foundstate = 0;
for (i = 0; i < (sizeof(state_table)/sizeof(state_table[1])); i++) {
if (newstate == state_table[i].state) {
state_table[i].operate();
foundstate = 1;
break;
}
}
if (!foundstate) {
printf ("Can't find state table entry for state %d.\n",
newstate);
Post by Tom Collins
}
--
http://vpopmail.sf.net/
Post by Tom Collins
Info on the Sniffter hand-held Network Tester: http://sniffter.com/
Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/rabbit-semi/

<*> To unsubscribe from this group, send an email to:
rabbit-semi-unsubscribe-***@public.gmane.org

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Enzo Gomez
2005-02-09 21:20:33 UTC
Permalink
I just want to add my tip: for state machines, use
enum, instead of several DEFINE's. It makes code easy to read, you can
go to the next state with just a State++. And combined with Don's idea
is a very usefull tool.

But, Tom's idea rocks!
--
____________________
Enzo Gomez, EE



Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/rabbit-semi/

<*> To unsubscribe from this group, send an email to:
rabbit-semi-unsubscribe-***@public.gmane.org

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Tom Collins
2005-02-10 18:33:01 UTC
Permalink
Post by Enzo Gomez
I just want to add my tip: for state machines, use
enum, instead of several DEFINE's. It makes code easy to read, you can
go to the next state with just a State++. And combined with Don's idea
is a very usefull tool.
AFAIK, DC doesn't support enum.

--
Tom Collins - tom-lnEA/wrDJtNWk0Htik3J/***@public.gmane.org
QmailAdmin: http://qmailadmin.sf.net/ Vpopmail: http://vpopmail.sf.net/
Info on the Sniffter hand-held Network Tester: http://sniffter.com/




Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/rabbit-semi/

<*> To unsubscribe from this group, send an email to:
rabbit-semi-unsubscribe-***@public.gmane.org

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
hartmanmaynard
2005-02-10 18:40:00 UTC
Permalink
Post by Tom Collins
Post by Enzo Gomez
I just want to add my tip: for state machines, use
enum, instead of several DEFINE's. It makes code easy to read, you can
go to the next state with just a State++. And combined with Don's idea
is a very usefull tool.
AFAIK, DC doesn't support enum.
--
QmailAdmin: http://qmailadmin.sf.net/ Vpopmail: http://vpopmail.sf.net/
Info on the Sniffter hand-held Network Tester: http://sniffter.com/
Sure it does :)






Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/rabbit-semi/

<*> To unsubscribe from this group, send an email to:
rabbit-semi-unsubscribe-***@public.gmane.org

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Steve Trigero
2005-02-10 19:10:44 UTC
Permalink
enum's are a very recent addition to DC. Historically
it has not supported them.

Steve
Post by Enzo Gomez
Post by Tom Collins
Post by Enzo Gomez
I just want to add my tip: for state machines,
use
Post by Tom Collins
Post by Enzo Gomez
enum, instead of several DEFINE's. It makes
code easy to read,
you can
Post by Tom Collins
Post by Enzo Gomez
go to the next state with just a State++. And
combined with Don's
idea
Post by Tom Collins
Post by Enzo Gomez
is a very usefull tool.
AFAIK, DC doesn't support enum.
--
http://vpopmail.sf.net/
http://sniffter.com/
Sure it does :)
Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/rabbit-semi/

<*> To unsubscribe from this group, send an email to:
rabbit-semi-unsubscribe-***@public.gmane.org

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Mike van Meeteren
2005-02-10 22:41:09 UTC
Permalink
Post by Steve Trigero
enum's are a very recent addition to DC. Historically
it has not supported them.
Really? I'm *pretty* sure 7.32 supported them, and I know for a fact that
8.01 does.

-Mike




Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/rabbit-semi/

<*> To unsubscribe from this group, send an email to:
rabbit-semi-unsubscribe-***@public.gmane.org

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
yair24
2005-02-10 13:14:14 UTC
Permalink
thanks,
I succeeded in replacing my code with this code, and its working fine.

Yair
Post by Tom Collins
Post by yair24
is there anyway to make the compiler give me a warning in case I add
more defines but forget to add the lines in the switch statement?
You should follow Don's advice, as it will help you catch missing
states.
You could build a table of the states and their corresponding
functions. If you had the table in your code near the defines of each
state, it would be easier to remember to add it. Of coure, you'll need
to include the function declarations before the state table, and
they'll all have to be of the same type (number and type of
parameters,
Post by Tom Collins
return value).
This is off the top of my head, and hasn't been tested, but here are
typedef struct {
int state;
int (*operate)();
} STATE_ENTRY
#define STBY 1
int OperateStbyState();
#define READY 2
int OperateReadyState();
#define FIRE 3
int OperateFireState();
const STATE_ENTRY state_table[] = {
{ STBY, &OperateStbyState },
{ READY, &OperateReadyState },
{ FIRE, &OperateFireState }
};
(later in code)
foundstate = 0;
for (i = 0; i < (sizeof(state_table)/sizeof(state_table[1])); i++) {
if (newstate == state_table[i].state) {
state_table[i].operate();
foundstate = 1;
break;
}
}
if (!foundstate) {
printf ("Can't find state table entry for state %d.\n",
newstate);
Post by Tom Collins
}
--
http://vpopmail.sf.net/
Post by Tom Collins
Info on the Sniffter hand-held Network Tester: http://sniffter.com/
Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/rabbit-semi/

<*> To unsubscribe from this group, send an email to:
rabbit-semi-unsubscribe-***@public.gmane.org

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
bmurthazw
2005-02-10 23:51:23 UTC
Permalink
From memory they were a new feature in DC8
7.20 - it's in the release notes







Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/rabbit-semi/

<*> To unsubscribe from this group, send an email to:
rabbit-semi-unsubscribe-***@public.gmane.org

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/

Loading...