C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[4800] TList 에 대한 질문입니다. 도움좀 주세요..
향기 [] 3435 읽음    2001-01-08 10:38
안녕하세요.. 간만에 들려서 질문을 하네요.. 요즘 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)
    );
}


+ -

관련 글 리스트
4800 TList 에 대한 질문입니다. 도움좀 주세요.. 향기 3435 2001/01/08
4832     Re:TList 에 대한 질문입니다. 도움좀 주세요.. 조재영 3653 2001/01/09
4808     Re:TList 에 대한 질문입니다. 도움좀 주세요.. 한동훈 3439 2001/01/08
4817         Re:Re:답변 감사.. . 한번 더.. 향기 3194 2001/01/09
4845             Re:Re:Re:답변 감사.. . 한번 더.. 한동훈 3489 2001/01/10
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.