mozilla

Revision 140534 of nsAutoRefTraits

  • Revision slug: NsAutoRefTraits
  • Revision title: nsAutoRefTraits
  • Revision id: 140534
  • Created:
  • Creator: Karlt
  • Is current revision? Yes
  • Comment 7 words added

Revision Content

nsAutoRefTraits<T> is a template class describing traits of resources held by objects of class nsAutoRef<T> and/or nsCountedRef<T>.

When the handle to the resource is a pointer to T the specialization may be derived from nsPointerRefTraits<T>, so that only definitions of the Release(T*) static method and possibly the AddRef(T*) static method need to be added. AddRef(T*) is necessary for nsCountedRef<T> but not for nsAutoRef<T>.

For example:

  NS_SPECIALIZE_TEMPLATE
  class nsAutoRefTraits<PRFileDesc> : public nsPointerRefTraits<PRFileDesc>
  {
  public:
      static void Release(PRFileDesc *ptr) { PR_Close(ptr); }
  };

or

  NS_SPECIALIZE_TEMPLATE
  class nsAutoRefTraits<FcPattern> : public nsPointerRefTraits<FcPattern>
  {
  public:
      static void Release(FcPattern *ptr) { FcPatternDestroy(ptr); }
      static void AddRef(FcPattern *ptr) { FcPatternReference(ptr); }
  };

nsAutoRefTraits is described in {{ Source("xpcom/base/nsAutoRef.h") }}.

See Also

nsAutoRef, nsCountedRef 

Revision Source

<p>nsAutoRefTraits&lt;T&gt; is a template class describing traits of resources held by objects of class <a class="internal" href="/En/NsAutoRef" title="En/NsAutoRef">nsAutoRef</a>&lt;T&gt; and/or nsCountedRef&lt;T&gt;.</p>
<p>When the handle to the resource is a pointer to T the specialization may be derived from nsPointerRefTraits&lt;T&gt;, so that only definitions of the Release(T*) static method and possibly the AddRef(T*) static method need to be added. AddRef(T*) is necessary for nsCountedRef&lt;T&gt; but not for nsAutoRef&lt;T&gt;.</p>
<p>For example:</p>
<pre>  NS_SPECIALIZE_TEMPLATE
  class nsAutoRefTraits&lt;PRFileDesc&gt; : public nsPointerRefTraits&lt;PRFileDesc&gt;
  {
  public:
      static void Release(PRFileDesc *ptr) { PR_Close(ptr); }
  };</pre>
<p>or</p>
<pre>  NS_SPECIALIZE_TEMPLATE
  class nsAutoRefTraits&lt;FcPattern&gt; : public nsPointerRefTraits&lt;FcPattern&gt;
  {
  public:
      static void Release(FcPattern *ptr) { FcPatternDestroy(ptr); }
      static void AddRef(FcPattern *ptr) { FcPatternReference(ptr); }
  };
</pre>
<p>nsAutoRefTraits is described in {{ Source("xpcom/base/nsAutoRef.h") }}.</p>
<h3 id="See_Also">See Also</h3>
<p><a class="internal" href="/En/NsAutoRef" title="En/NsAutoRef">nsAutoRef</a>, <a class="internal" href="/En/NsCountedRef" title="En/NsCountedRef">nsCountedRef</a> </p>
Revert to this revision