mozilla

Revision 140533 of nsAutoRefTraits

  • Revision slug: NsAutoRefTraits
  • Revision title: nsAutoRefTraits
  • Revision id: 140533
  • Created:
  • Creator: Karlt
  • Is current revision? No
  • Comment 2 words added, 1 words removed; page display name changed to 'nsAutoRefTraits'

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 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<br>
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<br>
derived from nsPointerRefTraits&lt;T&gt;, so that only definitions of the<br>
Release(T*) static method and possibly the AddRef(T*) static method need to be added.<br>
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 xpcom/base/nsAutoRef.h.</p>

<h3>See Also</h3>
<p><a class="internal" href="/En/NsAutoRef" title="En/NsAutoRef">nsAutoRef</a>, nsCountedRef</p>
Revert to this revision