强大的宏定义(macro)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/linfengmove/article/details/79210791

事件分发:(global.h)

#pragma once
#include <map>
#include <functional>
#include <Windows.h>
#include <tchar.h>
#include <vector>


class IExtension
{
public:
	virtual ~IExtension() {};
};


namespace
{
	auto f = [](GUID guid1, GUID guid2) {
		if (guid1.Data1 < guid2.Data1)
		{
			return true;
		}
		else
		{
			return false;
		}
		if (guid1.Data2 < guid2.Data2)
		{
			return true;
		}
		else
		{
			return false;
		}
		if (guid1.Data3 < guid2.Data3)
		{
			return true;
		}
		else
		{
			return false;
		}
		if (guid1.Data4 < guid2.Data4)
		{
			return true;
		}
		else
		{
			return false;
		}
	};


	std::map < GUID, std::vector<IExtension*>, decltype(f)> _coreMap(f);
}




template<class T>
BOOL RegisterCallBack(T* t)
{
	auto& vec = _coreMap[__uuidof(T)];
	vec.push_back(t);
	return TRUE;
}




#define NOTIFYCALLBACK(INTERFACE, FUNC) \
do\
{\
    wchar_t sz[] = _T(#INTERFACE"::"#FUNC);\
    std::wcout << sz <<std::endl;\
	auto iter = _coreMap.find(__uuidof(INTERFACE)); \
	if (iter != _coreMap.end())\
	{\
		auto vec = iter->second; \
		for(auto& inter : vec)\
		{\
			INTERFACE* pInter = dynamic_cast<INTERFACE*>(inter); \
			pInter->FUNC; \
		}\
	}\
}while(0)


class HuBigman
{
public:
	void PRINT()
	{
		std::cout << "HuBigman::PRINT" << std::endl;
	}
};
#define Patch(A ,ST, B)\
TCHAR szExpress[] = _T(#A#ST#B); \
std::wcout << szExpress <<std::endl;\
A##ST##B ob;\
ob.PRINT();


测试代码:

#include <Windows.h>
#include <iostream>
#include <functional>
#include "global.h"


class __declspec(uuid("{B0C5B255-50B0-4C0B-BC05-67998F345EAA}")) ICallListener : public IExtension
{
public:
	virtual void OnCall(int param1) = 0;
};
class __declspec(uuid("{F63AD8E3-E3D5-41C1-8FE0-373155713A00}")) IMMNotify : public IExtension
{
public:
	virtual void OnMM(int param) = 0;
};


class Student : public ICallListener
{
public:
	void OnCall(int param1)
	{
		std::cout << "I am a student: "<< param1 << std::endl;
	}
};


class Teacher : public ICallListener
{
public:
	void OnCall(int param1)
	{
		std::cout << "I am a teacher: " << param1 << std::endl;
	}
};


class LiMin : public IMMNotify
{
public:
	virtual void OnMM(int param)
	{
		std::cout << "I am LiMin: " << param << std::endl;
	}
};
class HanMeiMei : public IMMNotify
{
public:
	virtual void OnMM(int param)
	{
		std::cout << "I am HanMeiMei: " << param << std::endl;
	}
};
void main()
{
	ICallListener* pCallListener1 = new Student;
	ICallListener* pCallListener2 = new Teacher;
	RegisterCallBack(pCallListener1);
	RegisterCallBack(pCallListener2);


	IMMNotify* pNotify1 = new LiMin;
	IMMNotify* pNotify2 = new HanMeiMei;
	RegisterCallBack(pNotify1);
	RegisterCallBack(pNotify2);


	NOTIFYCALLBACK(ICallListener, OnCall(1));
	NOTIFYCALLBACK(IMMNotify, OnMM(1));
	Patch(Hu, Big, man);
	char wait;
	std::cin >> wait;
}



猜你喜欢

转载自blog.csdn.net/linfengmove/article/details/79210791