85 lines
2.6 KiB
C++
85 lines
2.6 KiB
C++
#pragma once
|
|
#include <string>
|
|
#include <map>
|
|
#include <shared_mutex>
|
|
#include "../Communication/BaseData.h"
|
|
|
|
//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, 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)); }
|
|
}
|
|
}
|
|
public:
|
|
Base() {}
|
|
virtual ~Base() {
|
|
std::unique_lock<std::shared_mutex> lock(m_mtx);
|
|
auto item = m_baseMp.begin();
|
|
while (item != m_baseMp.end()) {
|
|
delete item->second;
|
|
++item;
|
|
}
|
|
m_baseMp.clear();
|
|
}
|
|
void Update(const ReadData& msg) {
|
|
std::unique_lock<std::shared_mutex> lock(m_mtx);
|
|
|
|
for (auto start = msg.its.begin(); start != msg.its.end(); ++start) {
|
|
if (m_baseMp.find(start->nameKey) == m_baseMp.end()) {
|
|
printf("error, key %s do not find...\n", start->nameKey.c_str());
|
|
continue;
|
|
}
|
|
if (start->valueType == iFLOAT) {
|
|
float val = (float)atof(start->strValue.data());
|
|
m_baseMp[start->nameKey]->SetValue(val);
|
|
}
|
|
else if (start->valueType == iBOOL) {
|
|
bool val = (bool)atoi(start->strValue.data());
|
|
m_baseMp[start->nameKey]->SetValue(val);
|
|
}
|
|
else if (start->valueType == iSHORT) {
|
|
short val = (short)atoi(start->strValue.data());
|
|
m_baseMp[start->nameKey]->SetValue(val);
|
|
}
|
|
else if (start->valueType == iUSHORT) {
|
|
unsigned short val = (unsigned short)atoi(start->strValue.data());
|
|
m_baseMp[start->nameKey]->SetValue(val);
|
|
}
|
|
else if (start->valueType == iINT) {
|
|
int val = atoi(start->strValue.data());
|
|
m_baseMp[start->nameKey]->SetValue(val);
|
|
}
|
|
else if (start->valueType == iUINT) {
|
|
unsigned int val = atoi(start->strValue.data());
|
|
m_baseMp[start->nameKey]->SetValue(val);
|
|
}
|
|
else if (start->valueType == iUCHAR) {
|
|
unsigned char val = (unsigned char)atoi(start->strValue.data());
|
|
m_baseMp[start->nameKey]->SetValue(val);
|
|
}
|
|
else if (start->valueType == iSTRING) {
|
|
m_baseMp[start->nameKey]->SetValue(start->strValue);
|
|
}
|
|
else {
|
|
printf("error, type %d do not find...\n", start->valueType);
|
|
}
|
|
}
|
|
}
|
|
private:
|
|
std::map<std::string, BaseData*> m_baseMp;
|
|
public:
|
|
std::shared_mutex m_mtx;
|
|
};
|