Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
f8310f7bed | |||
1290afdc68 | |||
5a1d864e15 | |||
e01acbed26 | |||
27af86ebf2 | |||
|
7620b9f06f | ||
d9e0931a4d |
@@ -1,4 +1,4 @@
|
|||||||
cmake_minimum_required(VERSION 3.18)
|
cmake_minimum_required(VERSION 3.27)
|
||||||
project(Event
|
project(Event
|
||||||
VERSION 1.0
|
VERSION 1.0
|
||||||
LANGUAGES CXX
|
LANGUAGES CXX
|
||||||
@@ -10,20 +10,19 @@ endif()
|
|||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
|
||||||
if (WIN32)
|
|
||||||
set(CMAKE_CXX_FLAGS "-municode")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
file(GLOB_RECURSE SOURCES "src/*.cpp")
|
file(GLOB_RECURSE SOURCES "src/*.cpp")
|
||||||
|
|
||||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
||||||
|
|
||||||
include_directories("include")
|
include_directories("include")
|
||||||
|
|
||||||
add_library(Event SHARED ${SOURCES}
|
if (UNIX)
|
||||||
src/Event.cpp
|
add_library(Event SHARED ${SOURCES})
|
||||||
include/EventConnection.h
|
endif()
|
||||||
src/EventConnection.cpp)
|
|
||||||
|
if (WIN32)
|
||||||
|
add_library(Event STATIC ${SOURCES})
|
||||||
|
endif()
|
||||||
|
|
||||||
set_target_properties(Event PROPERTIES LINKER_LANGUAGE CXX)
|
set_target_properties(Event PROPERTIES LINKER_LANGUAGE CXX)
|
||||||
|
|
||||||
|
40
include/Base.h
Normal file
40
include/Base.h
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
//template< class RT, class CallableT, class... ArgTypes >
|
||||||
|
template<class CallableT, class... ArgTypes >
|
||||||
|
class Callable {
|
||||||
|
static_assert(std::is_invocable_v<CallableT(ArgTypes...)>);
|
||||||
|
using invoke = CallableT;
|
||||||
|
public:
|
||||||
|
invoke;
|
||||||
|
//CallableT invoke = CallableT();
|
||||||
|
//ArgTypes... args
|
||||||
|
//invoke() { /*return CallableT(args...);*/ return CallableT(RT()); }
|
||||||
|
};
|
||||||
|
|
||||||
|
template< class R, class Fn, class... ArgTypes >
|
||||||
|
class Cunt {
|
||||||
|
static_assert(std::is_invocable_r_v<R, Fn, ArgTypes...>);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template< class RT, class... ArgTypes>
|
||||||
|
class EventBase {
|
||||||
|
public:
|
||||||
|
//using delegate = CallableT;
|
||||||
|
using delegate = std::function<RT(ArgTypes...)>;
|
||||||
|
public:
|
||||||
|
RT Invoke(ArgTypes... args);
|
||||||
|
RT operator()(ArgTypes... args);
|
||||||
|
protected:
|
||||||
|
delegate callback;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
@@ -1,62 +1,71 @@
|
|||||||
/// @file Event.hpp
|
/// @file BasicEvent.hpp
|
||||||
/// @description Templated Event Hook Class modeled after C# events
|
/// @description Templated BasicEvent Hook Class modeled after C# events
|
||||||
/// @author Josh O'Leary - Redacted Software
|
/// @author Josh O'Leary - Redacted Software
|
||||||
/// @revision 3
|
/// @revision 3
|
||||||
/// @lastedit 2024-02-21
|
/// @lastedit 2024-06-01
|
||||||
/// @license Unlicense - Public Domain
|
/// @license Unlicense - Public Domain
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
// TODO: Document & Explain this
|
// TODO: ThreadSafeEvent / AsyncEvent version of the class.
|
||||||
|
// TODO: ConsumableEvent - a version that can be "consumed" by a callback by returning true. It will be sunk and not passed to further callbacks.
|
||||||
|
|
||||||
#include <EventConnection.h>
|
///
|
||||||
|
template <typename delegate, typename ... Args>
|
||||||
|
class Connection;
|
||||||
|
|
||||||
template <typename ... Args>
|
/// BasicEvent is a templated class that can be used to raise events that can be listened in on
|
||||||
class EventConnection;
|
/// at other points in a codebase without having to tightly couple the two pieces of code.
|
||||||
|
/// @param delegate A type that can be invoked via operator() 'Call operator'.
|
||||||
template <typename ... Args>
|
/// @param Args... The arguments that will be expected by this event and passed to it's delegate.
|
||||||
class Event {
|
template <typename delegate, typename ... Args>
|
||||||
|
class BasicEvent {
|
||||||
public:
|
public:
|
||||||
using delegate = std::function<void(Args...)>;
|
friend Connection<delegate, Args ...>;
|
||||||
using connection = EventConnection<Args ...>;
|
using connection = Connection<delegate, Args ...>;
|
||||||
using event_ptr = std::shared_ptr<connection>;
|
using event_ptr = std::shared_ptr<connection>;
|
||||||
public:
|
public:
|
||||||
void Await(Args& ... arg);
|
void Await(Args& ... arg);
|
||||||
void Invoke(Args... args);
|
virtual void Invoke(Args... args);
|
||||||
void operator()(Args... args);
|
void operator()(Args... args);
|
||||||
connection Connect(delegate callback);
|
connection Connect(delegate callback);
|
||||||
void Disconnect(connection &conn);
|
void Disconnect(connection &conn);
|
||||||
connection operator+=(delegate callback);
|
connection operator+=(delegate callback);
|
||||||
private:
|
protected:
|
||||||
std::vector<event_ptr> listeners;
|
std::vector<event_ptr> listeners;
|
||||||
uint64_t listenerCounter = 0;
|
//uint64_t listenerCounter = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename delegate, typename... Args>
|
||||||
EventConnection<Args...> Event<Args...>::operator+=(Event::delegate callback) { return Connect(callback); }
|
Connection<delegate, Args...> BasicEvent<delegate, Args...>::operator+=(delegate callback) { return Connect(callback); }
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename delegate, typename... Args>
|
||||||
void Event<Args...>::operator()(Args... args) { Invoke(args...);}
|
void BasicEvent<delegate, Args...>::operator()(Args... args) { Invoke(args...);}
|
||||||
|
|
||||||
template <typename ... Args>
|
template <typename delegate, typename ... Args>
|
||||||
void Event<Args...>::Invoke(Args... args) {
|
void BasicEvent<delegate, Args...>::Invoke(Args... args) {
|
||||||
for (event_ptr &connection_ptr: this->listeners) {
|
for (event_ptr &connection_ptr: this->listeners) {
|
||||||
connection_ptr->Invoke(args...);
|
connection_ptr->Invoke(args...);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename ... Args>
|
template <typename delegate, typename ... Args>
|
||||||
EventConnection<Args...> Event<Args...>::Connect(delegate callback)
|
Connection<delegate, Args...> BasicEvent<delegate, Args...>::Connect(delegate callback)
|
||||||
{
|
{
|
||||||
event_ptr retval(new connection(this, callback));
|
event_ptr retval(new connection(this, callback));
|
||||||
this->listeners.push_back(retval);
|
this->listeners.push_back(retval);
|
||||||
return *retval;
|
return *retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename ... Args>
|
template <typename delegate, typename ... Args>
|
||||||
void Event<Args...>::Disconnect(connection &conn) {
|
void BasicEvent<delegate, Args...>::Disconnect(connection &conn) {
|
||||||
listeners.erase(std::remove(listeners.begin(), listeners.end(), 99), listeners.end());
|
listeners.erase(std::remove(listeners.begin(), listeners.end(), 99), listeners.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename ... Args>
|
||||||
|
using Event = BasicEvent<std::function<void(Args...)>, Args...>;
|
||||||
|
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
/// @file EventConnection.hpp
|
/// @file Connection.hpp
|
||||||
/// @description Callback handler for event connections
|
/// @description Callback handler for event connections
|
||||||
/// @author Josh O'Leary - Redacted Software
|
/// @author Josh O'Leary - Redacted Software
|
||||||
/// @revision 3
|
/// @revision 3
|
||||||
@@ -10,32 +10,35 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
#include <Event.h>
|
#include <Event.h>
|
||||||
|
|
||||||
template <typename ... Args>
|
template <typename delegate, typename ... Args>
|
||||||
class Event;
|
class BasicEvent;
|
||||||
|
|
||||||
template <typename ... Args>
|
/// A type that represents a handle to an active event connection.
|
||||||
class EventConnection {
|
template <typename delegate, typename ... Args>
|
||||||
private:
|
class Connection {
|
||||||
using delegate = std::function<void(Args...)>;
|
|
||||||
public:
|
public:
|
||||||
EventConnection(Event<Args...> *creator, delegate cb);
|
friend BasicEvent<delegate, Args...>;
|
||||||
|
public:
|
||||||
|
Connection(BasicEvent<delegate, Args...> *creator, delegate cb);
|
||||||
bool Disconnect(); // Breaks the event connection, but does not destroy the instance
|
bool Disconnect(); // Breaks the event connection, but does not destroy the instance
|
||||||
void Invoke(Args... e);
|
void Invoke(Args... e);
|
||||||
private:
|
protected:
|
||||||
Event<Args...> * owner;
|
|
||||||
delegate callback;
|
delegate callback;
|
||||||
|
private:
|
||||||
|
BasicEvent<delegate, Args...> * owner;
|
||||||
|
//delegate callback;
|
||||||
bool active = true;
|
bool active = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename delegate, typename... Args>
|
||||||
EventConnection<Args...>::EventConnection(Event<Args...> *creator, EventConnection::delegate cb) : owner(creator), callback(std::move(cb)) {}
|
Connection<delegate, Args...>::Connection(BasicEvent<delegate, Args...> *creator, delegate cb) : owner(creator), callback(std::move(cb)) {}
|
||||||
|
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename delegate, typename... Args>
|
||||||
void EventConnection<Args...>::Invoke(Args... e) { callback(e...); }
|
void Connection<delegate, Args...>::Invoke(Args... e) { callback(e...); }
|
||||||
|
|
||||||
template <typename ... Args>
|
template <typename delegate, typename ... Args>
|
||||||
bool EventConnection<Args...>::Disconnect() {
|
bool Connection<delegate, Args...>::Disconnect() {
|
||||||
if (active) {
|
if (active) {
|
||||||
owner->Disconnect(this);
|
owner->Disconnect(this);
|
||||||
active = false;
|
active = false;
|
||||||
|
20
main.cpp
20
main.cpp
@@ -1,15 +1,25 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <functional>
|
||||||
#include <Event.h>
|
#include <Event.h>
|
||||||
|
#include <EventConnection.h>
|
||||||
|
|
||||||
void ProcessMessage(const std::string& message)
|
void ProcessMessage(const std::string& message)
|
||||||
{
|
{
|
||||||
std::cout << "Received: " << message << std::endl;
|
std::cout << "Received: " << message << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Event tests here.
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
Event<std::string> OnMessage;
|
Event<int> cum;
|
||||||
|
|
||||||
|
cum += [] (int i) { std::cout << i+5 << std::endl; };
|
||||||
|
|
||||||
|
cum.Invoke(69);
|
||||||
|
|
||||||
|
BasicEvent<std::function<void(std::string)>, std::string> OnMessage;
|
||||||
|
BasicEvent<std::function<void()>> VoidDelegate;
|
||||||
|
|
||||||
bool run = true;
|
bool run = true;
|
||||||
|
|
||||||
@@ -24,15 +34,19 @@ int main() {
|
|||||||
std::string input;
|
std::string input;
|
||||||
std::cin >> input;
|
std::cin >> input;
|
||||||
OnMessage.Invoke(input);
|
OnMessage.Invoke(input);
|
||||||
|
VoidDelegate.Invoke();
|
||||||
|
OnMessage(input);
|
||||||
|
VoidDelegate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
extern "C" {
|
extern "C" {
|
||||||
int wmain(int argc, wchar_t* argv[]) {
|
int wmain(int argc, wchar_t* argv[]) {
|
||||||
return main(0, nullptr);
|
return main();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@@ -1 +1,3 @@
|
|||||||
#include <Event.h>
|
#include <Event.h>
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user