AddObserver taking C++11 lambda?

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AddObserver taking C++11 lambda?

Elvis Stansvik
Hi all,

With C++11 now required, would there be interest in an AddObserver
version that takes a C++11 lambda? I've found myself wishing there was
one a couple of times.

Anyone know off the top of their head how easy/hard that would be to add?

Cheers,
Elvis
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Search the list archives at: http://markmail.org/search/?q=vtk-developers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtk-developers

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AddObserver taking C++11 lambda?

Sankhesh Jhaveri-2

+1 to using lambda functions for AddObserver.


On Tue, Mar 21, 2017 at 11:53 PM Elvis Stansvik <[hidden email]> wrote:
Hi all,

With C++11 now required, would there be interest in an AddObserver
version that takes a C++11 lambda? I've found myself wishing there was
one a couple of times.

Anyone know off the top of their head how easy/hard that would be to add?

Cheers,
Elvis
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Search the list archives at: http://markmail.org/search/?q=vtk-developers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtk-developers

--
Sankhesh Jhaveri
Sr. Research & Development Engineer | Kitware | (518) 881-4417

_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Search the list archives at: http://markmail.org/search/?q=vtk-developers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtk-developers

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AddObserver taking C++11 lambda?

David Gobbi
In reply to this post by Elvis Stansvik
It doesn't look hard.  New AddObserver methods would be needed that take std::function<> as a parameter, and they'd have to store the "function" object and call it.  Overall, it would probably be simpler than the code that currently exists in vtkObject.h for adding observers.

On Wed, Mar 22, 2017 at 12:53 AM, Elvis Stansvik <[hidden email]> wrote:
Hi all,

With C++11 now required, would there be interest in an AddObserver
version that takes a C++11 lambda? I've found myself wishing there was
one a couple of times.

Anyone know off the top of their head how easy/hard that would be to add?

Cheers,
Elvis
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Search the list archives at: http://markmail.org/search/?q=vtk-developers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtk-developers



_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Search the list archives at: http://markmail.org/search/?q=vtk-developers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtk-developers

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AddObserver taking C++11 lambda?

Elvis Stansvik

Den 22 mars 2017 6:48 em skrev "David Gobbi" <[hidden email]>:
>
> It doesn't look hard.  New AddObserver methods would be needed that take std::function<> as a parameter, and they'd have to store the "function" object and call it.  Overall, it would probably be simpler than the code that currently exists in vtkObject.h for adding observers.

Good to hear, I might have a go at it then.

Elvis

>
> On Wed, Mar 22, 2017 at 12:53 AM, Elvis Stansvik <[hidden email]> wrote:
>>
>> Hi all,
>>
>> With C++11 now required, would there be interest in an AddObserver
>> version that takes a C++11 lambda? I've found myself wishing there was
>> one a couple of times.
>>
>> Anyone know off the top of their head how easy/hard that would be to add?
>>
>> Cheers,
>> Elvis
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>
>> Search the list archives at: http://markmail.org/search/?q=vtk-developers
>>
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/vtk-developers
>>
>


_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Search the list archives at: http://markmail.org/search/?q=vtk-developers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtk-developers

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AddObserver taking C++11 lambda?

Robert Maynard-4
While allowing lambda's to be held by std::function is really nice, it
becomes very easy to mistakenly captures items whose lifetime is
shorter than the Observer you are binding too.

I am currently writing up the first draft of c++11 guidelines for VTK
and for lambda functions and these are the guidelines I currently
have:

Usage of lambda expressions are allowed with the following guidelines.

- Use default capture by value ([=]) only as a means of binding a few
variables for a short lambda, where the set of captured variables is
obvious at a glance. Prefer not to write long or complex lambdas with
default capture by value.
- Except for the above, all capture arguments must be explicitly
captured. Using the default capture by reference ([&]) is not allowed.
This is to done so that it is easier to evaluate lifespan and
reference ownership.
- Keep unnamed lambdas short. If a lambda body is more than maybe five
lines long, prefer using a named function instead of a lambda.
- Specify the return type of the lambda explicitly if that will make
it more obvious to readers.

On-top of these guidelines I have also stated that lambda functions
should be kept to implementation files in VTK, which is fine as we go
with an approach like David proposed.

