在开发一个产品时,其往往有很多子模块,子业务和子功能,这些都可以抽象成组件(C++中本质表现就是一个类)。但是如何更好的管理这些类的对象呢?我们可以借鉴微软的COM组件思想来进行对象的注册,创建,销毁等管理工作。本文以某产品(产品业务为Business)的子功能AAAAAA为例,探讨一下COM组件思想在开发中的应用。
目录
1.类图
2.代码结构
3.代码实现
4.调用测试
1.类图
2.代码结构
3.代码实现
3.1 aaaaaa_com_factory.h / aaaaaa_com_factory.cpp
#include "business_engine_factory.h"
class CAAAAAAComFactory : public VBusinessFactory
{
public:
/**
* 组件接口创建方法
* @param riid [in] 接口ID
* @param ppv [in/out] 组件接口指针
*
* @return 返回创建状态
* @note
*/
static BUSINESS_VHRESULT CreateInstanceSPtr(const VBUSINESSCUID& iid, CompSPtr& ppv);
};
#include "aaaaaa_com_factory.h"
#include "aaaaaa_if.h"
#include "comserver_control.h"
#include "comserver_define.h"
VBusinessFactory::~VBusinessFactory(){}
BUSINESS_VHRESULT CAAAAAAComFactory::CreateInstanceSPtr(const VBUSINESSCUID& iid, CompSPtr& ppv)
{
BUSINESS_VHRESULT result = BUSINESS_VE_NOTIMPL;
// 识别类型
if (iid.find(CComServerControl::CompClsID(ComServer_Basic_AAAAAA)) >= 0)
{
//使用aaaaaa的基类
std::shared_ptr<CAAAAAAInterface> spMapMatch = std::make_shared<CAAAAAA>();
if (spMapMatch == nullptr) {
result = BUSINESS_VE_FAIL;
} else {
ppv = spMapMatch;
result = BUSINESS_VS_OK;
}
}
return result;
}
3.2 aaaaaa_com_interface.h
#pragma once
#include <iostream>
#include "business_engine_interface.h"
class CAAAAAAInterface : public VBusinessInterface
{
public:
virtual ~CAAAAAAInterface();
virtual void fun1();
virtual void fun2();
};
using AAAAAAPtr = std::shared_ptr<CAAAAAAInterface>;
#define GetAAAAAAPtr(com_ptr) (std::dynamic_pointer_cast<CAAAAAAInterface>(com_ptr))
3.3 aaaaaa_if.h / aaaaaa_if.cpp
#pragma once
#include "aaaaaa_com_interface.h"
class CAAAAAA : public CAAAAAAInterface
{
public:
CAAAAAA() {
}
~CAAAAAA() {
}
void fun1() {
std::cout << "fun1" << std::endl;
}
void fun2() {
std::cout << "fun2" << std::endl;
}
};
#include "aaaaaa_if.h"
VBusinessInterface::~VBusinessInterface(){}
CAAAAAAInterface::~CAAAAAAInterface() {};
void CAAAAAAInterface::fun1() {}
void CAAAAAAInterface::fun2() {}
3.4 business_control_impl.h / business_control_impl.cpp
#pragma once
#include "business_control_interface.h"
#include "business_engine_interface.h"
#include "comserver_control.h"
#include "shared_pointer_guard.h"
class CBusinessControl : public CBusinessControlInterface , public SharedPointerGuard<CBusinessControl> {
public:
CBusinessControl();
virtual ~CBusinessControl();
public:
static void GlobalInit();
// 注册组件实例创建方法
static void RegisterComponent(int type, VBusinessComFactoryFuncSPtr factoryFunc);
virtual bool Init(const std::string & config);
virtual bool UnInit();
virtual bool InitComponent(const std::string & config, int type);
virtual bool GetComServerSPtr(const int& type, std::shared_ptr<VBusinessInterface>& compSPtr);
virtual bool GetComServerSPtr(const int& type, const std::string& vID, std::shared_ptr<VBusinessInterface>& compSPtr);
public:
CComServerControl comServerControl;
private:
static bool _bInit;
static std::recursive_mutex _mutex;
};
#include "business_control_impl.h"
CBusinessControlInterface::CBusinessControlInterface() {};
CBusinessControlInterface::~CBusinessControlInterface() {};
bool CBusinessControl::_bInit = false;
std::recursive_mutex CBusinessControl::_mutex;
CBusinessControl::CBusinessControl() {
CBusinessControl::GlobalInit();
}
CBusinessControl::~CBusinessControl() {
}
void CBusinessControl::GlobalInit() {
if (_bInit) {
return;
}
//别的初始胡...
CComServerControl::Init();
_bInit = true;
}
void CBusinessControl::RegisterComponent(int type, VBusinessComFactoryFuncSPtr factoryFunc) {
CComServerControl::RegistComponent(type, factoryFunc, false);
}
bool CBusinessControl::Init(const std::string & config) {
{
std::lock_guard<std::recursive_mutex> guard(_mutex);
SetID("8888");
//调用shared_from_this的类必须至少有一个share_ptr指向它。而且必须是new方式创建的,std::make_shraed还不可以
comServerControl.SetBusinessControl(shared_this());
}
return true;
}
bool CBusinessControl::UnInit() {
return true;
}
bool CBusinessControl::InitComponent(const std::string & config, int type) {
return true;
}
bool CBusinessControl::GetComServerSPtr(const int& type, std::shared_ptr<VBusinessInterface>& compSPtr) {
CBusinessControl::GlobalInit();
return comServerControl.GetComServerSPtr(type, compSPtr);
}
bool CBusinessControl::GetComServerSPtr(const int& type, const std::string& vID, std::shared_ptr<VBusinessInterface>& compSPtr) {
return true;
}
3.5 business_control_interface.h
#ifndef _business_control_interface_h_
#define _business_control_interface_h_
#include <memory>
#include <string>
//注意:此处通过include引用头文件会出现编译错误
//#include "business_engine_interface.h"
class VBusinessInterface;
typedef std::string CBusinessControlID;
//业务控制接口类
class CBusinessControlInterface {
public:
CBusinessControlInterface();
virtual ~CBusinessControlInterface();
CBusinessControlInterface(const CBusinessControlInterface&) = delete;
CBusinessControlInterface& operator=(const CBusinessControlInterface&) = delete;
public:
const CBusinessControlID ID() const { return _id; }
void SetID(const CBusinessControlID &id_) { _id = id_; }
public:
virtual bool Init(const std::string & config) = 0;
virtual bool UnInit() = 0;
virtual bool InitComponent(const std::string & config, int type) = 0;
/**
* 组件获取
*/
virtual bool GetComServerSPtr(const int& type, std::shared_ptr<VBusinessInterface>& compSPtr) = 0;
virtual bool GetComServerSPtr(const int& type, const std::string& vID, std::shared_ptr<VBusinessInterface>& compSPtr) = 0;
private:
CBusinessControlID _id;
};
typedef std::shared_ptr<CBusinessControlInterface> BusinessControlSPtr;
#endif
3.6 business_engine_comserver.h / business_engine_comserver.cpp
#pragma once
#include <mutex>
#include <map>
#include "business_engine_interface.h"
// 任意类型
typedef void* LPVVoid;
class CVBusinessComServer
{
public:
/**
* 初始化组件服务
*/
static BUSINESS_VHRESULT InitComServer();
/**
* 反初始化组件服务
*
*/
static BUSINESS_VHRESULT UnitComServer();
/**
* 创建组件并获取接口
* @param rclsid [in] 组件ID
* @param factoryfunc [in] 组件工厂
*/
static BUSINESS_VHRESULT ComRegist(const VBUSINESSCLSID& rclsid, const VBusinessComFactoryFuncSPtr factoryfunc);
/**
* 创建组件并获取接口
* @param rclsid [in] 组件ID
* @param riid [in] 接口ID
* @param ppv [in/out] 组件接口指针
*
* @return 返回创建状态
* @note
*/
static BUSINESS_VHRESULT ComCreateInstanceSPtr(const VBUSINESSCLSID& rclsid, const VBUSINESSCUID& iid, CompSPtr& ppv);
private:
static std::mutex m_mutex; // 多线程互斥锁
static std::map<std::string, LPVVoid> m_CLSID2FFMap; // 组件ID和组件工厂函数指针对照表
};
#include "business_engine_comserver.h"
#include "business_engine_factory.h"
BUSINESS_VHRESULT CVBusinessComServer::InitComServer() {
return BUSINESS_VS_OK;
}
BUSINESS_VHRESULT CVBusinessComServer::UnitComServer(){
return BUSINESS_VS_OK;
}
BUSINESS_VHRESULT CVBusinessComServer::ComRegist(const VBUSINESSCLSID& rclsid, const VBusinessComFactoryFuncSPtr factoryfunc){
LPVVoid pValue = NULL;
if (m_CLSID2FFMap.end() == m_CLSID2FFMap.find(rclsid)) {
m_CLSID2FFMap[rclsid] = (LPVVoid)factoryfunc;
return BUSINESS_VS_OK;
}
return BUSINESS_VE_UNEXPECTED;
}
BUSINESS_VHRESULT CVBusinessComServer::ComCreateInstanceSPtr(const VBUSINESSCLSID& rclsid, const VBUSINESSCUID& iid, CompSPtr& ppv){
BUSINESS_VHRESULT hresult;
VBusinessComFactoryFuncSPtr pValue = nullptr;
if (m_CLSID2FFMap.end() != m_CLSID2FFMap.find(rclsid))
{
pValue = (VBusinessComFactoryFuncSPtr)m_CLSID2FFMap[rclsid];
hresult = pValue(iid, ppv);
}
return BUSINESS_VS_OK;
}
std::map<std::string, LPVVoid> CVBusinessComServer::m_CLSID2FFMap;
3.7 business_engine_factory.h
#include "business_engine_comserver.h"
class VBusinessFactory
{
public:
virtual ~VBusinessFactory();
/**
* 组件接口创建方法
* @param riid [in] 接口ID
* @param ppv [in/out] 组件接口指针
*
* @return 返回创建状态
* @note
*/
virtual BUSINESS_VHRESULT CreateInstance1(const VBUSINESSCUID& iid, LPVVoid *ppv) = 0;
};
3.8 comserver_config.h
#pragma once
//编译时期控制宏
#define STATIC_ENABLE_AAAAAA 1
#define STATIC_ENABLE_BBBBBB 1
//运行期控制模版
typedef struct _ComServerDynamicConfig
{
bool DY_ENABLE_AAAAAA;
bool DY_ENABLE_BBBBBB;
_ComServerDynamicConfig()
{
#ifdef STATIC_ENABLE_AAAAAA
DY_ENABLE_AAAAAA = true;
#else
DY_ENABLE_AAAAAA = false;
#endif
#ifdef STATIC_ENABLE_BBBBBB
DY_ENABLE_BBBBBB = true;
#else
DY_ENABLE_BBBBBB = false;
#endif
};
void Reset()
{
#ifdef STATIC_ENABLE_AAAAAA
DY_ENABLE_AAAAAA = true;
#else
DY_ENABLE_AAAAAA = false;
#endif
#ifdef STATIC_ENABLE_BBBBBB
DY_ENABLE_BBBBBB = true;
#else
DY_ENABLE_BBBBBB = false;
#endif
};
void Init(_ComServerDynamicConfig& clConfig)
{
#ifdef STATIC_ENABLE_AAAAAA
DY_ENABLE_AAAAAA = clConfig.DY_ENABLE_AAAAAA;
#endif
#ifdef STATIC_ENABLE_BBBBBB
DY_ENABLE_BBBBBB = clConfig.DY_ENABLE_BBBBBB;
#endif
};
}ComServerDynamicConfig;
3.9 comserver_control.h / comserver_control.cpp
#pragma once
#include <string>
#include <map>
#include <mutex>
#include "business_control_interface.h"
#include "business_engine_interface.h"
#include "comserver_config.h"
typedef std::map<std::string, CompSPtr> CompSPtrMap;
typedef std::map<std::string, CompSPtrMap> CompMap;
typedef std::map<int, VBusinessComFactoryFuncSPtr> CompFactoryMap;
class CComServerControl
{
public:
CComServerControl();
~CComServerControl();
public:
static bool Init();
static void UnInit();
static bool InitComponents();
static void UnInitComponents();
static bool LoadDynamicConfig(const std::string& clDyJson);
static bool SetDefaultCompControl(BusinessControlSPtr pBusinessControl);
static bool RegistComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, const bool& isComRegist = true);
static bool AddDefaultComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, CompSPtr& compSPtr);
static bool GetDefaultComServerSPtr(const int& type, CompSPtr& compSPtr);
static bool CreateCompSPtr(const int& type, const std::string& tag, CompSPtr& compSPtr);
static std::string CompClsID(const int& type);
static std::string CompVID(const int& type);
static std::string CompVID(const int& type, const std::string& tag);
public:
bool AddComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, CompSPtr& compSPtr);
bool AddComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, const std::string& tag, CompSPtr& compSPtr);
bool SetBusinessControl(BusinessControlSPtr pBusinessControl);
bool GetComServerSPtr(const int& type, CompSPtr& compSPtr);
bool GetComServerSPtr(const int& type, const std::string& vID, CompSPtr& compSPtr);
private:
static bool AddDefaultComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, const std::string& clsID, const std::string& vID, CompSPtr& compSPtr);
static bool RegistComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, const std::string& clsID, const bool& isComRegist = true);
bool AddComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, const std::string& clsID, const std::string& vID, CompSPtr& compSPtr);
public:
static ComServerDynamicConfig m_clDyConfig; /**< 组件动态化配置表 */
private:
static CompMap _compMapDefault; /**< 组件容器*/
static std::mutex _Initmutex; /**<初始化锁*/
static CompFactoryMap _compFactoryMap; /**< 组件工厂类映射*/
static BusinessControlSPtr _pDefaultBusinessControl; /**< 默认业务控制control实例*/
BusinessControlSPtr _pBusinessControl; /**< 业务control实例*/
CompMap _compMap; /**< 类型到clsID映射*/
};
#include <vector>
#include <tuple>
#include "comserver_control.h"
#include "aaaaaa_com_interface.h"
#include "business_engine_comserver.h"
#include "comserver_define.h"
#include "aaaaaa_com_factory.h"
CComServerControl::CComServerControl() {
CComServerControl::Init();
}
CComServerControl::~CComServerControl() {
}
bool CComServerControl::Init() {
std::vector<std::tuple<int, VBusinessComFactoryFuncSPtr>> comFactoryConfigs = {
std::make_tuple(ComServer_Basic_AAAAAA, (VBusinessComFactoryFuncSPtr)CAAAAAAComFactory::CreateInstanceSPtr)
};
for (const auto &t : comFactoryConfigs) {
_compFactoryMap.insert(CompFactoryMap::value_type(std::get<0>(t), std::get<1>(t)));
}
InitComponents();
return true;
}
void CComServerControl::UnInit() {
}
bool CComServerControl::InitComponents() {
bool bRet = true;
std::vector<std::tuple<int, bool>> comConfigs = {
std::make_tuple(ComServer_Basic_AAAAAA, m_clDyConfig.DY_ENABLE_AAAAAA),
};
CompSPtr compSPtr;
for(const auto &t : comConfigs)
{
int type = std::get<0>(t);
if (std::get<1>(t) && _compFactoryMap.find(type) != _compFactoryMap.end())
{
if (!AddDefaultComponent(type, _compFactoryMap[type], compSPtr))
{
bRet = false;
break;
}
}
}
return bRet;
}
void CComServerControl::UnInitComponents() {
}
bool CComServerControl::LoadDynamicConfig(const std::string& clDyJson) {
return true;
}
bool CComServerControl::SetDefaultCompControl(BusinessControlSPtr pBusinessControl) {
// 第一个创建的
if (_pDefaultBusinessControl) return true;
for (auto &kv : _compMapDefault) {
for (auto &sub : kv.second) {
sub.second->SetBusinessControl(pBusinessControl);
}
}
_pDefaultBusinessControl = pBusinessControl;
return true;
}
bool CComServerControl::RegistComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, const bool& isComRegist) {
return RegistComponent(type, factoryFunc, CompClsID(type), isComRegist);
}
bool CComServerControl::AddDefaultComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, CompSPtr& compSPtr) {
return AddDefaultComponent(type, factoryFunc, CompClsID(type), CompVID(type), compSPtr);
}
bool CComServerControl::GetDefaultComServerSPtr(const int& type, CompSPtr& compSPtr) {
return true;
}
bool CComServerControl::CreateCompSPtr(const int& type, const std::string& tag, CompSPtr& compSPtr) {
return true;
}
std::string CComServerControl::CompClsID(const int& type) {
std::string strID = "";
strID += "comp_";
strID += std::to_string(type);
return strID;
}
std::string CComServerControl::CompVID(const int& type) {
return CompVID(type, "");
}
std::string CComServerControl::CompVID(const int& type, const std::string& tag) {
std::string strID = "";
strID += CompClsID(type);
if (!tag.empty()) {
strID.append("_");
strID.append(tag);
}
strID += "_instance";
return strID;
}
bool CComServerControl::AddComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, CompSPtr& compSPtr) {
std::string clsID = CompClsID(type);
std::string tag = "";
if (_pBusinessControl)
{
tag = _pBusinessControl->ID().c_str();
}
std::string vID = CompVID(type, tag);
return AddComponent(type, factoryFunc, clsID, vID, compSPtr);
}
bool CComServerControl::AddComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, const std::string& tag, CompSPtr& compSPtr) {
std::string clsID = CompClsID(type);
std::string vID = CompVID(type, tag);
return AddComponent(type, factoryFunc, clsID, vID, compSPtr);
}
bool CComServerControl::SetBusinessControl(BusinessControlSPtr pBusinessControl) {
_pBusinessControl = pBusinessControl;
SetDefaultCompControl(pBusinessControl);
return true;
}
bool CComServerControl::GetComServerSPtr(const int& type, CompSPtr& compSPtr) {
bool bRet = GetComServerSPtr(type,
CompVID(type, _pBusinessControl->ID().c_str()),
compSPtr
);
if (!bRet) {
bRet = GetComServerSPtr(type,
CompVID(type),
compSPtr
);
}
return bRet;
}
bool CComServerControl::GetComServerSPtr(const int& type, const std::string& vID, CompSPtr& compSPtr) {
bool bRet = false;
std::string clsID = CompClsID(type);
do{
if (vID == CompVID(type))
{
if (_compMapDefault.find(clsID) == _compMapDefault.end()) break;
if (_compMapDefault[clsID].find(vID) == _compMapDefault[clsID].end()) break;
compSPtr = _compMapDefault[clsID][vID];
}
else
{
if (_compMap.find(clsID) == _compMap.end()) break;
if (_compMap[clsID].find(vID) == _compMap[clsID].end()) break;
compSPtr = _compMap[clsID][vID];
}
bRet = true;
}while (0);
return bRet;
}
bool CComServerControl::AddDefaultComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, const std::string& clsID, const std::string& vID, CompSPtr& compSPtr) {
bool bRet = false;
if (!RegistComponent(type, factoryFunc, clsID)) return bRet;
if (clsID == "" || vID == "") return bRet;
if (_compMapDefault.find(clsID) == _compMapDefault.end())
{
CompSPtrMap subCompMap;
_compMapDefault.insert(CompMap::value_type(clsID, subCompMap));
}
std::map<std::string, CompSPtr> &subCompMap = _compMapDefault[clsID];
if (subCompMap.find(vID) == subCompMap.end())
{
CompSPtr compSptr;
if (CVBusinessComServer::ComCreateInstanceSPtr(clsID, vID, compSptr) == BUSINESS_VS_OK)
{
compSptr->SetBusinessControl(_pDefaultBusinessControl);
subCompMap.insert(CompSPtrMap::value_type(vID, compSptr));
bRet = true;
}
}
return bRet;
}
bool CComServerControl::RegistComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, const std::string& clsID, const bool& isComRegist) {
_compFactoryMap[type] = factoryFunc;
if (isComRegist)
{
return CVBusinessComServer::ComRegist(clsID, factoryFunc) == BUSINESS_VS_OK;
}
return true;
}
bool CComServerControl::AddComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, const std::string& clsID, const std::string& vID, CompSPtr& compSPtr) {
bool bRet = false;
if (!RegistComponent(type, factoryFunc, clsID)) return bRet;
if (clsID == "" || vID == "") return bRet;
if (_compMap.find(clsID) == _compMap.end())
{
CompSPtrMap subCompMap;
_compMap.insert(CompMap::value_type(clsID, subCompMap));
}
std::map<std::string, CompSPtr> &subCompMap = _compMap[clsID];
if (subCompMap.find(vID) == subCompMap.end())
{
if (CVBusinessComServer::ComCreateInstanceSPtr(clsID, vID, compSPtr) == BUSINESS_VS_OK)
{
compSPtr->SetBusinessControl(_pBusinessControl);
subCompMap.insert(CompSPtrMap::value_type(vID, compSPtr));
bRet = true;
}
}
return bRet;
}
ComServerDynamicConfig CComServerControl::m_clDyConfig;
CompMap CComServerControl::_compMapDefault;
std::mutex CComServerControl::_Initmutex;
CompFactoryMap CComServerControl::_compFactoryMap;
BusinessControlSPtr CComServerControl::_pDefaultBusinessControl;
3.10 comserver_define.h
#pragma once
typedef enum _ComServer_Basic_Enum
{
ComServer_Basic_Invalid = -1,
ComServer_Basic_AAAAAA,
ComServer_Basic_BBBBBB
}ComServer_Basic_Enum;
3.11 shared_pointer_guard.h
#pragma once
#include <memory>
#include <functional>
template<typename TActual>
class SharedPointerGuard : public std::enable_shared_from_this<SharedPointerGuard<TActual>>
{
typedef std::shared_ptr<TActual> Handle;
typedef std::weak_ptr<TActual> WeakHandle;
protected:
const Handle shared_this() {
return std::static_pointer_cast<TActual>(this->shared_from_this());
}
template<typename T, typename ...TArgs>
std::function<T(TArgs...)> this_guard(const std::function<T(TArgs...)> &func) {
WeakHandle weak_handle = shared_this();
std::function<T(TArgs...)> wrapper = [weak_handle, func](TArgs&&... args) {
if (Handle handle = weak_handle.lock()) {
return func(std::forward<TArgs>(args)...);
}
return T();
};
return wrapper;
}
template<typename ...TArgs>
std::function<void(TArgs...)> this_guard(const std::function<void(TArgs...)> &func) {
WeakHandle weak_handle = shared_this();
std::function<void(TArgs...)> wrapper = [weak_handle, func](TArgs&&... args) {
if (Handle handle = weak_handle.lock()) {
func(std::forward<TArgs>(args)...);
}
};
return wrapper;
}
std::function<void()> this_guard(const std::function<void()> &func) {
WeakHandle weak_handle = shared_this();
std::function<void()> wrapper = [weak_handle, func] {
if (Handle handle = weak_handle.lock()) {
func();
}
};
return wrapper;
}
};
3.12 test_com.cpp
#include <iostream>
#include "business_control_impl.h"
#include "comserver_define.h"
#include "aaaaaa_com_factory.h"
#include "aaaaaa_com_interface.h"
using namespace std;
int main(int argc, char* argv[]) {
std::cout << "test com compent ...." << std::endl;
//注册组件
CBusinessControl::RegisterComponent(ComServer_Basic_AAAAAA, (VBusinessComFactoryFuncSPtr)CAAAAAAComFactory::CreateInstanceSPtr);
//创建业务控制总对象,构造函数中COM组件管理初始化
//BusinessControlSPtr businessControl = std::make_shared<CBusinessControl>();
BusinessControlSPtr businessControl(new CBusinessControl());
//业务初始化
businessControl->Init("");
//获取com类型的业务组件
CompSPtr ptr;
businessControl->GetComServerSPtr(ComServer_Basic_AAAAAA, ptr);
AAAAAAPtr pclAAAAAA = GetAAAAAAPtr(ptr);
if(!pclAAAAAA)
{
std::cout << "pclAAAAAA is nullptr" << std::endl;
return -1;
}
//调用组件
pclAAAAAA->fun1();
pclAAAAAA->fun2();
return 0;
}
3.13 Makefile
test : test_com.o aaaaaa_if.o business_control_impl.o aaaaaa_com_factory.o business_engine_comserver.o comserver_control.o
g++ -g -o test test_com.o aaaaaa_if.o aaaaaa_com_factory.o business_control_impl.o business_engine_comserver.o comserver_control.o
%.o : %.cpp
g++ -c -g -o $@ $<
aaaaaa_com_factory.o : aaaaaa_com_factory.h
business_control_impl.o : business_control_impl.h
business_engine_comserver.o : business_engine_comserver.h
comserver_control.o : comserver_control.h
aaaaaa_if.o : aaaaaa_if.h
clean:
rm *.o test -f
3.14 类图
PlantUML Web Server
@startuml
class CBusinessControlInterface(产品功能控制类接口) {
}class VBusinessInterface(具体功能点接口) {
}class CVBusinessComServer(业务Com组件服务器类) {
static std::mutex m_mutex;
static std::map<std::string, LPVVoid> m_CLSID2FFMap;
}interface VBusinessFactory(业务组件创建工厂接口){
}class CAAAAAAComFactory(具体AAAAAA业务组件创建工厂类) {
}class CAAAAAAInterface(AAAAAA业务com组件接口类) {
}class CAAAAAA(AAAAAA业务组件类) {
}class CBusinessControl(产品功能控制实现类) {
}class CComServerControl(Com组件服务器管理) {
}CAAAAAAComFactory --|> VBusinessFactory
CAAAAAAInterface --|> VBusinessInterface
CAAAAAA --|> CAAAAAAInterface
CBusinessControl --|> CBusinessControlInterface
CComServerControl --> CVBusinessComServer
CVBusinessComServer *-- CAAAAAAComFactory
CAAAAAAComFactory --> CAAAAAA
CComServerControl *-- VBusinessInterface
@enduml
4.调用测试
make
./test
可以看到,通过一些列操作后最终以一种可控的方式将业务子组件对应的函数进行了调用
完整代码下载路径如下:
https://download.csdn.net/download/hsy12342611/87696147