From 026889c86c713c5039504944aa993c8ad878f5fd Mon Sep 17 00:00:00 2001 From: Dawid Drozd Date: Sat, 14 Sep 2019 15:58:02 +0200 Subject: [PATCH] Update cpp code for move and forward --- lib/include/dexode/EventBus.hpp | 12 ++++++------ lib/include/dexode/eventbus/strategy/Protected.hpp | 11 ++++++----- lib/include/dexode/eventbus/strategy/Transaction.hpp | 8 ++++---- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/include/dexode/EventBus.hpp b/lib/include/dexode/EventBus.hpp index 3e273d8..9a89619 100644 --- a/lib/include/dexode/EventBus.hpp +++ b/lib/include/dexode/EventBus.hpp @@ -32,17 +32,17 @@ public: EventBus& operator=(const EventBus&) = delete; template - constexpr void post(const Event& event) + constexpr void post(Event&& event) { static_assert(Dexode::Internal::validateEvent(), "Invalid event"); - _base.template post(event); + _base.template post(std::forward(event)); } template - constexpr void postpone(const Event& event) + constexpr void postpone(Event&& event) { static_assert(Dexode::Internal::validateEvent(), "Invalid event"); - _base.template postpone(event); + _base.template postpone(std::forward(event)); } constexpr std::size_t processAll() @@ -50,12 +50,12 @@ public: return processLimit(std::numeric_limits::max()); } - constexpr std::size_t processLimit(std::size_t maxCountOfEvents) + constexpr std::size_t processLimit(const std::size_t maxCountOfEvents) { return _base.processLimit(maxCountOfEvents); } - constexpr std::size_t getPostponeEventCount() const + [[nodiscard]] constexpr std::size_t getPostponeEventCount() const { return _base.getQueueEventCount(); } diff --git a/lib/include/dexode/eventbus/strategy/Protected.hpp b/lib/include/dexode/eventbus/strategy/Protected.hpp index 2b9a7c3..fd4a1f9 100644 --- a/lib/include/dexode/eventbus/strategy/Protected.hpp +++ b/lib/include/dexode/eventbus/strategy/Protected.hpp @@ -49,16 +49,17 @@ public: } template - void postpone(const Event& event) + void postpone(Event&& event) { { std::unique_lock writeLock{_mutex}; - _eventQueue.push_back([this, event]() { post(event); }); + _eventQueue.push_back( + [this, event = std::forward(event)]() { post(event); }); } _eventWaiting.notify_one(); } - std::size_t processLimit(const std::size_t maxCountOfEvents); + std::size_t processLimit(std::size_t maxCountOfEvents); std::size_t getPostponeEventCount() const { @@ -83,10 +84,10 @@ public: } assert(dynamic_cast(eventListeners->second.get())); auto* vectorImpl = static_cast(eventListeners->second.get()); - vectorImpl->add(listenerID, callback); + vectorImpl->add(listenerID, std::forward>(callback)); } - void unlistenAll(const std::uint32_t listenerID); + void unlistenAll(std::uint32_t listenerID); template void unlisten(const std::uint32_t listenerID) diff --git a/lib/include/dexode/eventbus/strategy/Transaction.hpp b/lib/include/dexode/eventbus/strategy/Transaction.hpp index 3f57825..8553cd9 100644 --- a/lib/include/dexode/eventbus/strategy/Transaction.hpp +++ b/lib/include/dexode/eventbus/strategy/Transaction.hpp @@ -46,9 +46,9 @@ public: } template - void postpone(const Event& event) + void postpone(Event&& event) { - _eventQueue.push_back([this, event]() { post(event); }); + _eventQueue.push_back([this, event = std::forward(event)]() { post(event); }); } std::size_t processLimit(const std::size_t maxCountOfEvents) @@ -65,7 +65,7 @@ public: return processed; } - std::size_t getQueueEventCount() const noexcept + [[nodiscard]] std::size_t getQueueEventCount() const noexcept { return _eventQueue.size(); } @@ -83,7 +83,7 @@ public: } assert(dynamic_cast(vector.get())); auto* vectorImpl = static_cast(vector.get()); - vectorImpl->add(listenerID, callback); + vectorImpl->add(listenerID, std::forward>(callback)); } void unlistenAll(const std::uint32_t listenerID)