On Wed, Mar 22, 2017 at 2:00 PM, Elvis Stansvik
<[hidden email]> wrote:

> Den 22 mars 2017 6:48 em skrev "David Gobbi" <[hidden email]>:
>>
>> It doesn't look hard.  New AddObserver methods would be needed that take
>> std::function<> as a parameter, and they'd have to store the "function"
>> object and call it.  Overall, it would probably be simpler than the code
>> that currently exists in vtkObject.h for adding observers.
>
> Good to hear, I might have a go at it then.
>
> Elvis
>
>>
>> On Wed, Mar 22, 2017 at 12:53 AM, Elvis Stansvik
>> <[hidden email]> wrote:
>>>
>>> Hi all,
>>>
>>> With C++11 now required, would there be interest in an AddObserver
>>> version that takes a C++11 lambda? I've found myself wishing there was
>>> one a couple of times.
>>>
>>> Anyone know off the top of their head how easy/hard that would be to add?
>>>
>>> Cheers,
>>> Elvis
>>> _______________________________________________
>>> Powered by www.kitware.com
>>>
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>>
>>> Search the list archives at: http://markmail.org/search/?q=vtk-developers
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://public.kitware.com/mailman/listinfo/vtk-developers
>>>
>>
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Search the list archives at: http://markmail.org/search/?q=vtk-developers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtk-developers
>
>
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Search the list archives at: http://markmail.org/search/?q=vtk-developers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtk-developers

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AddObserver taking C++11 lambda?

David Gobbi
Hi Robert,

Our AddObserver() methods only use weak pointers when the observer is derived from vtkObjectBase, which means that currently if the observer is not a VTK object, then we have a situation where we might call a dangling pointer.  I'm not aware of any uniform way around get around this issue except for using std::weak_ptr, which in turn demands that any non-vtk observer objects are wrapped with a std::shared_ptr.

When people start using lambdas with AddObserver, about the only thing we can do for safety is provide rules such as the ones that you describe to ensure that the lambda doesn't capture pointers that can be left dangling.

Thanks, by the way, for working on the c++11 guidelines.

 - David

On Thu, Mar 23, 2017 at 8:31 AM, Robert Maynard <[hidden email]> wrote:
While allowing lambda's to be held by std::function is really nice, it
becomes very easy to mistakenly captures items whose lifetime is
shorter than the Observer you are binding too.

I am currently writing up the first draft of c++11 guidelines for VTK
and for lambda functions and these are the guidelines I currently
have:

Usage of lambda expressions are allowed with the following guidelines.

- Use default capture by value ([=]) only as a means of binding a few
variables for a short lambda, where the set of captured variables is
obvious at a glance. Prefer not to write long or complex lambdas with
default capture by value.
- Except for the above, all capture arguments must be explicitly
captured. Using the default capture by reference ([&]) is not allowed.
This is to done so that it is easier to evaluate lifespan and
reference ownership.
- Keep unnamed lambdas short. If a lambda body is more than maybe five
lines long, prefer using a named function instead of a lambda.
- Specify the return type of the lambda explicitly if that will make
it more obvious to readers.

On-top of these guidelines I have also stated that lambda functions
should be kept to implementation files in VTK, which is fine as we go
with an approach like David proposed.

On Wed, Mar 22, 2017 at 2:00 PM, Elvis Stansvik
<[hidden email]> wrote:
> Den 22 mars 2017 6:48 em skrev "David Gobbi" <[hidden email]>:
>>
>> It doesn't look hard.  New AddObserver methods would be needed that take
>> std::function<> as a parameter, and they'd have to store the "function"
>> object and call it.  Overall, it would probably be simpler than the code
>> that currently exists in vtkObject.h for adding observers.
>
> Good to hear, I might have a go at it then.
>
> Elvis
>
>>
>> On Wed, Mar 22, 2017 at 12:53 AM, Elvis Stansvik
>> <[hidden email]> wrote:
>>>
>>> Hi all,
>>>
>>> With C++11 now required, would there be interest in an AddObserver
>>> version that takes a C++11 lambda? I've found myself wishing there was
>>> one a couple of times.
>>>
>>> Anyone know off the top of their head how easy/hard that would be to add?
>>>
>>> Cheers,
>>> Elvis
>>> _______________________________________________
>>> Powered by www.kitware.com
>>>
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>>
>>> Search the list archives at: http://markmail.org/search/?q=vtk-developers
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://public.kitware.com/mailman/listinfo/vtk-developers
>>>
>>
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Search the list archives at: http://markmail.org/search/?q=vtk-developers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtk-developers
>
>


