// eventpp library // Copyright (C) 2018 Wang Qi (wqking) // Github: https://github.com/wqking/eventpp // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef EVENTDISPATCHER_H_319010983013 #define EVENTDISPATCHER_H_319010983013 #include "callbacklist.h" namespace eventpp { namespace internal_ { template < typename EventType_, typename Prototype_, typename Policies_, typename MixinRoot_ > class EventDispatcherBase; template < typename EventType_, typename Policies_, typename MixinRoot_, typename ReturnType, typename ...Args > class EventDispatcherBase < EventType_, ReturnType (Args...), Policies_, MixinRoot_ > { protected: using ThisType = EventDispatcherBase< EventType_, ReturnType (Args...), Policies_, MixinRoot_ >; using MixinRoot = typename std::conditional< std::is_same::value, ThisType, MixinRoot_ >::type; using Policies = Policies_; using Threading = typename SelectThreading::value>::Type; using ArgumentPassingMode = typename SelectArgumentPassingMode< Policies_, HasTypeArgumentPassingMode::value, ArgumentPassingAutoDetect >::Type; using Callback_ = typename SelectCallback< Policies_, HasTypeCallback::value, std::function >::Type; using CallbackList_ = CallbackList; using Prototype = ReturnType (Args...); using Map = typename SelectMap< EventType_, CallbackList_, Policies_, HasTemplateMap::value >::Type; using Mixins = typename internal_::SelectMixins< Policies_, internal_::HasTypeMixins::value >::Type; public: using Handle = typename CallbackList_::Handle; using Callback = Callback_; using Event = EventType_; using Mutex = typename Threading::Mutex; public: EventDispatcherBase() : eventCallbackListMap(), listenerMutex() { } EventDispatcherBase(const EventDispatcherBase & other) : eventCallbackListMap(other.eventCallbackListMap), listenerMutex() { } EventDispatcherBase(EventDispatcherBase && other) noexcept : eventCallbackListMap(std::move(other.eventCallbackListMap)), listenerMutex() { } EventDispatcherBase & operator = (const EventDispatcherBase & other) { eventCallbackListMap = other.eventCallbackListMap; return *this; } EventDispatcherBase & operator = (EventDispatcherBase && other) noexcept { eventCallbackListMap = std::move(other.eventCallbackListMap); return *this; } void swap(EventDispatcherBase & other) noexcept { using std::swap; swap(eventCallbackListMap, other.eventCallbackListMap); } Handle appendListener(const Event & event, const Callback & callback) { std::lock_guard lockGuard(listenerMutex); return eventCallbackListMap[event].append(callback); } Handle prependListener(const Event & event, const Callback & callback) { std::lock_guard lockGuard(listenerMutex); return eventCallbackListMap[event].prepend(callback); } Handle insertListener(const Event & event, const Callback & callback, const Handle & before) { std::lock_guard lockGuard(listenerMutex); return eventCallbackListMap[event].insert(callback, before); } bool removeListener(const Event & event, const Handle handle) { CallbackList_ * callableList = doFindCallableList(event); if(callableList) { return callableList->remove(handle); } return false; } bool hasAnyListener(const Event & event) const { const CallbackList_ * callableList = doFindCallableList(event); if(callableList) { return ! callableList->empty(); } return false; } bool ownsHandle(const Event & event, const Handle & handle) const { const CallbackList_ * callableList = doFindCallableList(event); if(callableList) { return callableList->ownsHandle(handle); } return false; } template void forEach(const Event & event, Func && func) const { const CallbackList_ * callableList = doFindCallableList(event); if(callableList) { callableList->forEach(std::forward(func)); } } template bool forEachIf(const Event & event, Func && func) const { const CallbackList_ * callableList = doFindCallableList(event); if (callableList) { return callableList->forEachIf(std::forward(func)); } return true; } void dispatch(Args ...args) const { static_assert(ArgumentPassingMode::canIncludeEventType, "Dispatching arguments count doesn't match required (Event type should be included)."); using GetEvent = typename SelectGetEvent::value>::Type; // can't std::forward(args) in GetEvent::getEvent because the pass by value arguments will be moved to getEvent // then the other std::forward(args) to directDispatch will get empty values. directDispatch( GetEvent::getEvent(args...), std::forward(args)... ); } template void dispatch(T && first, Args ...args) const { static_assert(ArgumentPassingMode::canExcludeEventType, "Dispatching arguments count doesn't match required (Event type should NOT be included)."); using GetEvent = typename SelectGetEvent::value>::Type; directDispatch( GetEvent::getEvent(std::forward(first), args...), std::forward(args)... ); } // Bypass any getEvent policy. The first argument is the event type. // Most used for internal purpose. void directDispatch(const Event & e, Args ...args) const { if(! internal_::ForEachMixins::forEach( this, typename std::add_lvalue_reference::type(args)...)) { return; } const CallbackList_ * callableList = doFindCallableList(e); if(callableList) { (*callableList)(std::forward(args)...); } } protected: const CallbackList_ * doFindCallableList(const Event & e) const { return doFindCallableListHelper(this, e); } CallbackList_ * doFindCallableList(const Event & e) { return doFindCallableListHelper(this, e); } private: // template helper to avoid code duplication in doFindCallableList template static auto doFindCallableListHelper(T * self, const Event & e) -> typename std::conditional::value, const CallbackList_ *, CallbackList_ *>::type { std::lock_guard lockGuard(self->listenerMutex); auto it = self->eventCallbackListMap.find(e); if(it != self->eventCallbackListMap.end()) { return &it->second; } else { return nullptr; } } private: // Mixin related struct DoMixinBeforeDispatch { template static auto forEach(const Self * self, A && ...args) -> typename std::enable_if::value, bool>::type { return static_cast(self)->mixinBeforeDispatch(std::forward(args)...); } template static auto forEach(const Self * /*self*/, A && ... /*args*/) -> typename std::enable_if::value, bool>::type { return true; } }; private: Map eventCallbackListMap; mutable Mutex listenerMutex; }; } //namespace internal_ template < typename Event_, typename Prototype_, typename Policies_ = DefaultPolicies > class EventDispatcher : public internal_::InheritMixins< internal_::EventDispatcherBase, typename internal_::SelectMixins::value >::Type >::Type, public TagEventDispatcher { private: using super = typename internal_::InheritMixins< internal_::EventDispatcherBase, typename internal_::SelectMixins::value >::Type >::Type; public: using super::super; friend void swap(EventDispatcher & first, EventDispatcher & second) noexcept { first.swap(second); } }; } //namespace eventpp #endif