std::map容器序列化、反序列化测试

// testSerialize.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <map>
#include <sstream>

#include <string>
#include <iostream>

#include <fstream>

#include <boost/foreach.hpp>
#include <boost/shared_ptr.hpp>

// XML archive which uses wide characters (use for UTF-8 output ),
// defines boost::archive::xml_woarchive
// and boost::archive::xml_wiarchive
#include <boost/archive/xml_woarchive.hpp>
#include <boost/archive/xml_wiarchive.hpp>

#include <boost/serialization/map.hpp>
#include <boost/serialization/shared_ptr.hpp>

#include <boost/locale.hpp>
using namespace boost::locale;

/*
标题:std::map容器序列化、反序列化测试
Author:kagula
Date:2014-09-16
测试环境
 [1]Visual Studio 2013 Update3、boost 1.56
将来:
 完善测试用例
*/

namespace lijun
{
	class CPersistentLayer
	{
	private:
		friend class boost::serialization::access;
		template<class Archive>
		void serialize(Archive & ar, const unsigned int version)
		{
			ar & boost::serialization::make_nvp("MapWString", m_mapWString);
			ar & boost::serialization::make_nvp("MapInt", m_mapInt);
		}
	public:
		void SetWS(std::wstring key, std::wstring value)
		{
			m_mapWString[key] = value;
		}

		void SetInt(std::wstring key, int value)
		{
			m_mapInt[key] = value;
		}

		std::wstring GetWS(std::wstring key, std::wstring defaultValue=L"")
		{
			if (m_mapWString.find(key) == m_mapWString.end())
			{
				return defaultValue;
			}
			return m_mapWString[key];
		}

		int GetInt(std::wstring key, int defaultValue=0)
		{
			if (m_mapInt.find(key) == m_mapInt.end())
			{
				return defaultValue;
			}
			return m_mapInt[key];
		}

#ifdef _DEBUG
		void Reset()
		{
			m_mapWString.clear();
			m_mapInt.clear();
		}
#endif

		std::string m_lang = "zh_CN.UTF-8";
		std::wstring m_fileName = L"fileName.xml";

		std::map<std::wstring, std::wstring> m_mapWString;
		std::map<std::wstring, int> m_mapInt;
	};

	void LoadFromPersistentLayer(CPersistentLayer& pl)
	{
		std::locale loc = generator().generate(pl.m_lang);
		std::wifstream ifs(pl.m_fileName.c_str());
		ifs.imbue(loc);
		if (ifs.is_open())
		{
			boost::archive::xml_wiarchive ia(ifs, boost::archive::no_header);
			//ia >> BOOST_SERIALIZATION_NVP(pl, L"PersistentLayer");//这个宏使用"pl"变量名称作为节点名称
			ia >> boost::serialization::make_nvp("PersistentLayer", pl);
		}
	}

	void Save2PersistentLayer(CPersistentLayer& pl)
	{
		std::locale loc = generator().generate(pl.m_lang);
		std::wofstream ofs(pl.m_fileName.c_str());
		ofs.imbue(loc);
		if (ofs.is_open())
		{
			boost::archive::xml_woarchive oa(ofs, boost::archive::no_header);
			oa << boost::serialization::make_nvp("PersistentLayer", pl);
		}
	}
}
//这里设定要序列化的class的版本,如果不指定,版本默认为0
//BOOST_CLASS_VERSION(lijun::CPersistentLayer, 1)


void printMap(std::map<std::wstring,std::wstring> &mapT)
{
	for each (std::pair<std::wstring,std::wstring> var in mapT)
	{
		std::wcout << L"=>" << var.first << L"   [" << var.second << L"]" << std::endl;
	}
}

void testCase1()
{
	lijun::CPersistentLayer pl;
	pl.SetInt(L"int123", 123);
	pl.SetInt(L"int456", 456);
	pl.SetWS(L"WS_kagula", L"中文测试");
	pl.SetWS(L"WS_inuyasha", L"inuyasha");


	Save2PersistentLayer(pl);
	pl.Reset();
	LoadFromPersistentLayer(pl);
}

void testCase2()
{
	//manually add a item to a file,test if read from the file correctly!
	lijun::CPersistentLayer pl;
	LoadFromPersistentLayer(pl);

	//if value contain gbk encoding string,read correctly,but std::wcout not correctly!
	//IE open is correctly,but 
	printMap(pl.m_mapWString);
}

int _tmain(int argc, _TCHAR* argv[])
{
	testCase2();
	std::cin.get();
	return 0;
}

你可能感兴趣的:(std::map容器序列化、反序列化测试)