Compare commits
3 Commits
Release-11
...
Release-12
Author | SHA1 | Date | |
---|---|---|---|
741b8b58dd | |||
9cc1f3a5ed | |||
642bdde567 |
@@ -1,4 +1,4 @@
|
|||||||
cmake_minimum_required(VERSION 3.27)
|
cmake_minimum_required(VERSION 3.18..3.27)
|
||||||
project(Event
|
project(Event
|
||||||
VERSION 1.0
|
VERSION 1.0
|
||||||
LANGUAGES CXX
|
LANGUAGES CXX
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
/// @file BasicEvent.hpp
|
/// @file Connection.hpp
|
||||||
/// @description Templated BasicEvent 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-06-01
|
/// @lastedit 2024-02-21
|
||||||
/// @license Unlicense - Public Domain
|
/// @license Unlicense - Public Domain
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
@@ -11,45 +11,46 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
// TODO: ThreadSafeEvent / AsyncEvent version of the class.
|
/// A delegate can be any function object or function-like object. The Args are the arguments accepted by that object
|
||||||
// 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 BasicEvent {
|
||||||
|
|
||||||
/// @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:
|
public:
|
||||||
|
/// A type that represents a handle to an active event connection.
|
||||||
|
class Connection {
|
||||||
|
public:
|
||||||
Connection(BasicEvent<delegate, Args...> *creator, delegate cb);
|
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
|
||||||
|
// If improperly used could lead to run away events? - Maxine
|
||||||
void Invoke(Args... e);
|
void Invoke(Args... e);
|
||||||
public:
|
public:
|
||||||
// Fuck it make this public
|
// This is made public in the case of function-like objects which store extra data.
|
||||||
// Don't be stupid!!
|
// This allows us to access members of the object when desired. See JTest's usage of the event system for an example.
|
||||||
delegate callback;
|
delegate callback;
|
||||||
private:
|
private:
|
||||||
BasicEvent<delegate, Args...> * owner;
|
BasicEvent<delegate, Args...> *owner;
|
||||||
//delegate callback;
|
|
||||||
bool active = true;
|
bool active = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
using EventPtr = std::shared_ptr<Connection>;
|
||||||
|
public:
|
||||||
|
// See JTest Unit for why making this virtual is convenient
|
||||||
|
virtual void Invoke(Args... args);
|
||||||
|
void operator()(Args... args);
|
||||||
|
Connection Connect(delegate callback);
|
||||||
|
void Disconnect(Connection &conn);
|
||||||
|
Connection operator+=(delegate callback);
|
||||||
|
protected:
|
||||||
|
std::vector<EventPtr> listeners;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename delegate, typename... Args>
|
template<typename delegate, typename... Args>
|
||||||
Connection<delegate, Args...>::Connection(BasicEvent<delegate, Args...> *creator, delegate cb) : owner(creator), callback(std::move(cb)) {}
|
BasicEvent<delegate, Args...>::Connection::Connection(BasicEvent<delegate, Args...> *creator, delegate cb) : owner(creator), callback(std::move(cb)) {}
|
||||||
|
|
||||||
|
|
||||||
template <typename delegate, typename... Args>
|
template <typename delegate, typename... Args>
|
||||||
void Connection<delegate, Args...>::Invoke(Args... e) { callback(e...); }
|
void BasicEvent<delegate, Args...>::Connection::Invoke(Args... e) { callback(e...); }
|
||||||
|
|
||||||
template <typename delegate, typename ... Args>
|
template <typename delegate, typename ... Args>
|
||||||
bool Connection<delegate, Args...>::Disconnect() {
|
bool BasicEvent<delegate, Args...>::Connection::Disconnect() {
|
||||||
if (active) {
|
if (active) {
|
||||||
owner->Disconnect(this);
|
owner->Disconnect(this);
|
||||||
active = false;
|
active = false;
|
||||||
@@ -58,54 +59,32 @@ bool Connection<delegate, Args...>::Disconnect() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// BasicEvent is a templated class that can be used to raise events that can be listened in on
|
template<typename delegate, typename... Args>
|
||||||
/// at other points in a codebase without having to tightly couple the two pieces of code.
|
BasicEvent<delegate, Args...>::Connection BasicEvent<delegate, Args...>::operator+=(delegate callback) { return Connect(callback); }
|
||||||
/// @param delegate A type that can be invoked via operator() 'Call operator'.
|
|
||||||
/// @param Args... The arguments that will be expected by this event and passed to it's delegate.
|
|
||||||
template <typename delegate, typename ... Args>
|
|
||||||
class BasicEvent {
|
|
||||||
public:
|
|
||||||
using connection = Connection<delegate, Args ...>;
|
|
||||||
using event_ptr = std::shared_ptr<connection>;
|
|
||||||
public:
|
|
||||||
/// Invokes the event, calling all currently bound connections, and passing in the given arguments.
|
|
||||||
virtual void Invoke(Args... args);
|
|
||||||
/// The call operator also invokes the event
|
|
||||||
void operator()(Args... args);
|
|
||||||
connection Connect(delegate callback);
|
|
||||||
void Disconnect(connection &conn);
|
|
||||||
connection operator+=(delegate callback);
|
|
||||||
protected:
|
|
||||||
std::vector<event_ptr> listeners;
|
|
||||||
//uint64_t listenerCounter = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename delegate, typename... Args>
|
template<typename delegate, typename... Args>
|
||||||
Connection<delegate, Args...> BasicEvent<delegate, Args...>::operator+=(delegate callback) { return Connect(callback); }
|
void BasicEvent<delegate, Args...>::operator()(Args... args) { Invoke(args...); }
|
||||||
|
|
||||||
template<typename delegate, typename... Args>
|
|
||||||
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 (EventPtr &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)
|
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 delegate, typename ... Args>
|
template <typename delegate, typename ... Args>
|
||||||
void BasicEvent<delegate, 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());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The default event which uses a std::function as the delegate.
|
/// 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>
|
template <typename ... Args>
|
||||||
using Event = BasicEvent<std::function<void(Args...)>, Args...>;
|
using Event = BasicEvent<std::function<void(Args...)>, Args...>;
|
||||||
|
|
||||||
|
143
main.cpp
143
main.cpp
@@ -1,156 +1,43 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <Event.h>
|
#include <Event.h>
|
||||||
#include <cmath>
|
//#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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: BasicEvent 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() {
|
||||||
|
BasicEvent<std::function<void(std::string)>, std::string> OnMessage;
|
||||||
Event<int> very_simple_event;
|
|
||||||
|
|
||||||
very_simple_event += [] (int i) { std::cout << i+5 << std::endl; };
|
|
||||||
very_simple_event.Invoke(69);
|
|
||||||
|
|
||||||
//Const string reference
|
|
||||||
BasicEvent<std::function<void(const std::string&)>, const std::string&> OnMessage;
|
|
||||||
auto handler = OnMessage += [&] (const std::string& message)
|
|
||||||
{
|
|
||||||
//std::cout << "GOTS A MESSAGE: " << message << std::endl;
|
|
||||||
};
|
|
||||||
|
|
||||||
//void
|
|
||||||
BasicEvent<std::function<void()>> VoidDelegate;
|
BasicEvent<std::function<void()>> VoidDelegate;
|
||||||
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;
|
bool run = true;
|
||||||
auto start = std::chrono::high_resolution_clock::now();
|
|
||||||
unsigned long long iterations = 0;
|
auto handler = OnMessage += [&] (const std::string& message)
|
||||||
|
{
|
||||||
|
std::cout << "GOTS A MESSAGE: " << message << std::endl;
|
||||||
|
};
|
||||||
|
auto handler2 = OnMessage += ProcessMessage;
|
||||||
|
|
||||||
while (run)
|
while (run)
|
||||||
{
|
{
|
||||||
std::string input = "test";
|
std::string input;
|
||||||
|
std::cin >> input;
|
||||||
OnMessage.Invoke(input);
|
OnMessage.Invoke(input);
|
||||||
OnMessage(input);
|
|
||||||
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.Invoke();
|
||||||
|
OnMessage(input);
|
||||||
VoidDelegate();
|
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[]) {
|
||||||
|
Reference in New Issue
Block a user