sigslot 库的用法

 这里还是网友的一个经典例子,light和switch例子

#include <iostream>
#include "sigslot.h"
class CSwitch
{
public:
	sigslot::signal0<> m_clicked;
};

class CLight:public sigslot::has_slots<>
{
public:
	CLight(bool s) {
		m_b_state = s;
		DisplayState();
	}
	
public:
	void DisplayState() {
		std::cout << "Now state: " << m_b_state << std::endl;
	}
	void ToggleState() {
		m_b_state = !m_b_state;
		DisplayState();
	}
	void TurnOn() {
		m_b_state = true;
		DisplayState();
	}
	void TurnOff() {
		m_b_state = false;
		DisplayState();
	}
private:
	bool m_b_state;
};
int main()
{
	CSwitch s1;
	CLight l1(false);
	s1.m_clicked.connect(&l1, &CLight::TurnOn);
	s1.m_clicked();
	l1.TurnOff();
	s1.m_clicked();
}

运行结果:

Now state: 0
Now state: 1
Now state: 0
Now state: 1

刚开始我有疑惑的是

	s1.m_clicked.connect(&l1, &CLight::TurnOn);
	s1.m_clicked();

这里m_clicked是个类的实例,但是看起来做了函数的调用,后来F10 debug看了下,signal0有重载operator ()

		void operator()()
		{
			lock_block<mt_policy> lock(this);
			const_iterator itNext, it = this->m_connected_slots.begin();
			const_iterator itEnd = this->m_connected_slots.end();

			while(it != itEnd)
			{
				itNext = it;
				++itNext;

                (*it)->_emit();

				it = itNext;
			}
		}

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页