Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification that signal was disconnected #10

Open
geiseri opened this issue Dec 14, 2019 · 3 comments
Open

Notification that signal was disconnected #10

geiseri opened this issue Dec 14, 2019 · 3 comments

Comments

@geiseri
Copy link

geiseri commented Dec 14, 2019

Greetings is there a way to have a notification when a signal is disconnected from a target? I have a situation that I would like to know when disconnect is called I can change a reference count to clean up a connection. Is there a sane way to add an extra callback to the signal class?

@vdksoft
Copy link
Owner

vdksoft commented Dec 16, 2019

Hi! It depends on what exactly you would like to achieve.
If you want to call some callback every time your code calls disconnect(), then it doesn’t make much sense, because when you explicitly call disconnect, you can directly call any other code without any callbacks.
If you want to call callbacks when receiving object gets destroyed, then it is very hard to achieve, because in this case disconnection happens implicitly and not immediately, the receiving object just stops being reachable. It is a kind of lazy disconnection. Moreover, in multi-threaded version it wouldn’t be obvious what thread to call that callback in.
I guess you need something more sophisticated than signals and slots, some bidirectional communication, but it is just a guess, since I don’t know your exact use-case.

@geiseri
Copy link
Author

geiseri commented Dec 17, 2019

Ah, right, if you have more than one connection on a signal it gets "funny." I have used this in the past in the Qt toolkit to deal with unloading plugins that are no longer needed. I am sure they do some wacky stuff with moc to do that though. My current situation is that I have a signal that is connected to a https://github.com/ReactiveX/RxCpp observable. It starts when I connect a signal to it, but I would like it to terminate when the signal goes away. I wonder if I make a smart pointer-ish wrapper around it so that it will notify the observer when it's destroyed.

@vdksoft
Copy link
Owner

vdksoft commented Dec 21, 2019

If I understand you correctly, you need to destroy (unload, terminate) the observable precisely when the signal gets destroyed. In this case you have to create some RAII wrapper around the signal, or if the signal is inside some object, you can use that object's destructor. From signals library point of view, affecting the receiving object in any way from inside the signal during disconnection process would probably be a design error, because it goes beyond simple signal-slot communication mechanism.
As for Qt, it has some features that I haven't implemented here for similar reasons, even though some of them could be implemented without moc support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants