• 关于序列化:把某个对象序列化成字节流


    在网络编程中。一个常常的操作是将本地的数据块转换成字符流并将其发送到远端。远端将这个字符串流恢复成数据库。如果有例如以下一个类CObject。编写两个函数,分别将CObject中的成员变量转换为一个字符流(convert2Stream()函数)。并将字符流的数据又一次恢复到一个CObject对象中(convert2Object()函数):

    char* convert2Stream(const CObject* vObject);

    CObject* convert2Object(const char* vStream);

    class CObject

    {

    private:

    int m_NumSample;

    std::vector<int> m_Data;

    bool m_IsDirty;

    }

    #include <iostream>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <boost/assign/list_of.hpp>
    #include <boost/archive/text_iarchive.hpp>
    #include <boost/archive/text_oarchive.hpp>
    #include <boost/serialization/access.hpp>
    #include <boost/serialization/vector.hpp>  
    
    class CObject
    {
    public:
    	CObject() {}
    	~CObject() {}
    
    	void printMember() const
    	{
    		std::cout << "Number " << m_NumSample << "
    ";
    		std::cout << "vector: ";
    		for (int i=0; i<m_Data.size(); ++i)
    		{
    			std::cout << m_Data[i] << " ";
    		}std::cout << std::endl;
    		std::cout << "bool " << m_IsDirty << std::endl;
    	}
    
    	void setValue(int vNumber, const std::vector<int>& vData, bool vB)
    	{
    		m_NumSample = vNumber;
    		m_Data = vData;
    		m_IsDirty = vB;
    	}
    
    private:
    	friend boost::serialization::access;
    	template<typename Archive>
    	void serialize(Archive& ar, const unsigned int)
    	{
    		ar & m_NumSample;
    		ar & m_Data;
    		ar & m_IsDirty;
    	}
    private:
    	int m_NumSample;
    	std::vector<int> m_Data;
    	bool m_IsDirty;
    };
    
    //*********************************************************************************
    //FUNCTION:
    char* convert2Stream(const CObject* vObject)
    {
    	//没有推断vObject的合法性
    	std::ostringstream OutStream;
    	boost::archive::text_oarchive OArchive(OutStream);
    	OArchive << vObject;
    	std::string TempString = OutStream.str();
    	char *pBuffer = new char[TempString.size()+1];
    	memcpy(pBuffer, TempString.c_str(), TempString.size());
    	pBuffer[TempString.size()] = '';
    	return pBuffer;
    }
    
    //*********************************************************************************
    //FUNCTION: 要求vStream是以''结束
    CObject* convert2Object(const char* vStream)
    {
    	int Length = 0;
    	const char *pTemp = vStream;
    	while (*pTemp != '')
    	{
    		++pTemp;
    		++Length;
    	}
    	if (Length == 0) return NULL;
    	std::string TempString(vStream, Length);
    	std::istringstream InStream(TempString);
    	boost::archive::text_iarchive InArchive(InStream);
    	CObject* pObject = NULL;//这里要传一个空指针,序列化时会申请内存
    	InArchive >> pObject;
    	return pObject;
    }
    
    int main()
    {
    	std::vector<int>  TempData = boost::assign::list_of(1)(11)(111)(1111)(11111);
    	CObject Obj;
    	Obj.setValue(2, TempData, false);
    	char *pBuf = convert2Stream(&Obj);
    	CObject* pObject = convert2Object(pBuf);
    	assert(pObject != NULL);
    	pObject->printMember();
    	delete[] pBuf;
    	delete pObject;//注意内存泄露
    
    	getchar();
    	return 0;
    }


  • 相关阅读:
    logging模块
    解压序列
    python碎片
    python碎片化
    [转]pycharm快捷键
    ios开发之自定义textView
    iOS 开发之字典写入文件
    iOS 开发之内存泄漏问题
    自己写小测试 内容:添加,删除,修改,详情,导出,上传文件,easyui tree树
    使用 jxl 实现复杂的excel 表格导出 java代码
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/6853605.html
Copyright © 2020-2023  润新知