/*!
	@project Rune
	@file RuHelper_StreamIO.h

	Copyright (c) 2004-2005 All rights reserved

	@author John Tang
	@date 2006/02/08
*/

#ifndef _RUHELPER_STREAMIO_H_
#define _RUHELPER_STREAMIO_H_

#include "../Core/Stream/RuStreamIO.h"
#include "../Core/Math/RuMathVector.h"

#pragma unmanaged

// ************************************************************************************************************************************************************

template <typename DataType>
BOOL SerializeFrom(IRuStream *stream, DataType *key)
{
	return (stream->Read(key, sizeof(DataType)) == sizeof(DataType));
}

template <typename DataType>
BOOL SerializeTo(IRuStream *stream, DataType *key)
{
	return (stream->Write(key, sizeof(DataType)) == ruSEC_OK);
}

// ************************************************************************************************************************************************************

template <>
inline BOOL SerializeFrom(IRuStream *stream, CRuColor4 *key)
{
	stream->Read(&key->m_a, sizeof(REAL));
	stream->Read(&key->m_r, sizeof(REAL));
	stream->Read(&key->m_g, sizeof(REAL));
	stream->Read(&key->m_b, sizeof(REAL));

	return TRUE;
}

template <>
inline BOOL SerializeTo(IRuStream *stream, CRuColor4 *key)
{
	stream->Write(&key->m_a, sizeof(REAL));
	stream->Write(&key->m_r, sizeof(REAL));
	stream->Write(&key->m_g, sizeof(REAL));
	stream->Write(&key->m_b, sizeof(REAL));

	return TRUE;
}

// ************************************************************************************************************************************************************

template <>
inline BOOL SerializeFrom(IRuStream *stream, CRuVector3 *key)
{
	stream->Read(&key->m_x, sizeof(REAL));
	stream->Read(&key->m_y, sizeof(REAL));
	stream->Read(&key->m_z, sizeof(REAL));

	return TRUE;
}

template <>
inline BOOL SerializeTo(IRuStream *stream, CRuVector3 *key)
{
	stream->Write(&key->m_x, sizeof(REAL));
	stream->Write(&key->m_y, sizeof(REAL));
	stream->Write(&key->m_z, sizeof(REAL));

	return TRUE;
}

// ************************************************************************************************************************************************************

template <>
inline BOOL SerializeFrom(IRuStream *stream, char **key)
{
	// Read key string length
	INT32 keyLength = 0;
	stream->Read(&keyLength, sizeof(INT32));

	// Read key string
	*key = ruNEW char [keyLength];
	stream->Read(*key, keyLength);

	return TRUE;
}

template <>
inline BOOL SerializeTo(IRuStream *stream, char **key)
{
	// Write key string length
	INT32 keyLength = static_cast<INT32>(strlen(*key)) + 1;
	stream->Write(&keyLength, sizeof(INT32));

	// Write key string
	stream->Write(*key, keyLength);

	return TRUE;
}

// ************************************************************************************************************************************************************

#pragma managed

#endif
