-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
3,771 additions
and
2,658 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ enum LOG_TYPE | |
PIXELENGINE, | ||
PROCESSORINTERFACE, | ||
POWERPC, | ||
SCRIPTING, | ||
SERIALINTERFACE, | ||
SP1, | ||
SYMBOLS, | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
#include "Core/API/Events.h" | ||
|
||
namespace API | ||
{ | ||
|
||
template <typename T> | ||
static std::vector<Listener<T>> listeners; | ||
|
||
template <typename T> | ||
std::vector<Listener<T>>& GetListenersVector() | ||
{ | ||
return listeners<T>; | ||
} | ||
|
||
// Force template instantiations for all events. | ||
// If events get used which do not have a GetListenersVector | ||
// instantiation, spooky templating errors may appear. | ||
template std::vector<Listener<Events::Frameadvance>>& GetListenersVector(); | ||
template std::vector<Listener<Events::Memory>>& GetListenersVector(); | ||
template std::vector<Listener<Events::Interrupt>>& GetListenersVector(); | ||
|
||
} // namespace API |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
#include <vector> | ||
#include "Common/CommonTypes.h" | ||
|
||
namespace API | ||
{ | ||
namespace Events | ||
{ | ||
// Each event also needs a manual template instantiation in Events.cpp | ||
struct Frameadvance | ||
{ | ||
}; | ||
struct Memory | ||
{ | ||
bool write; | ||
u32 addr; | ||
u32 value; | ||
}; | ||
struct Interrupt | ||
{ | ||
u32 cause_mask; | ||
bool set; | ||
}; | ||
|
||
} // namespace API::Events | ||
|
||
// a listener on T is any function that takes a const reference to T as argument | ||
template <typename T> | ||
using Listener = std::function<void(const T&)>; | ||
template <typename T> | ||
using ListenerFuncPtr = void (*)(const T&); | ||
|
||
template <typename T> | ||
std::vector<Listener<T>>& GetListenersVector(); | ||
|
||
template <typename T> | ||
void EmitEvent(T evt) | ||
{ | ||
for (Listener<T>& listener : GetListenersVector<T>()) | ||
listener(evt); | ||
} | ||
|
||
template <typename T> | ||
void ListenEvent(Listener<T> listener) | ||
{ | ||
GetListenersVector<T>().push_back(std::move(listener)); | ||
} | ||
|
||
// convenience overload | ||
template <typename T> | ||
void ListenEvent(ListenerFuncPtr<T> listenerFuncPtr) | ||
{ | ||
ListenEvent(Listener<T>(listenerFuncPtr)); | ||
} | ||
|
||
template <typename T> | ||
bool UnlistenEvent(Listener<T> listener) | ||
{ | ||
auto ls = GetListenersVector<T>(); | ||
auto it = ls.begin(); | ||
while (it != ls.end()) | ||
{ | ||
Listener<T> l = *it; | ||
if (l.target_type() == listener.target_type()) | ||
{ | ||
// TODO how to check std::function for equality? Maybe do something completely different instead... | ||
//ls.erase(l); | ||
return true; | ||
} | ||
++it; | ||
} | ||
|
||
/*auto existing = std::find(ls.begin(), ls.end(), listener); | ||
if (existing != ls.end()) | ||
{ | ||
ls.erase(existing); | ||
return true; | ||
}*/ | ||
return false; | ||
} | ||
|
||
// convenience overload | ||
template <typename T> | ||
bool UnlistenEvent(ListenerFuncPtr<T> listenerFuncPtr) | ||
{ | ||
return UnlistenEvent(Listener<T>(listenerFuncPtr)); | ||
} | ||
|
||
} // namespace API |
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
Oops, something went wrong.