안녕하세요.. 간만에 들려서 질문을 하네요.. 요즘 TList 에대해서 고민하고 있습니다.
TList 를 사용하면 자료들을 LinkedList 형태로 관리할 수 가 있죠..
이놈이 Data 들을 void* 로 처리하기 때문에 여러가지 문제가 생기나 봅니다.
그래서 고민끝에..
http://www.bcbdev.com 에서 TTypedList 를 구했습니다. TList 를 계승해서
자기가 관리하는 class Type 을 알고 있는 List 입니다.
그런데 이놈을 사용하는 도중 IndexOf() 만 제대로 작동을 안합니다. 그래서 이것에 대한 질문을 드립니다. (저의 프로젝트를 올려 놓고 싶어도 올려놓기 기능이 없는것 같네요...)
1. 먼저 구한 TTypedList 입니다.
2. 제가 사용하는 클래스(TSample) 입니다.
3. IndexOf() Method 를 사용하는 곳입니다.
//------------------------------------------------------------------------
3. IndexOf() Method 를 사용하는 곳입니다. 어떻게 하면 올바른 작동을 하게 할 수 있나요?
//------------------------------------------------------------------------
void __fastcall TForm1::Button20Click(TObject *Sender)
{
SampleList->Add(new TSample);
SampleList->Items[0]->I = 100;
SampleList->Items[0]->C = 'c';
TSample *Temp = new TSample;
int indexof;
Temp->I = 100;
Temp->C = 'c';
indexof = SampleList->IndexOf(Temp);
if(indexof >=0)
Memo1->Lines->Add("Index Number : " + IntToStr(indexof));
else
Memo1->Lines->Add("Index Number : Can't Find" + IntToStr(indexof));
delete Temp;
}
//------------------------------------------------------------------------
1. 먼저 구한 TTypedList 입니다. (좀더 자세한 내용은 출처(
http://www.bcbdev.com) 를 참고..
//------------------------------------------------------------------------
#ifndef TTYPEDLIST_H
#define TTYPEDLIST_H
#include <classes.hpp>
#include <fstream.h>
#include <mem.h>
template <class T>
class TTypedList : public TList
{
private:
bool bAutoDelete;
protected:
T* __fastcall Get(int Index)
{
return (T*) TList::Get(Index);
}
void __fastcall Put(int Index, T* Item)
{
TList::Put(Index,Item);
}
public:
__fastcall TTypedList(bool bFreeObjects = false)
:TList(),
bAutoDelete(bFreeObjects)
{
}
// Note: No destructor needed. TList::Destroy calls Clear,
// and Clear is virtual, so our Clear runs.
int __fastcall Add(T* Item)
{
return TList::Add(Item);
}
void __fastcall Delete(int Index)
{
if(bAutoDelete)
delete Get(Index);
TList::Delete(Index);
}
void __fastcall Clear(void)
{
if(bAutoDelete)
{
for (int j=0; j<Count; j++)
delete Items[j];
}
TList::Clear();
}
T* __fastcall First(void)
{
return (T*)TList::First();
}
int __fastcall IndexOf(T* Item)
{
return TList::IndexOf(Item);
}
void __fastcall Insert(int Index, T* Item)
{
TList::Insert(Index,Item);
}
T* __fastcall Last(void)
{
return (T*) TList::Last();
}
int __fastcall Remove(T* Item)
{
int nIndex = TList::Remove(Item);
// Should I delete a pointer that is being passed to me.
// If bAutoDelete is true, then assume that we are always
// responsible for deleting a pointer that is added to the
// list. If the item was found, then delete the pointer.
if(bAutoDelete && (nIndex != -1))
delete Item;
return nIndex;
}
bool __fastcall SaveToFile(AnsiString FileName)
{
// Save not List itself but Datum in List
FILE *fp;
if((fp = fopen(FileName.c_str(),"wb")) == NULL)
{
return false;
}
for(int i=0; i<this->Count; i++)
{
fwrite(this->Items[i], sizeof(T),1,fp);
}
fclose(fp);
return true;
}
bool __fastcall LoadFromFile(AnsiString FileName)
{
// Restore Only datum from file.
FILE *fp;
T t; // <- T means TSample.
int i=0;
int ListSize=0;
long curpos, length;
if((fp = fopen(FileName.c_str(),"rb")) == NULL)
{
return false;
}
fseek(fp,0,SEEK_SET); // move fp to front
curpos = ftell(fp);
fseek(fp, 0L, SEEK_END);
length = ftell(fp);
fseek(fp, curpos, SEEK_SET);
ListSize = length / sizeof(T);
for(i=0; i<ListSize; i++)
{
this->Add(new T);
fread(&t, sizeof(T),1,fp); // sizeof(T)만큼 Data 를 읽는다.
memcpy(this->Items[i],&t,sizeof(T));
}
fclose(fp);
return true;
}
__property T* Items[int Index] = {read=Get, write=Put};
};
#endif
//------------------------------------------------------------------------
2. 제가 사용하는 클래스(TSample) 입니다.
//------------------------------------------------------------------------
// header file
//---------------------------------------------------------------------------
#ifndef MyClassH
#define MyClassH
//---------------------------------------------------------------------------
class TSample {
public:
int I;
char C;
TSample();
TSample(TSample& x);
~TSample();
AnsiString printout(void);
int SaveToFile(AnsiString FileName);
int LoadFromFile(AnsiString FileName);
friend operator==(const TSample& LeftArg, const TSample& RightArg);
friend operator!=(const TSample& LeftArg, const TSample& RightArg);
};
#endif
// implementation
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MyClass.h"
#include <fstream.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
TSample::TSample()
{
I = -1;
C = NULL;
}
TSample::TSample(TSample& x)
{
I = x.I;
C = x.C;
}
TSample::~TSample()
{
//
}
AnsiString TSample:: printout(void)
{
AnsiString astOutputText;
astOutputText.sprintf("I = %d,C = %c",I,C);
return astOutputText;
}
int TSample::SaveToFile(AnsiString FileName)
{
fstream *ioFile = new fstream;
ioFile->open(FileName.c_str(), ios::out | ios::binary);
if(ioFile == NULL)
{
return -1;
}
ioFile->write((char *)this, sizeof(*this));
ioFile->close();
delete ioFile;
return 1;
}
int TSample::LoadFromFile(AnsiString FileName)
{
fstream *ioFile = new fstream;
ioFile->open(FileName.c_str(),ios::in | ios::binary);
if(ioFile == NULL)
{
return -1;
}
ioFile->seekg(0,ios::beg);
ioFile->read((char *)this, sizeof(*this));
ioFile->close();
delete ioFile;
return 1;
}
int operator==(const TSample& LeftArg, const TSample& RightArg)
{
return (
(LeftArg.I == RightArg.I) &&
(LeftArg.C == RightArg.C)
);
}
int operator!=(const TSample& LeftArg, const TSample& RightArg)
{
return !(
(LeftArg.I == RightArg.I) &&
(LeftArg.C == RightArg.C)
);
}