Compare commits
8 Commits
Release-9
...
Release-11
Author | SHA1 | Date | |
---|---|---|---|
140b585854 | |||
217fe1df88 | |||
00b3857647 | |||
d85173ccd9 | |||
5e3e20a7e5 | |||
69e245632f | |||
e7f3f1ff95 | |||
d0222a0dce |
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.
|
@@ -1,40 +0,0 @@
|
|||||||
#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;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
@@ -14,10 +14,50 @@
|
|||||||
// TODO: ThreadSafeEvent / AsyncEvent version of the class.
|
// 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.
|
// TODO: ConsumableEvent - a version that can be "consumed" by a callback by returning true. It will be sunk and not passed to further callbacks.
|
||||||
|
|
||||||
///
|
/// See EventConnection.h
|
||||||
template <typename delegate, typename ... Args>
|
template <typename delegate, typename ... Args>
|
||||||
class Connection;
|
class Connection;
|
||||||
|
|
||||||
|
/// @see Event.h
|
||||||
|
template <typename delegate, typename ... Args>
|
||||||
|
class BasicEvent;
|
||||||
|
|
||||||
|
/// A type that represents a handle to an active event connection.
|
||||||
|
template <typename delegate, typename ... Args>
|
||||||
|
class Connection {
|
||||||
|
public:
|
||||||
|
friend BasicEvent<delegate, Args...>;
|
||||||
|
public:
|
||||||
|
Connection(BasicEvent<delegate, Args...> *creator, delegate cb);
|
||||||
|
bool Disconnect(); // Breaks the event connection, but does not destroy the instance
|
||||||
|
void Invoke(Args... e);
|
||||||
|
public:
|
||||||
|
// Fuck it make this public
|
||||||
|
// Don't be stupid!!
|
||||||
|
delegate callback;
|
||||||
|
private:
|
||||||
|
BasicEvent<delegate, Args...> * owner;
|
||||||
|
//delegate callback;
|
||||||
|
bool active = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename delegate, typename... Args>
|
||||||
|
Connection<delegate, Args...>::Connection(BasicEvent<delegate, Args...> *creator, delegate cb) : owner(creator), callback(std::move(cb)) {}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename delegate, typename... Args>
|
||||||
|
void Connection<delegate, Args...>::Invoke(Args... e) { callback(e...); }
|
||||||
|
|
||||||
|
template <typename delegate, typename ... Args>
|
||||||
|
bool Connection<delegate, Args...>::Disconnect() {
|
||||||
|
if (active) {
|
||||||
|
owner->Disconnect(this);
|
||||||
|
active = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/// BasicEvent is a templated class that can be used to raise events that can be listened in on
|
/// BasicEvent is a templated class that can be used to raise events that can be listened in on
|
||||||
/// at other points in a codebase without having to tightly couple the two pieces of code.
|
/// 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'.
|
/// @param delegate A type that can be invoked via operator() 'Call operator'.
|
||||||
@@ -25,12 +65,12 @@ class Connection;
|
|||||||
template <typename delegate, typename ... Args>
|
template <typename delegate, typename ... Args>
|
||||||
class BasicEvent {
|
class BasicEvent {
|
||||||
public:
|
public:
|
||||||
friend Connection<delegate, Args ...>;
|
|
||||||
using connection = Connection<delegate, 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);
|
/// Invokes the event, calling all currently bound connections, and passing in the given arguments.
|
||||||
virtual void Invoke(Args... args);
|
virtual void Invoke(Args... args);
|
||||||
|
/// The call operator also invokes the event
|
||||||
void operator()(Args... args);
|
void operator()(Args... args);
|
||||||
connection Connect(delegate callback);
|
connection Connect(delegate callback);
|
||||||
void Disconnect(connection &conn);
|
void Disconnect(connection &conn);
|
||||||
@@ -48,10 +88,9 @@ void BasicEvent<delegate, Args...>::operator()(Args... args) { Invoke(args...);}
|
|||||||
|
|
||||||
template <typename delegate, typename ... Args>
|
template <typename delegate, typename ... Args>
|
||||||
void BasicEvent<delegate, 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 delegate, typename ... Args>
|
template <typename delegate, typename ... Args>
|
||||||
Connection<delegate, Args...> BasicEvent<delegate, Args...>::Connect(delegate callback)
|
Connection<delegate, Args...> BasicEvent<delegate, Args...>::Connect(delegate callback)
|
||||||
@@ -66,6 +105,7 @@ 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());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The default event which uses a std::function as the delegate.
|
||||||
template <typename ... Args>
|
template <typename ... Args>
|
||||||
using Event = BasicEvent<std::function<void(Args...)>, Args...>;
|
using Event = BasicEvent<std::function<void(Args...)>, Args...>;
|
||||||
|
|
||||||
|
@@ -1,48 +0,0 @@
|
|||||||
/// @file Connection.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 delegate, typename ... Args>
|
|
||||||
class BasicEvent;
|
|
||||||
|
|
||||||
/// A type that represents a handle to an active event connection.
|
|
||||||
template <typename delegate, typename ... Args>
|
|
||||||
class Connection {
|
|
||||||
public:
|
|
||||||
friend BasicEvent<delegate, Args...>;
|
|
||||||
public:
|
|
||||||
Connection(BasicEvent<delegate, Args...> *creator, delegate cb);
|
|
||||||
bool Disconnect(); // Breaks the event connection, but does not destroy the instance
|
|
||||||
void Invoke(Args... e);
|
|
||||||
protected:
|
|
||||||
delegate callback;
|
|
||||||
private:
|
|
||||||
BasicEvent<delegate, Args...> * owner;
|
|
||||||
//delegate callback;
|
|
||||||
bool active = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename delegate, typename... Args>
|
|
||||||
Connection<delegate, Args...>::Connection(BasicEvent<delegate, Args...> *creator, delegate cb) : owner(creator), callback(std::move(cb)) {}
|
|
||||||
|
|
||||||
|
|
||||||
template <typename delegate, typename... Args>
|
|
||||||
void Connection<delegate, Args...>::Invoke(Args... e) { callback(e...); }
|
|
||||||
|
|
||||||
template <typename delegate, typename ... Args>
|
|
||||||
bool Connection<delegate, Args...>::Disconnect() {
|
|
||||||
if (active) {
|
|
||||||
owner->Disconnect(this);
|
|
||||||
active = false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
142
main.cpp
142
main.cpp
@@ -1,48 +1,156 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <Event.h>
|
#include <Event.h>
|
||||||
#include <EventConnection.h>
|
#include <cmath>
|
||||||
|
|
||||||
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 SigFigsTable[] = {0,0,0,1,0,0,1,0,0,1};
|
||||||
|
|
||||||
|
int DivBy[] = {1,1,1, 1000,1000,1000, 1000000, 1000000, 1000000, 1000000000, 1000000000,1000000000};
|
||||||
|
|
||||||
|
std::vector<std::string> Suffixes = {
|
||||||
|
"", "", "",
|
||||||
|
" Thousand", " Thousand", " Thousand",
|
||||||
|
" Million", " Million", " Million",
|
||||||
|
" Billion", " Billion", " Billion",
|
||||||
|
" Trillion", " Trillion", " Trillion",
|
||||||
|
" Quadrillion", " Quadrillion", " Quadrillion"
|
||||||
|
};
|
||||||
|
|
||||||
|
float Round(float f, float decimalPlaces) {
|
||||||
|
float mult = std::pow(10, decimalPlaces);
|
||||||
|
return std::floor(f * mult + 0.5f) / mult;
|
||||||
|
}
|
||||||
|
|
||||||
|
float Sign(float f) { return f >= 0.f ? 1.f : -1.f;}
|
||||||
|
|
||||||
|
std::string Truncate(float input) {
|
||||||
|
|
||||||
|
std::stringstream ss;
|
||||||
|
std::string str = "";
|
||||||
|
|
||||||
|
if (input < 1000)
|
||||||
|
ss << std::fixed << std::setprecision(0) << input;
|
||||||
|
else {
|
||||||
|
int figs = std::ceil(std::log10(input)) - 1;
|
||||||
|
auto suffix = Suffixes[figs];
|
||||||
|
auto roundTo = SigFigsTable[figs];
|
||||||
|
auto divBy = DivBy[figs];
|
||||||
|
auto fractional = input / (float)divBy;
|
||||||
|
|
||||||
|
// Increment roundTo for extra precision!!
|
||||||
|
|
||||||
|
ss << std::fixed << std::setprecision(roundTo) << fractional << suffix;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
str = ss.str();
|
||||||
|
return str;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
Event<int> cum;
|
Event<int> very_simple_event;
|
||||||
|
|
||||||
cum += [] (int i) { std::cout << i+5 << std::endl; };
|
very_simple_event += [] (int i) { std::cout << i+5 << std::endl; };
|
||||||
|
very_simple_event.Invoke(69);
|
||||||
cum.Invoke(69);
|
|
||||||
|
|
||||||
BasicEvent<std::function<void(std::string)>, std::string> OnMessage;
|
|
||||||
BasicEvent<std::function<void()>> VoidDelegate;
|
|
||||||
|
|
||||||
bool run = true;
|
|
||||||
|
|
||||||
|
//Const string reference
|
||||||
|
BasicEvent<std::function<void(const std::string&)>, const std::string&> OnMessage;
|
||||||
auto handler = OnMessage += [&] (const std::string& message)
|
auto handler = OnMessage += [&] (const std::string& message)
|
||||||
{
|
{
|
||||||
std::cout << "GOTS A MESSAGE: " << message << std::endl;
|
//std::cout << "GOTS A MESSAGE: " << message << std::endl;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//void
|
||||||
|
BasicEvent<std::function<void()>> VoidDelegate;
|
||||||
auto handler2 = OnMessage += ProcessMessage;
|
auto handler2 = OnMessage += ProcessMessage;
|
||||||
|
|
||||||
|
//Float
|
||||||
|
BasicEvent<std::function<void(float)>, float> floatEvent;
|
||||||
|
auto floatHandler = floatEvent += [&] (float){};
|
||||||
|
|
||||||
|
//long long
|
||||||
|
BasicEvent<std::function<void(unsigned long long)>, unsigned long long> longlongEvent;
|
||||||
|
auto longlongHandler = longlongEvent += [&] (unsigned long long){};
|
||||||
|
|
||||||
|
bool run = true;
|
||||||
|
auto start = std::chrono::high_resolution_clock::now();
|
||||||
|
unsigned long long iterations = 0;
|
||||||
|
|
||||||
while (run)
|
while (run)
|
||||||
{
|
{
|
||||||
std::string input;
|
std::string input = "test";
|
||||||
std::cin >> input;
|
|
||||||
OnMessage.Invoke(input);
|
OnMessage.Invoke(input);
|
||||||
VoidDelegate.Invoke();
|
|
||||||
OnMessage(input);
|
OnMessage(input);
|
||||||
VoidDelegate();
|
iterations++;
|
||||||
|
|
||||||
|
auto now = std::chrono::high_resolution_clock::now();
|
||||||
|
std::chrono::duration<double> elapsed = now - start;
|
||||||
|
if (elapsed.count() >= 1.0)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cout << "std::string& iterations in 1 second: " << Truncate(iterations) << std::endl;
|
||||||
|
start = std::chrono::high_resolution_clock::now();
|
||||||
|
iterations = 0;
|
||||||
|
|
||||||
|
while (run)
|
||||||
|
{
|
||||||
|
VoidDelegate.Invoke();
|
||||||
|
VoidDelegate();
|
||||||
|
iterations++;
|
||||||
|
|
||||||
|
auto now = std::chrono::high_resolution_clock::now();
|
||||||
|
std::chrono::duration<double> elapsed = now - start;
|
||||||
|
if (elapsed.count() >= 1.0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::cout << "void iterations in 1 second: " << Truncate(iterations) << std::endl;
|
||||||
|
|
||||||
|
start = std::chrono::high_resolution_clock::now();
|
||||||
|
iterations = 0;
|
||||||
|
|
||||||
|
while (run)
|
||||||
|
{
|
||||||
|
floatEvent.Invoke(1.25);
|
||||||
|
floatEvent(1.25);
|
||||||
|
iterations++;
|
||||||
|
|
||||||
|
auto now = std::chrono::high_resolution_clock::now();
|
||||||
|
std::chrono::duration<double> elapsed = now - start;
|
||||||
|
if (elapsed.count() >= 1.0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "float iterations in 1 second: " << Truncate(iterations) << std::endl;
|
||||||
|
|
||||||
|
start = std::chrono::high_resolution_clock::now();
|
||||||
|
iterations = 0;
|
||||||
|
|
||||||
|
while (run)
|
||||||
|
{
|
||||||
|
longlongEvent.Invoke(20);
|
||||||
|
longlongEvent(20);
|
||||||
|
iterations++;
|
||||||
|
|
||||||
|
auto now = std::chrono::high_resolution_clock::now();
|
||||||
|
std::chrono::duration<double> elapsed = now - start;
|
||||||
|
if (elapsed.count() >= 1.0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::cout << "long long iterations in 1 second: " << Truncate(iterations) << std::endl;
|
||||||
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[]) {
|
||||||
|
@@ -1 +0,0 @@
|
|||||||
#include <EventConnection.h>
|
|
Reference in New Issue
Block a user