我们在程序中经常会遇到数据类型转换
例如:本人写的一个小类中就要用到
//==============================================================================
// = 三元组 (名字,类型,值)
const string eDataTypeFloat = "FLOAT";
...
class DataValue
{
public:
string name; //值名
string type; //值类型
string value;//值数值
};
/*! @class
********************************************************************************
类名称 : CSensorShadow
功能 : 用来保存其它在线网关关联的传感器信息
异常类 : 无
--------------------------------------------------------------------------------
备注 :
典型用法 :
--------------------------------------------------------------------------------
作者 : 毛勇
*******************************************************************************/
class CSensorShadow
{
public:
CSensorShadow(void);
~CSensorShadow(void);
public:
bool SetAttr(IN const DataValue &attrValue);
bool GetAttr(OUT DataValue &attrValue);
private:
uint64_t m_u64SensorId;
uint16_t m_u16ManufacturerId;
uint16_t m_u16Type;
bool m_bHaveToken;
uint8_t m_u8Priority;
uint64_t m_u64TokenPriorityGatewayId;
};
#include
#include
using boost::lexical_cast;
using std::string;
bool CSensorShadow::SetAttr( IN const DataValue &attrValue )
{
try
{
if (attrValue.name == "SensorId") //&& attrValue.type ==
{// !传感器id 属性
m_u64SensorId = lexical_cast(attrValue.value);
}
else if (attrValue.name == "ManufacturerId")
{//
m_u16ManufacturerId = lexical_cast(attrValue.value);
}
esle if (attrValue.name == "Type")
{//
m_u16Type = lexical_cast(attrValue.value);
}
else if (attrValue.name == "Token")
{//
m_bHaveToken = lexical_cast(attrValue.value);
}
else if (attrValue.name == "Priority")
{//
m_u8Priority = lexical_cast(attrValue.value);
}
else if (attrValue.name == "TokenPriorityGatewayId")
{//
m_u64TokenPriorityGatewayId = lexical_cast(attrValue.value);
}
}
catch (bad_lexical_cast & e)
{
printf("%s\r\n", e.what());
return FALSE;
}
return TRUE;
}
阅读(1364) | 评论(0) | 转发(0) |