IT翻訳 Nobuyuki の仕事部屋

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

Mozilla DOM Hacking Guide: Introduction to XPCOM 9

2006-01-18 23:31:29 | InDraft
昨日の続きをする。

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

NS_GET_IID is a macro that evaluates to the IID of the argument. It is a convenient way of comparing two interfaces for equality. We have already seen what getter_AddRefs() does to nsCOMPtr's. In this case we pass the address of the ifooptr2 pointer. Since nsFoo implements nsIFoo2, ifooptr2 will be assigned the address of the current instance of nsFoo. We can now call the methods defined on nsIFoo2 through ifooptr2:
 ifooptr2->FunctionOfnsIFoo2();


NS_GET_IID は引数の IID に対して評価をするマクロです。2 つのインターフェイスを比較して同一性を調べるのに便利です。nsCOMPtr への getter_AddRefs() の働きについてはすでに見たとおりです。今回は、ifooptr2 のアドレスを渡します。nsFoo は nsIFoo2 を実装するので、ifooptr2 は nsFoo の現在のインスタンスのアドレスを代入されます。すると、ifooptr2 経由で nsIFoo2 で定義されたメソッドを呼び出せます:
 ifooptr2->FunctionOfnsIFoo2();


If we now try to QueryInterface to an interface that is not implemented by nsFoo, the pointer passed in will be null. This is why, unless you are really really really sure of what you are doing, you should always check for the null-ness of the nsCOMPtr. The following example demonstrates this.
  nsCOMPtr iptr(do_QueryInterface(ifooptr));
if(!iptr) {
// nsFoo doesn't implement nsINotImplemented. iptr is thus null.
return NS_OK;
}


nsFoo で実装されないインターフェイスへ QueryInterface を今試みるならば、渡されたポインタは null となります。そんな訳で、自分の処理を本当に確実に明白に間違いないと思われないなら、nsCOMPtr が null になっていないかどうか必ず確認してください。以下のサンプルを参考にしてください。
  nsCOMPtr iptr(do_QueryInterface(ifooptr));
if(!iptr) {
// nsFoo は nsINotImplemented を実装しない。 iptr はそのため null である。
return NS_OK;
}


It is also worth noting that QueryInterface is null-safe. For example, in the previous example, nothing bad will happen if ifooptr is null. Additionally, the return value of a call to QueryInterface should not be returned unless there is a good reason for that. If you are concerned that it could return NS_NOINTERFACE, return NS_OK, as the previous example shows.

QueryInterface が null-safe であることに注意することも大切です。たとえば前の例では、ifooptr がnull であっても、不都合は発生しません。さらに、QueryInterface への呼び出しの戻り値は正当な理由がない限りは返されるべきではありません。NS_NOINTERFACE を返すことが心配ならば、前のサンプルが示すように NS_OK を返してください。

see you in the soup.(^Q^)/"