#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;
没有合适的资源?快使用搜索试试~ 我知道了~
C++中COM组件思想实践
需积分: 5 0 下载量 163 浏览量
2023-04-16
19:55:35
上传
评论
收藏 6KB GZ 举报
温馨提示
共19个文件
h:12个
cpp:6个
makefile:1个
在开发一个产品时,其往往有很多子模块,子业务和子功能,这些都可以抽象成组件(C++中本质表现就是一个类)。但是如何更好的管理这些类的对象呢?我们可以借鉴微软的COM组件思想来进行对象的注册,创建,销毁等管理工作。本文以某产品(产品业务为Business)的子功能AAAAAA为例,探讨一下COM组件思想在开发中的应用。
资源推荐
资源详情
资源评论
收起资源包目录
com_compent.tar.gz (19个子文件)
com_compent
comserver_control.cpp 8KB
comserver_control.h 3KB
aaaaaa_if.cpp 176B
test_com.cpp 1KB
comserver_define.h 186B
business_engine_interface.h 1KB
Makefile 535B
shared_pointer_guard.h 2KB
aaaaaa_if.h 276B
business_control_interface.h 1KB
comserver_config.h 1KB
business_engine_comserver.cpp 1KB
aaaaaa_com_factory.h 419B
business_engine_factory.h 433B
business_engine_comserver.h 1KB
aaaaaa_com_factory.cpp 726B
business_control_impl.cpp 2KB
aaaaaa_com_interface.h 359B
business_control_impl.h 1KB
共 19 条
- 1
资源评论
hsy12342611
- 粉丝: 2884
- 资源: 30
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功