Created
January 26, 2019 07:20
-
-
Save superswanman/bea06d1e2a56e7a91c365520a86a3a9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit ForUsing; | |
interface | |
type | |
TUsing<T: class> = record | |
private | |
type | |
TEnumerator = class | |
private | |
FValue: T; | |
FUsed: Boolean; | |
public | |
constructor Create(const Value: T); | |
destructor Destroy; override; | |
function MoveNext: Boolean; | |
property Current: T read FValue; | |
end; | |
var | |
FValue: T; | |
public | |
function GetEnumerator: TEnumerator; | |
end; | |
TObjectHelper = class helper for TObject | |
public | |
class function Using<T: class>(const Value: T): TUsing<T>; static; | |
end; | |
implementation | |
{ TUsing<T> } | |
function TUsing<T>.GetEnumerator: TEnumerator; | |
begin | |
Result := TEnumerator.Create(FValue); | |
end; | |
{ TUsing<T>.TEnumerator } | |
constructor TUsing<T>.TEnumerator.Create(const Value: T); | |
begin | |
FValue := Value; | |
FUsed := False; | |
end; | |
destructor TUsing<T>.TEnumerator.Destroy; | |
begin | |
FValue.Free; | |
inherited; | |
end; | |
function TUsing<T>.TEnumerator.MoveNext: Boolean; | |
begin | |
Result := not FUsed; | |
FUsed := True; | |
end; | |
{ TObjectHelper } | |
class function TObjectHelper.Using<T>(const Value: T): TUsing<T>; | |
begin | |
Result.FValue := Value; | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment