Click here to Skip to main content
15,891,951 members
Articles / Programming Languages / XML

XmlBind: putting PugXML on steroïds !

Rate me:
Please Sign up or sign in to vote.
3.36/5 (10 votes)
2 Oct 20035 min read 132K   580   31  
A mutant XML parser using IoBind, EDOM and PugXML.
// iobind library
//
// author: Jonathan de Halleux, 2003

#ifndef IOBIND_STRING_POLICY_HPP
#define IOBIND_STRING_POLICY_HPP

#include <sstream>
#include <boost/spirit.hpp>
#include <iobind/policy.hpp>

namespace iobind{
namespace detail{

	template<
		char const* FormatString
	>
	struct to_string_policy
	{
		typedef std::string return_type;
		typedef std::string string_type;
		typedef std::string const& string_param_type;

		template<typename T>
		return_type encode( T const& value_) const
		{
			return boost::io::str( boost::format( FormatString ) % value_ );
		};
	};

	template<typename T>
	struct from_string_policy
	{
		typedef boost::call_traits<T>::value_type value_type;
		typedef boost::call_traits<T>::value_type return_type;
		typedef boost::call_traits<std::string>::param_type param_type;

		return_type encode( param_type str_) const
		{
			std::istringstream input(str_.c_str());
			value_type rt;
			input>>rt;

			if (!input.fail())
				return rt;
			else
				return return_type();
		};
	};


}; // detail

template<
	char const* FormatString
> 
struct to_string : 
	detail::policy_cons< detail::to_string_policy<FormatString> , detail::identity_policy >
{
	typedef detail::to_string_policy<FormatString> to_string_policy_type;
	typedef detail::policy_cons< to_string_policy_type  , detail::identity_policy > policy_type;
	typedef to_string type;
	typedef to_string_policy_type::string_param_type string_param_type;
};

template<typename T>
struct from_string : 
	detail::policy_cons< detail::from_string_policy<T> , detail::identity_policy >
{
	typedef from_string type;
};

extern const char* to_string_default_format_string;

typedef to_string<to_string_default_format_string> to_string_p_type;
const to_string_p_type to_string_p = to_string_p_type();

typedef from_string<std::string> from_string_p_type;
const from_string_p_type from_string_p = from_string_p_type();

}; //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