[Tfug] Developer question

Bexley Hall bexley401 at yahoo.com
Fri Jul 6 14:46:37 MST 2007


Greetings!

--- jblais <joe.blais at pti-instruments.com> wrote:

> > enum color_t {
> > 	// By convention, must begin with '0'
> > 	RED = 0,
> > 	GREEN,
> > 	BLUE,
>      COLOR_COUNT
> 
> > };
> >
> > static const char *
> > colornames[ COLOR_COUNT ][x] {
> > 	"Red",
> > 	"Green",
> > 	"Blue",
> > };
> >
> 
> Sometimes I set the text programmatically in a
> constructor somewhere (no
> longer static const however):
> 
> strcpy( colornames[RED], "Red" ) ;
> strcpy( colornames[GREEN], "Green" ) ;
> 
> This way, even if one item is missed, say in
> colornames, I dont end up with
> some ugliness like
> colornames[FUCIA] == "orange"
> Things will still line up.
> 
> You could clear all the names before assigning them,
> and then after
> assigning them run a check that all are populated,
> and assert if one is
> missing.  This could all be in the constructor and
> "discovered" real fast

This expects colornames[] to reside in the DATA
segment/writeable memory, etc.  I want to be able
to move the table into ROM (think XIP).

Also, your approach requires code to execute to
"set" these values.  Since they will *never*
change (i.e. its a lookup table), let the *compiler*
do the work so *no* CPU cycles are used at run time.

> for ( int iC = RED ; iC < COLOR_COUNT ; ++iC ) {
> memset(colornames[iC],0,x)
> ; }
> strcpy( colornames[RED], "Red" ) ;
> strcpy( colornames[GREEN], "Green" ) ;
> for ( int iC = RED ; iC < COLOR_COUNT ; ++iC ) {
> assert( colornames[iC][0] >
> 0 ) ; }
> 
> If they are static members of some class, you could
> check that this stuff
> happens only the first time a constructor is called.
>  Or you clould make the
> whole thing a singleton class or something.



       
____________________________________________________________________________________
Take the Internet to Go: Yahoo!Go puts the Internet in your pocket: mail, news, photos & more. 
http://mobile.yahoo.com/go?refer=1GNXIC




More information about the tfug mailing list