diff options
Diffstat (limited to 'core/src/Utils/Sigslot.hpp')
-rw-r--r-- | core/src/Utils/Sigslot.hpp | 45 |
1 files changed, 30 insertions, 15 deletions
diff --git a/core/src/Utils/Sigslot.hpp b/core/src/Utils/Sigslot.hpp index 2a191b4..5638f12 100644 --- a/core/src/Utils/Sigslot.hpp +++ b/core/src/Utils/Sigslot.hpp @@ -8,21 +8,25 @@ #include <utility> #include <vector> -class SignalStub { +class SignalStub +{ public: /// Non-template interface for Signal<T...> to implement (a barrier to stop template /// arguments propagation). - class IWrapper { + class IWrapper + { public: virtual ~IWrapper() = default; virtual void RemoveFunction(int id) = 0; }; - enum { + enum + { InvalidId = -1, }; - struct Connection { + struct Connection + { SlotGuard* guard; int slotId; int id = InvalidId; // If `InvalidId`, then this "spot" is unused @@ -55,7 +59,8 @@ private: }; template <class... TArgs> -class Signal : public SignalStub::IWrapper { +class Signal : public SignalStub::IWrapper +{ private: // Must be in this order so that mFunctions is still intact when mStub's destructor runs std::vector<std::function<void(TArgs...)>> mFunctions; @@ -63,7 +68,8 @@ private: public: Signal() - : mStub(*this) { + : mStub(*this) + { } virtual ~Signal() = default; @@ -73,7 +79,8 @@ public: Signal(Signal&&) = default; Signal& operator=(Signal&&) = default; - void operator()(TArgs... args) { + void operator()(TArgs... args) + { for (auto& conn : mStub.GetConnections()) { if (conn.IsOccupied()) { mFunctions[conn.id](std::forward<TArgs>(args)...); @@ -82,7 +89,8 @@ public: } template <class TFunction> - int Connect(TFunction slot) { + int Connect(TFunction slot) + { auto& conn = mStub.InsertConnection(); mFunctions.resize(std::max(mFunctions.size(), (size_t)conn.id + 1)); mFunctions[conn.id] = std::move(slot); @@ -90,26 +98,31 @@ public: } template <class TFunction> - int Connect(SlotGuard& guard, TFunction slot) { + int Connect(SlotGuard& guard, TFunction slot) + { auto& conn = mStub.InsertConnection(&guard); mFunctions.resize(std::max(mFunctions.size(), (size_t)conn.id + 1)); mFunctions[conn.id] = std::move(slot); return conn.id; } - void Disconnect(int id) { + void Disconnect(int id) + { mStub.RemoveConnection(id); } - void DisconnectFor(SlotGuard& guard) { + void DisconnectFor(SlotGuard& guard) + { mStub.RemoveConnectionFor(guard); } - void DisconnectAll() { + void DisconnectAll() + { mStub.RemoveAllConnections(); } - virtual void RemoveFunction(int id) { + virtual void RemoveFunction(int id) + { mFunctions[id] = {}; } }; @@ -117,9 +130,11 @@ public: /// Automatic disconnection mechanism for Signal<>. /// Bind connection to this guard by using the Connect(SlotGuard&, TFunction) overload. /// Either DisconnectAll() or the destructor disconnects all connections bound to this guard. -class SlotGuard { +class SlotGuard +{ private: - struct Connection { + struct Connection + { SignalStub* stub = nullptr; int stubId = SignalStub::InvalidId; }; |