Re: #defines and strings
- From: Eric Sosman <eric.sosman@xxxxxxx>
- Date: Thu, 30 Jun 2005 11:36:12 -0400
GMM50 wrote:
> Hello:
>
> I am working on a design that has a large number of messages. These
> messages are saved in the system and used by the system. I need to
> convert from message names to internal storage location.
>
> I will force a naming convention something like names from SM1001 to
> SM1999. Not all SMxxxx numbers are used. And as the design changes
> old SMxxxx will be deleted and new ones added.
>
> In the code I use these messages as constants as:
> PlayMsg(SM1023, OutputChannel, Volume);
> So I'm planning to use a define to convert SM1023 to the internal
> storage number (= location).
>
> #define SM1023 19 // Hello Message
>
> But then when the user downloads a message I need to programatically
> determine it's internal storage number (= location).
>
> So a table of structures
> struct MSG_CVRT {
> int msg;
> int loc;
> }
>
> struct MSG_CVRG MsgTbl[] = {
> 1023, 19; // Hello Message
> 0, 0;
> }
>
> And I can look for MsgTbl[i].msg and then extract the location.
>
> My problem is I now have data in two places and they will get out of
> sync. Anyone come across this issue and have advice.
I'm not 100% sure I understand exactly what happens when
"the user downloads a message." The download must be tagged
with some kind of identifier, which I'm guessing is the numeric
value 1023 -- if that's not it, then just disregard the rest
of this message.
Your problem (if I've understood it) is how to keep the
#define and the translation table synchronized, that is, how
to be sure that if either 1023 or 19 changes it changes in
both places simultaneously.
One way is to eliminate the #define, or to replace it
with something vacuous like `#define SM1023 1023'. Functions
like PlayMsg() could be changed so they didn't understand the
first argument as a "storage number," but as a value to be
looked up in MsgTbl[]. According to your description there
will be fewer than one thousand entries in the table, so the
lookup cost will be small (binary search will average fewer
than nine probes).
Another way is to retain both, but to generate them from
a single common source. For example, you might treat MsgTbl[]
as The Authoritative Word, and run a little "helper" program
to generate a .h file from the table's contents. For more
complicated situations, the "helper" could read some other
kind of data file and generate both the #define lines and the
content of MsgTbl[], but that might be overkill in your case.
--
Eric.Sosman@xxxxxxx
.
- Follow-Ups:
- Re: #defines and strings
- From: GMM50
- Re: #defines and strings
- References:
- #defines and strings
- From: GMM50
- #defines and strings
- Prev by Date: Re: Microsoft Visual C++ 6.0
- Next by Date: Re: how to let gcc warn me when I use = in conditions
- Previous by thread: Re: #defines and strings
- Next by thread: Re: #defines and strings
- Index(es):