[Tfug] Developer question
jblais
joe.blais at pti-instruments.com
Mon Jul 2 18:13:55 MST 2007
> -----Original Message-----
> From: tfug-bounces at tfug.org [mailto:tfug-bounces at tfug.org]On Behalf Of
> Bexley Hall
> A puzzle for lurking developers... :>
>
a thing that might help
>
> 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
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.
joe
More information about the tfug
mailing list