(40分快速结贴) 如何在运行时得知union的具体类型

影子传说_Hades 2010-05-17 12:54:09
程序得到了一个union的数据指针

CMColor *ps_color = &((*alpha_display_info)->ps_color);


CMColor 具体类型如下:


/* Apple colour space data structures */

/* RGB Color Value
A color value expressed in the RGB color space is composed of red, green,
and blue component values. Each color component is expressed as a numeric
value within the range of 0 to 65535.
*/
typedef struct
{
guint16 red;
guint16 green;
guint16 blue;
}CMRGBColor;

/* HSV Color Value
A color value expressed in the HSV color space is composed of hue,
saturation, and value component values. Each color component is
expressed as a numeric value within the range of 0 to 65535 inclusive.
The hue value represents a fraction of a circle in which red is
positioned at 0.
*/

typedef struct
{
guint16 hue;
guint16 saturation;
guint16 value;
}CMHSVColor;

/* CMYK Color Value
A color value expressed in the CMYK color space is composed of cyan, magenta,
yellow, and black component values. Each color component is expressed as a
numeric value within the range of 0 to 65535 inclusive, with 0 representing
100% ink (e.g. pure cyan = 0, 65535, 65535, 65535).
*/

typedef struct
{
guint16 cyan;
guint16 magenta;
guint16 yellow;
guint16 black;
}CMCMYKColor;

/* L*a*b* Color Value
The first three values in the color data are, respectively, the colors
lightness, a chrominance, and b chrominance components. The lightness
component is a 16bit value ranging from 0 to 10000. The chrominance
components are each 16bit values ranging from 12800 to 12700. Gray
values are represented by chrominance components of 0 (e.g. pure white
is defined as 10000, 0, 0).
*/
typedef struct
{
guint16 L;
gint16 a;
gint16 b;
} CMLabColor;

/* Gray Color Value
A color value expressed in the Gray color space is composed of a single component,
gray, represented as a numeric value within the range of 0 to 10000.
*/
typedef struct
{
guint16 gray;
} CMGrayColor ;

/* The color union is defined by the CMColor type definition.
*/
typedef union
{
CMRGBColor rgb;
CMHSVColor hsv;
CMLabColor Lab;
CMCMYKColor cmyk;
CMGrayColor gray;
} CMColor;


但是我下面的方法肯定是错误的,因为打印的所有内容均有值,有没有什么办法判断union具体是哪个类型



// CMColor
static int print_cmcolor_to_pro_file(SaveFileDes saveFileDes,
CMColor *ps_color_p, char* openMod) {
if (!ps_color_p) {
return 0;
}
FILE *stream;
char* linefeedp = saveFileDes.linefeedp;
int ret = 1;
stream = fopen(saveFileDes.fileName, openMod);
if (stream == NULL) {
fprintf(stderr, "Cannot open output file.\r\n");
return ret;
}
CMColor ps_color = *ps_color_p;
CMRGBColor rgb = ps_color.rgb;
CMHSVColor hsv = ps_color.hsv;
CMLabColor Lab = ps_color.Lab;
CMCMYKColor cmyk = ps_color.cmyk;
CMGrayColor gray = ps_color.gray;
if (rgb.red) {
//
fprintf(stream, "CMColor.CMRGBColor.red = %d %s", rgb.red, linefeedp);
printf("CMColor.CMRGBColor.red =%d %s", rgb.red, linefeedp);
//
fprintf(stream, "CMColor.CMRGBColor.green = %d %s", rgb.green,
linefeedp);
printf("CMColor.CMRGBColor.green =%d %s", rgb.green, linefeedp);
//
fprintf(stream, "CMColor.CMRGBColor.blue = %d %s", rgb.blue, linefeedp);
printf("CMColor.CMRGBColor.blue =%d %s", rgb.blue, linefeedp);
}
if (hsv.hue) {
//hue
fprintf(stream, "CMColor.CMHSVColor.hue = %d %s", hsv.hue, linefeedp);
printf("CMColor.CMHSVColor.hue =%d %s", hsv.hue, linefeedp);
//saturation
fprintf(stream, "CMColor.CMHSVColor.saturation = %d %s",
hsv.saturation, linefeedp);
printf("CMColor.CMHSVColor.saturation =%d %s", hsv.saturation,
linefeedp);
//value
fprintf(stream, "CMColor.CMHSVColor.value = %d %s", hsv.value,
linefeedp);
printf("CMColor.CMHSVColor.value =%d %s", hsv.value, linefeedp);
}
if (Lab.L) {
//L
fprintf(stream, "CMColor.CMLabColor.L = %d %s", Lab.L, linefeedp);
printf("CMColor.CMLabColor.L =%d %s", Lab.L, linefeedp);
//a
fprintf(stream, "CMColor.CMLabColor.a = %d %s", Lab.a, linefeedp);
printf("CMColor.CMLabColor.a =%d %s", Lab.a, linefeedp);
//b
fprintf(stream, "CMColor.CMLabColor.b = %d %s", Lab.b, linefeedp);
printf("CMColor.CMLabColor.b =%d %s", Lab.b, linefeedp);
}
if (cmyk.cyan) {
//cyan
fprintf(stream, "CMColor.CMCMYKColor.cyan = %d %s", cmyk.cyan,
linefeedp);
printf("CMColor.CMCMYKColor.cyan =%d %s", cmyk.cyan, linefeedp);
//magenta
fprintf(stream, "CMColor.CMCMYKColor.magenta = %d %s", cmyk.magenta,
linefeedp);
printf("CMColor.CMCMYKColor.magenta =%d %s", cmyk.magenta, linefeedp);
//yellow
fprintf(stream, "CMColor.CMCMYKColor.yellow = %d %s", cmyk.yellow,
linefeedp);
printf("CMColor.CMCMYKColor.yellow =%d %s", cmyk.yellow, linefeedp);
//black
fprintf(stream, "CMColor.CMCMYKColor.black = %d %s", cmyk.black,
linefeedp);
printf("CMColor.CMCMYKColor.black =%d %s", cmyk.black, linefeedp);
}
if (gray.gray) {
//L
fprintf(stream, "CMColor.CMGrayColor.gray= %d %s", gray.gray, linefeedp);
printf("CMColor.CMGrayColor.gray =%d %s", gray.gray, linefeedp);
}
ret = fclose(stream);
return ret;
}


...全文
77 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
暗黑帝国 2010-05-17
  • 打赏
  • 举报
回复
同意楼上的,自己加一个标志位
某某9 2010-05-17
  • 打赏
  • 举报
回复
赵4老师 2010-05-17
  • 打赏
  • 举报
回复
自己加数据类型标志。
zenny_chen 2010-05-17
  • 打赏
  • 举报
回复
呵呵,必须要有一个标识来说明。
否则你无法得到你所指的是哪个联合。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