/*	XGSizeArray.cpp
 *
 *		This is the code to deal with objects which require no special
 *	assistance
 */

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

  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

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

#include <XConfig.h>			// must be first to optimize compile on MSVC++
#include <string.h>
#include <XDynArray.h>

/************************************************************************/
/*																		*/
/*	Construction/Destruction											*/
/*																		*/
/************************************************************************/

/*$	XGSizeArray::XGSizeArray
 *
 *		Creates an empty array of memory. As we know any templates based
 *	on me can use the memory copy operation and don't have destructors,
 *	we can freely trash the array at will.
 */

XGSizeArray::XGSizeArray(size_t s)
{
	fArray = NULL;
	fSize = s;
}

/*$	XGSizeArray::~XGSizeArray
 *
 *		Deletes me and releases the resources without calling any item
 *	destructors.
 */

XGSizeArray::~XGSizeArray()
{
	if (fArray) delete[] fArray;
}

/************************************************************************/
/*																		*/
/*	Copy operations														*/
/*																		*/
/************************************************************************/

/*$	XGSizeArray::ChangeArray
 *
 *		Change the size of this array. This method simply reallocates
 *	the array and performs a simple memory copy operation to copy the
 *	contents across.
 */

void XGSizeArray::ChangeArray(long newsize, long copy)
{
	unsigned char *array;
	
	if (newsize) array = new unsigned char[newsize * fSize];
	else array = NULL;
	if (copy) memcpy(array,fArray,fSize * copy);
	if (fArray) delete[] fArray;
	fArray = array;
}

/*$	XGSizeArray::CopyArray
 *
 *		Copy array elements. This copies the entire array of items
 *	from one location to another in one memcpy() call.
 */

void XGSizeArray::CopyArray(long dest, long src)
{
	memcpy(fArray + dest * fSize, fArray + src * fSize, fSize);
}

/*$	XGSizeArray::CopyElement
 *
 *		Copy a single element. This simply copies the data across.
 */

void XGSizeArray::CopyElement(long dest, const void *src)
{
	memcpy(fArray + dest * fSize, src, fSize);
}

/*$	XGSizeArray::GetElement
 *
 *		Get an element. This returns a pointer to a single element.
 */

void *XGSizeArray::GetElement(long i) const
{
	return (void *)(fArray + i * fSize);
}

