本文介绍了问:输入臭名昭着的'旗帜'字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了很多驱动程序,并且不可避免地有一些硬件寄存器或

缓冲区描述符字段称为''flags''。标志通常定义为位b。
作为位位置。我想我可以得到一些编译时类型

在分配到一个标志字段时检查,但我不认为我可以。这是'/ b $ b我的想法:


typedef unsigned long HwFlag;

struct HwThing

{

SomeType fieldOne;

SomeType fieldTwo;

HwFlag标志;

};


static const HwFlag flagA = 1<< 0;

static const HwFlag flagB = 1<< 1;

static const HwFlag flagC = 1<< 2;


但问题在于组合标志时:


struct HwThing dev;


dev.flags = flagA | flagB;


我想我需要一个演员回到HwFlag,因为flagA和B将提升为

某些整数类型不是吗?也就是说,我需要写:


dev.flags =(HwFlag)(flagA | flagB);


打败了我的意图。有什么建议吗?


谢谢。


-

- 马克 - >

-

解决方案




吸引你的大脑。


Dan

-

Dan Pop

DESY Zeuthen,RZ集团

电子邮件:

目前在欧盟寻找工作





我猜不是。如果类型在编译时兼容,我认为它将rvalue的值赋给了左值的

位置。

即使flagA | flagB有一个不同于
dev.flags的整数类型,转换将由
赋值运算符*自动*执行。




'太棒了,但如果有人尝试,我想要编译时警告:


int myIllegalFlagValue = 75;


HwFlag dev;


dev.flags = myIllegalFlagValue; //请注意某种警告。



吸引你的大脑。


像往常一样感谢Dan。有没有考虑过客户支持的工作? :-)


-

- 马克 - >

-




这很棒但是如果有人尝试,我想要编译时警告:

int myIllegalFlagValue = 75;

HwFlag dev;

dev.flags = myIllegalFlagValue; //请注意某种警告。




然后学习Pascal。正如我在上一篇文章中所说,C不是你想要的正确的

工具。



吸引你的大脑。



像往常一样感谢Dan。有没有考虑过客户支持的工作? : - )




我目前的工作涉及大量的用户支持。例如


日期:星期四,2004年9月16日10:53:14 +0200

来自:Carsten Urbach< Ca ******** ****@physik.fu-berlin.de>

收件人:Dan Pop< Da ***** @ ifh.de>

主题:回复: PBS工作的问题


嗨Dan,


非常感谢您的快速帮助!


Carsten


Dan

-

Dan Pop

DESY Zeuthen,RZ group

电子邮件:

目前在欧盟寻找工作


I write a lot of drivers and there is inevitably some hardware register or
buffer descriptor field called ''flags''. The flags are defined, typically,
as bit positions. I was thinking I could get some compile-time type
checking when assigning to a flag field but I don''t think I can. Here''s
what I was thinking:

typedef unsigned long HwFlag;
struct HwThing
{
SomeType fieldOne;
SomeType fieldTwo;
HwFlag flags;
};

static const HwFlag flagA = 1 << 0;
static const HwFlag flagB = 1 << 1;
static const HwFlag flagC = 1 << 2;

But the problem is when combining flags:

struct HwThing dev;

dev.flags = flagA | flagB;

I think I require a cast back to HwFlag since flagA and B will promote to
some integer type won''t they? That is, I''d need to write:

dev.flags = (HwFlag) (flagA | flagB);

which rather defeats my intent. Any suggestions?

Thanks.

--
- Mark ->
--

解决方案



Engage your brain.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union




I guess not. I thought it assigned the value of rvalue to the
location of the lvalue if the types were compatible at compile time.
Even if flagA | flagB had a different integer type than
dev.flags, the conversion would be *automatically* performed by the
assignment operator.



That''s great but I want a compile time warning if someone attempts:

int myIllegalFlagValue = 75;

HwFlag dev;

dev.flags = myIllegalFlagValue; // Warning of some sort please.



Engage your brain.



Thanks Dan, as usual. Ever considered a job in customer support? :-)

--
- Mark ->
--




That''s great but I want a compile time warning if someone attempts:

int myIllegalFlagValue = 75;

HwFlag dev;

dev.flags = myIllegalFlagValue; // Warning of some sort please.



Then learn Pascal. As I said in my previous post, C is not the right
tool for what you want.



Engage your brain.



Thanks Dan, as usual. Ever considered a job in customer support? :-)



My current job involves a great deal of user support. E.g.

Date: Thu, 16 Sep 2004 10:53:14 +0200
From: Carsten Urbach <Ca************@physik.fu-berlin.de>
To: Dan Pop <Da*****@ifh.de>
Subject: Re: Problems with PBS jobs

Hi Dan,

Thanks a lot for your fast help!

Carsten

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union


这篇关于问:输入臭名昭着的'旗帜'字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 14:19