Slice defines a number of rules for the naming and contents of Slice source files.
Slice 定义了许多 Slice 源文件的命名和内容的规则。
File Naming
Files containing Slice definitions must end in a .ice file extension, for example, Clock.ice is a valid file name. Other file extensions are rejected by the compilers.
For case-insensitive file systems, the file extension may be written as uppercase or lowercase, so Clock.ICE is legal. For case-sensitive file systems (such as Unix), Clock.ICE is illegal. (The extension must be in lowercase.)
文件命名
包含Slice定义的文件必须以 .ice 文件扩展名结尾,例如,Clock.ice 是有效的文件名。 其他文件扩展名会被编译器拒绝。
对于不区分大小写的文件系统,文件扩展名可以写为大写或小写,因此 Clock.ICE 是合法的。 对于区分大小写的文件系统(例如Unix),Clock.ICE 是非法的。 (扩展名必须为小写。)
File Format
Slice is a free-form language so you can use spaces, horizontal and vertical tab stops, form feeds, and newline characters to lay out your code in any way you wish. (White space characters are token separators). Slice does not attach semantics to the layout of a definition. You may wish to follow the style we have used for the Slice examples throughout this book.
Slice files can be ASCII text files or use the UTF-8 character encoding with an optional byte order marker (BOM) at the beginning of each file. However, Slice identifiers are limited to ASCII letters and digits; non-ASCII letters can appear only in comments and string literals.
文件格式
Slice 是一种自由格式的语言,因此您可以使用空格、水平和垂直制表位、换页符和换行符以您希望的任何方式布局代码。 (空白字符是标记分隔符)。 Slice 不会将语义附加到定义的布局上。 您可能希望遵循我们在本书中使用的 Slice 示例的风格。
Slice文件可以是 ASCII 文本文件,也可以使用 UTF-8 字符编码,并在每个文件的开头带有可选的字节顺序标记 (BOM)。 然而,Slice标识符仅限于 ASCII 字母和数字; 非 ASCII 字母只能出现在注释和字符串文字中。
Preprocessing
Slice supports the same preprocessor directives as C++, so you can use directives such as #include and macro definitions. However, Slice permits #include directives only at the beginning of a file, before any Slice definitions.
If you use #include directives, it is a good idea to protect them with guards to prevent double inclusion of a file:
预处理
Slice 支持与 C++ 相同的预处理器指令,因此您可以使用 #include 和宏定义等指令。 但是,Slice 只允许在文件开头、任何 Slice 定义之前使用 #include 指令。
如果您使用 #include 指令,最好使用防护装置来保护它们,以防止文件的双重包含:
1 | // File Clock.ice |
The following #pragma directive offers a simpler way to achieve the same result:
以下 #pragma 指令提供了一种更简单的方法来实现相同的结果:
1 | // File Clock.ice |
#include directives permit a Slice definition to use types defined in a different source file. The Slice compilers parse all of the code in a source file, including the code in subordinate #include files. However, the compilers generate code only for the top-level file(s) nominated on the command line. You must separately compile subordinate #include files to obtain generated code for all the files that make up your Slice definition.
Note that you should avoid #include with double quotes:
#include 指令允许 Slice 定义使用不同源文件中定义的类型。 Slice 编译器解析源文件中的所有代码,包括从属 #include 文件中的代码。 但是,编译器仅为命令行上指定的顶级文件生成代码。 您必须单独编译从属 #include 文件,以获得构成 Slice 定义的所有文件的生成代码。
请注意,您应该避免使用双引号#include:
1 |
While double quotes will work, the directory in which the preprocessor tries to locate the file can vary depending on the operating system, so the included file may not always be found where you expect it. Instead, use angle brackets (<>); you can control which directories are searched for the file with the -I option of the Slice compiler.
Also note that, if you include a path separator in a #include directive, you must use a forward slash:
虽然双引号可以工作,但预处理器尝试在其中查找文件的目录可能会因操作系统而异,因此可能并不总是能在您期望的位置找到包含的文件。 相反,请使用尖括号 (<>); 您可以使用 Slice 编译器的 -I 选项来控制在哪些目录中搜索文件。
另请注意,如果在 #include 指令中包含路径分隔符,则必须使用正斜杠:
1 |
You cannot use a backslash in #include directives:
您不能在 #include 指令中使用反斜杠:
1 |
Detecting Ice Versions
The Slice compilers define the preprocessor macro ICE_VERSION with a numeric representation of the Ice version. The value of this macro is the same as the C++ macro ICE_INT_VERSION. You can use this macro to make your Slice definitions backward-compatible with older Ice releases, while still taking advantage of newer Ice features when possible. For example, the Slice definition shown below makes use of custom enumerator values:
检测 Ice 版本
Slice 编译器使用 Ice 版本的数字表示来定义预处理器宏 ICE_VERSION 。 该宏的值与 C++ 宏 ICE_INT_VERSION 相同。 您可以使用此宏使您的 Slice 定义向后兼容较旧的 Ice 版本,同时尽可能利用较新的 Ice 功能。 例如,下面显示的 Slice 定义使用自定义枚举值:
1 |
|
Although this example is intended to show how to use the ICE_VERSION macro, it also highlights a potential pitfall that you must be aware of when trying to maintain backward compatibility: the two definitions of Fruit are not wire-compatible.
尽管此示例旨在展示如何使用 ICE_VERSION 宏,但它也强调了在尝试保持向后兼容性时必须注意的潜在陷阱:Fruit 的两个定义不兼容。
Detecting Slice Compilers
Each Slice compiler defines its own macro so that you can customize your Slice code for certain language mappings. The macro name is
检测Slice编译器
每个 Slice 编译器都定义自己的宏,以便您可以针对某些语言映射自定义 Slice 代码。 宏名称为 __<大写的编译器名称>__,例如 slice2cpp 的宏名称为 SLICE2CPP 和 __SLICE2MATLAB__。 slice2matlab。
For example, .NET developers may elect to avoid the use of default values for structure members because the presence of default values changes the C# mapping of the structure from struct to class:
例如,.NET 开发人员可能会选择避免对结构成员使用默认值,因为默认值的存在会更改结构从结构到类的 C# 映射:
1 | struct Record |
Definition Order
Slice constructs, such as modules, interfaces, or type definitions, can appear in any order you prefer. However, identifiers must be declared before they can be used.
定义顺序
Slice构造(例如模块、接口或类型定义)可以按您喜欢的任何顺序出现。 但是,标识符在使用之前必须先声明。