龙仪的家

导航

<2006年7月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

随笔分类

文章分类

收藏夹

随笔档案

文章档案

统计

我的常用网址

一个错误引发N小时的调试-看看你能否找出错误


简单介绍一下,这个类实现的是一个双索引的MAP方式

///////////////////////////////////////////////H///////////////////////////////////////////

#include <string>
#include <map>
#include "Lg_SyncLock.h"
#include "lg_guid.h"
using namespace std;
namespace lglib
{
 enum GetFileResult
 {
  AllHaveGet = 0,
  GetOk,
  ListEmpty,
  CanNotFindById
 };
 class FileCell
 {
 public:
  FileCell(string fn,lg_AtomicCounter count)
  {
   m_filename = fn;
   m_id = count;
   nFlag = 0;
  }
  ~FileCell(){}
  GetFileResult GetFileName(TransType type,string &fn)
  {
   nFlag |= type;
   fn = m_filename;
   if(nFlag & (TP_TxtInfo|TP_PlateInfo|TP_ImageInfo))
   {
    return AllHaveGet;
   }
   return GetOk;
  }
  void replace(string fn)
  {
   m_filename = fn;
  }
  int32 GetFlag()
  {
   return nFlag;
  }
  int32 GetId()
  {
   return m_id;
  }
 private:
  string m_filename;
  int32 m_id;
  int32 nFlag;
 };
 class lg_FileList 
 {
 public:
  lg_FileList();
  virtual ~lg_FileList();

  BOOL push(string fn);
  BOOL replace(string newone,string oldone);
  BOOL DeleteCell(string fn);
  void ClearAllCell();
  GetFileResult GetString(TransType type,string & fn);
 private:
  uint32 dwPos[TP_EndPos];
  map<string,FileCell*> m_StringMap;
  typedef map<string,FileCell*>::iterator SMapIt;
  map<long,FileCell*> m_LongMap;
  typedef map<int32,FileCell*>::iterator LMapIt;
  lg_Mutex m_Mutex;
  lg_AtomicCounter m_Counter;
 };
}

////////////////////////////////////////////////////////CPP///////////////////////////////////////////////////////
namespace lglib
{

 lg_FileList::lg_FileList()
 {
  memset(dwPos,0,sizeof(dwPos)*TP_EndPos); 
  }

 lg_FileList::~lg_FileList()
 {
  //ClearAllCell();
 }
 BOOL lg_FileList::push(string fn)
 {
  m_Mutex.lock();
  SMapIt mapit = m_StringMap.find(fn);
  if(mapit == m_StringMap.end())
  {
   FileCell* p = new FileCell(fn,m_Counter);
   ++m_Counter;
   m_StringMap[fn] = p;
   m_LongMap[p->GetId()] = p;
   m_Mutex.unlock();
   return TRUE;
  }
  m_Mutex.unlock();
  return FALSE;
 }
 BOOL lg_FileList::replace(string newone,string oldone)
 {
  m_Mutex.lock();
  SMapIt mapit = m_StringMap.find(oldone);
  if(mapit != m_StringMap.end())
  {
   FileCell* p = (*mapit).second;

   //必须保证 p有效
   ASSERT(p != NULL);

   m_StringMap.erase(mapit);
   p->replace(newone);
   m_StringMap[newone] = p;
   m_Mutex.unlock();
   return TRUE;
  }
  m_Mutex.unlock();
  return FALSE;
 }
 BOOL lg_FileList::DeleteCell(string fn)
 {
  m_Mutex.lock();
  SMapIt it = m_StringMap.find(fn);
  if(it != m_StringMap.end())
  {
   FileCell* p = (*it).second;

   //必须保证 p有效
   ASSERT(p != NULL);

   m_StringMap.erase(it);
   LMapIt lMapit = m_LongMap.find(p->GetId());
   if(lMapit != m_LongMap.end())
   {
    m_LongMap.erase(lMapit);
   }
   delete p;
   m_Mutex.unlock();
   return TRUE;
  }
  m_Mutex.unlock();
  return FALSE;
 }
 void lg_FileList::ClearAllCell()
 {
  m_Mutex.lock();
  if(m_StringMap.size() > 0){
   for(SMapIt mapit = m_StringMap.begin();mapit != m_StringMap.end();mapit++){
    delete ((*mapit).second);
   }
   m_StringMap.clear();
  }
  if(m_LongMap.size()>0)
   m_LongMap.clear();
  m_Mutex.unlock();
 }


 GetFileResult lg_FileList::GetString(TransType type,string & fn)
 {
  if(m_StringMap.size() <= 0||m_LongMap.size()<=0)
   return ListEmpty;

  LMapIt it = m_LongMap.find(dwPos[type]);

  dwPos[type]++;
  
  if(it != m_LongMap.end())
  {
   FileCell* p = (*it).second;

   //必须保证 p有效
   ASSERT(p != NULL);

   return p->GetFileName(type,fn);
  }
  return CanNotFindById;
 }
}

posted on 2006-07-09 02:25 龙仪 阅读(1709) 评论(4)  编辑 收藏

评论

# re: 一个错误引发N小时的调试-看看你能否找出错误 2006-07-09 04:50 panic

lg_FileList::GetString的实现很奇怪。。。

# 细节补充 2006-07-09 12:41 龙仪

enum TransType
{
TP_TxtInfo = 0x0001,
TP_PlateInfo = 0x0002,
TP_ImageInfo = 0x0004,
TP_EndPos
};
dwPos[type];其中dwPos[0]dwPos[3]没有用到,这么做是为了保持代码的简洁

# re: 一个错误引发N小时的调试-看看你能否找出错误 2006-07-12 16:03 ilovevc

memset(dwPos,0,sizeof(dwPos)*TP_EndPos); 
看到这里就知道错了

# 好眼力 2006-07-25 10:23 龙仪

自己写的错误就比较难找

标题  
姓名  
主页
验证码 *
内容   
  登录  使用高级评论  Top
[使用Ctrl+Enter键可以直接提交]