Closed Bug 857645 Opened 11 years ago Closed 11 years ago

Make it possible to copy-initialize an nsCOMPtr from a superclass already_AddRefed

Categories

(Core :: XPCOM, defect)

x86
macOS
defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla23

People

(Reporter: bzbarsky, Assigned: bzbarsky)

References

Details

Attachments

(1 file, 2 obsolete files)

Basically, want code like this:

  nsRefPtr<nsTextNode> foo;
  nsCOMPtr<nsIContent> baz = foo.forget();

to compile.  Right now it doesn't.
Whiteboard: [need review[ → [need review]
Assignee: nobody → bzbarsky
Comment on attachment 732899 [details] [diff] [review]
Make it possible to copy-initialize an nsCOMPtr<T> from an already_AddRefed<U> as long as U inherits from T.

Review of attachment 732899 [details] [diff] [review]:
-----------------------------------------------------------------

::: xpcom/glue/nsCOMPtr.h
@@ +555,5 @@
> +        {
> +          // But make sure that U actually inherits from T, not the
> +          // other way around.
> +          T* unused = aSmartPtr.mRawPtr;
> +          (void) unused;

Instead of this, please use a static assert with mozilla::IsBaseOf.
Attachment #732899 - Flags: review?(ehsan) → review-
Can I use that in nsCOMPtr?  If so, happy to do it.
Attached patch Let's try this (obsolete) — Splinter Review
Attachment #733157 - Flags: review?(ehsan)
Attachment #732899 - Attachment is obsolete: true
Comment on attachment 733157 [details] [diff] [review]
Let's try this

Review of attachment 733157 [details] [diff] [review]:
-----------------------------------------------------------------

Great, thanks!
Attachment #733157 - Flags: review?(ehsan) → review+
https://hg.mozilla.org/integration/mozilla-inbound/rev/8396d7543197
Flags: in-testsuite?
Whiteboard: [need review]
Target Milestone: --- → mozilla23
Fix that by making our operator= also accept any relevant already_AddRefed.
Attachment #733750 - Flags: review?(ehsan)
Comment on attachment 733750 [details] [diff] [review]
Make it possible to copy-initialize an nsCOMPtr<T> from an already_AddRefed<U> as long as U inherits from T.

Why do you need to static_cast to T* once you know U is a subclass of T?
Because I'm assigning into an nsISupports*, and U may have ambiguous inheritance from nsISupports, but T is known not to.
Attachment #733157 - Attachment is obsolete: true
Whiteboard: [need review]
Attachment #733750 - Flags: review?(ehsan) → review+
https://hg.mozilla.org/integration/mozilla-inbound/rev/d93dab0b19fc
Flags: in-testsuite? → in-testsuite-
Whiteboard: [need review]
https://hg.mozilla.org/mozilla-central/rev/d93dab0b19fc
Status: NEW → RESOLVED
Closed: 11 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: