/*******************************************************************************
 *
 *	Copyright (C) 2008 ITS Co., Ltd. All rights reserved.
 *
 *	SubSystem Name : 
 *	Initial Author : hsahn
 *	Description:
 ******************************************************************************/

#ifndef __PTR_ARRAY_H__
#define __PTR_ARRAY_H__

#include "cstring.h"
#include "exception.h"

class V2_CLASS_API CPtrArrayItem
{
public:
	virtual ~CPtrArrayItem() {}
};


typedef CPtrArrayItem* T_CPtrArrayItemPtr;

class V2_CLASS_API CPtrArray
{
private:
	T_CPtrArrayItemPtr *m_pData;
	int m_iSize;
	int m_iBufferSize;
	bool m_bAdopt;

public:
	CPtrArray(bool p_bAdopt=true);
	CPtrArray(int p_iSize, CPtrArrayItem *p_pDefVal=NULL, bool p_bAdopt=true);
	~CPtrArray();
public:
	void Clear();	// data clear but don't delete data buffer

	int Add(CPtrArrayItem *p_pNewData);
	int GetSize() const { return m_iSize; }
	void Reset(int p_iSize, CPtrArrayItem *p_pDefVal);

	CPtrArrayItem *GetAt(int p_iIndex) const;
	void SetAt(int p_iIndex, CPtrArrayItem *p_pNewData);
	CPtrArrayItem *GetAtLast(void) const;
	int InsertAt(int p_iIndex, CPtrArrayItem *p_pNewData);

	bool IsEmpty(void) const { return (m_iSize == 0) ? true : false; }
	bool IsIn(CPtrArrayItem *p_pPtr) const;
	bool IsValid(void) const { return (m_iSize > 0) ? true : false; }
	CString Repr(const char *p_szSep=", ", bool p_bInfo=false) const;

	int RemoveAt(int p_iIndex);
	void RemoveLast(void);
	void RemoveAll();	// data clear and delete data buffer

	void SetAdoptFlag(bool p_bAdopt) { m_bAdopt = p_bAdopt; }
	bool GetAdoptFlag() { return m_bAdopt; }

	//T_CPtrArrayItemPtr &operator[](int p_iIndex) const; // prohibit from using lvalue
	T_CPtrArrayItemPtr operator[](int p_iIndex) const;	// inline

	CPtrArray &operator=(const CPtrArray &p_Obj);
private:
	void GrowBuffer(int p_iSize = 0);

	/* prohibit CPtrarray instance from being copied. if this is called, occurs error */
	CPtrArray(CPtrArray &p_Obj);
};




#endif	/* __PTR_ARRAY_H__ */
