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

globalListen() + EventListener operator= #1218

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions loader/include/Geode/loader/Event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
#include "../utils/casts.hpp"

#include <Geode/DefaultInclude.hpp>
#include <Geode/loader/Log.hpp>
#include <functional>
#include <memory>
#include <type_traits>
#include <mutex>
#include <deque>
#include <unordered_set>
#include <atomic>
#include <vector>

namespace geode {
class Mod;
Expand Down Expand Up @@ -190,6 +193,32 @@ namespace geode {
this->enable();
}

EventListener& operator=(EventListener const& other) {
if (this == &other) {
return *this;
}

m_callback = other.m_callback;
m_filter = other.m_filter;
m_filter.setListener(this);

return *this;
}

EventListener& operator=(EventListener&& other) {
if (this == &other) {
return *this;
}

m_callback = std::move(other.m_callback);
m_filter = std::move(other.m_filter);

m_filter.setListener(this);
other.disable();

return *this;
}

void bind(std::function<Callback> fn) {
m_callback = fn;
}
Expand Down Expand Up @@ -239,4 +268,10 @@ namespace geode {

virtual ~Event();
};
}

// Creates an EventListener that is active for the entire lifetime of the game. You have no way of disabling the listener, so only use this if you want to always listen for certain events!
template <is_filter T>
void globalListen(typename T::Callback callback, T filter = T()) {
new EventListener<T>(callback, filter);
}
}
Loading