Skip to content

Instantly share code, notes, and snippets.

@freeonterminate
Last active August 29, 2015 14:17
Show Gist options
  • Save freeonterminate/ab292434fc3a57654c26 to your computer and use it in GitHub Desktop.
Save freeonterminate/ab292434fc3a57654c26 to your computer and use it in GitHub Desktop.
TMemo.Lines.Add を高速化する。
(*
* TMemoHelper
8 [Method List]
* FastAdd - Fast Add Method
*
* [How to use]
* 1. uses FMX.MemoHelper;
* 2. Memo1.FastAdd('something');
*)
unit FMX.MemoHelper;
interface
uses
FMX.Memo;
type
TMemoHelper = class helper for TMemo
public
function FastAdd(const iStr: String): Integer;
end;
implementation
uses
System.Classes
, System.Rtti
, FMX.Types
;
{ TMemoHelper }
function TMemoHelper.FastAdd(const iStr: String): Integer;
var
RttiType: TRttiType;
RttiField: TRttiField;
FLines: TObject;
FList: TValue;
Item: Pointer;
Added: Boolean;
begin
Added := False;
Result := -1;
RttiType := SharedContext.GetType(ClassType);
if (RttiType <> nil) then
begin
RttiField := RttiType.GetField('FLines');
if (RttiField <> nil) then
begin
FLines := RttiField.GetValue(Self).AsObject;
if (FLines <> nil) then
begin
RttiType := SharedContext.GetType(FLines.ClassType);
RttiField := RttiType.GetField('FList');
if (RttiField <> nil) then
begin
FList := RttiField.GetValue(FLines);
Lines.BeginUpdate;
try
Result := Lines.Add('');
Item := FList.GetReferenceToRawArrayElement(Result);
if (Item <> nil) then
PStringItem(Item)^.FString := iStr;
Added := True;
finally
Lines.EndUpdate
end;
end;
end;
end;
end;
if (not Added) then
Result := Lines.Add(iStr);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment