Last active
August 13, 2023 09:05
-
-
Save rioki/1290004d7505380f2b1d to your computer and use it in GitHub Desktop.
node.js' style EventEmitter for C++ (http://www.rioki.org/2014/12/29/eventemitter-in-c.html)
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
// | |
// Copyright (c) 2014 Sean Farrell | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
#include "EventEmitter.h" | |
#include <stdexcept> | |
EventEmitter::EventEmitter() {} | |
EventEmitter::~EventEmitter() {} | |
unsigned int EventEmitter::add_listener(unsigned int event_id, std::function<void ()> cb) | |
{ | |
if (!cb) | |
{ | |
throw std::invalid_argument("EventEmitter::add_listener: No callbak provided."); | |
} | |
std::lock_guard<std::mutex> lock(mutex); | |
unsigned int listener_id = ++last_listener; | |
listeners.insert(std::make_pair(event_id, std::make_shared<Listener<>>(listener_id, cb))); | |
return listener_id; | |
} | |
unsigned int EventEmitter::on(unsigned int event_id, std::function<void ()> cb) | |
{ | |
return add_listener(event_id, cb); | |
} | |
void EventEmitter::remove_listener(unsigned int listener_id) | |
{ | |
std::lock_guard<std::mutex> lock(mutex); | |
auto i = std::find_if(listeners.begin(), listeners.end(), [&] (std::pair<const unsigned int, std::shared_ptr<ListenerBase>> p) { | |
return p.second->id == listener_id; | |
}); | |
if (i != listeners.end()) | |
{ | |
listeners.erase(i); | |
} | |
else | |
{ | |
throw std::invalid_argument("EventEmitter::remove_listener: Invalid listener id."); | |
} | |
} |
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
// | |
// Copyright (c) 2014 Sean Farrell | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
#ifndef _EVENT_EMITTER_H_ | |
#define _EVENT_EMITTER_H_ | |
#include <functional> | |
#include <map> | |
#include <memory> | |
#include <mutex> | |
#include <list> | |
#include <algorithm> | |
class EventEmitter | |
{ | |
public: | |
EventEmitter(); | |
~EventEmitter(); | |
template <typename... Args> | |
unsigned int add_listener(unsigned int event_id, std::function<void (Args...)> cb); | |
unsigned int add_listener(unsigned int event_id, std::function<void ()> cb); | |
template <typename... Args> | |
unsigned int on(unsigned int event_id, std::function<void (Args...)> cb); | |
unsigned int on(unsigned int event_id, std::function<void ()> cb); | |
void remove_listener(unsigned int listener_id); | |
template <typename... Args> | |
void emit(unsigned int event_id, Args... args); | |
private: | |
struct ListenerBase | |
{ | |
ListenerBase() {} | |
ListenerBase(unsigned int i) | |
: id(i) {} | |
virtual ~ListenerBase() {} | |
unsigned int id; | |
}; | |
template <typename... Args> | |
struct Listener : public ListenerBase | |
{ | |
Listener() {} | |
Listener(unsigned int i, std::function<void (Args...)> c) | |
: ListenerBase(i), cb(c) {} | |
std::function<void (Args...)> cb; | |
}; | |
std::mutex mutex; | |
unsigned int last_listener; | |
std::multimap<unsigned int, std::shared_ptr<ListenerBase>> listeners; | |
EventEmitter(const EventEmitter&) = delete; | |
const EventEmitter& operator = (const EventEmitter&) = delete; | |
}; | |
template <typename... Args> | |
unsigned int EventEmitter::add_listener(unsigned int event_id, std::function<void (Args...)> cb) | |
{ | |
if (!cb) | |
{ | |
throw std::invalid_argument("EventEmitter::add_listener: No callbak provided."); | |
} | |
std::lock_guard<std::mutex> lock(mutex); | |
unsigned int listener_id = ++last_listener; | |
listeners.insert(std::make_pair(event_id, std::make_shared<Listener<Args...>>(listener_id, cb))); | |
return listener_id; | |
} | |
template <typename... Args> | |
unsigned int EventEmitter::on(unsigned int event_id, std::function<void (Args...)> cb) | |
{ | |
return add_listener(event_id, cb); | |
} | |
template <typename... Args> | |
void EventEmitter::emit(unsigned int event_id, Args... args) | |
{ | |
std::list<std::shared_ptr<Listener<Args...>>> handlers; | |
{ | |
std::lock_guard<std::mutex> lock(mutex); | |
auto range = listeners.equal_range(event_id); | |
handlers.resize(std::distance(range.first, range.second)); | |
std::transform(range.first, range.second, handlers.begin(), [] (std::pair<const unsigned int, std::shared_ptr<ListenerBase>> p) { | |
auto l = std::dynamic_pointer_cast<Listener<Args...>>(p.second); | |
if (l) | |
{ | |
return l; | |
} | |
else | |
{ | |
throw std::logic_error("EventEmitter::emit: Invalid event signature."); | |
} | |
}); | |
} | |
for (auto& h : handlers) | |
{ | |
h->cb(args...); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mmm, that compiles for you? When I try that it says cannot convert from lambda to std::function.