`

ZeroC ICE之旅------Slice

    博客分类:
  • ice
阅读更多
Slice是在ICE所特有的特殊语言,ICE 提供基于Ice语言的多语言映射工具。Slice主要针对程序所涉及到的接口和类型进行定义。不涉及到具体实现和具体语言的特征。
既然ICE支持跨语言的调用,那么是不是Slice支持每种的特有数据类型呢?
当然不是,ICE只是保留各种具体语言数据类型的最小交集。
提供:
Java代码

1. 1.bool (false or true,>=1bit)
2. 2.byte (-128-127@,>=8bits)
3. 3.short (-2^15 to 2^15-1,>=16bits)
4. 4.int (-2^31 to 2^31-1,>=32bits)
5. 5.long (-2^63 to 2^63-1,>=64bits)
6. 6.float (IEEE single-precision,>=32bits)
7. 7.double (IEEE double-precision,>=64bits)
8. 8.string (All Unicode characters, excluding ,the character with all bits zero.)

1.bool (false or true,>=1bit)
2.byte (-128-127@,>=8bits)
3.short (-2^15 to 2^15-1,>=16bits)
4.int (-2^31 to 2^31-1,>=32bits)
5.long (-2^63 to 2^63-1,>=64bits)
6.float (IEEE single-precision,>=32bits)
7.double (IEEE double-precision,>=64bits)
8.string (All Unicode characters, excluding ,the character with all bits zero.)



string 采用Unicode编码,具有很好的国际化语言支持。

下面主要在介绍一下Slice的特性和约束:
1. Slice文件必须以.ice结尾,其他结尾方案的文件会被编译器拒绝。
2. Slice 文件格式实际就是text文本描述,由于Slice形式自由,可以用任何文本编辑器进行编辑。
3. Slice 支持#ifndef,#define,#endif,#include
例如:
Java代码

1. // File Clock.ice
2. #ifndef _CLOCK_ICE
3. #define _CLOCK_ICE
4. // #include directives here...
5. // Definitions here...
6. #endif _CLOCK_ICE

// File Clock.ice
#ifndef _CLOCK_ICE
#define _CLOCK_ICE
// #include directives here...
// Definitions here...
#endif _CLOCK_ICE


在include 要避免使用双引号"",以及"\",而要使用"/"
例如:
#include "Clock.ice" // 不推荐采用;
#include // 正确
#include <SliceDefs\Clock.ice> // 非法
4.Slice 中文件的结构,模块,接口,类型出现的顺序,,可以按照你的喜好而自由定义。
5.Slice 的词法规则很多来源于C++和Java,只有少量的差异。
6.可以采用c++的注释规则。// 或 /* xxx */
7.Slice 的关键词需要区分大小写:
关键词:
Java代码

1. bool enum implements module struct
2. byte exception int Object throws
3. class extends interface out true
4. const false local sequence void
5. dictionary float LocalObject short
6. double idempotent long string

bool enum implements module struct
byte exception int Object throws
class extends interface out true
const false local sequence void
dictionary float LocalObject short
double idempotent long string

8.在接口定义的时候也避免使用非Slice关键字,但是C++ 或 Java关键字的标识符。

例如:switch

9.Ice保留了所有以"ice"作为开始标识符。保留以"Helper","Holder","Prx","Ptr"结尾的标识符。所以大家定义的时候最好避免一些不必要的麻烦。

10.Ice可以通过module嵌套的方式,类似于c++的namespace和java的包的概念。


11.除了刚才提到的Slice支持的基础类型外,还支持用户自定义类型:enumerations, structures, sequences, and dictionaries.

enumerations:枚举就不要介绍了,采用C++的语法形式
enum Fruit { Apple, Pear, Orange };

Structures:结构,也是采用C++的语法形式,避免,结构中定义结构
Java代码

1. 合法:
2. struct TimeOfDay {
3. short hour; // 0 - 23
4. short minute; // 0 - 59
5. short second; // 0 - 59
6. };
7. 无效:
8. struct TwoPoints {
9. struct Point { // Illegal!
10. short x;
11. short y;
12. };
13. Point coord1;
14. Point coord2;
15. };

合法:
struct TimeOfDay {
short hour; // 0 - 23
short minute; // 0 - 59
short second; // 0 - 59
};
无效:
struct TwoPoints {
struct Point { // Illegal!
short x;
short y;
};
Point coord1;
Point coord2;
};


sequence:序列号类型,映射到java采用的数组方式实现,而不是所谓的集合容器类型对象存储。映射到C++中则采用STL容器存储。
sequence values;
dictionary:字典类型,映射到java采用Map 进行存储,映射到C++采用map进行存储.
dictionary myValues;
12.常量定义可以直接使用,但常量的定义必须是基本类型或枚举类型.定义形式也是采用C++定义方式.
13.方法的定义,形式类型java的方法定义,方法返回可以使void或对象类型.
14.Slice支持方法异常的定义,以及异常的继承.关于异常机制,我想java开发者可能更加熟悉,
例如:
Java代码

exception Error {}; // Empty exceptions are legal
exception RangeError {
TimeOfDay errorTime;
TimeOfDay minTime;
TimeOfDay maxTime;
};
interface Clock {
idempotent TimeOfDay getTime();
idempotent void setTime(TimeOfDay time)
throws RangeError, Error;
};

exception Error {}; // Empty exceptions are legal
exception RangeError {
TimeOfDay errorTime;
TimeOfDay minTime;
TimeOfDay maxTime;
};
interface Clock {
idempotent TimeOfDay getTime();
idempotent void setTime(TimeOfDay time)
throws RangeError, Error;
};

Ice的错误机制也是异常强大,吸取了很多Java关于异常机制的特点.

Slice还包括很多良好的特性,在这里就不一一列举,对于支持自定义类型,基本类型,异常支持,对于一般的网络通讯应用已经足够了,更多Slice语言的介绍参阅其帮助.

Ice为我们提供了强大而又丰富的Slice,可以利用Slice,结合我们应用的特点,定义间接的Slice描述。

虽然Slice提供给我们丰富的功能和特性,在此我还是建议大家尽量采用基本类型和非异常机制.这样会对系统性能会带有一定的帮助。
另外接口的定义我想大家能够保持KIS(Keep It Simple)设计原则,会使我们的应用看起来更美丽。
不要过度的使用Slice,否则会给我们带来无尽的烦恼。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics