diff --git a/solanaceae/toxcore/tox_event_interface.hpp b/solanaceae/toxcore/tox_event_interface.hpp index b5ca36e..ea68f2d 100644 --- a/solanaceae/toxcore/tox_event_interface.hpp +++ b/solanaceae/toxcore/tox_event_interface.hpp @@ -3,6 +3,7 @@ #include #include +#include // defines the full event interface for tox event subscription struct ToxEventI { @@ -63,12 +64,41 @@ struct ToxEventI { }; // defines the interface where to subscribe +// tox event enum is defined differently than what we expect for utils struct ToxEventProviderI { static constexpr const char* version {"2"}; + using enumType = Tox_Event_Type; + + // TODO: keep in sync with utils + struct SubscriptionReference { + ToxEventProviderI& _ep; + ToxEventI* _object {nullptr}; + std::vector _subs; + + SubscriptionReference(ToxEventProviderI& ep, ToxEventI* object) : + _ep(ep), _object(object) + { + } + + ~SubscriptionReference(void) { + for (const enumType et : _subs) { + _ep.unsubscribe(_object, et); + } + } + + SubscriptionReference& subscribe(const enumType event_type) { + _ep.subscribe(_object, event_type); + _subs.push_back(event_type); + return *this; + } + }; + + virtual ~ToxEventProviderI(void) {} - // TODO: unsub + virtual void subscribe(ToxEventI* object, const Tox_Event_Type event_type) = 0; + virtual void unsubscribe(ToxEventI* object, const Tox_Event_Type event_type) = 0; }; constexpr Tox_Event_Type tox_event_from_string(const std::string_view str) { diff --git a/solanaceae/toxcore/tox_event_logger.cpp b/solanaceae/toxcore/tox_event_logger.cpp index 70d26ff..97335b9 100644 --- a/solanaceae/toxcore/tox_event_logger.cpp +++ b/solanaceae/toxcore/tox_event_logger.cpp @@ -176,7 +176,8 @@ bool ToxEventLogger::onToxEvent(const Tox_Event_Group_Custom_Packet*) { } bool ToxEventLogger::onToxEvent(const Tox_Event_Group_Custom_Private_Packet*) { - _out << "TOX_EVENT: " << tox_event_to_string(TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET) << "\n"; + // too spammy for now + //_out << "TOX_EVENT: " << tox_event_to_string(TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET) << "\n"; return false; } diff --git a/solanaceae/toxcore/tox_event_provider_base.cpp b/solanaceae/toxcore/tox_event_provider_base.cpp index aea77bf..dbd4653 100644 --- a/solanaceae/toxcore/tox_event_provider_base.cpp +++ b/solanaceae/toxcore/tox_event_provider_base.cpp @@ -77,3 +77,13 @@ void ToxEventProviderBase::subscribe(ToxEventI* object, const Tox_Event_Type eve _subscribers.at(event_type).push_back(object); } +void ToxEventProviderBase::unsubscribe(ToxEventI* object, const Tox_Event_Type event_type) { + auto& o_vec = _subscribers.at(static_cast(event_type)); + for (auto o_it = o_vec.cbegin(); o_it != o_vec.cend(); o_it++) { + if (*o_it == object) { + o_vec.erase(o_it); + break; + } + } +} + diff --git a/solanaceae/toxcore/tox_event_provider_base.hpp b/solanaceae/toxcore/tox_event_provider_base.hpp index 2f4dcf2..7fc9239 100644 --- a/solanaceae/toxcore/tox_event_provider_base.hpp +++ b/solanaceae/toxcore/tox_event_provider_base.hpp @@ -12,6 +12,7 @@ struct ToxEventProviderBase : public ToxEventProviderI { public: // event provider virtual void subscribe(ToxEventI* object, const Tox_Event_Type event_type) override; + virtual void unsubscribe(ToxEventI* object, const Tox_Event_Type event_type) override; protected: std::array, 256> _subscribers; // rn 39 event types