Created
February 18, 2023 20:27
-
-
Save nboutin/7775e59e8f774336bf8aca5c3e545743 to your computer and use it in GitHub Desktop.
RAII subscribe unscribe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.youtube.com/watch?v=nLSm3Haxz0I | |
class EventSource | |
{ | |
void add(Listener& listener); | |
void remove(Listener& listener); | |
public: | |
[[nodiscard]] | |
Subscription subscribe(Listener& listener) { | |
return Subscription(*this, listener); } | |
}; | |
class EventSource::Subscription | |
{ | |
Subscription(EventSource& source, Listener& listener) | |
: source_(source), listener_(listener) | |
{ source_.add_listener(listener_); } | |
~Subscription() | |
{ source_.remove(listener_);} | |
// special function members | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment