X
Xiangliang Meng
Hi, all.
Recently, I find there is a way in our project to maintain a global set in
many files by using preprocessing directives. I'm wondering if we could find
a better method for this.
Many colors are referred in different subsystems in our projects. They are
defined as enumeration constants and a single color must be the same value
all across our projects.
-------------------color.h-------------------
#ifndef _COLOR_
#define _COLOR_
enum colorSet
{
yellow = 0;
red = 1;
green = 2;
blue = 3;
pink = 4;
white = 5;
black = 6;
brown = 7;
cyan = 8;
purple = 9;
gray = 10;
};
#endif // _COLOR_
-------------------deskcolor.h-------------------
#ifndef _COLOR_
#define _COLOR_
enum deskcolor
{
yellow = 0;
gray = 10;
};
#endif // _COLOR_
-------------------chaircolor.h-------------------
#ifndef _COLOR_
#define _COLOR_
enum chaircolor
{
red = 1;
green = 2;
blue = 3;
black = 6;
};
#endif // _COLOR_
-------------------computercolor.h-------------------
#ifndef _COLOR_
#define _COLOR_
enum computercolor
{
white = 5;
black = 6;
gray = 10;
};
#endif // _COLOR_
--------------------------------------
Every .c file in the different subsystems, include only a proper 'color' .h
file, but color.h will not be included by any .c file. For emaple, chair.c
includes chaircolor.h, and desk.c includes deskcolor.h. In order to prevent
including more than a 'color' .h file in a .c file by a mistake, all those
'color'. h files are using the same preprocessing directives.
When adding a new color into the color set, it's first added into color.h
and then only into the proper 'color' .h files if the subsystem really need
this kind of color.
What are the merits and defects of this method? As for me, it properly meets
our goal, but it's confusing for a newcomer and a little diffcult to
maintain. Who could point out more and provide a better method?
Best Regards,
Xiangliang Meng
Recently, I find there is a way in our project to maintain a global set in
many files by using preprocessing directives. I'm wondering if we could find
a better method for this.
Many colors are referred in different subsystems in our projects. They are
defined as enumeration constants and a single color must be the same value
all across our projects.
-------------------color.h-------------------
#ifndef _COLOR_
#define _COLOR_
enum colorSet
{
yellow = 0;
red = 1;
green = 2;
blue = 3;
pink = 4;
white = 5;
black = 6;
brown = 7;
cyan = 8;
purple = 9;
gray = 10;
};
#endif // _COLOR_
-------------------deskcolor.h-------------------
#ifndef _COLOR_
#define _COLOR_
enum deskcolor
{
yellow = 0;
gray = 10;
};
#endif // _COLOR_
-------------------chaircolor.h-------------------
#ifndef _COLOR_
#define _COLOR_
enum chaircolor
{
red = 1;
green = 2;
blue = 3;
black = 6;
};
#endif // _COLOR_
-------------------computercolor.h-------------------
#ifndef _COLOR_
#define _COLOR_
enum computercolor
{
white = 5;
black = 6;
gray = 10;
};
#endif // _COLOR_
--------------------------------------
Every .c file in the different subsystems, include only a proper 'color' .h
file, but color.h will not be included by any .c file. For emaple, chair.c
includes chaircolor.h, and desk.c includes deskcolor.h. In order to prevent
including more than a 'color' .h file in a .c file by a mistake, all those
'color'. h files are using the same preprocessing directives.
When adding a new color into the color set, it's first added into color.h
and then only into the proper 'color' .h files if the subsystem really need
this kind of color.
What are the merits and defects of this method? As for me, it properly meets
our goal, but it's confusing for a newcomer and a little diffcult to
maintain. Who could point out more and provide a better method?
Best Regards,
Xiangliang Meng