XPIDL簡明教程

來源:互聯網
上載者:User
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++ mappingvoid     voidboolean    PRBooloctet     PRUint8short     PRInt16long     PRInt32long long    PRInt64unsigned short   PRUint16unsigned long   PRUint32unsigned long long  PRUint64float     floatdouble     doublechar     charwchar     PRUnicharstring     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的字串加以替代,只是使用不方便,所以我們還是選擇使用前者。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.