diff --git a/includes/eventpp/callbacklist.h b/includes/eventpp/callbacklist.h deleted file mode 100644 index 71267bb..0000000 --- a/includes/eventpp/callbacklist.h +++ /dev/null @@ -1,498 +0,0 @@ -// 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 CALLBACKLIST_H_588722158669 -#define CALLBACKLIST_H_588722158669 - -#include "eventpolicies.h" - -#include -#include -#include - -namespace eventpp { - -namespace internal_ { - -template < - typename Prototype, - typename PoliciesType -> -class CallbackListBase; - -template < - typename PoliciesType, - typename ReturnType, typename ...Args -> -class CallbackListBase< - ReturnType (Args...), - PoliciesType -> -{ -private: - using Policies = PoliciesType; - - using Threading = typename SelectThreading::value>::Type; - - using Callback_ = typename SelectCallback< - Policies, - HasTypeCallback::value, - std::function - >::Type; - - using CanContinueInvoking = typename SelectCanContinueInvoking< - Policies, HasFunctionCanContinueInvoking::value - >::Type; - - struct Node; - using NodePtr = std::shared_ptr; - - struct Node - { - using Counter = unsigned int; - - Node(const Callback_ & callback, const Counter counter) - : callback(callback), counter(counter) - { - } - - NodePtr previous; - NodePtr next; - Callback_ callback; - Counter counter; - }; - - class Handle_ : public std::weak_ptr - { - private: - using super = std::weak_ptr; - - public: - using super::super; - - operator bool () const noexcept { - return ! this->expired(); - } - }; - - using Counter = typename Node::Counter; - enum : Counter { - removedCounter = 0 - }; - -public: - using Callback = Callback_; - using Handle = Handle_; - using Mutex = typename Threading::Mutex; - -public: - CallbackListBase() noexcept - : - head(), - tail(), - mutex(), - currentCounter(0) - { - } - - CallbackListBase(const CallbackListBase & other) - : CallbackListBase() - { - cloneFrom(other.head); - } - - CallbackListBase(CallbackListBase && other) noexcept - : CallbackListBase() - { - swap(other); - } - - // If we use pass by value idiom and omit the 'this' check, - // when assigning to self there is a deep copy which is inefficient. - CallbackListBase & operator = (const CallbackListBase & other) { - if(this != &other) { - CallbackListBase copied(other); - swap(copied); - } - return *this; - } - - CallbackListBase & operator = (CallbackListBase && other) noexcept { - if(this != &other) { - doFreeAllNodes(); - - head = std::move(other.head); - tail = std::move(other.tail); - currentCounter = other.currentCounter.load(); - } - return *this; - } - - ~CallbackListBase() { - // Don't lock mutex here since it may throw exception - - doFreeAllNodes(); - } - - void swap(CallbackListBase & other) noexcept { - using std::swap; - - swap(head, other.head); - swap(tail, other.tail); - - const auto value = currentCounter.load(); - currentCounter.exchange(other.currentCounter.load()); - other.currentCounter.exchange(value); - } - - bool empty() const { - // Don't lock the mutex for performance reason. - // !head still works even when the underlying raw pointer is garbled (for other thread is writting to head) - // And empty() doesn't guarantee the list is still empty after the function returned. - //std::lock_guard lockGuard(mutex); - - return ! head; - } - - operator bool() const { - return ! empty(); - } - - Handle append(const Callback & callback) - { - NodePtr node(doAllocateNode(callback)); - - std::lock_guard lockGuard(mutex); - - if(head) { - node->previous = tail; - tail->next = node; - tail = node; - } - else { - head = node; - tail = node; - } - - return Handle(node); - } - - Handle prepend(const Callback & callback) - { - NodePtr node(doAllocateNode(callback)); - - std::lock_guard lockGuard(mutex); - - if(head) { - node->next = head; - head->previous = node; - head = node; - } - else { - head = node; - tail = node; - } - - return Handle(node); - } - - Handle insert(const Callback & callback, const Handle & before) - { - // Disable this assertion because it's too slow in debug mode. - //assert(before.expired() || ownsHandle(before)); - - NodePtr beforeNode = before.lock(); - if(beforeNode) { - NodePtr node(doAllocateNode(callback)); - - std::lock_guard lockGuard(mutex); - - doInsert(node, beforeNode); - - return Handle(node); - } - - return append(callback); - } - - bool remove(const Handle & handle) - { - // Disable this assertion because it's too slow in debug mode. - //assert(handle.expired() || ownsHandle(handle)); - - // It looks like the lock can be put inside the `if` below, - // but that doesn't work in multi-threading and cause related unit tests fail. - std::lock_guard lockGuard(mutex); - - auto node = handle.lock(); - if(node) { - doFreeNode(node); - return true; - } - - return false; - } - - bool ownsHandle(const Handle & handle) const - { - std::lock_guard lockGuard(mutex); - - auto node = handle.lock(); - if(node) { - while(node->previous) { - node = node->previous; - } - return node == head; - } - - return false; - } - - template - void forEach(Func && func) const - { - doForEachIf([&func, this](NodePtr & node) -> bool { - doForEachInvoke(func, node); - return true; - }); - } - - template - bool forEachIf(Func && func) const - { - return doForEachIf([&func, this](NodePtr & node) -> bool { - return doForEachInvoke(func, node); - }); - } - -#if !defined(__GNUC__) || __GNUC__ >= 5 - void operator() (Args ...args) const - { - forEachIf([&args...](Callback & callback) -> bool { - // We can't use std::forward here, because if we use std::forward, - // for arg that is passed by value, and the callback prototype accepts it by value, - // std::forward will move it and may cause the original value invalid. - // That happens on any value-to-value passing, no matter the callback moves it or not. - - callback(args...); - return CanContinueInvoking::canContinueInvoking(args...); - }); - } -#else - // This is a patch version for GCC 4. It inlines the unrolled doForEachIf. - // GCC 4.8.3 doesn't supporting parameter pack catpure in lambda, see, - // https://github.com/wqking/eventpp/issues/19 - // This is a compromised patch for GCC 4, it may be not maintained or updated unless there are bugs. - // We don't use the patch as main code because the patch generates longer code, and duplicated with doForEachIf. - void operator() (Args ...args) const - { - NodePtr node; - - { - std::lock_guard lockGuard(mutex); - node = head; - } - - const Counter counter = currentCounter.load(std::memory_order_acquire); - - while(node) { - if(node->counter != removedCounter && counter >= node->counter) { - node->callback(args...); - if(! CanContinueInvoking::canContinueInvoking(args...)) { - break; - } - } - - { - std::lock_guard lockGuard(mutex); - node = node->next; - } - } - } -#endif - -private: - template - bool doForEachIf(F && f) const - { - NodePtr node; - - { - std::lock_guard lockGuard(mutex); - node = head; - } - - const Counter counter = currentCounter.load(std::memory_order_acquire); - - while(node) { - if(node->counter != removedCounter && counter >= node->counter) { - if(! f(node)) { - return false; - } - } - - { - std::lock_guard lockGuard(mutex); - node = node->next; - } - } - - return true; - } - - template - auto doForEachInvoke(Func && func, NodePtr & node) const - -> typename std::enable_if::value, RT>::type - { - return func(Handle(node), node->callback); - } - - template - auto doForEachInvoke(Func && func, NodePtr & node) const - -> typename std::enable_if::value, RT>::type - { - return func(node->callback); - } - - void doInsert(NodePtr & node, NodePtr & beforeNode) - { - node->previous = beforeNode->previous; - node->next = beforeNode; - if(beforeNode->previous) { - beforeNode->previous->next = node; - } - beforeNode->previous = node; - - if(beforeNode == head) { - head = node; - } - } - - NodePtr doAllocateNode(const Callback & callback) - { - return std::make_shared(callback, getNextCounter()); - } - - void doFreeNode(NodePtr & node) - { - if(node->next) { - node->next->previous = node->previous; - } - if(node->previous) { - node->previous->next = node->next; - } - - // Mark it as deleted, this must be before the assignment of head and tail below, - // because node can be a reference to head or tail, and after the assignment, node - // can be null pointer. - node->counter = removedCounter; - - if(head == node) { - head = node->next; - } - if(tail == node) { - tail = node->previous; - } - - // don't modify node->previous or node->next - // because node may be still used in a loop. - } - - void doFreeAllNodes() { - NodePtr node = head; - head.reset(); - while(node) { - NodePtr next = node->next; - node->previous.reset(); - node->next.reset(); - node = next; - } - node.reset(); - } - - Counter getNextCounter() - { - Counter result = ++currentCounter;; - if(result == 0) { // overflow, let's reset all nodes' counters. - { - std::lock_guard lockGuard(mutex); - NodePtr node = head; - while(node) { - node->counter = 1; - node = node->next; - } - } - result = ++currentCounter; - } - - return result; - } - - void cloneFrom(const NodePtr & fromHead) { - NodePtr fromNode(fromHead); - NodePtr node; - const Counter counter = getNextCounter(); - while(fromNode) { - const NodePtr nextNode(std::make_shared(fromNode->callback, counter)); - - nextNode->previous = node; - - if(node) { - node->next = nextNode; - } - else { - node = nextNode; - head = node; - } - - node = nextNode; - fromNode = fromNode->next; - } - - tail = node; - } - -private: - NodePtr head; - NodePtr tail; - mutable Mutex mutex; - typename Threading::template Atomic currentCounter; - -}; - - -} //namespace internal_ - - -template < - typename Prototype_, - typename Policies_ = DefaultPolicies -> -class CallbackList : public internal_::CallbackListBase, public TagCallbackList -{ -private: - using super = internal_::CallbackListBase; - -public: - using super::super; - - friend void swap(CallbackList & first, CallbackList & second) noexcept { - first.swap(second); - } -}; - - -} //namespace eventpp - - -#endif diff --git a/includes/eventpp/eventdispatcher.h b/includes/eventpp/eventdispatcher.h deleted file mode 100644 index 49c6712..0000000 --- a/includes/eventpp/eventdispatcher.h +++ /dev/null @@ -1,328 +0,0 @@ -// 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 - diff --git a/includes/eventpp/eventpolicies.h b/includes/eventpp/eventpolicies.h deleted file mode 100644 index 886ac10..0000000 --- a/includes/eventpp/eventpolicies.h +++ /dev/null @@ -1,186 +0,0 @@ -// 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 EVENTOPTIONS_H_730367862613 -#define EVENTOPTIONS_H_730367862613 - -#include "internal/typeutil_i.h" - -#include -#include -#include -#include -#include - -namespace eventpp { - -struct TagHomo {}; -struct TagCallbackList : public TagHomo {}; -struct TagEventDispatcher : public TagHomo {}; -struct TagEventQueue : public TagHomo {}; - -struct TagHeter {}; -struct TagHeterCallbackList : public TagHeter {}; -struct TagHeterEventDispatcher : public TagHeter {}; -struct TagHeterEventQueue : public TagHeter {}; - -struct SpinLock -{ -public: - void lock() { - while(locked.test_and_set(std::memory_order_acquire)) { - } - } - - void unlock() { - locked.clear(std::memory_order_release); - } - -private: - std::atomic_flag locked = ATOMIC_FLAG_INIT; -}; - -template < - typename Mutex_, - template class Atomic_ = std::atomic, - typename ConditionVariable_ = std::condition_variable -> -struct GeneralThreading -{ - using Mutex = Mutex_; - - template - using Atomic = Atomic_; - - using ConditionVariable = ConditionVariable_; -}; - -struct MultipleThreading -{ - using Mutex = std::mutex; - - template - using Atomic = std::atomic; - - using ConditionVariable = std::condition_variable; -}; - -struct SingleThreading -{ - struct Mutex - { - void lock() {} - void unlock() {} - }; - - template - struct Atomic - { - Atomic() noexcept = default; - constexpr Atomic(T desired) noexcept - : value(desired) - { - } - - void store(T desired, std::memory_order /*order*/ = std::memory_order_seq_cst) noexcept - { - value = desired; - } - - T load(std::memory_order /*order*/ = std::memory_order_seq_cst) const noexcept - { - return value; - } - - T exchange(T desired, std::memory_order /*order*/ = std::memory_order_seq_cst) noexcept - { - const T previous = value; - value = desired; - return previous; - } - - T operator ++ () noexcept - { - return ++value; - } - - T operator -- () noexcept - { - return --value; - } - - T value; - }; - - struct ConditionVariable - { - void notify_one() noexcept - { - } - - template - void wait(std::unique_lock & /*lock*/, Predicate /*pred*/) - { - } - - template - bool wait_for(std::unique_lock & /*lock*/, - const std::chrono::duration & /*rel_time*/, - Predicate /*pred*/ - ) - { - return true; - } - }; -}; - -struct ArgumentPassingAutoDetect -{ - enum { - canIncludeEventType = true, - canExcludeEventType = true - }; -}; - -struct ArgumentPassingIncludeEvent -{ - enum { - canIncludeEventType = true, - canExcludeEventType = false - }; -}; - -struct ArgumentPassingExcludeEvent -{ - enum { - canIncludeEventType = false, - canExcludeEventType = true - }; -}; - -struct DefaultPolicies -{ -}; - -template