IT翻訳 Nobuyuki の仕事部屋

ボランティアでソフトウエアーローカライズのために翻訳をしている。

Mozilla DOM Hacking Guide: Introduction to XPCOM 8

2006-01-17 23:14:58 | InDraft
再び寒さが戻って着た。

ホリエモンファンのワイフの浮かない顔を後に、仕事部屋に入る。

静かな夜だ。パソコンのドライヴの音が、静かなうなりを上げている。

************************************

原文:  青色表示
訳文:  黒色表示
注記/訂正: 赤色表示

Let's say that you now want to execute a function declared on the nsIFoo2 interface, also implemented by nsFoo. However, ifooptr does not give you access to that function, since it is a pointer to nsIFoo, for the reasons mentioned in Section 1.B. XPCOM provides a handy method to get a pointer to an interface when you have a pointer to another interface, and both interfaces are implemented by the same object. This method is QueryInterface(). It is defined on the nsISupports interface. Every single interface in Mozilla inherits from nsISupports. This is one of the main rules of XPCOM. The goal is the following: find out whether an object (an instance of a class) implements a given interface. This is what QueryInterface() does. You pass an interface and the address of a pointer to store the interface to QueryInterface(). If the interface is implemented by the object, the pointer passed in will be assigned to the address of the object. If the interface is not implemented by the object, QueryInterface() will return NS_NOINTERFACE, and the pointer passed in will be null.

今 nsIFoo2 インターフェイスで宣言され、nsFoo で実装された関数を実行したいとしましょう。しかしこの時、ifooptr 経由ではその関数にアクセスできません。それは 1.B の節で述べた理由のために ifooptr が nsIFoo へのポインタだからです。XPCOM には便利なメソッドがあり、別のインターフェイスへのポインタがある時、インターフェイスへのポインタを持てます。そして、2 つのインターフェイスは同じオブジェクトによって実装されます。この時のメソッドが QueryInterface() です。それは nsISupports インターフェイスで定義されます。Mozilla の各々のインターフェイスは nsISupports を継承しています。この事は、XPCOM の主要ルールの 1 つです。目的はこうです。オブジェクト(クラスのインスタンス)が所定のインターフェイスを実装するかどうかを知る。これが QueryInterface() の働きです。インターフェイスとインターフェイスを保持するポインタのアドレスを QueryInterface() に渡します。その時インターフェイスがオブジェクトによって実装されていれば、渡されたポインタはオブジェクトのアドレスを代入されます。もし実装されていなければ、 QueryInterface() は NS_NOINTERFACE を返し、渡されたポインタは null になります。

QueryInterface() is handy to hide the implementation of an object from the user. Just call QueryInterface() then call the method on the interface. You don't need to know anything else. So how do we use QueryInterface() to get a pointer to nsIFoo2 if we have a pointer to nsIFoo? Since we cannot re-use ifooptr, we create a new pointer to nsIFoo2, ifooptr2. The following syntax is the preferred one (to use only with nsCOMPtr's):

nsCOMPtr<nsIFoo2> ifooptr2 (do_QueryInterface(ifooptr));


QueryInterface() はユーザへオブジェクトの実装を隠蔽するのに便利です。 QueryInterface() をただ呼び出してから、インターフェイスのメソッドを呼び出します。その外は知る必要がありません。それでは、nsIFoo へのポインタがある時、どのように QueryInterface() を使って nsIFoo2 へのポインタを得るのでしょうか? ifooptr を再利用できない以上、nsIFoo2 への新ポインタである ifooptr2 を作ります。以下の構文が(nsCOMPtr とだけ使う場合)推奨されます:

nsCOMPtr<nsIFoo2> ifooptr2 (do_QueryInterface(ifooptr));


That syntax is the preferred one to declare and initialize an nsCOMPtr at the same time. If you have to assign it another address later, you can easily do

ifooptr2 = do_QueryInterface(another_pointer);


この構文は nsCOMPtr の宣言と初期化を同時にするのに推奨されます。あとで、別のアドレスをそこへ代入する必要があるならば、簡単にこうできます

ifooptr2 = do_QueryInterface(another_pointer);



This syntax however is just a convenient shortcut to the real function. Below is how one would use the QueryInterface() function with raw pointers.

nsIFoo2 *ifooptr2;
ifooptr->QueryInterface(NS_GET_IID(nsIFoo2), (void **)&ifooptr2);


しかしこの構文は、単に実際の関数への便利なショートカットにすぎません。以下の構文は生のポインタで QueryInterface() を使用する方法を表します。

nsIFoo2 *ifooptr2;
ifooptr->QueryInterface(NS_GET_IID(nsIFoo2), (void **)&ifooptr2);


See you tomorrow (^-^)/~~~~