--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
--
Hey Albert,Great to see improvements in these classes and great that there's now a specific way to declare ownership transfers.I am assuming that this replaces these two possible ways of accomplishing the same thing:TakeOwnership(my_scoped_ptr.release());andTakeOwnership(&my_scoped_ptr);where:void TakeOwnership(scoped_ptr<type>* p) { my_foo_.reset(p->release()); }
I've got one question and a question/feature request:Q: How does the new approach compare in code size to the above approaches?
Question/Feature request (no good deed goes unpunished, right?):One gripe I have with the scoped_* classes is that there is no way to transfer ownership of an object when it is passed back via an outputparameter. I'm actually very surprised that this isn't supported as in my experience (which lies mostly on Windows), this is a very commonthing to do. In fact, it's so common in Windows APIs that I included Receive() methods (we're discouraged from overriding operators) inScopedVariant, ScopedComPtr and ScopedBstr when I wrote them. Without them, one would have to either use a temporary, or not use ascoped class at all - i.e. having a Receive() is a necessity.
Is there a solution for the scoped_* classes that supports the below scenario? Would adding a .receive() method to the scoped_ classeswith the accompanied checks for leaks etc be a bad idea or is there perhaps an even better (and more efficient) way of doing it?int CreateFoo(Foo** ret);(think of the return value being 0 for success or valuable info on why the operation failed when it does)
This looks neat. Couple quick questions -- Should Pass() have WARN_UNUSED_RESULT annotation?
- Do you plan to merge it into the other ~4 copies of scope_ptr.h we have?I'm passively wondering about the consequences of forking this from the 'upstream' source of scoped_ptr. While usage of it is hidden under Bind() it's one thing, as hand written code makes increasing use of it, it will gradually make the code less familiar for anyone used to other google3-style projects (with implications for anyone seeking c++ readability, for example), and impact on code portability
--
This is great! Please correct me if I'm wrong, but with this change, can we now require that all dynamic allocations take place within the constructor of a scoped_* object? If we could get rid of all explicit deletes, that would be really nice.
Should http://www.chromium.org/developers/coding-style be updated to mention required use of scoped_ptr + Pass for all transfers of ownership?
I was wondering why we wouldn't instead usevoid Initialize(const scoped_ptr<A>& var);to save a temporary + copy, but then I realized var needs to be mutable or you can't call Pass.
If we do create a list of files to update with the new Pass mechanism, chrome_resource_dispatcher_host_delegate.cc should be on it - it's quite difficult to follow all the transfers of ownership.
On Mon, Dec 12, 2011 at 12:36 PM, Peter Kasting <pkas...@chromium.org> wrote:
This is super awesome. I would love to see the same sort of initiative to replace existing ownership transfers with this machinery that we've had to upgrade code to use base::Bind(). I suspect it's harder to find all the cases where this could happen, though.
e in the case of output pointers is because they make sense in the case of input pointe
Hah, well, not the solution I was hoping for :)
The reason I think that scoped_ptr makes sensrs.
In both cases ownership is being transferred from one scope to the other. Now there's "official" support for one direction but I'm wondering about the other. :)
RetValue MyFunction(scoped_ptr<X>* p);
Pretty nicely documents "I want you to return me a pointer, and I'm
required to handle ownership." The only downside to me would seem to
be the need to refer to (*p) in the implementation, rather than p, but
anything to resolve that would necessarily involve a certain amount of
hocus-pocus like a proxy object which only exists for purposes of
sugaring this case. But I think that passing a pointer to a
scoped_ptr<X> is easier to understand - it's hard to imagine anyone
doing Chromium development without having to know scoped_ptr<X>, but I
can easily imagine not having to deal with scoped_ptr<X>* very often
at all.
If it materially impacts things, you could always do:
RetValue MyFunction(scoped_ptr<X>* outp) {
scoped_ptr<X> p(new X(...));
// Working on p.
p.swap(*outp);
return GoodReturn;
}
-scott
On Mon, Dec 12, 2011 at 5:46 PM, Albert J. Wong (王重傑)
I may be missing something about this discussion, but I have always
thought that:
RetValue MyFunction(scoped_ptr<X>* p);
Pretty nicely documents "I want you to return me a pointer, and I'm
required to handle ownership."
Delaying the swap until the point of "good return" is generally very good because that avoids setting out params in the case of an early return.
-Albert
-Albert
Taking a step back to reframe the problem:We're trying to solve 2 distinct use cases for passing ownership back from an function call:(1) API is in chromium.(2) We're calling a third-party API that uses a T**.For (1), I think scoped_ptr<>* works great and we should probably encourage that in the majority of our code.
Looking at (2) more, I think Tommi's original reciever() that returns T** is probably the cleanest. It'd be nice to name the function in a way that (eg., AdaptToHandleApi()) that discourages people from using it inside a pure chromium API. But maybe it's not worth the extra typing?The global receiver type type/function works, but I'm not sure I see a type safety or documentation benefit over embedding a AdaptToHandleApi() call. On the down side, it adds another template symbol and will generate constructor/destructor calls for the temporary at the call site.-Albert
For (1), I think scoped_ptr<>* works great and we should probably encourage that in the majority of our code.
On Wed, Dec 14, 2011 at 1:20 PM, Albert J. Wong (王重傑) <ajw...@chromium.org> wrote:Taking a step back to reframe the problem:We're trying to solve 2 distinct use cases for passing ownership back from an function call:(1) API is in chromium.(2) We're calling a third-party API that uses a T**.For (1), I think scoped_ptr<>* works great and we should probably encourage that in the majority of our code.Agreed.Looking at (2) more, I think Tommi's original reciever() that returns T** is probably the cleanest. It'd be nice to name the function in a way that (eg., AdaptToHandleApi()) that discourages people from using it inside a pure chromium API. But maybe it's not worth the extra typing?The global receiver type type/function works, but I'm not sure I see a type safety or documentation benefit over embedding a AdaptToHandleApi() call. On the down side, it adds another template symbol and will generate constructor/destructor calls for the temporary at the call site.-AlbertThe main benefit to the global type is that it avoids us having to make changes to scoped_ptr.h or any of the other Scoped* classes.
--
Hi Albert,scoped_ptr<>::Pass() is great and I'm pretty happy to see it in Chromium.I have one question: How do I transfer the ownership of a pointer as base class's pointer?
struct RValue { \
template<typedef U> explicit RValue(U& obj) : obj_(obj) {} \ scoper& obj_; \ }; \
On 27 December 2011 07:01, Yuta Kitamura <yu...@chromium.org> wrote:
Hi Albert,scoped_ptr<>::Pass() is great and I'm pretty happy to see it in Chromium.I have one question: How do I transfer the ownership of a pointer as base class's pointer?
I think it could be as simple as introducing a templated constructor on scoped_ptr's RValue to fix this?struct RValue { \template<typedef U> explicit RValue(U& obj) : obj_(obj) {} \ scoper& obj_; \ }; \question would be, what other and more dangerous conversions would this then allow? Even in the example below, it likely be wrong if B does not have a virtual destructor, and if the ownership transfer happens transparently it would be easy to overlook this mistake. It feels akin to object slicing, although is not quite as bad as that!
On Tue, Dec 27, 2011 at 5:51 PM, Jonathan Dixon <jo...@chromium.org> wrote:On 27 December 2011 07:01, Yuta Kitamura <yu...@chromium.org> wrote:
Hi Albert,scoped_ptr<>::Pass() is great and I'm pretty happy to see it in Chromium.I have one question: How do I transfer the ownership of a pointer as base class's pointer?
I think it could be as simple as introducing a templated constructor on scoped_ptr's RValue to fix this?struct RValue { \template<typedef U> explicit RValue(U& obj) : obj_(obj) {} \ scoper& obj_; \ }; \question would be, what other and more dangerous conversions would this then allow? Even in the example below, it likely be wrong if B does not have a virtual destructor, and if the ownership transfer happens transparently it would be easy to overlook this mistake. It feels akin to object slicing, although is not quite as bad as that!My intention was that B and D have virtual destructor; actually there were virtual destructors in my original example but they slipped away while I was trying to make the code example as small as possible. Object slicing -- do not want!
-1 for adding copy constructor & assignment operator to scoped_ptr.
+akalinI missed this discussion while on leave, but this is a significant flaw here. We should fix it. I'm going to read the issue in detail next week, but if you have strong opinions on the right way to fix this, I'd love to hear. Especially since Albert's on leave.
On Fri, Feb 3, 2012 at 3:16 PM, William Chan (陈智昌)
<will...@chromium.org> wrote: