/*

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>

typedef oess_1::uchar_t tag_type_t;

void
write_to(
	oess_1::io::obinstream_t & s )
{
	oess_1::tlv::basic_string_tlv_t<
			tag_type_t, oess_1::char_t >
		hello( 1, "hello" );
	oess_1::tlv::basic_string_tlv_t<
			tag_type_t, oess_1::char_t >
		empty( 2 );

	s << hello << empty;
}

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;
		oess_1::tlv::basic_string_tlv_t<
				tag_type_t, oess_1::char_t >
			tlv_value( tag.query_tag() );
//		s >> oess_1::tlv::tlv_extractor_t( tag, tlv_value );
		oess_1::tlv::tlv_extractor_t extractor( tag, tlv_value );
		s >> extractor;

		std::cout << "Value: " << tlv_value.query_value() << 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;
}

