Created
April 22, 2016 09:17
-
-
Save anonymous/ffbbaa3dcb82d7e085b101213fbaae7a to your computer and use it in GitHub Desktop.
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
#include "Dispatcher.h" | |
#include <utility> | |
Dispatcher::Dispatcher() noexcept { } | |
Dispatcher::Dispatcher(std::initializer_list<container_type::value_type> initL) | |
: cont_{ std::move(initL) } { } | |
void Dispatcher::registerNotification(Notification notif, value_type const &callable) { | |
cont_.emplace(notif, callable); | |
} | |
void Dispatcher::registerNotification(Notification notif, value_type &&callable) { | |
cont_.emplace(notif, std::move(callable)); | |
} | |
void Dispatcher::unregisterNotification(Notification notif) { | |
auto it = cont_.find(notif); | |
if (it != std::end(cont_)) { | |
cont_.erase(it); | |
} | |
} | |
void Dispatcher::callHandler(Notification notif) { | |
auto it = cont_.find(notif); | |
if (it != std::end(cont_)) { | |
it->second(); | |
} | |
} |
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
#pragma once | |
#include <functional> | |
#include <map> | |
#include <initializer_list> | |
class Dispatcher final { | |
public: | |
using value_type = std::function<void (void)>; | |
enum class Notification { | |
one, | |
two, | |
three | |
}; | |
using container_type = std::map<Notification, value_type>; | |
Dispatcher() noexcept; | |
Dispatcher(std::initializer_list<container_type::value_type> initL); | |
void registerNotification(Notification notif, value_type const &callable); | |
void registerNotification(Notification notif, value_type &&callable); | |
void unregisterNotification(Notification); | |
void callHandler(Notification notif); | |
private: | |
container_type cont_; | |
}; |
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
int main() { | |
Dispatcher disp{ { Dispatcher::Notification::one, [] { std::cout << "one\n"; } }, | |
{ Dispatcher::Notification::two, [] { std::cout << "two\n"; } } | |
}; | |
disp.registerNotification(Dispatcher::Notification::three, [] { std::cout << "three\n"; }); | |
disp.callHandler(Dispatcher::Notification::one); | |
disp.callHandler(Dispatcher::Notification::two); | |
disp.callHandler(Dispatcher::Notification::three); | |
disp.unregisterNotification(Dispatcher::Notification::one); | |
disp.callHandler(Dispatcher::Notification::one); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment