Browse Source

Merge pull request #2745 from chriseth/signal

Copy map before looping over it because it might be modified.
cl-refactor
Gav Wood 10 years ago
parent
commit
0de565d8e7
  1. 20
      libdevcore/CommonData.h
  2. 12
      libethcore/Common.h

20
libdevcore/CommonData.h

@ -349,6 +349,26 @@ std::vector<T> keysOf(std::unordered_map<T, U> const& _m)
return ret; return ret;
} }
template<class T, class U>
std::vector<U> valuesOf(std::map<T, U> const& _m)
{
std::vector<U> ret;
ret.reserve(_m.size());
for (auto const& i: _m)
ret.push_back(i.second);
return ret;
}
template<class T, class U>
std::vector<U> valuesOf(std::unordered_map<T, U> const& _m)
{
std::vector<U> ret;
ret.reserve(_m.size());
for (auto const& i: _m)
ret.push_back(i.second);
return ret;
}
template <class T, class V> template <class T, class V>
bool contains(T const& _t, V const& _v) bool contains(T const& _t, V const& _v)
{ {

12
libethcore/Common.h

@ -154,9 +154,9 @@ public:
friend class Signal; friend class Signal;
public: public:
~HandlerAux() { if (m_s) m_s->m_fire.erase(m_i); m_s = nullptr; } ~HandlerAux() { if (m_s) m_s->m_fire.erase(m_i); }
void reset() { m_s = nullptr; } void reset() { m_s = nullptr; }
void fire(Args&&... _args) { m_h(std::forward<Args>(_args)...); } void fire(Args const&... _args) { m_h(_args...); }
private: private:
HandlerAux(unsigned _i, Signal* _s, Callback const& _h): m_i(_i), m_s(_s), m_h(_h) {} HandlerAux(unsigned _i, Signal* _s, Callback const& _h): m_i(_i), m_s(_s), m_h(_h) {}
@ -181,11 +181,11 @@ public:
return h; return h;
} }
void operator()(Args&... _args) void operator()(Args const&... _args)
{ {
for (auto const& f: m_fire) for (auto const& f: valuesOf(m_fire))
if (auto h = f.second.lock()) if (auto h = f.lock())
h->fire(std::forward<Args>(_args)...); h->fire(_args...);
} }
private: private:

Loading…
Cancel
Save