面向外部的接口:
#pragma once
#include <functional>
#ifdef CORECENTER_EXPORTS
#define CORECENTER_EXPORTS __declspec(dllexport)
#else
#define CORECENTER_EXPORTS
#endif
extern "C"
{
CORECENTER_EXPORTS int GetInterfaceCount(const char* strNotify);
CORECENTER_EXPORTS void* GetInterface(const char* strNotify, int index);
CORECENTER_EXPORTS void AddInterface(const char* strNotify, void* pNotifyObject);
CORECENTER_EXPORTS void RemoveInterface(const char* strNotify, void* pNotifyObject);
CORECENTER_EXPORTS void* GetCoreInstance(const char* strInterface);
CORECENTER_EXPORTS void AddCreateInstanceFunc(const char* strInterface, std::function<void*()> func);
CORECENTER_EXPORTS void AddNotifyInstance(const char* strNotify, const char* strInterface);
CORECENTER_EXPORTS void ActiveNotifyInstance(const char* strNotify);
CORECENTER_EXPORTS void StartComponent();
}
//***********************************************************//
//************************ 通知接口 **************************//
//***********************************************************//
//註冊通知
#define LISTEN(INotify)\
AddInterface(#INotify, (void*)(INotify*)this);
//去除通知
#define UNLISTEN(INotify)\
RemoveInterface(#INotify, (void*)(INotify*)this);
//分發通知
#define NOTIFY(INotify, function)\
ActiveNotifyInstance(#INotify);\
for(int i=0; i!= GetInterfaceCount(#INotify); ++i)\
{\
INotify* pInterface = (INotify*)(GetInterface(#INotify, i));\
if (nullptr != pInterface)\
{\
pInterface->function;\
}\
}\
//***********************************************************//
//************************ 單例接口 **************************//
//***********************************************************//
//獲取單例對象
#define GETINSTANCE(interface, object)\
{\
object = (interface*)GetCoreInstance(#interface);\
}
//註冊單例接口
#define REGINSTANCE(interface, CLASS)\
static auto interface##Func = []()\
{\
static CLASS object;\
return &object;\
};\
AddCreateInstanceFunc(#interface, interface##Func);
//通知拉活接口
#define REGNOTIFYINSTANCE(INotify, interface, CLASS)\
static auto interface##Func = []()\
{\
static CLASS object;\
return &object;\
};\
AddCreateInstanceFunc(#interface, interface##Func);\
AddNotifyInstance(#INotify, #interface);
//啓動框架
#define STARTCOMPONENTSERVICE StartComponent();
擴展框架爲了簡潔以宏的形式向外暴露
通知框架:
通知框架有LISTEN, UNLISTEN, NOTIFY三個宏接口組成,接受通知的對象需要用LISTEN註冊自己感興趣的接口,UNLISTEN可以取消接收通知。NOTIFY是用來發出通知的,參數有兩個第一個是接口,第二個是接口的方法,主要LISTEN了這個接口的對象都可以收到通知。
單例框架:
REGINSTANCE, GETINSTANCE兩個接口,第一個宏是用來註冊單例對象的,參數由接口和實現了接口的class組成。第二個是用來獲取單例對象的,參數爲接口和接受單例的一個接口指針。
插件框架:
STARTCOMPONENTSERVICE是用來啓動插件的,通俗講就是加載dll,並且調用dll的統一初始化方法。
插件框架和單例框架搭配起來最好,一般在插件初始化時就調用REGINSTANCE將一些單例註冊好,在使用時就可以隨時調用
GETINSTANCE來獲取單例對象。