You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
3.8 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "GearModeStructData.h"
#include <sstream>
#include <iomanip>
#include <ctime>
#include "Utils.h"
GearModeStructData::GearModeStructData()
: m_prefixLength(1) // 档位等级数量
, m_data(nullptr)
{
}
GearModeStructData::GearModeStructData(GearModeParam_t *data)
: m_prefixLength(1) // 档位等级数量
, m_data(nullptr)
{
setData(data);
}
GearModeStructData::~GearModeStructData()
{
if(m_data != nullptr)
{
m_data->datas.clear();
delete m_data;
m_data = nullptr;
}
}
// 从本对象clone一个对象并返回
Data* GearModeStructData::clone()
{
Data* data = new GearModeStructData(m_data);
GearModeStructData *dt = dynamic_cast<GearModeStructData *>(data);
dt->copyData(m_data);
return data;
}
// 从其他对象复制数据到本对象
void GearModeStructData::copy(Data* data)
{
GearModeStructData *dt = dynamic_cast<GearModeStructData *>(data);
if(dt->getData() == nullptr)
{
return;
}
if(m_data == nullptr)
{
m_data = new GearModeParam_t();
}
m_data->datas.clear();
std::copy(dt->getData()->datas.begin(), dt->getData()->datas.end(), std::back_inserter(m_data->datas));
}
void GearModeStructData::copy(Data& data)
{
copy(&data);
}
// 1字节的档位等级数量
// 1字节的档位等级
// 4字节的档位取值
int GearModeStructData::length()
{
return (m_prefixLength + (1 + 4) * m_data->datas.size());
}
std::string GearModeStructData::toStr()
{
std::stringstream ss;
ss << "level count : " << m_data->datas.size() << std::endl;
int levelIndex = 0;
std::list<GearModeParam>::iterator iter = m_data->datas.begin();
while(iter != m_data->datas.end())
{
ss << "level " << levelIndex << " : " << (*iter).getLevel() << ", value : " << (*iter).getValue() << std::endl;
levelIndex++;
iter++;
}
return ss.str();
}
// 1. 第1个字节/共1字节 档位等级数量
// 2. 第2个字节/共1字节 档位等级
// 3. 第3,4,5,6字节/共4字节档位取值*10000
// 重复2和3
std::string GearModeStructData::toByteStr()
{
std::string s;
unsigned char nCount = m_data->datas.size() >= 255 ? 255 : m_data->datas.size();
s.append(Utils::charToByteStr(nCount));
std::list<GearModeParam>::iterator iter = m_data->datas.begin();
while(iter != m_data->datas.end())
{
unsigned char level;
unsigned int value;
level = (*iter).getLevel();
value = (*iter).getValue();
s.append(Utils::charToByteStr(level));
s.append(Utils::intToByteStr(value));
iter++;
}
return s;
}
std::string GearModeStructData::toHexStr()
{
std::string s = toByteStr();
return Utils::strToHexStr(s);
}
bool GearModeStructData::fromByteStr(std::string str)
{
if(str.length() < (size_t)m_prefixLength)
{
return false;
}
std::string s;
s = str.substr(0, 1);
unsigned char nCount = Utils::byteStrToChar(s);
if(str.length() < (size_t)(m_prefixLength + (1 + 4) * nCount))
{
return false;
}
for(unsigned char i=0; i<nCount; i++)
{
unsigned char level = Utils::byteStrToChar(str.substr(m_prefixLength + (1 + 4) * i, 1));
unsigned int value = Utils::byteStrToInt(str.substr(m_prefixLength + (1 + 4) * i + 1, 4));
GearModeParam param(level, value);
m_data->datas.push_back(param);
}
return true;
}
void GearModeStructData::setData(GearModeParam_t* data)
{
if(m_data != nullptr)
{
m_data->datas.clear();
delete m_data;
m_data = nullptr;
}
m_data = data;
}
void GearModeStructData::copyData(GearModeParam_t* data)
{
if(m_data == nullptr)
{
m_data = new GearModeParam_t();
}
m_data->datas.clear();
std::copy(data->datas.begin(), data->datas.end(), std::back_inserter(m_data->datas));
}