diff --git a/.gitattributes b/.gitattributes index c546aa75fe..fdc9ec680b 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3168,6 +3168,7 @@ components/lazutils/lazloggerbase.pas svneol=native#text/pascal components/lazutils/lazloggerdummy.pas svneol=native#text/pascal components/lazutils/lazloggerprofiling.pas svneol=native#text/pascal components/lazutils/lazmethodlist.pas svneol=native#text/pascal +components/lazutils/lazpasreadutil.pas svneol=native#text/plain components/lazutils/lazunicode.pas svneol=native#text/plain components/lazutils/lazutf16.pas svneol=native#text/pascal components/lazutils/lazutf8.pas svneol=native#text/pascal diff --git a/components/codetools/tests/testcompreaderwriterpas.pas b/components/codetools/tests/testcompreaderwriterpas.pas index 3dbfd8b89c..770c4106db 100644 --- a/components/codetools/tests/testcompreaderwriterpas.pas +++ b/components/codetools/tests/testcompreaderwriterpas.pas @@ -3,32 +3,7 @@ ./runtests --format=plain --suite=TTestCompReaderWriterPas ./runtests --format=plain --suite=TTestCompReaderWriterPas.TestBaseTypesMaxValues -Working: -- boolean, set of boolean -- char, widechar, custom char, set of custom char -- integers, custom int, set of custom int -- strings, codepage system and UTF8 -- float, currency -- enum, custom enum range -- set of enum, set of custom enum range -- variant: integers, boolean, string, floats, currency -- method -- persistent -- component children, use SetParentComponent or optional Parent:= -- collection -- IInterfaceComponentReference -- with ancestor -- ancestor: change ComponentIndex -> call SetChildPos -- reference foreign root, reference foreign component -- create components before setting properties to avoid having to set references - later -- inline component, csInline, call SetInline, inherited inline, inline on inherited -- TComponent.Left/Right via DesignInfo -- DefineProperties - ToDo: -- OnWriteMethodProperty -- OnWriteStringProperty - RegisterPascalProperties(aClass,@); - enum: add unit, avoid nameclash with-do - custom integer TColor, add unit, avoid nameclash with-do @@ -52,8 +27,8 @@ unit TestCompReaderWriterPas; interface uses - Classes, SysUtils, typinfo, LazLoggerBase, LazUTF8, LazLogger, - CompWriterPas, fpcunit, testregistry, CodeToolManager, LinkScanner, + Classes, SysUtils, typinfo, LazLoggerBase, LazUTF8, LazLogger, CompWriterPas, + LazPasReadUtil, fpcunit, testregistry, CodeToolManager, LinkScanner, CodeToolsStructs, CodeCache, BasicCodeTools, TestStdCodetools, TestGlobals, variants; @@ -1985,47 +1960,6 @@ begin end; end; -type - - { TCSPReader } - - TCSPReader = class(TReader) - public - procedure ReadProperties(Instance: TPersistent); - end; - -{ TCSPReader } - -procedure TCSPReader.ReadProperties(Instance: TPersistent); -begin - while not EndOfList do - ReadProperty(Instance); -end; - -procedure ExecCustomLFM(Instance: TPersistent; const Data: array of string); -var - MemStream: TMemoryStream; - i: Integer; - s: String; - Reader: TCSPReader; -begin - MemStream:=TMemoryStream.Create; - Reader:=nil; - try - for i:=low(Data) to High(Data) do - begin - s:=Data[i]; - MemStream.Write(s[1],length(s)); - end; - MemStream.Position:=0; - Reader:=TCSPReader.Create(MemStream,1024); - Reader.ReadProperties(Instance); - finally - Reader.Free; - MemStream.Free; - end; -end; - procedure TTestCompReaderWriterPas.TestDefineProperites_ListOfStrings; var ARoot: TSimpleControlWithStrings; @@ -2042,11 +1976,11 @@ begin end; Expected:=#7'Strings'#1#6#5'First'#6#6'Second'#0#0; TestWriteDescendant('TestDefineProperites_ListOfStrings',ARoot,nil,[ - CSPDefaultExecCustomLFM+'(Lines,[#7''Strings''#1#6#5''First''#6#6''Second''#0#0]);', + CSPDefaultExecCustomCSP+'(Lines,[#7''Strings''#1#6#5''First''#6#6''Second''#0#0]);', '']); Lines2:=TStringList.Create; - ExecCustomLFM(Lines2,[Expected]); + ExecCustomCSP(Lines2,[Expected]); AssertEquals('read TStrings.Text',ARoot.Lines.Text,Lines2.Text); finally diff --git a/components/lazutils/compwriterpas.pas b/components/lazutils/compwriterpas.pas index 054a8d5b55..f8b7585bff 100644 --- a/components/lazutils/compwriterpas.pas +++ b/components/lazutils/compwriterpas.pas @@ -6,28 +6,35 @@ for details about the license. ***************************************************************************** - Component serialisation drivers for pascal. +Component serialisation into Pascal. - Works: - - simple properties: integer, strings, events, ... - - nested components (e.g. the child controls of a form) - - class properties (e.g. TControl.Font) +Author: Mattias Gaertner - ToDo: - - TCollection needs a typecast to the item class - - variants - - widestrings need special encoding conversions, but the driver does not - know, that a widestring is assigned - - what to do with DefineProperties? - - the 'with' can conflict - - circle dependencies: - Edit1:=TEdit.Create(Form1); - Edit1.AnchorSide[akLeft].Control:=Label1; - Label1:=TLabel.Create(Form1); - Label1.AnchorSide[akTop].Control:=Edit1; - - ChildPos - - Flags - - a reader +Working: +- boolean, set of boolean +- char, widechar, custom char, set of custom char +- integers, custom int, set of custom int +- strings, codepage system and UTF8 +- float, currency +- enum, custom enum range +- set of enum, set of custom enum range +- variant: integers, boolean, string, floats, currency +- method +- persistent +- component children, use SetParentComponent or optional Parent:= +- collection +- IInterfaceComponentReference +- with ancestor +- ancestor: change ComponentIndex -> call SetChildPos +- reference foreign root, reference foreign component +- create components before setting properties to avoid having to set references + later +- inline component, csInline, call SetInline, inherited inline, inline on inherited +- TComponent.Left/Right via DesignInfo +- DefineProperties + +ToDo: +- RegisterPascalProperties(aClass,@); } unit CompWriterPas; @@ -47,7 +54,7 @@ const CSPDefaultSignatureBegin = CSPDefaultSignature+' - Begin'; CSPDefaultSignatureEnd = CSPDefaultSignature+' - End'; CSPDefaultAccessClass = 'TPasStreamAccess'; - CSPDefaultExecCustomLFM = 'ExecCustomLFM'; + CSPDefaultExecCustomCSP = 'ExecCustomCSP'; CSPDefaultMaxColumn = 80; CWPSkipParentName = '-'; type @@ -1255,7 +1262,7 @@ begin FAssignOp:=':='; FSignature:=CSPDefaultSignature; FMaxColumn:=CSPDefaultMaxColumn; - FExecCustomData:=CSPDefaultExecCustomLFM; + FExecCustomData:=CSPDefaultExecCustomCSP; FAccessClass:=CSPDefaultAccessClass; C:=TAccessComp.Create(nil); FDefaultDefineProperties:=TMethod(@C.DefineProperties).Code; diff --git a/components/lazutils/lazpasreadutil.pas b/components/lazutils/lazpasreadutil.pas new file mode 100644 index 0000000000..b125869eb5 --- /dev/null +++ b/components/lazutils/lazpasreadutil.pas @@ -0,0 +1,71 @@ +{ + ***************************************************************************** + This file is part of the Lazarus Component Library (LCL) + + See the file COPYING.modifiedLGPL.txt, included in this distribution, + for details about the license. + ***************************************************************************** + + Helper functions for component serialized Pascal. + + Author: Mattias Gaertner +} +unit LazPasReadUtil; + +{$mode objfpc}{$H+} + +interface + +uses + Classes; + +{ ExecCustomCSP: Call Instance.DefineProperties with a list of properties in + TBinaryObjectWriter format. This function is used by the auto generated + Pascal of TCompWriterPas for custom DefineProperties. } +procedure ExecCustomCSP(Instance: TPersistent; const Data: array of string); + +implementation + +type + + { TCSPReader } + + TCSPReader = class(TReader) + public + procedure ReadProperties(Instance: TPersistent); + end; + +{ TCSPReader } + +procedure TCSPReader.ReadProperties(Instance: TPersistent); +begin + while not EndOfList do + ReadProperty(Instance); +end; + +procedure ExecCustomCSP(Instance: TPersistent; const Data: array of string); +var + MemStream: TMemoryStream; + i: Integer; + s: String; + Reader: TCSPReader; +begin + MemStream:=TMemoryStream.Create; + Reader:=nil; + try + for i:=low(Data) to High(Data) do + begin + s:=Data[i]; + MemStream.Write(s[1],length(s)); + end; + MemStream.Position:=0; + Reader:=TCSPReader.Create(MemStream,1024); + Reader.ReadProperties(Instance); + finally + Reader.Free; + MemStream.Free; + end; +end; + +end. + diff --git a/components/lazutils/lazutils.lpk b/components/lazutils/lazutils.lpk index 39e5e04122..ee31c094d5 100644 --- a/components/lazutils/lazutils.lpk +++ b/components/lazutils/lazutils.lpk @@ -16,7 +16,7 @@ - + @@ -391,8 +391,12 @@ - + + + + + diff --git a/components/lazutils/lazutils.pas b/components/lazutils/lazutils.pas index 33729974a6..80caefcef4 100644 --- a/components/lazutils/lazutils.pas +++ b/components/lazutils/lazutils.pas @@ -21,7 +21,7 @@ uses StringHashList, TextStrings, Translations, TTCache, TTCalc, TTCMap, TTDebug, TTError, TTFile, TTGLoad, TTInterp, TTLoad, TTMemory, TTObjs, TTProfile, TTRASTER, TTTables, TTTypes, UTF8Process, HTML2TextRender, Laz_AVL_Tree, - compwriterpas, LazarusPackageIntf; + CompWriterPas, LazPasReadUtil, LazarusPackageIntf; implementation