Giter Site home page Giter Site logo

autoscope's Introduction

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+.

autoscope's People

Contributors

pda0 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.