其值显示为字符串的枚举是什么类型?

发布于 2024-07-14 01:43:20 字数 754 浏览 8 评论 0原文

我正在使用 Apple 的 ScriptingBridge 框架,并为 iTunes 生成了一个头文件,其中包含几个如下所示的枚举:

typedef enum {
    iTunesESrcLibrary = 'kLib',
    iTunesESrcIPod = 'kPod',
    iTunesESrcAudioCD = 'kACD',
    iTunesESrcMP3CD = 'kMCD',
    iTunesESrcDevice = 'kDev',
    iTunesESrcRadioTuner = 'kTun',
    iTunesESrcSharedLibrary = 'kShd',
    iTunesESrcUnknown = 'kUnk'
} iTunesESrc;

我的理解是枚举值必须类似于整数,但这个定义似乎违反了该规则。 此外,似乎将这些 enum 值视为整数(例如,在 NSPredicate 中)并没有做正确的事情。

我将上面的 enum 声明添加到带有空 main 函数的 C 文件中,并使用 i686-apple-darwin9-gcc-4.0.1 进行编译代码>. 因此,虽然这些类型的enum可能不符合C标准(正如Parappa在下面指出的那样),但它们至少被gcc编译为some类型。

那么,该类型是什么,以及如何在格式字符串中使用它?

I am working with Apple's ScriptingBridge framework, and have generated a header file for iTunes that contains several enums like this:

typedef enum {
    iTunesESrcLibrary = 'kLib',
    iTunesESrcIPod = 'kPod',
    iTunesESrcAudioCD = 'kACD',
    iTunesESrcMP3CD = 'kMCD',
    iTunesESrcDevice = 'kDev',
    iTunesESrcRadioTuner = 'kTun',
    iTunesESrcSharedLibrary = 'kShd',
    iTunesESrcUnknown = 'kUnk'
} iTunesESrc;

My understanding was that enum values had to be integer-like, but this definition seems to violate that rule. Furthermore, it seems as though treating these enum values as integers (in an NSPredicate, for example) doesn't do the right thing.

I added the enum declaration above to a C file with an empty main function, and it compiled using i686-apple-darwin9-gcc-4.0.1. So, while these kinds of enums may not conform to the C standard (as Parappa points out below), they are at least being compiled to some type by gcc.

So, what is that type, and how can I use it, for instance, in a format string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遮了一弯 2024-07-21 01:43:20

C99,TC3 内容如下:

6.4.4.4 §2:

整数字符常量是用单引号括起来的一个或多个多字节字符的序列,如“x”。 [...]

6.4.4.4 §10:

整型字符常量的类型为 int。 包含映射到单字节执行字符的单个字符的整型字符常量的值是解释为整数的映射字符表示形式的数值。 包含多个字符(例如“ab”)或包含不映射到单字节执行字符的字符或转义序列的整型字符常量的值是实现定义的。 如果整型字符常量包含单个字符或转义序列,则其值为将单个字符或转义序列的值的 char 类型对象转换为 int 类型时得到的值。

在大多数实现中,使用最多 4 个一字节字符的整数字符常量是安全的。 不过,不同系统之间的实际值可能有所不同(字节序?)。


这实际上已经在 ANSI-C89 标准第 3.1.3.4 节中定义:

整数字符常量是一个或多个的序列
用单引号括起来的多字节字符,如“x”或“ab”。 [...]

整型字符常量的类型为 int。 的价值
包含映射的单个字符的整数字符常量
转换为基本执行字符集的成员是数值
映射字符的表示值解释为
整数。 包含更多内容的整数字符常量的值
多于一个字符,或包含不包含字符或转义序列
在基本执行字符集中表示为
实现定义的。 特别是,在其中的实现中
char 类型与有符号 char 具有相同的值范围,即高位
单字符整型字符常量的位位置是
被视为符号位。

C99, TC3 reads:

6.4.4.4 §2:

An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'. [...]

6.4.4.4 §10:

An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int.

In most implementations, it's safe to use integer character constants of up to 4 one-byte characters. The actual value might differ between different systems (endianness?) though.


This is actually already defined in the ANSI-C89 standard, section 3.1.3.4:

An integer character constant is a sequence of one or more
multibyte characters enclosed in single-quotes, as in 'x' or 'ab'. [...]

An integer character constant has type int. The value of an
integer character constant containing a single character that maps
into a member of the basic execution character set is the numerical
value of the representation of the mapped character interpreted as an
integer. The value of an integer character constant containing more
than one character, or containing a character or escape sequence not
represented in the basic execution character set, is
implementation-defined. In particular, in an implementation in which
type char has the same range of values as signed char, the high-order
bit position of a single-character integer character constant is
treated as a sign bit.

栖迟 2024-07-21 01:43:20

单引号表示字符,而不是 C 中的字符串。因此每个枚举都有一个 32 位值,由四个字符的字符代码组成。 实际值取决于字符编码,但我假设 8 位字符。 请注意,没有附加 \0。

您可以将枚举用于正常的比较/分配目的。 与任何枚举一样,基础类型是整数。

我已经在嵌入式系统中多次使用这种技术来创建 4 个字符的“名称”,这些名称在十六进制转储/调试器上下文中是人类可读的。

The single quotes indicate characters, rather than strings in C. So each of the enums will have a 32 bit value consisting of the character codes for the four characters. Actual value will depend in character encodings, but I am assuming 8 bit characters. Note there is no appended \0.

You can use the enums for normal comparison/assignment purposes. As with any enum the underlying type is integer.

I've used this technique in embedded systems many times to create 4 character 'names' that were human readable in hex dump/debugger contexts.

烟凡古楼 2024-07-21 01:43:20

如前所述,这些是使用字符常量声明的整数。

当使用多个字符的字符常量声明整数时,它对 为其开发常量的机器的字节顺序。 由于所有原始 Mac API 都在 PPC 或更早的计算机上,因此它们相对于 Intel Little-Endian< /a> 机器。

如果您只是为英特尔构建,您可以手动颠倒顺序。

如果您正在构建通用二进制文件,则需要使用 翻转函数 例如 CFSwapInt32BigToHost

如果不更正这些代码,无论是否存在编译器错误,您的代码都只能在 PowerPC 机器上运行。

As already stated, those are integers declared using character constants.

When an integer is declared using a character constant of more than one character, it is sensitive to the byte order of the machine for which the constant was developed. As all the original Mac APIs were on PPC or earlier machines, they are backwards with respect to Intel Little-Endian machines.

If you are only building for Intel you can just reverse the order by hand.

If you are building a Universal binary you need to use a flipping function such as CFSwapInt32BigToHost.

Failure to correct those codes will leave you with code that could only work on PowerPC machines, regardless of the lack of compiler errors.

哀由 2024-07-21 01:43:20

这是 Apple 对 C 的扩展, 它基本上将这些枚举转换为:

typedef enum {
    iTunesESrcLibrary = 'k'<<24 | 'L'<<16 | 'i'<<8 | 'b',
 ...
 }

编辑:抱歉,显然它是有效的 C。我只在 Mac 代码中看到它们,所以错误地认为它是 Apple 特定的。

That is an Apple extension to C, which It basically translates those enums to:

typedef enum {
    iTunesESrcLibrary = 'k'<<24 | 'L'<<16 | 'i'<<8 | 'b',
 ...
 }

EDIT: Sorry, apparently it's valid C. I've only seem them in Mac code, so wrongly assumed that it was Apple specific.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文