Recent

Author Topic: FHandle in THandleStream better protected  (Read 7656 times)

Borneq

  • Full Member
  • ***
  • Posts: 238
FHandle in THandleStream better protected
« on: December 27, 2009, 03:25:27 pm »
I write class TFileStreamW which is similar to TFileStream but argument filename is WideString insted of string
I want
constructor TFileStreamW.Create(const FileName: WideString; Mode: Word);
begin
  if Mode = fmCreate then
  begin
    inherited Create(FileCreateW(FileName));
    if FHandle < 0 then
      raise EFCreateError.CreateResFmt(@SFCreateError, [FileName]);
  end
  else
  begin
    inherited Create(FileOpenW(FileName, Mode));
    if FHandle < 0 then
      raise EFOpenError.CreateResFmt(@SFOpenError, [FileName]);
  end;
end;

I must use FHandle but it is private in classesh.inc
Better if it will protected

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2519
Re: FHandle in THandleStream better protected
« Reply #1 on: December 27, 2009, 05:16:30 pm »
why not checking the result of FileCreateW(FileName) ?
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Borneq

  • Full Member
  • ***
  • Posts: 238
Re: FHandle in THandleStream better protected
« Reply #2 on: December 27, 2009, 06:57:41 pm »
why not checking the result of FileCreateW(FileName) ?
Right!
Constructor will be:

constructor TFileStreamW.Create(const FileName: WideString; Mode: Word);
var
  Handle: Integer;
begin
  if Mode = fmCreate then
  begin
    Handle := FileCreateW(FileName);
    if Handle < 0 then
      raise EFCreateError.CreateResFmt(@SFCreateError, [FileName]);
  end
  else
  begin
    Handle := FileOpenW(FileName, Mode);
    if Handle < 0 then
      raise EFOpenError.CreateResFmt(@SFOpenError, [FileName]);
  end;
  inherited Create(Handle);
end;


Borneq

  • Full Member
  • ***
  • Posts: 238
Re: FHandle in THandleStream better protected
« Reply #3 on: December 27, 2009, 07:01:52 pm »
But..
How to close?

destructor TFileStreamW.Destroy;
begin
  if FHandle >= 0 then FileClose(FHandle);
  inherited Destroy;
end;

Must exists static variable indicating whether correctly open/create operation passed

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2519
Re: FHandle in THandleStream better protected
« Reply #4 on: December 27, 2009, 07:05:53 pm »
Wouldn't the inherited handle stream close it ?

(otherwise keep a ref yourself)
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Borneq

  • Full Member
  • ***
  • Posts: 238
Re: FHandle in THandleStream better protected
« Reply #5 on: December 27, 2009, 08:31:16 pm »
Wouldn't the inherited handle stream close it ?
Yes,I noticed after sending message
Thanks

 

TinyPortal © 2005-2018