_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Search the list archives at: http://markmail.org/search/?q=vtk-developers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtk-developers

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AddObserver taking C++11 lambda?

Chris Harris
Qt provides help for this situation ( in the signal  world ) by allowing a "context object" to be provided on the connect, which causes the signal to be disconnected when the object is destroyed. Could something similar work here?

Chris

On Thu, Mar 23, 2017 at 10:55 AM, David Gobbi <[hidden email]> wrote:
Hi Robert,

Our AddObserver() methods only use weak pointers when the observer is derived from vtkObjectBase, which means that currently if the observer is not a VTK object, then we have a situation where we might call a dangling pointer.  I'm not aware of any uniform way around get around this issue except for using std::weak_ptr, which in turn demands that any non-vtk observer objects are wrapped with a std::shared_ptr.

When people start using lambdas with AddObserver, about the only thing we can do for safety is provide rules such as the ones that you describe to ensure that the lambda doesn't capture pointers that can be left dangling.

Thanks, by the way, for working on the c++11 guidelines.

 - David

On Thu, Mar 23, 2017 at 8:31 AM, Robert Maynard <[hidden email]> wrote:
While allowing lambda's to be held by std::function is really nice, it
becomes very easy to mistakenly captures items whose lifetime is
shorter than the Observer you are binding too.

I am currently writing up the first draft of c++11 guidelines for VTK
and for lambda functions and these are the guidelines I currently
have:

Usage of lambda expressions are allowed with the following guidelines.

- Use default capture by value ([=]) only as a means of binding a few
variables for a short lambda, where the set of captured variables is
obvious at a glance. Prefer not to write long or complex lambdas with
default capture by value.
- Except for the above, all capture arguments must be explicitly
captured. Using the default capture by reference ([&]) is not allowed.
This is to done so that it is easier to evaluate lifespan and
reference ownership.
- Keep unnamed lambdas short. If a lambda body is more than maybe five
lines long, prefer using a named function instead of a lambda.
- Specify the return type of the lambda explicitly if that will make
it more obvious to readers.

On-top of these guidelines I have also stated that lambda functions
should be kept to implementation files in VTK, which is fine as we go
with an approach like David proposed.

On Wed, Mar 22, 2017 at 2:00 PM, Elvis Stansvik
<[hidden email]> wrote:
> Den 22 mars 2017 6:48 em skrev "David Gobbi" <[hidden email]>:
>>
>> It doesn't look hard.  New AddObserver methods would be needed that take
>> std::function<> as a parameter, and they'd have to store the "function"
>> object and call it.  Overall, it would probably be simpler than the code
>> that currently exists in vtkObject.h for adding observers.
>
> Good to hear, I might have a go at it then.
>
> Elvis
>
>>
>> On Wed, Mar 22, 2017 at 12:53 AM, Elvis Stansvik
>> <[hidden email]> wrote:
>>>
>>> Hi all,
>>>
>>> With C++11 now required, would there be interest in an AddObserver
>>> version that takes a C++11 lambda? I've found myself wishing there was
>>> one a couple of times.
>>>
>>> Anyone know off the top of their head how easy/hard that would be to add?
>>>
>>> Cheers,
>>> Elvis
>>> _______________________________________________
>>> Powered by www.kitware.com
>>>
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>>
>>> Search the list archives at: http://markmail.org/search/?q=vtk-developers
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://public.kitware.com/mailman/listinfo/vtk-developers
>>>
>>
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Search the list archives at: http://markmail.org/search/?q=vtk-developers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtk-developers
>
>


_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Search the list archives at: http://markmail.org/search/?q=vtk-developers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtk-developers




_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Search the list archives at: http://markmail.org/search/?q=vtk-developers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtk-developers

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AddObserver taking C++11 lambda?

