/*==================================================================
=  文件名  : CObArray_类定义文件                                       
=  主要功能: 提供对指针类型数组操作          
=  修改日期: 2005-10                                           
=  作者    : shen beide
====================================================================*/

#if !defined(_OBARRAY_H_)
#define _OBARRAY_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "PubHeader.h"
#include "Base.h"


////////////////////////////////
class CNode
{
protected:
    void*   pData;
    DWORD   wparam;

public:
    CNode() {  pData=NULL; wparam=0; }
   ~CNode() {}

    inline void  set(void *data,DWORD param) 
    { 
        pData=data; wparam=param; 
    }

    inline void* get(DWORD* lpParam=NULL)
    { 
        if (lpParam) *lpParam=wparam;
        return pData; 
    }
    inline void*& _get()
    { 
        return pData; 
    }
    
    inline DWORD& getparam()
    { 
        return wparam; 
    }
};

///////////////////////////////////////////////////
#ifdef _DLL_PROJECT
class CLASS_EXPORT ObArray: public TName
#else
class ObArray: public TName
#endif
{
private:
    int	    m_cur_array_size;
    CNode*  m_array;

    int     m_block_size;
    int     m_max_array_size;

public:
    ObArray(int block_size=25);     // when realloc array,add member_Size's memory
    virtual ~ObArray();

    int    GetSize();
    void   SetSize(int num);        // setsize and set all member NULL
    void   SetBlockSize(int block_size);

    void   RemoveAll();

    int    Add(void *pdata, DWORD param=0); // return current arraysize  //!! 添加时指针是否重复不做检查
    int    InsertAt(int nIndex, void *pdata, DWORD param=0);

    void*  GetAt(int nIndex, DWORD* lpParam=NULL);
    void*  GetTail(DWORD* lpParam=NULL);
    bool   SetAt(int nIndex, void*  pdata, DWORD param=0);
    void*& operator [](int nIndex);         // 直接操作内部, 用于[]=pdata;
    
    int    Find(void *pdata);

    void*  RemoveAt(int nIndex, DWORD* lpParam=NULL);
    void*  RemoveTail(DWORD* lpParam=NULL);
    
protected:
    void   initial_();
    void   ReSizeArray(int mode=1);

    void   GetNode(int nIndex);
    
    CNode* Add_(void *pdata,DWORD param);
    CNode* GetAt_(int nIndex);

    void*  m_pErrData;
    
    friend class StringArray;
};

//////////////////////////////////////////////////
#ifdef _DLL_PROJECT
template <class TYPE>
class CLASS_EXPORT TObArray: public ObArray
#else
template <class TYPE>
class TObArray: public ObArray
#endif
{
public:
    int Add(TYPE *pdata, DWORD param=0)
    {
        return ObArray::Add(pdata,param);
    }
    int InsertAt(int nIndex, TYPE *pdata, DWORD param=0)
    {
        return ObArray::InsertAt(nIndex,pdata,param);
    }
    TYPE* GetAt(int nIndex, DWORD* lpParam=NULL)
    {
        return (TYPE*)ObArray::GetAt(nIndex,lpParam);;        
    }
    TYPE*& operator[](int nIndex)
    { 
        return (TYPE*&)ObArray::operator[](nIndex); 
    }
    TYPE* GetTail(DWORD* lpParam=NULL)
    {
        return (TYPE*)ObArray::GetTail(lpParam);;        
    }
    TYPE* RemoveAt(int nIndex, DWORD* lpParam=NULL)
    {
        return (TYPE*)ObArray::RemoveAt(nIndex,lpParam);        
    }
    TYPE* RemoveTail(DWORD* lpParam=NULL)
    {
        return (TYPE*)ObArray::RemoveTail(lpParam);        
    }
};


#endif
