Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1040037
  • 博文数量: 264
  • 博客积分: 6005
  • 博客等级: 大校
  • 技术积分: 2798
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-08 20:15
文章分类

全部博文(264)

文章存档

2011年(42)

2010年(213)

2009年(4)

2008年(2)

2007年(3)

分类:

2010-07-07 22:27:29

我们在程序中经常会遇到数据类型转换
例如:本人写的一个小类中就要用到


//==============================================================================
// = 三元组 (名字,类型,值)
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;
}






阅读(1334) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~