Hahn, Steven E.
In reply to this post by Robert Maynard-4
Would it be more efficient to pass the lambda via a template parameter, like what is done in vtkClassMemberCallback? std::function<> adds a lot of overhead (see https://vittorioromeo.info/index/blog/passing_functions_to_functions.html ) and may be overkill.

Steven

On 3/23/17, 10:31 AM, "vtk-developers on behalf of Robert Maynard" <[hidden email] on behalf of [hidden email]> wrote:

    While allowing lambda's to be held by std::function is really nice, it
    becomes very easy to mistakenly captures items whose lifetime is
    shorter than the Observer you are binding too.
   
    I am currently writing up the first draft of c++11 guidelines for VTK
    and for lambda functions and these are the guidelines I currently
    have:
   
    Usage of lambda expressions are allowed with the following guidelines.
   
    - Use default capture by value ([=]) only as a means of binding a few
    variables for a short lambda, where the set of captured variables is
    obvious at a glance. Prefer not to write long or complex lambdas with
    default capture by value.
    - Except for the above, all capture arguments must be explicitly
    captured. Using the default capture by reference ([&]) is not allowed.
    This is to done so that it is easier to evaluate lifespan and
    reference ownership.
    - Keep unnamed lambdas short. If a lambda body is more than maybe five
    lines long, prefer using a named function instead of a lambda.
    - Specify the return type of the lambda explicitly if that will make
    it more obvious to readers.
   
    On-top of these guidelines I have also stated that lambda functions
    should be kept to implementation files in VTK, which is fine as we go
    with an approach like David proposed.
   
    On Wed, Mar 22, 2017 at 2:00 PM, Elvis Stansvik
    <[hidden email]> wrote:
    > Den 22 mars 2017 6:48 em skrev "David Gobbi" <[hidden email]>:
    >>
    >> It doesn't look hard.  New AddObserver methods would be needed that take
    >> std::function<> as a parameter, and they'd have to store the "function"
    >> object and call it.  Overall, it would probably be simpler than the code
    >> that currently exists in vtkObject.h for adding observers.
    >
    > Good to hear, I might have a go at it then.
    >
    > Elvis
    >
    >>
    >> On Wed, Mar 22, 2017 at 12:53 AM, Elvis Stansvik
    >> <[hidden email]> wrote:
    >>>
    >>> Hi all,
    >>>
    >>> With C++11 now required, would there be interest in an AddObserver
    >>> version that takes a C++11 lambda? I've found myself wishing there was
    >>> one a couple of times.
    >>>
    >>> Anyone know off the top of their head how easy/hard that would be to add?
    >>>
    >>> Cheers,
    >>> Elvis
    >>> _______________________________________________
    >>> Powered by www.kitware.com
    >>>
    >>> Visit other Kitware open-source projects at
    >>> http://www.kitware.com/opensource/opensource.html
    >>>
    >>> Search the list archives at: http://markmail.org/search/?q=vtk-developers
    >>>
    >>> Follow this link to subscribe/unsubscribe:
    >>> http://public.kitware.com/mailman/listinfo/vtk-developers
    >>>
    >>
    >
    >
    > _______________________________________________
    > Powered by www.kitware.com
    >
    > Visit other Kitware open-source projects at
    > http://www.kitware.com/opensource/opensource.html
    >
    > Search the list archives at: http://markmail.org/search/?q=vtk-developers
    >
    > Follow this link to subscribe/unsubscribe:
    > http://public.kitware.com/mailman/listinfo/vtk-developers
    >
    >
    _______________________________________________
    Powered by www.kitware.com
   
    Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
   
    Search the list archives at: http://markmail.org/search/?q=vtk-developers
   
    Follow this link to subscribe/unsubscribe:
    http://public.kitware.com/mailman/listinfo/vtk-developers
   
   
   

_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Search the list archives at: http://markmail.org/search/?q=vtk-developers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtk-developers

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AddObserver taking C++11 lambda?

David Gobbi
In reply to this post by Chris Harris
Hi Chris,

If I'm reading the Qt docs correctly, then the "context" object that connect() uses is simply any Qt object.  If we add an AddObserver() that takes lambdas or function pointers, then we could similarly take a "context" object that would be any vtkObjectBase-derived object.  But as far as I understand, this wouldn't be generalizable to non-vtk objects.

 - David


On Thu, Mar 23, 2017 at 9:13 AM, Chris Harris <[hidden email]> wrote:
Qt provides help for this situation ( in the signal  world ) by allowing a "context object" to be provided on the connect, which causes the signal to be disconnected when the object is destroyed. Could something similar work here?

Chris

On Thu, Mar 23, 2017 at 10:55 AM, David Gobbi <[hidden email]> wrote:
Hi Robert,

Our AddObserver() methods only use weak pointers when the observer is derived from vtkObjectBase, which means that currently if the observer is not a VTK object, then we have a situation where we might call a dangling pointer.  I'm not aware of any uniform way around get around this issue except for using std::weak_ptr, which in turn demands that any non-vtk observer objects are wrapped with a std::shared_ptr.

When people start using lambdas with AddObserver, about the only thing we can do for safety is provide rules such as the ones that you describe to ensure that the lambda doesn't capture pointers that can be left dangling.

Thanks, by the way, for working on the c++11 guidelines.

 - David

On Thu, Mar 23, 2017 at 8:31 AM, Robert Maynard <[hidden email]> wrote:
While allowing lambda's to be held by std::function is really nice, it
becomes very easy to mistakenly captures items whose lifetime is
shorter than the Observer you are binding too.

I am currently writing up the first draft of c++11 guidelines for VTK
and for lambda functions and these are the guidelines I currently
have:

Usage of lambda expressions are allowed with the following guidelines.

- Use default capture by value ([=]) only as a means of binding a few
variables for a short lambda, where the set of captured variables is
obvious at a glance. Prefer not to write long or complex lambdas with
default capture by value.
- Except for the above, all capture arguments must be explicitly
captured. Using the default capture by reference ([&]) is not allowed.
This is to done so that it is easier to evaluate lifespan and
reference ownership.
- Keep unnamed lambdas short. If a lambda body is more than maybe five
lines long, prefer using a named function instead of a lambda.
- Specify the return type of the lambda explicitly if that will make
it more obvious to readers.

On-top of these guidelines I have also stated that lambda functions
should be kept to implementation files in VTK, which is fine as we go
with an approach like David proposed.

On Wed, Mar 22, 2017 at 2:00 PM, Elvis Stansvik
<[hidden email]> wrote:
> Den 22 mars 2017 6:48 em skrev "David Gobbi" <[hidden email]>:
>>
>> It doesn't look hard.  New AddObserver methods would be needed that take
>> std::function<> as a parameter, and they'd have to store the "function"
>> object and call it.  Overall, it would probably be simpler than the code
>> that currently exists in vtkObject.h for adding observers.
>
> Good to hear, I might have a go at it then.
>
> Elvis
>
>>
>> On Wed, Mar 22, 2017 at 12:53 AM, Elvis Stansvik
>> <[hidden email]> wrote:
>>>
>>> Hi all,
>>>
>>> With C++11 now required, would there be interest in an AddObserver
>>> version that takes a C++11 lambda? I've found myself wishing there was
>>> one a couple of times.
>>>
>>> Anyone know off the top of their head how easy/hard that would be to add?
>>>
>>> Cheers,
>>> Elvis
>>> _______________________________________________
>>> Powered by www.kitware.com
>>>
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>>
>>> Search the list archives at: http://markmail.org/search/?q=vtk-developers
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://public.kitware.com/mailman/listinfo/vtk-developers
>>>
>>
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Search the list archives at: http://markmail.org/search/?q=vtk-developers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtk-developers
>
>


_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Search the list archives at: http://markmail.org/search/?q=vtk-developers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtk-developers





_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Search the list archives at: http://markmail.org/search/?q=vtk-developers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtk-developers

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AddObserver taking C++11 lambda?

David Gobbi
In reply to this post by Hahn, Steven E.
Hi Steve,

I don't think we have to worry about overhead in VTK's observers.  Our main concerns should be safety and usability.  The VTK observer implementation is heavy by design, it is meant to used for overall application control (e.g. for the "controller" part of the MVC pattern).  It is generally not meant to be used within the VTK computational algorithms themselves.

 - David


On Thu, Mar 23, 2017 at 9:14 AM, Hahn, Steven E. <[hidden email]> wrote:
Would it be more efficient to pass the lambda via a template parameter, like what is done in vtkClassMemberCallback? std::function<> adds a lot of overhead (see https://vittorioromeo.info/index/blog/passing_functions_to_functions.html ) and may be overkill.

Steven

On 3/23/17, 10:31 AM, "vtk-developers on behalf of Robert Maynard" <[hidden email] on behalf of [hidden email]> wrote:

    While allowing lambda's to be held by std::function is really nice, it
    becomes very easy to mistakenly captures items whose lifetime is
    shorter than the Observer you are binding too.

    I am currently writing up the first draft of c++11 guidelines for VTK
    and for lambda functions and these are the guidelines I currently
    have:

    Usage of lambda expressions are allowed with the following guidelines.

    - Use default capture by value ([=]) only as a means of binding a few
    variables for a short lambda, where the set of captured variables is
    obvious at a glance. Prefer not to write long or complex lambdas with
    default capture by value.
    - Except for the above, all capture arguments must be explicitly
    captured. Using the default capture by reference ([&]) is not allowed.
    This is to done so that it is easier to evaluate lifespan and
    reference ownership.
    - Keep unnamed lambdas short. If a lambda body is more than maybe five
    lines long, prefer using a named function instead of a lambda.
    - Specify the return type of the lambda explicitly if that will make
    it more obvious to readers.

    On-top of these guidelines I have also stated that lambda functions
    should be kept to implementation files in VTK, which is fine as we go
    with an approach like David proposed.

    On Wed, Mar 22, 2017 at 2:00 PM, Elvis Stansvik
    <[hidden email]> wrote:
    > Den 22 mars 2017 6:48 em skrev "David Gobbi" <[hidden email]>:
    >>
    >> It doesn't look hard.  New AddObserver methods would be needed that take
    >> std::function<> as a parameter, and they'd have to store the "function"
    >> object and call it.  Overall, it would probably be simpler than the code
    >> that currently exists in vtkObject.h for adding observers.
    >
    > Good to hear, I might have a go at it then.
    >
    > Elvis
    >
    >>
    >> On Wed, Mar 22, 2017 at 12:53 AM, Elvis Stansvik
    >> <[hidden email]> wrote:
    >>>
    >>> Hi all,
    >>>
    >>> With C++11 now required, would there be interest in an AddObserver
    >>> version that takes a C++11 lambda? I've found myself wishing there was
    >>> one a couple of times.
    >>>
    >>> Anyone know off the top of their head how easy/hard that would be to add?
    >>>
    >>> Cheers,
    >>> Elvis
    >>> _______________________________________________
    >>> Powered by www.kitware.com
    >>>
    >>> Visit other Kitware open-source projects at
    >>> http://www.kitware.com/opensource/opensource.html
    >>>
    >>> Search the list archives at: http://markmail.org/search/?q=vtk-developers
    >>>
    >>> Follow this link to subscribe/unsubscribe:
    >>> http://public.kitware.com/mailman/listinfo/vtk-developers
    >>>
    >>
    >
    >
    > _______________________________________________
    > Powered by www.kitware.com
    >
    > Visit other Kitware open-source projects at
    > http://www.kitware.com/opensource/opensource.html
    >
    > Search the list archives at: http://markmail.org/search/?q=vtk-developers
    >
    > Follow this link to subscribe/unsubscribe:
    > http://public.kitware.com/mailman/listinfo/vtk-developers
    >
    >
    _______________________________________________
    Powered by www.kitware.com

    Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

    Search the list archives at: http://markmail.org/search/?q=vtk-developers

    Follow this link to subscribe/unsubscribe:
    http://public.kitware.com/mailman/listinfo/vtk-developers






_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Search the list archives at: http://markmail.org/search/?q=vtk-developers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtk-developers

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AddObserver taking C++11 lambda?

Chris Harris
In reply to this post by David Gobbi
David,

Correct the "context" could have to be vtkObjectBase derived object, so would only help with vtkObjectBase derived objects used in the lamdba, but that would help in many cases I would think.

Chris

On Thu, Mar 23, 2017 at 12:54 PM, David Gobbi <[hidden email]> wrote:
Hi Chris,

If I'm reading the Qt docs correctly, then the "context" object that connect() uses is simply any Qt object.  If we add an AddObserver() that takes lambdas or function pointers, then we could similarly take a "context" object that would be any vtkObjectBase-derived object.  But as far as I understand, this wouldn't be generalizable to non-vtk objects.

 - David


On Thu, Mar 23, 2017 at 9:13 AM, Chris Harris <[hidden email]> wrote:
Qt provides help for this situation ( in the signal  world ) by allowing a "context object" to be provided on the connect, which causes the signal to be disconnected when the object is destroyed. Could something similar work here?

Chris

On Thu, Mar 23, 2017 at 10:55 AM, David Gobbi <[hidden email]> wrote:
Hi Robert,

Our AddObserver() methods only use weak pointers when the observer is derived from vtkObjectBase, which means that currently if the observer is not a VTK object, then we have a situation where we might call a dangling pointer.  I'm not aware of any uniform way around get around this issue except for using std::weak_ptr, which in turn demands that any non-vtk observer objects are wrapped with a std::shared_ptr.

When people start using lambdas with AddObserver, about the only thing we can do for safety is provide rules such as the ones that you describe to ensure that the lambda doesn't capture pointers that can be left dangling.

Thanks, by the way, for working on the c++11 guidelines.

 - David

On Thu, Mar 23, 2017 at 8:31 AM, Robert Maynard <[hidden email]> wrote:
While allowing lambda's to be held by std::function is really nice, it
becomes very easy to mistakenly captures items whose lifetime is
shorter than the Observer you are binding too.

I am currently writing up the first draft of c++11 guidelines for VTK
and for lambda functions and these are the guidelines I currently
have:

Usage of lambda expressions are allowed with the following guidelines.

- Use default capture by value ([=]) only as a means of binding a few
variables for a short lambda, where the set of captured variables is
obvious at a glance. Prefer not to write long or complex lambdas with
default capture by value.
- Except for the above, all capture arguments must be explicitly
captured. Using the default capture by reference ([&]) is not allowed.
This is to done so that it is easier to evaluate lifespan and
reference ownership.
- Keep unnamed lambdas short. If a lambda body is more than maybe five
lines long, prefer using a named function instead of a lambda.
- Specify the return type of the lambda explicitly if that will make
it more obvious to readers.

On-top of these guidelines I have also stated that lambda functions
should be kept to implementation files in VTK, which is fine as we go
with an approach like David proposed.

On Wed, Mar 22, 2017 at 2:00 PM, Elvis Stansvik
<[hidden email]> wrote:
> Den 22 mars 2017 6:48 em skrev "David Gobbi" <[hidden email]>:
>>
>> It doesn't look hard.  New AddObserver methods would be needed that take
>> std::function<> as a parameter, and they'd have to store the "function"
>> object and call it.  Overall, it would probably be simpler than the code
>> that currently exists in vtkObject.h for adding observers.
>
> Good to hear, I might have a go at it then.
>
> Elvis
>
>>
>> On Wed, Mar 22, 2017 at 12:53 AM, Elvis Stansvik
>> <[hidden email]> wrote:
>>>
>>> Hi all,
>>>
>>> With C++11 now required, would there be interest in an AddObserver
>>> version that takes a C++11 lambda? I've found myself wishing there was
>>> one a couple of times.
>>>
>>> Anyone know off the top of their head how easy/hard that would be to add?
>>>
>>> Cheers,
>>> Elvis
>>> _______________________________________________
>>> Powered by www.kitware.com
>>>
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>>
>>> Search the list archives at: http://markmail.org/search/?q=vtk-developers
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://public.kitware.com/mailman/listinfo/vtk-developers
>>>
>>
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Search the list archives at: http://markmail.org/search/?q=vtk-developers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtk-developers
>
>


_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Search the list archives at: http://markmail.org/search/?q=vtk-developers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtk-developers






_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Search the list archives at: http://markmail.org/search/?q=vtk-developers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtk-developers

Loading...