[Development] Setters: Clarifying the ownership

Philippe philwave at gmail.com
Fri Jan 19 17:26:21 CET 2018


+20 years ago, the (good) Taligent crossplatform project, in its guideline, proposed:

adopt() aka "take ownership"

orphan() aka "release ownership"

Philippe

On Fri, 19 Jan 2018 16:09:21 +0000
Jaroslaw Kobus <Jaroslaw.Kobus at qt.io> wrote:

> "give" may be confused with "get", which is usually an accessor. I may also think "Am I giving (to QCoreApplication)" or "The QCoreApplication is giving (me)". Maybe it is just a matter of the other verb? Absorb, hand over, hand on, suck in, swallow...
>  
>  
>  Jarek
>  
>  
>  
> ------------------------------------------------------------
> From: Development <development-bounces+jaroslaw.kobus=qt.io at qt-project.org> on behalf of Jesus Fernandez <jesus.fernandez at qt.io>
>  Sent: Friday, January 19, 2018 4:15:52 PM
>  To: development at qt-project.org
>  Subject: [Development] Setters: Clarifying the ownership 
>  
>  
>  Hi all!
>  
>  I always found something annoying in the Qt API. The problem comes with the setters of our properties. When I want to pass an object to a property I never know if I need to take care of the object or relay on parenting system to avoid memory leaks.
>  To know if the object is going to be reparented, I open the assistant and look for the setter to try to find the famous "takes ownership of" in the function description. 
>  
>  Mårten Nordheim and I were talking about possible solutions to this problem. Typical things came to the discussion:
>  - adding a macro like Q_TAKES_OWNERSHIP to the function that expands to nothing
>  - wrapping the parameters with a template class (gsl::owner<T>)
>  - ...
>  
>  After some discussion he came with the idea of add a different "verb" to the setter, replace "set" with "give". So when we are giving the ownership of an object to instead of setSomething(&object); we will write giveSomething(&object); I really like this solution, it will improve a lot the readability of the client (and internal) code.
>  
>  For example: QCoreApplication::setEventDispatcher will be QCoreApplication::giveEventDispatcher. 
>  Of course at the beginning this will be a new function and the old set* functions will be kept, but marked as deprecated.
>  
>  
>  -- 
>  Best regards,
>  Jesús
>  





More information about the Development mailing list