IT翻訳 Nobuyuki の仕事部屋

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

Mozilla DOM Hacking Guide: Introduction to XPCOM 7

2006-01-15 18:52:51 | InDraft
本日は冬の寒さが緩んだ一日だった。床屋へ行った。12月の初旬以来1ヵ月ぶりになる。不思議なのは、年を取っても髪の伸びる速さは変わらないことだ。いや、むしろ若いころよりは、速く伸びるのではと思う程だ。髪の量は少なくなってきているのにさ。(-_-;

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

A getter is a function defined in global scope or in class scope that will "return" a pointer to the required interface. Generally getters work like this: First, declare a pointer to an interface, ifooptr without assigning it. Pass the address of the pointer to the getter function. The getter function will then assign to the pointer the correct address and will QueryInterface the pointer to that interface. ifooptr is now a pointer to an interface assigned to the address of a real object, and you can thus now call methods defined on nsIFoo through ifooptr. Here is some example code.

nsCOMPtr<nsIFoo> ifooptr;
GetInterfaceIFoo(getter_AddRefs(ifooptr));
ifooptr->FunctionOfnsIFoo();


getter は、グローバルな領域や、要求されたインターフェイスへポインタを”返す”クラスの領域で定義される関数です。一般的に getter は以下のように機能します:最初にインターフェイスへのポインタである ifooptr を代入なしに宣言します。それから、getter 関数へポインタのアドレスを渡します。この時 getter 関数は、ポインタへ適当なアドレスを代入し、インターフェイスへのポインタへ QueryInterface します。ifooptr はすでに、実際のオブジェクトのアドレスを代入されたインターフェイスへのポインタとなっています。このようにして、ifooptr 経由で nsIFoo で定義されたメソッドをすぐに呼び出せます。以下にコードの例を上げます。

nsCOMPtr<nsIFoo> ifooptr;
GetInterfaceIFoo(getter_AddRefs(ifooptr));
ifooptr->FunctionOfnsIFoo();


The peculiar syntax, getter_AddRefs(pointer), is the nsCOMPtr counterpart to the usual "&" (address-of) C++ operator. It means that the Getter method will call the AddRef method for you. This is a contract between the caller, who says "I don't add a reference to this object because you have already done it", and the callee who says "I add a reference to this object, so don't do it!". If both the caller and the callee perform the AddRef, the object will be leaked, because one of the two references will never be released!

独特の構文である getter_AddRefs(pointer) は、通常の "&" (address-of) C++ 演算子に対して nsCOMPtr が対応するものと同じです。すなわち、Getter メソッドは AddRef メソッドを呼び出します。これは、「このオブジェクトに参照を追加しません。もうすでにあなたが追加していますからね」という呼び出し元と「このオブジェクトへは私が参照を追加しますので、追加しないでください」という呼び出し先との間の契約になります。もし、呼び出し元と呼び出し先が両方で AddRef を実行すると、どちらかの参照は解放されないので、オブジェクトはメモリリークとなります。

Please note that all Getter functions have to AddRef the returned pointer. If you're just using the Getter function though, you don't need to worry about it. More about this in the XPCOM Ownership Guide.

すべての Getter 関数は返されたポインタを AddRef しなければならない ことに注意してください。それでも今 Getter 関数 を使用しているとしても、心配することはありません。 XPCOM 所有の手引きでは詳しい情報を参照できます。

Please also note that some interfaces are not refcounted, like the frames and views. For those you have to use raw interface pointers, and you don't have to perform any manual refcounting.

フレームやビューのように、インターフェイスには参照カウントされないものがあることにも注意してください。これらに対しては生のインターフェイスポインタを使用しなければなりません。マニュアルで参照カウントする必要はありません。

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