/*

oess_1: Object Entity Serialization & Stability
Yauheni A. Akhotnikau (C) 2002-2003
eao197@yahoo.com
-------------------------------------------------

Permission is granted to anyone to use this software for any purpose on any
computer system, and to redistribute it freely, subject to the following
restrictions:

1. This software is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

2. The origin of this software must not be misrepresented, either by
   explicit claim or by omission.

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

-------------------------------------------------

*/
/*
	ObjESSty
*/

#include <iostream>
#include <exception>

#include <oess_1/io/h/mem_buf.hpp>

#include <oess_1/tlv/h/basic_string.hpp>
#include <oess_1/tlv/h/scalar.hpp>
#include <oess_1/tlv/h/tagged_scalar_wrapper.hpp>
#include <oess_1/tlv/h/compound.hpp>

typedef oess_1::uchar_t tag_type_t;

typedef oess_1::tlv::scalar_tlv_t< tag_type_t, oess_1::uint_t >
	uint_scalar_tlv_t;

typedef oess_1::tlv::basic_string_tlv_t< tag_type_t, oess_1::char_t >
	string_tlv_t;

const	tag_type_t	c_tag_scalar = 0x10;
const	tag_type_t	c_tag_string = 0x20;

typedef oess_1::tlv::compound_tlv_t<tag_type_t > compound_t;
class	my_compound_tlv_t :
	public compound_t
{
	private :
		oess_1::tlv::tagged_scalar_wrapper_t<
				0x02, string_tlv_t >
			m_member_1;
		const tlv_add_t	m_member_1_inserter;

		oess_1::tlv::tagged_scalar_wrapper_t<
				0x03, string_tlv_t >
			m_member_2;
		const tlv_add_t	m_member_2_inserter;

		oess_1::tlv::tagged_scalar_wrapper_t<
				0x04, oess_1::tlv::scalar_tlv_t<
					tag_type_t, oess_1::uint_t > >
			m_member_3;
		const tlv_add_t	m_member_3_inserter;

		oess_1::tlv::tagged_scalar_wrapper_t<
				0x05, oess_1::tlv::scalar_tlv_t<
					tag_type_t, oess_1::ushort_t > >
			m_member_4;
		const tlv_add_t	m_member_4_inserter;

		oess_1::tlv::tagged_scalar_wrapper_t<
				c_tag_scalar, uint_scalar_tlv_t >
			m_member_5;
		const tlv_add_t	m_member_5_inserter;

		oess_1::tlv::tagged_scalar_wrapper_t<
				c_tag_string, string_tlv_t >
			m_member_6;
		const tlv_add_t	m_member_6_inserter;

	public :
		my_compound_tlv_t(
			const std::string & member_1,
			const std::string & member_2,
			oess_1::uint_t member_3,
			oess_1::ushort_t member_4,
			oess_1::uint_t member_5,
			const std::string & member_6 )
			:
				compound_t( 0x01 ),
				m_member_1( member_1 ),
				m_member_1_inserter( tlv_self(), m_member_1 ),

				m_member_2( member_2 ),
				m_member_2_inserter( tlv_self(), m_member_2 ),

				m_member_3( member_3 ),
				m_member_3_inserter( tlv_self(), m_member_3 ),

				m_member_4( member_4 ),
				m_member_4_inserter( tlv_self(), m_member_4 ),

				m_member_5( member_5 ),
				m_member_5_inserter( tlv_self(), m_member_5 ),

				m_member_6( member_6 ),
				m_member_6_inserter( tlv_self(), m_member_6 )
		{
		}

		my_compound_tlv_t()
			:
				compound_t( 0x01 ),
				m_member_1_inserter( tlv_self(), m_member_1 ),
				m_member_2_inserter( tlv_self(), m_member_2 ),
				m_member_3_inserter( tlv_self(), m_member_3 ),
				m_member_4_inserter( tlv_self(), m_member_4 ),
				m_member_5_inserter( tlv_self(), m_member_5 ),
				m_member_6_inserter( tlv_self(), m_member_6 )
		{
		}

		std::ostream &
		show( std::ostream & s ) const
		{
			return ( s << "member_1: "
				<< m_member_1.query_value()
				<< ", member_2: "
				<< m_member_2.query_value()
				<< ", member_3: "
				<< m_member_3.query_value()
				<< ", member_4: "
				<< m_member_4.query_value()
				<< ", member_5: "
				<< m_member_5.query_value()
				<< ", member_6: "
				<< m_member_6.query_value() );
		}
};

std::ostream &
operator<<( std::ostream & s, const my_compound_tlv_t & o )
{
	return o.show( s );
}

void
write_to(
	oess_1::io::obinstream_t & s )
{
	my_compound_tlv_t t1( "hello", "", 5, 6, 100, "-100-" );
	my_compound_tlv_t t2( "", "hello", 7, 8, 200, "-200-" );

	s << t1 << t2;
}

void
read_from(
	oess_1::io::ibinstream_t & s )
{
	oess_1::tlv::default_tag_processor_t< tag_type_t > tag;

	while( !s.eof() )
	{
		s >> tag;
		std::cout << "Tag: " << tag.query_tag() << std::endl;
		my_compound_tlv_t t;
		t.tlv_unpack( s, tag.query_tag() );

		std::cout << "Value: " << t << std::endl;
	}
}

void
do_test()
{
	oess_1::io::mem_buf_t buf;

	write_to( buf );

	std::cout << "buf size: " << buf.size() << std::endl;
	buf.set_pos( 0 );

	read_from( buf );
}

int
main( int, char ** )
{
	try
	{
		do_test();
	}
	catch( std::exception & x )
	{
		std::cerr << "caught: " << x.what() << std::endl;
	}

	return 0;
}

