Skip to content

Commit

Permalink
Core/Signal: Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLynix committed Jun 6, 2015
1 parent d7889dc commit 2f6aba9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
6 changes: 3 additions & 3 deletions include/Nazara/Core/Signal.hpp
Expand Up @@ -9,7 +9,7 @@

#include <functional>
#include <memory>
#include <list>
#include <vector>

template<typename... Args>
class NzSignal
Expand Down Expand Up @@ -40,7 +40,7 @@ class NzSignal
struct Slot;

using SlotPtr = std::shared_ptr<Slot>;
using SlotList = std::list<SlotPtr>;
using SlotList = std::vector<SlotPtr>;

struct Slot
{
Expand All @@ -51,7 +51,7 @@ class NzSignal

Callback callback;
NzSignal* signal;
typename SlotList::iterator it;
typename SlotList::size_type index;
};

void Disconnect(const SlotPtr& slot);
Expand Down
23 changes: 16 additions & 7 deletions include/Nazara/Core/Signal.inl
Expand Up @@ -3,7 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp

#include <Nazara/Core/Error.hpp>
#include <algorithm>
#include <utility>
#include <Nazara/Core/Debug.hpp>

template<typename... Args>
Expand All @@ -27,16 +27,15 @@ typename NzSignal<Args...>::Connection&& NzSignal<Args...>::Connect(const Callba
template<typename... Args>
typename NzSignal<Args...>::Connection&& NzSignal<Args...>::Connect(Callback&& func)
{
NazaraAssert(func, "Invalid function");

auto tempPtr = std::make_shared<Slot>(this);
tempPtr->callback = std::move(func);
tempPtr->index = m_slots.size();

m_slots.emplace_back(std::move(tempPtr));

const auto& slotPtr = m_slots.back();
slotPtr->it = m_slots.end();
--slotPtr->it;

return Connection(slotPtr);
return Connection(m_slots.back());
}

template<typename... Args>
Expand Down Expand Up @@ -81,7 +80,17 @@ NzSignal<Args...>& NzSignal<Args...>::operator=(NzSignal&& signal)
template<typename... Args>
void NzSignal<Args...>::Disconnect(const SlotPtr& slot)
{
m_slots.erase(slot->it);
NazaraAssert(slot, "Invalid slot pointer");
NazaraAssert(slot->index < m_slots.size(), "Invalid slot index");

// "Swap this slot with the last one and pop" idiom
// This will preserve slot indexes
SlotPtr& current = m_slots[slot->index];

std::swap(current, m_slots.back());
m_slots.pop_back();

current->index = slot->index; //< Update the moved slot index
}


Expand Down

0 comments on commit 2f6aba9

Please sign in to comment.