Initial Commit
This commit is contained in:
31
CMakeLists.txt
Normal file
31
CMakeLists.txt
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.27)
|
||||||
|
project(Event
|
||||||
|
VERSION 1.0
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
|
if (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
|
||||||
|
message(FATAL_ERROR "In-Source builds are not allowed")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
|
||||||
|
if (WIN32)
|
||||||
|
set(CMAKE_CXX_FLAGS "-municode")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
file(GLOB_RECURSE SOURCES "src/*.cpp")
|
||||||
|
|
||||||
|
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
||||||
|
|
||||||
|
include_directories("include")
|
||||||
|
|
||||||
|
add_library(Event SHARED ${SOURCES}
|
||||||
|
src/Event.cpp)
|
||||||
|
|
||||||
|
set_target_properties(Event PROPERTIES LINKER_LANGUAGE CXX)
|
||||||
|
|
||||||
|
add_executable(EventDemo main.cpp)
|
||||||
|
|
||||||
|
target_link_libraries(EventDemo PUBLIC Event)
|
||||||
|
|
81
include/Event.h
Normal file
81
include/Event.h
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
// TODO: Document & Explain this
|
||||||
|
|
||||||
|
template <typename ... Args>
|
||||||
|
class Event;
|
||||||
|
|
||||||
|
template <typename ... Args>
|
||||||
|
class EventConnection {
|
||||||
|
private:
|
||||||
|
using delegate = std::function<void(Args...)>;
|
||||||
|
public:
|
||||||
|
EventConnection(Event<Args...> *creator, delegate cb) : owner(creator), callback(std::move(cb)) {}
|
||||||
|
bool Disconnect(); // Breaks the event connection, but does not destroy the instance
|
||||||
|
void Invoke(Args... e);
|
||||||
|
private:
|
||||||
|
Event<Args...> * owner;
|
||||||
|
delegate callback;
|
||||||
|
bool active = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename ... Args>
|
||||||
|
class Event {
|
||||||
|
public:
|
||||||
|
using delegate = std::function<void(Args...)>;
|
||||||
|
using connection = EventConnection<Args ...>;
|
||||||
|
using event_ptr = std::shared_ptr<connection>;
|
||||||
|
public:
|
||||||
|
void Await(Args& ... arg);
|
||||||
|
void Invoke(Args... args);
|
||||||
|
void operator()(Args... args);
|
||||||
|
connection Connect(delegate callback);
|
||||||
|
void Disconnect(connection &conn);
|
||||||
|
connection operator+=(delegate callback);
|
||||||
|
private:
|
||||||
|
std::vector<event_ptr> listeners;
|
||||||
|
uint64_t listenerCounter = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
EventConnection<Args...> Event<Args...>::operator+=(Event::delegate callback) { return Connect(callback); }
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void Event<Args...>::operator()(Args... args) { Invoke(args...);}
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
void EventConnection<Args...>::Invoke(Args... e) { callback(e...); }
|
||||||
|
|
||||||
|
template <typename ... Args>
|
||||||
|
bool EventConnection<Args...>::Disconnect() {
|
||||||
|
if (active) {
|
||||||
|
owner->Disconnect(this);
|
||||||
|
active = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename ... Args>
|
||||||
|
void Event<Args...>::Invoke(Args... args) {
|
||||||
|
for (event_ptr &connection_ptr: this->listeners) {
|
||||||
|
connection_ptr->Invoke(args...);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename ... Args>
|
||||||
|
EventConnection<Args...> Event<Args...>::Connect(delegate callback)
|
||||||
|
{
|
||||||
|
event_ptr retval(new connection(this, callback));
|
||||||
|
this->listeners.push_back(retval);
|
||||||
|
return *retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename ... Args>
|
||||||
|
void Event<Args...>::Disconnect(connection &conn) {
|
||||||
|
listeners.erase(std::remove(listeners.begin(), listeners.end(), 99), listeners.end());
|
||||||
|
}
|
30
main.cpp
Normal file
30
main.cpp
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <Event.h>
|
||||||
|
|
||||||
|
void ProcessMessage(const std::string& message)
|
||||||
|
{
|
||||||
|
std::cout << "Received: " << message << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
Event<std::string> OnMessage;
|
||||||
|
|
||||||
|
bool run = true;
|
||||||
|
|
||||||
|
auto handler = OnMessage += [&] (const std::string& message)
|
||||||
|
{
|
||||||
|
std::cout << "GOTS A MESSAGE: " << message << std::endl;
|
||||||
|
};
|
||||||
|
auto handler2 = OnMessage += ProcessMessage;
|
||||||
|
|
||||||
|
while (run)
|
||||||
|
{
|
||||||
|
std::string input;
|
||||||
|
std::cin >> input;
|
||||||
|
OnMessage.Invoke(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
1
src/Event.cpp
Normal file
1
src/Event.cpp
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#include <Event.h>
|
Reference in New Issue
Block a user