#pragma once #include #include #include #include #include "../Communication/BaseData.h" #include "../DataManage/ClientInfo.h" #include "../utils/ConverType.hpp" //size_t ptrSize = sizeof(nullptr); //指针大小 //void* startPtr = &m_DoluyoChillerStat.m_startFlag + 1; //size_t count = ((size_t)&m_DoluyoChillerStat.m_endFlag - (size_t)startPtr) / ptrSize; //InsertMp(startPtr, count); class Base { protected: void InsertMp(void* startPtr, size_t count = 1, const std::string& suff = "") { size_t ptrSize = sizeof(nullptr); //指针大小 for (size_t i = 0; i < count; ++i) { BaseData* bd = *((BaseData**)((char*)startPtr + ptrSize * i)); std::string key = bd->GetCode() + suff; if (m_baseMp.find(key) != m_baseMp.end()) { printf("%s is repeated...\n", key.data()); } else { m_baseMp.insert(make_pair(key, bd)); } } } void InsertMp(const std::string& key, BaseData* baseData, const std::string& suff = "") { if (m_baseMp.find(key) != m_baseMp.end()) { printf("%s is repeated...\n", key.data()); } else { m_baseMp.insert(make_pair(key, baseData)); } } public: Base() {} virtual ~Base() { std::unique_lock lock(m_mtx); auto item = m_baseMp.begin(); while (item != m_baseMp.end()) { delete item->second; ++item; } m_baseMp.clear(); } virtual void SendToClients(WRITETYPE type, const std::string& addKey = "") { std::list its; { std::shared_lock lock(m_mtx); //读锁 auto baseItem = m_baseMp.begin(); while (baseItem != m_baseMp.end()) { std::string tempKey(baseItem->first + addKey); its.emplace_back(Item{ tempKey,baseItem->second->GetValueStr(),baseItem->second->GetDataType() }); ++baseItem; } } ClientWrapper::Instance()->PushAllClient(WriteData(type, its)); } void Update(const ReadData& rd, WRITETYPE type = DEFAULT) { if (m_baseMp.find(rd.nameKey) != m_baseMp.end()) { std::unique_lock lock(m_mtx); if (rd.valueType == iBOOL) { m_baseMp[rd.nameKey]->SetValue((bool)ConverType::TryToI(rd.strValue)); } else if (rd.valueType == iINT) { m_baseMp[rd.nameKey]->SetValue(ConverType::TryToI(rd.strValue)); } else if (rd.valueType == iUINT) { m_baseMp[rd.nameKey]->SetValue((unsigned int)ConverType::TryToI(rd.strValue)); } else if (rd.valueType == iFLOAT) { m_baseMp[rd.nameKey]->SetValue(ConverType::TryToF(rd.strValue)); } else if (rd.valueType == iSHORT) { m_baseMp[rd.nameKey]->SetValue((short)ConverType::TryToI(rd.strValue)); } else if (rd.valueType == iUSHORT) { m_baseMp[rd.nameKey]->SetValue((unsigned short)ConverType::TryToI(rd.strValue)); } else if (rd.valueType == iSTRING) { m_baseMp[rd.nameKey]->SetValue(rd.strValue); } else if (rd.valueType == iDOUBLE) { m_baseMp[rd.nameKey]->SetValue(ConverType::TryToD(rd.strValue)); } else if (rd.valueType == iTIMET) { m_baseMp[rd.nameKey]->SetValue((time_t)ConverType::TryToLL(rd.strValue)); } } else { printf("error, %s do not find...", rd.nameKey.c_str()); } if(type != DEFAULT) SendToClients(type); //客户端更新后在发送给客户端 } protected: std::map m_baseMp; public: std::shared_mutex m_mtx; };