/*==================================================================
=  文件名  :
=  主要功能: 提供对指针类型数据的动态链表操作          
=  修改日期: 2005-10                                            
=  作者    : shen beide
====================================================================*/

#if !defined(_OBLIST_H)
#define _OBLIST_H

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

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

///////////////////////////////////////
class DLNode_
{
private:
    void*       p_data; 
    DWORD       wparam;
    DLNode_*    p_next;      
    DLNode_*    p_prev;      
    
public:
    DLNode_();
    DLNode_(void* data,DWORD param);

    inline void  setNULL();

    inline void* getdata(DWORD* lpParam=NULL);
    inline void  setdata(void* data,DWORD param); 

    inline DLNode_* next(void) const; 
    inline DLNode_* prev(void) const; 
    
    inline void  setnext(DLNode_* next); 
    inline void  setprev(DLNode_* prev); 
};

///////////////////////////////////////
///////////////////////////////////////
#ifdef _DLL_PROJECT
class CLASS_EXPORT ObList: public TName
#else
class ObList: public TName
#endif
{
private:
    DLNode_*      p_head;        //First node in the list. 
    DLNode_*      p_tail;        //Last  node in the list. 
    long          nodenum;

    unsigned long poolsize;
    DLNode_*      p_idlehead;    //First idlenode in the list. 
    long          idlenodenum;
    
public:
    ObList();
   ~ObList(); 

    void  Setpoolsize(unsigned long default_poolsize=0); 
                      // poolsize=0 时, 直接使用系统 new, delete 
                      // poolsize>0 时, 采用优化静态内存分配, 可以提高分配效率
    long  GetCount();
    bool  IsEmpty();  
       
    void  AddHead(void* data, DWORD param=0);   //!! 添加时指针是否重复不做检查
    void  AddTail(void* data, DWORD param=0);

    void* GetHead(DWORD* lpParam=NULL);
    void* GetTail(DWORD* lpParam=NULL);
    void* GetAt(long nIndex,DWORD* lpParam=NULL);

    void* RemoveHead(DWORD* lpParam=NULL);
    void* RemoveTail(DWORD* lpParam=NULL);
    void  RemoveAll();

    OTSTR ShowAllListNode();
        
private:
    inline DLNode_* GetHeadNode(void) const ;
    inline DLNode_* GetTailNode(void) const ;

    inline DLNode_* MallocNode(void* data, DWORD param=0);
    inline void     FreeNode(DLNode_* pNode);
};

///////////////////////////////////////////////////
///////////////////////////////////////////////////

#ifdef _DLL_PROJECT
template<class TYPE>
class CLASS_EXPORT TObList: public ObList
#else
template<class TYPE>
class TObList: public ObList
#endif
{
public:
    TObList()  { };
    
    //////////////////////////////////////
   ~TObList()  { };
    
    //////////////////////////////////////
    TYPE* GetHead(DWORD* lpParam=NULL)
    {
        return (TYPE*)ObList::GetHead(lpParam);
    }
    TYPE* GetTail(DWORD* lpParam=NULL)
    {
        return (TYPE*)ObList::GetTail(lpParam);
    }
    TYPE* GetAt(long nIndex,DWORD* lpParam=NULL)
    {
        return (TYPE*)ObList::GetAt(nIndex,lpParam);
    }
    
    TYPE* RemoveHead(DWORD* lpParam=NULL)
    {
        return (TYPE*)ObList::RemoveHead(lpParam);
    }
    TYPE* RemoveTail(DWORD* lpParam=NULL)
    {
        return (TYPE*)ObList::RemoveTail(lpParam);
    }
};


#endif






















