Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C++

IoBind, a serializer code factory.

Rate me:
Please Sign up or sign in to vote.
4.89/5 (10 votes)
29 Jun 20037 min read 78.9K   765   29  
IoBind proposes a new approach to object serialization.
// PugArchive.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <vector>
#include <list>
#include <map>

#include <iobind/pair_policy.hpp>
#include "xml_bind.hpp"

int main(int argc, char* argv[])
{
	using namespace pug;
	using namespace iobind;

	double d(1);
	float f(2);
	int i(3);
	short s(4);
	long l(5);
	std::string str("escaped string: <>&\'\"");
	LPCTSTR sz("static string");

	std::vector<float> vf(5);
	for (i=0;i<static_cast<int>(vf.size());++i)
		vf[i]=i*1.1f;

	std::list<std::string> ls;
	for (i=0;i<3;++i)
		ls.push_back( boost::io::str( boost::format("l_%1%") % i) );

	std::map<size_t,std::string> mis;
	for (i=0;i<3;++i)
		mis[i]=boost::io::str( boost::format("m_%1%") % i);

	xml_bind ar;
	std::cerr<<"Testing xml_archive"<<std::endl;
	
	ar.append_child_elem("test_append");
	ar.into_elem();
	// addind child elem and attribute
		ar.append_child_elem("float", f);
		ar.append_child_attribute("a_float",f);

		ar.append_child_elem("double", d);
		ar.append_child_attribute("a_double",d);

		ar.append_child_elem("int", i);
		ar.append_child_attribute("a_int",i);

		ar.append_child_elem("long", l);
		ar.append_child_attribute("a_long",l);

		ar.append_child_elem("short", s);
		ar.append_child_attribute("a_short",s);

		ar.append_child_elem("static_string", sz);
		ar.append_child_attribute("a_static_string",sz);

		ar.append_child_elem("std::string", str);
		ar.append_child_attribute("a_string",str);

		ar.append_child_elem("base64", encode(str, to_base64_p) );

		ar.append_child_elem("add_containers");
		ar.into_elem();
			ar.append_child_elem("vector", vf.begin(), vf.end());
			ar.append_child_elem("list", ls.begin(), ls.end(),true);
			ar.append_child_elem_p(
					"map", 
					mis.begin(), 
					mis.end(), 
					sequence_to_string_p
					  .set_item_decorators("",",","") 
					  << pair_to_string_p 
					);
		ar.out_of_elem();

	ar.out_of_elem();
	std::cerr<<ar<<std::endl;

	// find child elem and attribute

	ar.reset_child();
	ar.into_elem();

		f=0;
		std::cerr<<"find - get  for child named float."<<std::endl;
		ar.find_get_child_data("float",f);

		std::cerr<<"float: "<<f<<std::endl;

		str.clear();
		std::cerr<<"find - get  for child named std::string"<<std::endl;
		ar.find_get_child_data("std::string",str);
		std::cerr<<"std::string: "<<str<<std::endl;

		ar.find_get_child_data("base64",str);
		std::cerr<<"base64: "<<encode(str, from_base64_p )<<std::endl;
	ar.out_of_elem();

	// loading from file.
	ar.parse_file( "xml_file.xml");
	ar.reset_cursors();
	ar.into_elem();
	std::cerr<<"Xml from file:\n"<<ar<<std::endl;

	// traversing
/*	do
	{
		std::cerr<<ar.get_child_elem().name()<<std::endl;
		ar.next_child();
	}
	while (ar.next_elem());
*/
	vf.clear();
	ar.find_get_child_data_p(
		"child2", 
		vf, 
		sequence_from_string_p
			.set_item_decorators("",",","") 
			% vf
			<< from_string<float>()
		);
	std::cerr<<"child2 data = "<<encode(vf.begin(), vf.end(), sequence_to_string_p)<<std::endl;

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions