Click here to Skip to main content
15,883,744 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.8K   763   29  
IoBind proposes a new approach to object serialization.
/*
IoBind Library License:
--------------------------

The zlib/libpng License Copyright (c) 2003 Jonathan de Halleux

This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.

3. This notice may not be removed or altered from any source distribution
*/


#ifndef IOBIND_PAIR_STREAM_POLICY_HPP
#define IOBIND_PAIR_STREAM_POLICY_HPP

#include <boost/call_traits.hpp>
#include <iobind/policy.hpp>
#include <iobind/stream_property.hpp>
#include <iobind/string_policy.hpp>
#include <iobind/pair_parser.hpp>

namespace iobind{
namespace detail{

template<
	typename FirstPolicy,
	typename SecondPolicy
>
class pair_to_stream_policy :
	public property::ostream_property
{
public:
	typedef property::ostream_property ostream_property_type;

	typedef bool return_type;
	typedef typename boost::call_traits<FirstPolicy>::const_reference first_policy_const_reference;
	typedef typename boost::call_traits<SecondPolicy>::const_reference second_policy_const_reference;

	explicit pair_to_stream_policy(
		first_policy_const_reference first_policy_,
		second_policy_const_reference second_policy_,
		ostream_reference ostream_
		)
	:
		m_first_policy(first_policy_),
		m_second_policy(second_policy_),
		ostream_property_type(ostream_)
	{};

	template<typename Pair>
	return_type encode( Pair const&  pair_) const
	{
		get_ostream()
			<<m_first_policy.encode(pair_.first)
			<<m_second_policy.encode(pair_.second)
			;

		return get_ostream().good();
	};

	first_policy_const_reference get_first_policy() const	{	return m_first_policy;};
	second_policy_const_reference get_second_policy() const	{	return m_second_policy;};

private:
	first_policy_const_reference m_first_policy;
	second_policy_const_reference m_second_policy;
};

};//detail

template<
	typename FirstPolicy,
	typename SecondPolicy
>
struct pair_to_stream : 
	detail::policy_cons< detail::pair_to_stream_policy<FirstPolicy,SecondPolicy> , detail::identity_policy >
{
		typedef detail::pair_to_stream_policy<FirstPolicy,SecondPolicy> pair_to_stream_policy_type;
		typedef detail::policy_cons< pair_to_stream_policy_type , detail::identity_policy > policy_type;
		typedef pair_to_stream<FirstPolicy,SecondPolicy> type;
		typedef type& reference;
		typedef type const& const_reference;
		typedef typename boost::call_traits<FirstPolicy>::const_reference first_policy_const_reference;
		typedef typename boost::call_traits<SecondPolicy>::const_reference second_policy_const_reference;
		typedef typename pair_to_stream_policy_type::ostream_reference ostream_reference;

		explicit pair_to_stream(
			first_policy_const_reference first_policy_,
			second_policy_const_reference second_policy_,
			ostream_reference ostream_
			)
		: policy_type(
			pair_to_stream_policy_type(
							first_policy_,
							second_policy_,
							ostream_
							),
			detail::identity_policy()
			)
		{};

		//change first policy
		template<
			typename H,
			typename T
		>
		pair_to_stream< detail::policy_cons<H, T> , SecondPolicy >
				change_first_policy( detail::policy_cons<H,T> const& first_policy_) const
		{
			return pair_to_stream< detail::policy_cons<H,T> , SecondPolicy >(
				first_policy_,
				get_head().get_second_policy(),
				get_head().get_ostream()
				);
		};

		template<
			typename H,
			typename T
		>
		pair_to_stream< detail::policy_cons<H, T> , SecondPolicy >
				operator << ( detail::policy_cons<H,T> const& first_policy_) const
		{ 
			return change_first_policy<H,T>(first_policy_); 
		};


		// change second policy
		template<
			typename H,
			typename T
		>
		pair_to_stream< FirstPolicy, detail::policy_cons<H,T> >
				change_second_policy( detail::policy_cons<H,T> const& second_policy_) const
		{
			return pair_to_stream< FirstPolicy, detail::policy_cons<H,T> >(
				get_head().get_first_policy(),
				second_policy_,
				get_head().get_ostream()
				);
		};

		template<
			typename H,
			typename T
		>
		pair_to_stream< FirstPolicy, detail::policy_cons<H,T> >
				operator >>( detail::policy_cons<H,T> const& second_policy_) const
		{
			return change_second_policy(second_policy_);
		}
};

typedef pair_to_stream<detail::identity_policy,detail::identity_policy> pair_to_stream_p_type;
const pair_to_stream_p_type pair_to_stream_p 
	= pair_to_stream_p_type( 
		detail::identity_policy(), 
		detail::identity_policy(),
		std::cout
		);

};//iobind

#endif


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