From 140b585854609faa22ba2ad99469d561ecb4875d Mon Sep 17 00:00:00 2001 From: josh Date: Thu, 22 Aug 2024 18:49:02 -0400 Subject: [PATCH] Refactor to a single-header so you don't run into that psychotic linkage error because you forgot to include the other header. :) --- include/Base.h | 40 ------------------------------ include/Event.h | 40 ++++++++++++++++++++++++++++++ include/EventConnection.h | 51 --------------------------------------- main.cpp | 12 +++++---- src/EventConnection.cpp | 1 - 5 files changed, 47 insertions(+), 97 deletions(-) delete mode 100644 include/Base.h delete mode 100644 include/EventConnection.h delete mode 100644 src/EventConnection.cpp diff --git a/include/Base.h b/include/Base.h deleted file mode 100644 index c7efc30..0000000 --- a/include/Base.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include - -//template< class RT, class CallableT, class... ArgTypes > -template -class Callable { - static_assert(std::is_invocable_v); - 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); -}; - - - - -template< class RT, class... ArgTypes> -class EventBase { -public: - //using delegate = CallableT; - using delegate = std::function; -public: - RT Invoke(ArgTypes... args); - RT operator()(ArgTypes... args); -protected: - delegate callback; - -}; - - -}; - diff --git a/include/Event.h b/include/Event.h index 42b623f..5db3c90 100644 --- a/include/Event.h +++ b/include/Event.h @@ -18,6 +18,46 @@ template class Connection; +/// @see Event.h +template +class BasicEvent; + +/// A type that represents a handle to an active event connection. +template +class Connection { +public: + friend BasicEvent; +public: + Connection(BasicEvent *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 * owner; + //delegate callback; + bool active = true; +}; + +template +Connection::Connection(BasicEvent *creator, delegate cb) : owner(creator), callback(std::move(cb)) {} + + +template +void Connection::Invoke(Args... e) { callback(e...); } + +template +bool Connection::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 /// 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'. diff --git a/include/EventConnection.h b/include/EventConnection.h deleted file mode 100644 index 2645549..0000000 --- a/include/EventConnection.h +++ /dev/null @@ -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 -#include - -/// @see Event.h -template -class BasicEvent; - -/// A type that represents a handle to an active event connection. -template -class Connection { -public: - friend BasicEvent; -public: - Connection(BasicEvent *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 * owner; - //delegate callback; - bool active = true; -}; - -template -Connection::Connection(BasicEvent *creator, delegate cb) : owner(creator), callback(std::move(cb)) {} - - -template -void Connection::Invoke(Args... e) { callback(e...); } - -template -bool Connection::Disconnect() { - if (active) { - owner->Disconnect(this); - active = false; - return true; - } - return false; -} diff --git a/main.cpp b/main.cpp index 944d512..3f2919a 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include void ProcessMessage(const std::string& message) @@ -48,6 +47,9 @@ std::string Truncate(float input) { ss << std::fixed << std::setprecision(roundTo) << fractional << suffix; } + + + str = ss.str(); return str; @@ -96,7 +98,7 @@ int main() { 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(); iterations = 0; @@ -111,7 +113,7 @@ int main() { if (elapsed.count() >= 1.0) 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(); iterations = 0; @@ -128,7 +130,7 @@ int main() { 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(); iterations = 0; @@ -144,7 +146,7 @@ int main() { if (elapsed.count() >= 1.0) 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; } diff --git a/src/EventConnection.cpp b/src/EventConnection.cpp deleted file mode 100644 index ba7d7ab..0000000 --- a/src/EventConnection.cpp +++ /dev/null @@ -1 +0,0 @@ -#include \ No newline at end of file