/** @file bytestream.cpp @headerfile bytestream.h "EABaseTypesPlus/bytestream.h"
*	Simple bytestream with support for Functions and UserData
*	@author Chris Burns (cburns@ea.com)
*
*	CTUI
*	(c) 2013 Electronic Arts Inc.
*/

#include "EABaseTypes/datatypes.h"
#include "EABaseTypesPlus/bytestream.h"
#include "EABaseTypesPlus/bytestreamencoder.h"

namespace EA
{
	namespace Types
	{
		class ReleaseEncoder
		{
		public:

			void Reset() {}

			void BeginArray() {}
			void EndArray() {}

			void BeginArrayValue(size_t index) {}
			void EndArrayValue(size_t index) {}

			void BeginObject() {}
			void EndObject() {}

			void BeginObjectValue(const char* key, size_t length) {}
			void EndObjectValue(const char* key, size_t length) {}

			void Boolean(bool boolean) {}
			void Number(BaseTypeFloat number) {}
			void String(const char* str, size_t length) {}
			void Function(EA::Types::FunctionIn function) { function->Release(); }
			void UserData(EA::Types::UserDataIn userData) { userData->Release(); }
			void Null() {}
		};
	
		ByteStream::ByteStream(EA::Allocator::ICoreAllocator* allocator):
			mStream(allocator) {}
			
		ByteStream::~ByteStream()
		{
			ReleaseEncoder encoder;
			ByteStreamDecoder<ReleaseEncoder>::Decode(&encoder, this);
		}
		
		void ByteStream::Write(unsigned char c)
		{
			mStream.push_back(c);
		}

		void ByteStream::Write(const unsigned char* c, size_t length)
		{
			mStream.insert(mStream.end(), c, c + length);
		}
		
		void ByteStream::Clear()
		{
			ReleaseEncoder encoder;
			ByteStreamDecoder<ReleaseEncoder>::Decode(&encoder, this);
			mStream.clear();
		}
	}
}
