#pragma once
#include<iostream>
#include<vector>
template <class... Param>
class SlotBase
{
public:
virtual void Exec(Param... argc) = 0;
virtual ~SlotBase() {};
};
template<class Recver, class... Param>
class Slot : public SlotBase<Param...>
{
public:
Slot(Recver* pObject, void(Recver::* func)(Param...))
{
m_pRecver = pObject;
m_func = func;
}
void Exec(Param... argc)
{
(m_pRecver->*m_func)(argc...);
}
private:
Recver* m_pRecver = nullptr;
void(Recver::* m_func)(Param...);
};
template<class... Param>
class Signal
{
public:
template<class Recver>
void Bind(Recver* pObject, void(Recver::* func)(Param...))
{
m_pSlotSet.push_back(new Slot<Recver, Param...>(pObject, func));
}
void operator()(Param... argc)
{
for (int i = 0; i < m_pSlotSet.size(); ++i)
{
m_pSlotSet[i]->Exec(argc...);
}
}
~Signal()
{
for (int i = 0; i < m_pSlotSet.size(); ++i)
{
delete m_pSlotSet[i];
}
}
private:
std::vector<SlotBase<Param...>*> m_pSlotSet;
};
#define Signalconnect(sender, signal, recver, method) ((sender)->signal.Bind(recver, method))
07-17
550
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-12
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交