Compare commits
19 Commits
Release-3
...
Release-12
Author | SHA1 | Date | |
---|---|---|---|
fd15df57ed | |||
741b8b58dd | |||
9cc1f3a5ed | |||
642bdde567 | |||
140b585854 | |||
217fe1df88 | |||
00b3857647 | |||
d85173ccd9 | |||
5e3e20a7e5 | |||
69e245632f | |||
e7f3f1ff95 | |||
d0222a0dce | |||
f8310f7bed | |||
1290afdc68 | |||
5a1d864e15 | |||
e01acbed26 | |||
27af86ebf2 | |||
|
7620b9f06f | ||
d9e0931a4d |
@@ -1,4 +1,4 @@
|
|||||||
cmake_minimum_required(VERSION 3.18)
|
cmake_minimum_required(VERSION 3.18..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)
|
||||||
|
|
||||||
|
98
README.md
98
README.md
@@ -0,0 +1,98 @@
|
|||||||
|
# Event
|
||||||
|
### A C++20 Event system inspired by C# syntax.
|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|
[](http://unlicense.org/)
|
||||||
|
|
||||||
|
## Features
|
||||||
|
* Fast
|
||||||
|
* (TODO: Profile!)
|
||||||
|
* Flexible
|
||||||
|
* Event delegate can be of any  type.
|
||||||
|
* Supports any set of parameters via templates.
|
||||||
|
* Easy To Use
|
||||||
|
* Written in portable and standard C++, no hacks or quirks.
|
||||||
|
* Utilizes Modern C++ features for elegant design.
|
||||||
|
* Zero dependencies. Fully self-contained.
|
||||||
|
* Unrestricted
|
||||||
|
* No GNU Copyleft communism. Totally Free & Public Domain.
|
||||||
|
|
||||||
|
## Coming Soon
|
||||||
|
|
||||||
|
* Event Filters / Policy
|
||||||
|
* Asynchronous
|
||||||
|
* Thread-Safe Version
|
||||||
|
* Priority Queue
|
||||||
|
* Auto disconnecting
|
||||||
|
|
||||||
|
## Supported Compilers
|
||||||
|
|
||||||
|
* GCC 14.2.1 (RedHat)
|
||||||
|
* MSVC 19.40
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
This project is designed to be integrated via [CMake Package Manager](https://github.com/cpm-cmake/CPM.cmake).
|
||||||
|
|
||||||
|
```cmake
|
||||||
|
CPMAddPackage(
|
||||||
|
NAME Event
|
||||||
|
URL https://git.redacted.cc/josh/Event/archive/Release-7.zip
|
||||||
|
)
|
||||||
|
```
|
||||||
|
This snippet will automatically download a stable release of Event into your project's build directory, so it won't clutter your sources.
|
||||||
|
|
||||||
|
Take note of the release version and make sure it's the latest available in the Releases tab above.
|
||||||
|
|
||||||
|
```cmake
|
||||||
|
target_include_directories(${PROJECT_NAME} PUBLIC ${Event_SOURCE_DIR}/include)
|
||||||
|
target_link_libraries(${PROJECT_NAME} PUBLIC Event)
|
||||||
|
```
|
||||||
|
|
||||||
|
This snippet will import the headers, and then link the library to your library/executable. You should now be able to use Event.
|
||||||
|
|
||||||
|
The C++ snippet below reads input from the terminal and fires an event that received the input message.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <Event.h>
|
||||||
|
#include <EventConnection.h>
|
||||||
|
|
||||||
|
void ReadMessage(const std::string& message) {
|
||||||
|
std::cout << "Received: " << message << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Event<std::string> OnInput; // This and the declaration below are equivalent.
|
||||||
|
BasicEvent<std::function<void(std::string)>, std::string> OnInput2;
|
||||||
|
bool running = true;
|
||||||
|
while (running) {
|
||||||
|
std::string input;
|
||||||
|
std::cin >> input;
|
||||||
|
OnInput.Invoke(input);
|
||||||
|
OnInput(input); // Call operator is defined as well.
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
More advanced examples can be found in the main.cpp file.
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
Documentation is automatically generated from latest commit and is hosted at https://doc.redacted.cc/event .
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Contributions to this project are welcome! If you find a bug, have a feature request, or would like to contribute code, please submit an issue or pull request to the GitHub repository.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Event is expressly licensed to the Public Domain under the Unlicense. See the LICENSE file for details.
|
||||||
|
|
||||||
|
## Acknowledgements
|
||||||
|
|
||||||
|
Event is developed and maintained by Joshua O'Leary @ Redacted Software, and contributors.
|
||||||
|
Special thanks to Maxine Hayes.
|
121
include/Event.h
121
include/Event.h
@@ -1,5 +1,5 @@
|
|||||||
/// @file Event.hpp
|
/// @file Connection.hpp
|
||||||
/// @description Templated Event Hook Class modeled after C# events
|
/// @description Callback handler for event connections
|
||||||
/// @author Josh O'Leary - Redacted Software
|
/// @author Josh O'Leary - Redacted Software
|
||||||
/// @revision 3
|
/// @revision 3
|
||||||
/// @lastedit 2024-02-21
|
/// @lastedit 2024-02-21
|
||||||
@@ -9,54 +9,103 @@
|
|||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
// TODO: Document & Explain this
|
/// A delegate can be any function object or function-like object. The Args are the arguments accepted by that object
|
||||||
|
template <typename delegate, typename ... Args>
|
||||||
#include <EventConnection.h>
|
class BasicEvent {
|
||||||
|
|
||||||
template <typename ... Args>
|
|
||||||
class EventConnection;
|
|
||||||
|
|
||||||
template <typename ... Args>
|
|
||||||
class Event {
|
|
||||||
public:
|
public:
|
||||||
using delegate = std::function<void(Args...)>;
|
/// A type that represents a handle to an active event connection.
|
||||||
using connection = EventConnection<Args ...>;
|
class Connection {
|
||||||
using event_ptr = std::shared_ptr<connection>;
|
public:
|
||||||
|
Connection(BasicEvent<delegate, Args...> *creator, delegate cb);
|
||||||
|
bool Disconnect(); // Breaks the event connection, but does not destroy the instance
|
||||||
|
// If improperly used could lead to run away events? - Maxine
|
||||||
|
void Invoke(Args... e);
|
||||||
|
public:
|
||||||
|
// This is made public in the case of function-like objects which store extra data.
|
||||||
|
// This allows us to access members of the object when desired. See JTest's usage of the event system for an example.
|
||||||
|
delegate callback;
|
||||||
|
private:
|
||||||
|
BasicEvent<delegate, Args...> *owner;
|
||||||
|
bool active = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
using EventPtr = std::shared_ptr<Connection>;
|
||||||
public:
|
public:
|
||||||
void Await(Args& ... arg);
|
// See JTest Unit for why making this virtual is convenient
|
||||||
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:
|
|
||||||
std::vector<event_ptr> listeners;
|
/// Also support binding callbacks that don't read in arguments, as a shorthand.
|
||||||
uint64_t listenerCounter = 0;
|
// TODO: No need to wrap the callback if the template args type is void.
|
||||||
|
Connection Connect(const std::function<void()>& default_callback);
|
||||||
|
/// Also support binding callbacks that don't read in arguments, as a shorthand.
|
||||||
|
// TODO: No need to wrap the callback if the template args type is void.
|
||||||
|
Connection operator += (const std::function<void()>& default_callback);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::vector<EventPtr> listeners;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename delegate, typename... Args>
|
||||||
EventConnection<Args...> Event<Args...>::operator+=(Event::delegate callback) { return Connect(callback); }
|
BasicEvent<delegate, Args...>::Connection::Connection(BasicEvent<delegate, Args...> *creator, delegate cb) : owner(creator), callback(std::move(cb)) {}
|
||||||
|
|
||||||
template<typename... Args>
|
template <typename delegate, typename... Args>
|
||||||
void Event<Args...>::operator()(Args... args) { Invoke(args...);}
|
void BasicEvent<delegate, Args...>::Connection::Invoke(Args... e) { callback(e...); }
|
||||||
|
|
||||||
template <typename ... Args>
|
template <typename delegate, typename ... Args>
|
||||||
void Event<Args...>::Invoke(Args... args) {
|
bool BasicEvent<delegate, Args...>::Connection::Disconnect() {
|
||||||
for (event_ptr &connection_ptr: this->listeners) {
|
if (active) {
|
||||||
|
owner->Disconnect(this);
|
||||||
|
active = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename delegate, typename... Args>
|
||||||
|
BasicEvent<delegate, Args...>::Connection BasicEvent<delegate, Args...>::operator+=(delegate callback) { return Connect(callback); }
|
||||||
|
|
||||||
|
template<typename delegate, typename ... Args>
|
||||||
|
typename BasicEvent<delegate, Args...>::Connection BasicEvent<delegate, Args...>::Connect(
|
||||||
|
const std::function<void()> &default_callback) {
|
||||||
|
delegate wrapper = [default_callback] (Args... args) { default_callback(); };
|
||||||
|
return Connect(wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename delegate, typename ... Args>
|
||||||
|
typename BasicEvent<delegate, Args...>::Connection BasicEvent<delegate, Args...>::operator+=(
|
||||||
|
const std::function<void()> &default_callback) {
|
||||||
|
return Connect(default_callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename delegate, typename... Args>
|
||||||
|
void BasicEvent<delegate, Args...>::operator()(Args... args) { Invoke(args...); }
|
||||||
|
|
||||||
|
template <typename delegate, typename ... Args>
|
||||||
|
void BasicEvent<delegate, Args...>::Invoke(Args... args) {
|
||||||
|
for (EventPtr &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)
|
BasicEvent<delegate, Args...>::Connection BasicEvent<delegate, Args...>::Connect(delegate callback) {
|
||||||
{
|
EventPtr 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(BasicEvent<delegate, Args...>::Connection &conn) {
|
||||||
listeners.erase(std::remove(listeners.begin(), listeners.end(), 99), listeners.end());
|
listeners.erase(std::remove(listeners.begin(), listeners.end(), 99), listeners.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Event is a generic event type. It is more often we will be using a void function rather than a function-like object.
|
||||||
|
template <typename ... Args>
|
||||||
|
using Event = BasicEvent<std::function<void(Args...)>, Args...>;
|
||||||
|
|
||||||
|
@@ -1,45 +0,0 @@
|
|||||||
/// @file EventConnection.hpp
|
|
||||||
/// @description Callback handler for event connections
|
|
||||||
/// @author Josh O'Leary - Redacted Software
|
|
||||||
/// @revision 3
|
|
||||||
/// @lastedit 2024-02-21
|
|
||||||
/// @license Unlicense - Public Domain
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
#include <Event.h>
|
|
||||||
|
|
||||||
template <typename ... Args>
|
|
||||||
class Event;
|
|
||||||
|
|
||||||
template <typename ... Args>
|
|
||||||
class EventConnection {
|
|
||||||
private:
|
|
||||||
using delegate = std::function<void(Args...)>;
|
|
||||||
public:
|
|
||||||
EventConnection(Event<Args...> *creator, delegate 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>
|
|
||||||
EventConnection<Args...>::EventConnection(Event<Args...> *creator, EventConnection::delegate cb) : owner(creator), callback(std::move(cb)) {}
|
|
||||||
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
21
main.cpp
21
main.cpp
@@ -1,15 +1,20 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <functional>
|
||||||
#include <Event.h>
|
#include <Event.h>
|
||||||
|
//#include <EventConnection.h>
|
||||||
|
//#include "bullshit.h"
|
||||||
|
#include "stack"
|
||||||
|
|
||||||
void ProcessMessage(const std::string& message)
|
void ProcessMessage(const std::string& message)
|
||||||
{
|
{
|
||||||
std::cout << "Received: " << message << std::endl;
|
std::cout << "Received: " << message << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
// TODO: BasicEvent tests here.
|
||||||
|
|
||||||
Event<std::string> OnMessage;
|
int main() {
|
||||||
|
BasicEvent<std::function<void(std::string)>, std::string> OnMessage;
|
||||||
|
BasicEvent<std::function<void()>> VoidDelegate;
|
||||||
|
|
||||||
bool run = true;
|
bool run = true;
|
||||||
|
|
||||||
@@ -19,20 +24,28 @@ int main() {
|
|||||||
};
|
};
|
||||||
auto handler2 = OnMessage += ProcessMessage;
|
auto handler2 = OnMessage += ProcessMessage;
|
||||||
|
|
||||||
|
auto handler3 = OnMessage += [&] {
|
||||||
|
std::cout << "Connect to event, with default callback, if we don't care about using the provided arguments." << std::endl;
|
||||||
|
};
|
||||||
|
|
||||||
while (run)
|
while (run)
|
||||||
{
|
{
|
||||||
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>
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1 +0,0 @@
|
|||||||
#include <EventConnection.h>
|
|
Reference in New Issue
Block a user