/*  $Id: netvariables_generic.h,v 1.3 2003/10/14 15:52:13 mbn Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library 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.  See the GNU
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
**
*/

#ifndef header_netvariables_generic
#define header_netvariables_generic

#if _MSC_VER > 1000
#pragma once
#endif

#include <list>
#include "API/Core/IOData/inputsource.h"
#include "API/Core/IOData/outputsource.h"

class CL_NetVariable
{
public:
	virtual ~CL_NetVariable() { return; }
	virtual CL_NetVariable *clone()=0;
	virtual void save(CL_OutputSource *output)=0;
	virtual void load(CL_InputSource *input)=0;
	virtual bool is_different()=0;
};

class CL_NetVariables_Generic
{
public:
	std::list<CL_NetVariable *> vars;
};

template<class T>
class CL_NetVariableT : public CL_NetVariable
{
public:
	CL_NetVariableT(T *var, int array, bool do_endian_swap = true)
	: var(var), array(array), do_endian_swap(do_endian_swap)
	{
		old = new T[array];
		memcpy(old, var, sizeof(T)*array);
	}

	virtual ~CL_NetVariableT()
	{
		delete[] old;
	}

	virtual CL_NetVariable *clone()
	{
		return new CL_NetVariableT<T>(var, array, do_endian_swap);
	}

	virtual void save(CL_OutputSource *output)
	{
		// todo: if (do_endian_swap)
		output->write(var, sizeof(T)*array);
		memcpy(old, var, sizeof(T)*array);
	}

	virtual void load(CL_InputSource *input)
	{
		input->read(var, sizeof(T)*array);
		// todo: if (do_endian_swap)
	}

	virtual bool is_different()
	{
		for (int i=0; i<array; i++) if (var[i] != old[i]) return true;
		return false;
	}

private:
	T *var, *old;
	int array;
	bool do_endian_swap;
};

#endif
