Update cpp code for move and forward

This commit is contained in:
Dawid Drozd 2019-09-14 15:58:02 +02:00
parent 8f6eb24549
commit 026889c86c
3 changed files with 16 additions and 15 deletions

View File

@ -32,17 +32,17 @@ public:
EventBus& operator=(const EventBus&) = delete;
template <typename Event>
constexpr void post(const Event& event)
constexpr void post(Event&& event)
{
static_assert(Dexode::Internal::validateEvent<Event>(), "Invalid event");
_base.template post<Event>(event);
_base.template post<Event>(std::forward<Event>(event));
}
template <typename Event>
constexpr void postpone(const Event& event)
constexpr void postpone(Event&& event)
{
static_assert(Dexode::Internal::validateEvent<Event>(), "Invalid event");
_base.template postpone<Event>(event);
_base.template postpone<Event>(std::forward<Event>(event));
}
constexpr std::size_t processAll()
@ -50,12 +50,12 @@ public:
return processLimit(std::numeric_limits<std::size_t>::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();
}

View File

@ -49,16 +49,17 @@ public:
}
template <typename Event>
void postpone(const Event& event)
void postpone(Event&& event)
{
{
std::unique_lock writeLock{_mutex};
_eventQueue.push_back([this, event]() { post<Event>(event); });
_eventQueue.push_back(
[this, event = std::forward<Event>(event)]() { post<Event>(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<Vector*>(eventListeners->second.get()));
auto* vectorImpl = static_cast<Vector*>(eventListeners->second.get());
vectorImpl->add(listenerID, callback);
vectorImpl->add(listenerID, std::forward<std::function<void(const Event&)>>(callback));
}
void unlistenAll(const std::uint32_t listenerID);
void unlistenAll(std::uint32_t listenerID);
template <typename Event>
void unlisten(const std::uint32_t listenerID)

View File

@ -46,9 +46,9 @@ public:
}
template <typename Event>
void postpone(const Event& event)
void postpone(Event&& event)
{
_eventQueue.push_back([this, event]() { post<Event>(event); });
_eventQueue.push_back([this, event = std::forward<Event>(event)]() { post<Event>(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*>(vector.get()));
auto* vectorImpl = static_cast<Vector*>(vector.get());
vectorImpl->add(listenerID, callback);
vectorImpl->add(listenerID, std::forward<std::function<void(const Event&)>>(callback));
}
void unlistenAll(const std::uint32_t listenerID)