现在的位置: 首页 > 综合 > 正文

XPIDL简明教程

2012年01月25日 ⁄ 综合 ⁄ 共 2496字 ⁄ 字号 评论关闭
XPIDL是为mozilla平台开发XPCOM的基础,在这里简单介绍如下。
XPIDL之旅
1.接口
XPIDL是用来描述XPCOM的语言,它可以生成给C++使用的头文件,以及给其他语言使用的类型库‘
最简单的例子:
interface nsIFoo {
};
如果有父接口,则可以像下面写:
interface nsIFoo : nsIParent {
};
按照约定,接口应该有uuid,所以更完善的版本应该如下:
[uuid(00000000-0000-0000-c000-000000000046)]
interface nsIFoo : nsIParent {
};
如果只是想声明一个接口,不想定义它,可以像下面那样写:
interface nsIForward;

2.方法和属性
有一个方法和一个属性的例子:
[uuid(00000000-0000-0000-c000-000000000046)]
interface nsIFoo {
attribute long attr;
void fun();
};

方法可以有带in, out 或者 inout 修饰符的任意数目的参数,参数可以有类型,以下是其可选的类型:

代码:
Type C++ mapping void void boolean PRBool octet PRUint8 short PRInt16 long PRInt32 long long PRInt64 unsigned short PRUint16 unsigned long PRUint32 unsigned long long PRUint64 float float double double char char wchar PRUnichar string char* wstring PRUnichar*

左边的是XPIDL语言内置的变量类型,右边的是映射后对应C++的。
也可以指定返回值不为void的方法,但是生成的C++代码会自动把返回值添加到方法的形参中的最后。
attribute之前可以用readonly加以修饰。
可以使用native name(native_type);的语法定义自己的类型,并在XPIDL中使用,如:
native nsNativeFileRef(string);
native nsNativeFilePtr(string);

interface foo {
void openByRef(in nsNativeFileRef aFileSpecRef);
void openByPtr(in nsNativeFilePtr aFileSpecPtr);
};
可以在native之前增加[ref]或者[ptr]修饰符表示这个变量类型为引用或者指针。
注意的是:如果方法中的形参设置为自己定义的类型,那么通常意味着该方法不能够给脚本调用。
可以使用#include关键字包含其他idl文件。
如果用const修饰某个常量,那么映射到C++代码的话会变为enum类型,所以const只能用来修饰整型。
如果想定义float的常量,那么自己定义一个方法,返回某个浮点值吧。[/quote]

附件:
关键字列表

代码:
关键字 作用 例子 interface 声明接口 interface foo : public nsISupports {}; attribute 属性的声明 inteface foo { attribute short bar;} readonly 修饰只读属性 readonly attribute short myAttribute; [uuid()] 定义接口uuid [uuid(00000000-0000-0000-c000-000000000046)] interface foo {}; [scriptable] 将接口设置为可以被脚本化 [scriptable] interface foo {}; const 在接口内声明一个常量 interface foo { const short bar = 5;}; [const] 声明函数调用中某个形参为常量 void foo([const] in voidStar bar); native 用来声明本地类型 native myName(nsFileSpec); [ptr] 在本地类型声明中表示类型为指针 [ptr] native nsFileSpecPtr(nsFileSpec); [ref] 在本地类型声明中表示类型为引用 [ref] native nsFileSpecRef(nsFileSpec); [retval] 标志方法中的某个形参为 interface nsISupports { [iid_is] 标志方法中某个返回的形参是个接口的uuid void QueryInterface(in nsIIDRef uuid,[iid_is(uuid),retval] out nsQIResult result);}; [noscript] 声明某个方法不可被脚本化 in 方法中的某个参量是输入参数 out 方法中的某个参量是输出参数 inout 方法中的某个参量是输入参数也是输出参数 %{C++ 范围内的代码直接拷贝进生成的C++代码中 %}

所有的XPCOM接口都继承了nsISupports,它是什么呢,如下
[uuid(00000000-0000-0000-c000-000000000046)]
interface nsISupports
{
void QueryInterface(in nsIIDRef uuid, out nsQIResult result);
nsrefcnt AddRef();
nsrefcnt Release();
};

我们看一句firefox的javascript的例子已进一步深入了解XPCOM插件的使用:
// 创建一个组件
var file = components.classes["@mozilla.org/file/local;1"].createInstance();

@mozilla.org/file/local;1,这样的一个奇怪的字符串引起了我们研究下去的注意,它的一般格式是:
◎<互联网域名>/模块[/子模块[...]];<版本>[?<名字>=<取值>[&<名字>=<取值>[...]]]
其实这个字符串也可以用个表示某个类的uuid的字符串加以替代,只是使用不方便,所以我们还是选择使用前者。

抱歉!评论已关闭.