/*
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