Skip to content

Commit

Permalink
Bug 1116905 - part 2 - add MakeAndAddRef helper function to facilitat…
Browse files Browse the repository at this point in the history
…e constructing TemporaryRef; r=Ms2ger

With implicit conversion to TemporaryRef going away, one can no longer write:

  return new T(...);

in a function returning TemporaryRef<T>.  Instead, provide MakeAndAddRef
to prevent people from having to construct boilerplate RefPtrs or
similar.  It also makes converting from TemporaryRef to already_AddRefed
somewhat easier.
  • Loading branch information
froydnj committed Apr 30, 2015
1 parent 971b1c5 commit eec74c4
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions mfbt/RefPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,28 @@ byRef(RefPtr<T>& aPtr)
return OutParamRef<T>(aPtr);
}

/**
* Helper function to be able to conveniently write things like:
*
* TemporaryRef<T>
* f(...)
* {
* return MakeAndAddRef<T>(...);
* }
*
* since explicitly constructing TemporaryRef is unsightly. Having an
* explicit construction of TemporaryRef from T* also inhibits a future
* auto-conversion from TemporaryRef to already_AddRefed, since the semantics
* of TemporaryRef(T*) differ from already_AddRefed(T*).
*/
template<typename T, typename... Args>
TemporaryRef<T>
MakeAndAddRef(Args&&... aArgs)
{
RefPtr<T> p(new T(Forward<Args>(aArgs)...));
return p.forget();
}

} // namespace mozilla

#endif /* mozilla_RefPtr_h */

0 comments on commit eec74c4

Please sign in to comment.