Skip to content

pda0/AutoScope

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AutoScope

Have you tired of infinite try/finally of proper class handling like:

SomeObject1 := TSomeObject1.Create;
try
  SomeObject2 := TSomeObject2.Create;
  try
    SomeObject3 := TSomeObject3.Create;
    try
      <...>
    finally
      SomeObject3.Free;
    end;
  finally
    SomeObject2.Free;
  end;
finally
  SomeObject1.Free;
end;

or slightly more smart but still annoying:

  SomeObject1 := nil;
  SomeObject2 := nil;
  SomeObject3 := nil;
  try
    SomeObject1 := TSomeObject1.Create;
    SomeObject2 := TSomeObject2.Create;
    SomeObject3 := TSomeObject3.Create;
    <...>
  finally
    SomeObject3.Free;
    SomeObject2.Free;
    SomeObject1.Free;
  end;

Then try AutoScope. With it you can write like this:

uses
  AutoScope,<...>;
<...>
procedure SomeProc;
var
  Scoped: TScoped;
  <...>
begin
  SomeObject1 := Scoped[TSomeObject1.Create] as TSomeObject1;
  SomeObject2 := Scoped[TSomeObject2.Create] as TSomeObject2;
  SomeObject3 := Scoped[TSomeObject3.Create] as TSomeObject3;
  <...>
end;

That's it! At the end an implicit call of Scoped.Finalize happens and all of stored objects will freed in reverse order. AutoScope also can handle with raw memory block, allocated by GetMem/FreeMem functions. (But not with typed New/Dispose pointers.)

Compatible with Delphi 2007+ (may be with 2006+) and Free Pascal 2.6.0+.

About

Delphi/Free Pascal auto scope object

Resources

License

LGPL-2.1, Unknown licenses found

Licenses found

LGPL-2.1
LICENSE
Unknown
COPYING.FPC

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages