Refactor to a single-header so you don't run into that psychotic linkage error because you forgot to include the other header. :)

This commit is contained in:
2024-08-22 18:49:02 -04:00
parent 217fe1df88
commit 140b585854
5 changed files with 47 additions and 97 deletions

View File

@@ -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;
};
};

View File

@@ -18,6 +18,46 @@
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'.

View File

@@ -1,51 +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>
/// @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;
}

View File

@@ -1,7 +1,6 @@
#include <iostream> #include <iostream>
#include <functional> #include <functional>
#include <Event.h> #include <Event.h>
#include <EventConnection.h>
#include <cmath> #include <cmath>
void ProcessMessage(const std::string& message) void ProcessMessage(const std::string& message)
@@ -48,6 +47,9 @@ std::string Truncate(float input) {
ss << std::fixed << std::setprecision(roundTo) << fractional << suffix; ss << std::fixed << std::setprecision(roundTo) << fractional << suffix;
} }
str = ss.str(); str = ss.str();
return str; return str;
@@ -96,7 +98,7 @@ int main() {
break; break;
} }
std::cout << "std::string& iterations in 1 second: ~" << Truncate(iterations) << std::endl; std::cout << "std::string& iterations in 1 second: " << Truncate(iterations) << std::endl;
start = std::chrono::high_resolution_clock::now(); start = std::chrono::high_resolution_clock::now();
iterations = 0; iterations = 0;
@@ -111,7 +113,7 @@ int main() {
if (elapsed.count() >= 1.0) if (elapsed.count() >= 1.0)
break; break;
} }
std::cout << "void iterations in 1 second: ~" << Truncate(iterations) << std::endl; std::cout << "void iterations in 1 second: " << Truncate(iterations) << std::endl;
start = std::chrono::high_resolution_clock::now(); start = std::chrono::high_resolution_clock::now();
iterations = 0; iterations = 0;
@@ -128,7 +130,7 @@ int main() {
break; break;
} }
std::cout << "float iterations in 1 second: ~" << Truncate(iterations) << std::endl; std::cout << "float iterations in 1 second: " << Truncate(iterations) << std::endl;
start = std::chrono::high_resolution_clock::now(); start = std::chrono::high_resolution_clock::now();
iterations = 0; iterations = 0;
@@ -144,7 +146,7 @@ int main() {
if (elapsed.count() >= 1.0) if (elapsed.count() >= 1.0)
break; break;
} }
std::cout << "long long iterations in 1 second: ~" << Truncate(iterations) << std::endl; std::cout << "long long iterations in 1 second: " << Truncate(iterations) << std::endl;
return 0; return 0;
} }

View File

@@ -1 +0,0 @@
#include <EventConnection.h>