classesh.inc 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532
  1. {
  2. This file is part of the Free Pascal Run Time Library (rtl)
  3. Copyright (c) 1999-2008 by Michael Van Canneyt, Florian Klaempfl,
  4. and Micha Nelissen
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$H+}
  12. {$ifdef CLASSESINLINE}{$inline on}{$endif}
  13. type
  14. { extra types to compile with FPC }
  15. HRSRC = TFPResourceHandle deprecated;
  16. TComponentName = string;
  17. THandle = System.THandle;
  18. TPoint=Types.TPoint;
  19. TRect=Types.TRect;
  20. TSmallPoint=Types.TSmallPoint;
  21. {$ifndef FPC_HAS_FEATURE_DYNLIBS}
  22. HMODULE = ptrint;
  23. {$else}
  24. HModule = System.HModule;
  25. {$endif}
  26. const
  27. { Maximum TList size }
  28. {$ifdef cpu16}
  29. MaxListSize = {Maxint div 16}1024;
  30. {$else cpu16}
  31. MaxListSize = Maxint div 16;
  32. {$endif cpu16}
  33. { values for TShortCut }
  34. scShift = $2000;
  35. scCtrl = $4000;
  36. scAlt = $8000;
  37. scNone = 0;
  38. { TStream seek origins }
  39. const
  40. soFromBeginning = 0;
  41. soFromCurrent = 1;
  42. soFromEnd = 2;
  43. type
  44. TSeekOrigin = (soBeginning, soCurrent, soEnd);
  45. TDuplicates = Types.TDuplicates;
  46. // For Delphi and backwards compatibility.
  47. const
  48. dupIgnore = Types.dupIgnore;
  49. dupAccept = Types.dupAccept;
  50. dupError = Types.dupError;
  51. { TFileStream create mode }
  52. const
  53. fmCreate = $FF00;
  54. fmOpenRead = 0;
  55. fmOpenWrite = 1;
  56. fmOpenReadWrite = 2;
  57. { TParser special tokens }
  58. toEOF = AnsiChar(0);
  59. toSymbol = AnsiChar(1);
  60. toString = AnsiChar(2);
  61. toInteger = AnsiChar(3);
  62. toFloat = AnsiChar(4);
  63. toWString = AnsiChar(5);
  64. Const
  65. FilerSignature : Array[1..4] of Ansichar = 'TPF0';
  66. FilerSignature1 : Array[1..4] of Ansichar = 'TPF1';
  67. type
  68. { Text alignment types }
  69. TAlignment = (taLeftJustify, taRightJustify, taCenter);
  70. TLeftRight = taLeftJustify..taRightJustify;
  71. TVerticalAlignment = (taAlignTop, taAlignBottom, taVerticalCenter);
  72. TTopBottom = taAlignTop..taAlignBottom;
  73. TBiDiMode = (bdLeftToRight,bdRightToLeft,bdRightToLeftNoAlign,bdRightToLeftReadingOnly);
  74. { Types used by standard events }
  75. TShiftStateEnum = (ssShift, ssAlt, ssCtrl,
  76. ssLeft, ssRight, ssMiddle, ssDouble,
  77. // Extra additions
  78. ssMeta, ssSuper, ssHyper, ssAltGr, ssCaps, ssNum,
  79. ssScroll,ssTriple,ssQuad,ssExtra1,ssExtra2,
  80. ssTouch, ssPen, ssHorizontal);
  81. {$packset 1}
  82. TShiftState = set of TShiftStateEnum;
  83. {$packset default}
  84. THelpContext = -MaxLongint..MaxLongint;
  85. THelpType = (htKeyword, htContext);
  86. TShortCut = Low(Word)..High(Word);
  87. { Standard events }
  88. TNotifyEvent = procedure(Sender: TObject) of object;
  89. THelpEvent = function (Command: Word; Data: Longint;
  90. var CallHelp: Boolean): Boolean of object;
  91. TGetStrProc = procedure(const S: string) of object;
  92. { Exception classes }
  93. EStreamError = class(Exception);
  94. EFCreateError = class(EStreamError);
  95. EFOpenError = class(EStreamError);
  96. EFilerError = class(EStreamError);
  97. EReadError = class(EFilerError);
  98. EWriteError = class(EFilerError);
  99. EClassNotFound = class(EFilerError);
  100. EMethodNotFound = class(EFilerError);
  101. EInvalidImage = class(EFilerError);
  102. EResNotFound = class(Exception);
  103. {$ifdef FPC_TESTGENERICS}
  104. EListError = fgl.EListError;
  105. {$else}
  106. EListError = class(Exception);
  107. {$endif}
  108. EBitsError = class(Exception);
  109. EStringListError = class(Exception);
  110. EComponentError = class(Exception);
  111. EParserError = class(Exception);
  112. EOutOfResources = class(EOutOfMemory);
  113. EInvalidOperation = class(Exception);
  114. TExceptionClass = Class of Exception;
  115. { ---------------------------------------------------------------------
  116. Free Pascal Observer support
  117. ---------------------------------------------------------------------}
  118. Const
  119. // Delphi compatibility
  120. ssCommand = ssMeta;
  121. SGUIDObserved = '{663C603C-3F3C-4CC5-823C-AC8079F979E5}';
  122. SGUIDObserver = '{BC7376EA-199C-4C2A-8684-F4805F0691CA}';
  123. Type
  124. // Notification operations :
  125. // Observer has changed, is freed, item added to/deleted from list, custom event.
  126. TFPObservedOperation = (ooChange,ooFree,ooAddItem,ooDeleteItem,ooCustom);
  127. {$INTERFACES CORBA}
  128. { IFPObserved }
  129. IFPObserved = Interface [SGUIDObserved]
  130. // attach a new observer
  131. Procedure FPOAttachObserver(AObserver : TObject);
  132. // Detach an observer
  133. Procedure FPODetachObserver(AObserver : TObject);
  134. // Notify all observers of a change.
  135. Procedure FPONotifyObservers(ASender : TObject; AOperation : TFPObservedOperation; Data : Pointer);
  136. end;
  137. { IFPObserver }
  138. IFPObserver = Interface [SGUIDObserver]
  139. // Called by observed when observers are notified.
  140. Procedure FPOObservedChanged(ASender : TObject; Operation : TFPObservedOperation; Data : Pointer);
  141. end;
  142. {$INTERFACES COM}
  143. EObserver = Class(Exception);
  144. { Forward class declarations }
  145. TStream = class;
  146. TFiler = class;
  147. TReader = class;
  148. TWriter = class;
  149. TComponent = class;
  150. { TFPList class }
  151. PPointerList = ^TPointerList;
  152. TPointerList = array[0..MaxListSize - 1] of Pointer;
  153. TListSortCompare = function (Item1, Item2: Pointer): Integer;
  154. TListCallback = Types.TListCallback;
  155. TListStaticCallback = Types.TListStaticCallback;
  156. {$IFNDEF FPC_TESTGENERICS}
  157. TListAssignOp = (laCopy, laAnd, laOr, laXor, laSrcUnique, laDestUnique);
  158. TFPList = class;
  159. TFPListEnumerator = class
  160. private
  161. FList: TFPList;
  162. FPosition: Integer;
  163. public
  164. constructor Create(AList: TFPList);
  165. function GetCurrent: Pointer;
  166. function MoveNext: Boolean;
  167. property Current: Pointer read GetCurrent;
  168. end;
  169. TFPList = class(TObject)
  170. private
  171. FList: PPointerList;
  172. FCount: Integer;
  173. FCapacity: Integer;
  174. procedure CopyMove (aList : TFPList);
  175. procedure MergeMove (aList : TFPList);
  176. procedure DoCopy(ListA, ListB : TFPList);
  177. procedure DoSrcUnique(ListA, ListB : TFPList);
  178. procedure DoAnd(ListA, ListB : TFPList);
  179. procedure DoDestUnique(ListA, ListB : TFPList);
  180. procedure DoOr(ListA, ListB : TFPList);
  181. procedure DoXOr(ListA, ListB : TFPList);
  182. protected
  183. function Get(Index: Integer): Pointer; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  184. procedure Put(Index: Integer; Item: Pointer); {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  185. procedure SetCapacity(NewCapacity: Integer);
  186. procedure SetCount(NewCount: Integer);
  187. Procedure RaiseIndexError(Index: Integer); deprecated;
  188. Procedure CheckIndex(AIndex : Integer); {$ifdef CLASSESINLINE} inline;{$ENDIF}
  189. public
  190. Type
  191. TDirection = (FromBeginning, FromEnd);
  192. destructor Destroy; override;
  193. Procedure AddList(AList : TFPList);
  194. function Add(Item: Pointer): Integer; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  195. procedure Clear;
  196. procedure Delete(Index: Integer); {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  197. class procedure Error(const Msg: string; Data: PtrInt); static;
  198. procedure Exchange(Index1, Index2: Integer);
  199. function Expand: TFPList; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  200. function Extract(Item: Pointer): Pointer;
  201. function First: Pointer;
  202. function GetEnumerator: TFPListEnumerator;
  203. function IndexOf(Item: Pointer): Integer;
  204. function IndexOfItem(Item: Pointer; Direction: TDirection): Integer;
  205. procedure Insert(Index: Integer; Item: Pointer); {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  206. function Last: Pointer;
  207. procedure Move(CurIndex, NewIndex: Integer);
  208. procedure Assign (ListA: TFPList; AOperator: TListAssignOp=laCopy; ListB: TFPList=nil);
  209. function Remove(Item: Pointer): Integer;
  210. procedure Pack;
  211. procedure Sort(Compare: TListSortCompare);
  212. procedure Sort(Compare: TListSortCompare; SortingAlgorithm: PSortingAlgorithm);
  213. procedure Sort(Compare: TListSortComparer_Context; Context: Pointer);
  214. procedure Sort(Compare: TListSortComparer_Context; Context: Pointer; SortingAlgorithm: PSortingAlgorithm);
  215. procedure ForEachCall(proc2call:TListCallback;arg:pointer);
  216. procedure ForEachCall(proc2call:TListStaticCallback;arg:pointer);
  217. property Capacity: Integer read FCapacity write SetCapacity;
  218. property Count: Integer read FCount write SetCount;
  219. property Items[Index: Integer]: Pointer read Get write Put; default;
  220. property List: PPointerList read FList;
  221. end;
  222. {$else}
  223. TFPPtrList = specialize TFPGList<Pointer>;
  224. TFPList = class(TFPPtrList)
  225. public
  226. procedure Assign(Source: TFPList);
  227. procedure Sort(Compare: TListSortCompare);
  228. procedure Sort(Compare: TListSortCompare; SortingAlgorithm: PSortingAlgorithm);
  229. procedure ForEachCall(Proc2call: TListCallback; Arg: Pointer);
  230. procedure ForEachCall(Proc2call: TListStaticCallback; Arg: Pointer);
  231. end;
  232. {$endif}
  233. { TList class}
  234. TListNotification = (lnAdded, lnExtracted, lnDeleted);
  235. TList = class;
  236. TListEnumerator = class(TFPListEnumerator)
  237. public
  238. constructor Create(AList: TList);
  239. end;
  240. TList = class(TObject,IFPObserved)
  241. private
  242. FList: TFPList;
  243. FObservers : TFPList;
  244. procedure CopyMove (aList : TList);
  245. procedure MergeMove (aList : TList);
  246. procedure DoCopy(ListA, ListB : TList);
  247. procedure DoSrcUnique(ListA, ListB : TList);
  248. procedure DoAnd(ListA, ListB : TList);
  249. procedure DoDestUnique(ListA, ListB : TList);
  250. procedure DoOr(ListA, ListB : TList);
  251. procedure DoXOr(ListA, ListB : TList);
  252. protected
  253. function Get(Index: Integer): Pointer; inline;
  254. procedure Grow; virtual;
  255. procedure Put(Index: Integer; Item: Pointer);
  256. procedure Notify(Ptr: Pointer; Action: TListNotification); virtual;
  257. procedure SetCapacity(NewCapacity: Integer); inline;
  258. function GetCapacity: Integer; inline;
  259. procedure SetCount(NewCount: Integer);
  260. function GetCount: Integer; inline;
  261. function GetList: PPointerList; inline;
  262. public
  263. constructor Create;
  264. destructor Destroy; override;
  265. Procedure FPOAttachObserver(AObserver : TObject);
  266. Procedure FPODetachObserver(AObserver : TObject);
  267. Procedure FPONotifyObservers(ASender : TObject; AOperation : TFPObservedOperation; Data : Pointer);
  268. Procedure AddList(AList : TList);
  269. function Add(Item: Pointer): Integer;
  270. procedure Clear; virtual;
  271. procedure Delete(Index: Integer);
  272. class procedure Error(const Msg: string; Data: PtrInt); virtual;
  273. procedure Exchange(Index1, Index2: Integer);
  274. function Expand: TList;
  275. function Extract(item: Pointer): Pointer;
  276. function First: Pointer; inline;
  277. function GetEnumerator: TListEnumerator;
  278. function IndexOf(Item: Pointer): Integer; inline;
  279. procedure Insert(Index: Integer; Item: Pointer);
  280. function Last: Pointer; inline;
  281. procedure Move(CurIndex, NewIndex: Integer); inline;
  282. procedure Assign (ListA: TList; AOperator: TListAssignOp=laCopy; ListB: TList=nil);
  283. function Remove(Item: Pointer): Integer;
  284. procedure Pack; inline;
  285. procedure Sort(Compare: TListSortCompare); inline;
  286. procedure Sort(Compare: TListSortCompare; SortingAlgorithm: PSortingAlgorithm); inline;
  287. procedure Sort(Compare: TListSortComparer_Context; Context: Pointer); inline;
  288. procedure Sort(Compare: TListSortComparer_Context; Context: Pointer; SortingAlgorithm: PSortingAlgorithm); inline;
  289. property Capacity: Integer read GetCapacity write SetCapacity;
  290. property Count: Integer read GetCount write SetCount;
  291. property Items[Index: Integer]: Pointer read Get write Put; default;
  292. property List: PPointerList read GetList;
  293. end;
  294. { TThreadList class }
  295. TThreadList = class
  296. private
  297. FList: TList;
  298. FDuplicates: TDuplicates;
  299. FLock: TRTLCriticalSection;
  300. public
  301. constructor Create;
  302. destructor Destroy; override;
  303. procedure Add(Item: Pointer);
  304. procedure Clear;
  305. function LockList: TList;
  306. procedure Remove(Item: Pointer);
  307. procedure UnlockList;
  308. property Duplicates: TDuplicates read FDuplicates write FDuplicates;
  309. end;
  310. {TBits Class}
  311. type
  312. TBitsBase = PtrInt;
  313. PBitsBase = ^TBitsBase;
  314. TBitsBaseUnsigned = PtrUint;
  315. const
  316. MaxBitFlags = High(SizeInt) - (bitsizeof(TBitsBase) - 1);
  317. type
  318. TBits = class(TObject)
  319. private
  320. FBits : PBitsBase;
  321. FSize : SizeInt; { total TBitsBases currently allocated }
  322. FBSize: SizeInt; {total bits currently allocated}
  323. findIndex : SizeInt;
  324. findXorMask : int8; { 0 (all zeros) or -1 (all ones), sign-extended to TBitsBase on read.
  325. 0 is for searching ones, -1 is for searching zeros. }
  326. function ScanFor1(start : SizeInt; xorMask : TBitsBase) : SizeInt;
  327. function ScanFor1Rev(start : SizeInt; xorMask : TBitsBase) : SizeInt;
  328. { functions and properties to match TBits class }
  329. procedure SetBit(bit : SizeInt; value : Boolean);
  330. procedure SetSize(value : SizeInt);
  331. Protected
  332. procedure CheckBitIndex (Bit : SizeInt;CurrentSize : Boolean);
  333. public
  334. constructor Create(TheSize : longint = 0); virtual;
  335. destructor Destroy; override;
  336. function GetFSize : SizeInt;
  337. procedure SetOn(Bit : SizeInt);
  338. procedure Clear(Bit : SizeInt);
  339. procedure Clearall;
  340. procedure CopyBits(BitSet : TBits);
  341. procedure AndBits(BitSet : TBits);
  342. procedure OrBits(BitSet : TBits);
  343. procedure XorBits(BitSet : TBits);
  344. procedure NotBits(BitSet : TBits);
  345. function Get(Bit : SizeInt) : boolean;
  346. procedure Grow(NBit : SizeInt);
  347. function Equals(Obj : TObject): Boolean; override; overload;
  348. function Equals(BitSet : TBits) : Boolean; overload;
  349. procedure SetIndex(Index : SizeInt);
  350. function FindFirstBit(State : boolean) : SizeInt;
  351. function FindNextBit : SizeInt;
  352. function FindPrevBit : SizeInt;
  353. { functions and properties to match TBits class }
  354. function OpenBit: SizeInt;
  355. property Bits[Bit: SizeInt]: Boolean read get write SetBit; default;
  356. property Size: SizeInt read FBSize write setSize;
  357. end;
  358. { TPersistent abstract class }
  359. {$M+}
  360. TPersistent = class(TObject,IFPObserved)
  361. private
  362. FObservers : TFPList;
  363. procedure AssignError(Source: TPersistent);
  364. protected
  365. procedure AssignTo(Dest: TPersistent); virtual;
  366. procedure DefineProperties(Filer: TFiler); virtual;
  367. function GetOwner: TPersistent; dynamic;
  368. public
  369. Destructor Destroy; override;
  370. procedure Assign(Source: TPersistent); virtual;
  371. Procedure FPOAttachObserver(AObserver : TObject);
  372. Procedure FPODetachObserver(AObserver : TObject);
  373. Procedure FPONotifyObservers(ASender : TObject; AOperation : TFPObservedOperation; Data : Pointer);
  374. function GetNamePath: string; virtual; {dynamic;}
  375. end;
  376. {$M-}
  377. { TPersistent class reference type }
  378. TPersistentClass = class of TPersistent;
  379. { TInterfaced Persistent }
  380. TInterfacedPersistent = class(TPersistent, IInterface)
  381. private
  382. FOwnerInterface: IInterface;
  383. protected
  384. { IInterface }
  385. function _AddRef: Longint; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  386. function _Release: Longint; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  387. public
  388. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} IID: TGUID; out Obj): HResult; virtual; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  389. procedure AfterConstruction; override;
  390. end;
  391. { TRecall class }
  392. TRecall = class(TObject)
  393. private
  394. FStorage, FReference: TPersistent;
  395. public
  396. constructor Create(AStorage, AReference: TPersistent);
  397. destructor Destroy; override;
  398. procedure Store;
  399. procedure Forget;
  400. property Reference: TPersistent read FReference;
  401. end;
  402. { TCollection class }
  403. TCollection = class;
  404. TCollectionItem = class(TPersistent)
  405. private
  406. FCollection: TCollection;
  407. FID: Integer;
  408. FUpdateCount: Integer;
  409. function GetIndex: Integer;
  410. protected
  411. procedure SetCollection(Value: TCollection);virtual;
  412. procedure Changed(AllItems: Boolean);
  413. function GetOwner: TPersistent; override;
  414. function GetDisplayName: string; virtual;
  415. procedure SetIndex(Value: Integer); virtual;
  416. procedure SetDisplayName(const Value: string); virtual;
  417. property UpdateCount: Integer read FUpdateCount;
  418. public
  419. constructor Create(ACollection: TCollection); virtual;
  420. destructor Destroy; override;
  421. function GetNamePath: string; override;
  422. property Collection: TCollection read FCollection write SetCollection;
  423. property ID: Integer read FID;
  424. property Index: Integer read GetIndex write SetIndex;
  425. property DisplayName: string read GetDisplayName write SetDisplayName;
  426. end;
  427. TCollectionEnumerator = class
  428. private
  429. FCollection: TCollection;
  430. FPosition: Integer;
  431. public
  432. constructor Create(ACollection: TCollection);
  433. function GetCurrent: TCollectionItem;
  434. function MoveNext: Boolean;
  435. property Current: TCollectionItem read GetCurrent;
  436. end;
  437. TCollectionItemClass = class of TCollectionItem;
  438. TCollectionNotification = (cnAdded, cnExtracting, cnDeleting);
  439. TCollectionSortCompare = function (Item1, Item2: TCollectionItem): Integer;
  440. TCollection = class(TPersistent)
  441. private
  442. FItemClass: TCollectionItemClass;
  443. FItems: TFpList;
  444. FUpdateCount: Integer;
  445. FNextID: Integer;
  446. FPropName: string;
  447. function GetCount: Integer;
  448. function GetPropName: string;
  449. procedure InsertItem(Item: TCollectionItem);
  450. procedure RemoveItem(Item: TCollectionItem);
  451. procedure DoClear;
  452. protected
  453. { Design-time editor support }
  454. function GetAttrCount: Integer; dynamic;
  455. function GetAttr(Index: Integer): string; dynamic;
  456. function GetItemAttr(Index, ItemIndex: Integer): string; dynamic;
  457. procedure Changed;
  458. function GetItem(Index: Integer): TCollectionItem;
  459. procedure SetItem(Index: Integer; Value: TCollectionItem);
  460. procedure SetItemName(Item: TCollectionItem); virtual;
  461. procedure SetPropName; virtual;
  462. procedure Update(Item: TCollectionItem); virtual;
  463. procedure Notify(Item: TCollectionItem;Action: TCollectionNotification); virtual;
  464. property PropName: string read GetPropName write FPropName;
  465. property UpdateCount: Integer read FUpdateCount;
  466. public
  467. constructor Create(AItemClass: TCollectionItemClass);
  468. destructor Destroy; override;
  469. function Owner: TPersistent;
  470. function Add: TCollectionItem;
  471. procedure Assign(Source: TPersistent); override;
  472. procedure BeginUpdate; virtual;
  473. procedure Clear;
  474. procedure EndUpdate; virtual;
  475. procedure Delete(Index: Integer);
  476. function GetEnumerator: TCollectionEnumerator;
  477. function GetNamePath: string; override;
  478. function Insert(Index: Integer): TCollectionItem;
  479. function FindItemID(ID: Integer): TCollectionItem;
  480. procedure Exchange(Const Index1, index2: integer);
  481. procedure Move(Const Index1, index2: integer);
  482. procedure Sort(Const Compare : TCollectionSortCompare);
  483. property Count: Integer read GetCount;
  484. property ItemClass: TCollectionItemClass read FItemClass;
  485. property Items[Index: Integer]: TCollectionItem read GetItem write SetItem;
  486. end;
  487. TOwnedCollection = class(TCollection)
  488. private
  489. FOwner: TPersistent;
  490. protected
  491. Function GetOwner: TPersistent; override;
  492. public
  493. Constructor Create(AOwner: TPersistent;AItemClass: TCollectionItemClass);
  494. end;
  495. TStrings = class;
  496. { IStringsAdapter interface }
  497. { Maintains link between TStrings and IStrings implementations }
  498. IStringsAdapter = interface ['{739C2F34-52EC-11D0-9EA6-0020AF3D82DA}']
  499. procedure ReferenceStrings(S: TStrings);
  500. procedure ReleaseStrings;
  501. end;
  502. { TStringsEnumerator class }
  503. TStringsEnumerator = class
  504. private
  505. FStrings: TStrings;
  506. FPosition: Integer;
  507. public
  508. constructor Create(AStrings: TStrings);
  509. function GetCurrent: String;
  510. function MoveNext: Boolean;
  511. property Current: String read GetCurrent;
  512. end;
  513. { TStrings class }
  514. TStringsFilterMethod = function(const s: string): boolean of object;
  515. TStringsReduceMethod = function(const s1, s2: string): string of object;
  516. TStringsMapMethod = function(const s: string): string of object;
  517. TStringsForEachMethodExObj = procedure(const CurrentValue: string; const index: integer; Obj : TObject) of object;
  518. TStringsForEachMethodEx = procedure(const CurrentValue: string; const index: integer) of object;
  519. TStringsForEachMethod = procedure(const CurrentValue: string) of object;
  520. TMissingNameValueSeparatorAction = (mnvaValue,mnvaName,mnvaEmpty,mnvaError);
  521. TMissingNameValueSeparatorActions = set of TMissingNameValueSeparatorAction;
  522. TStringsOption = (soStrictDelimiter,soWriteBOM,soTrailingLineBreak,soUseLocale,soPreserveBOM);
  523. TStringsOptions = set of TStringsOption;
  524. TStrings = class(TPersistent)
  525. private
  526. FDefaultEncoding: TEncoding;
  527. FEncoding: TEncoding;
  528. FMissingNameValueSeparatorAction: TMissingNameValueSeparatorAction;
  529. FSpecialCharsInited : boolean;
  530. FAlwaysQuote: Boolean;
  531. FQuoteChar : AnsiChar;
  532. FDelimiter : AnsiChar;
  533. FNameValueSeparator : AnsiChar;
  534. FUpdateCount: Integer;
  535. FAdapter: IStringsAdapter;
  536. FLBS : TTextLineBreakStyle;
  537. FOptions : TStringsOptions;
  538. FLineBreak : String;
  539. function GetCommaText: string;
  540. function GetLineBreakCharLBS: string;
  541. function GetMissingNameValueSeparatorAction: TMissingNameValueSeparatorAction;
  542. function GetName(Index: Integer): string;
  543. function GetStrictDelimiter: Boolean;
  544. function GetTrailingLineBreak: Boolean;
  545. function GetUseLocale: Boolean;
  546. function GetValue(const Name: string): string;
  547. function GetWriteBOM: Boolean;
  548. Function GetLBS : TTextLineBreakStyle;
  549. procedure SetDefaultEncoding(const ADefaultEncoding: TEncoding);
  550. procedure SetEncoding(const AEncoding: TEncoding);
  551. Procedure SetLBS (AValue : TTextLineBreakStyle);
  552. procedure ReadData(Reader: TReader);
  553. procedure SetCommaText(const Value: string);
  554. procedure SetMissingNameValueSeparatorAction(AValue: TMissingNameValueSeparatorAction);
  555. procedure SetStringsAdapter(const Value: IStringsAdapter);
  556. procedure SetStrictDelimiter(AValue: Boolean);
  557. procedure SetTrailingLineBreak(AValue: Boolean);
  558. procedure SetUseLocale(AValue: Boolean);
  559. procedure SetWriteBOM(AValue: Boolean);
  560. procedure SetValue(const Name, Value: string);
  561. procedure SetDelimiter(c:AnsiChar);
  562. procedure SetQuoteChar(c:AnsiChar);
  563. procedure SetNameValueSeparator(c:AnsiChar);
  564. procedure WriteData(Writer: TWriter);
  565. procedure DoSetTextStr(const Value: string; DoClear : Boolean);
  566. Function GetDelimiter : AnsiChar;
  567. Function GetNameValueSeparator : AnsiChar;
  568. Function GetQuoteChar: AnsiChar;
  569. Function GetLineBreak : String;
  570. procedure SetLineBreak(const S : String);
  571. Function GetSkipLastLineBreak : Boolean;
  572. procedure SetSkipLastLineBreak(const AValue : Boolean);
  573. Procedure DoSetDelimitedText(const AValue: string; DoClear,aStrictDelimiter : Boolean; aQuoteChar,aDelimiter : AnsiChar);
  574. protected
  575. function CompareStrings(const s1,s2 : string) : Integer; virtual;
  576. procedure DefineProperties(Filer: TFiler); override;
  577. procedure Error(const Msg: string; Data: Integer);
  578. procedure Error(const Msg: pstring; Data: Integer);
  579. function Get(Index: Integer): string; virtual; abstract;
  580. function GetCapacity: Integer; virtual;
  581. function GetCount: Integer; virtual; abstract;
  582. function GetObject(Index: Integer): TObject; virtual;
  583. function GetTextStr: string; virtual;
  584. procedure Put(Index: Integer; const S: string); virtual;
  585. procedure PutObject(Index: Integer; AObject: TObject); virtual;
  586. procedure SetCapacity(NewCapacity: Integer); virtual;
  587. procedure SetTextStr(const Value: string); virtual;
  588. procedure SetUpdateState(Updating: Boolean); virtual;
  589. property UpdateCount: Integer read FUpdateCount;
  590. Function DoCompareText(const s1,s2 : string) : PtrInt; virtual;
  591. Function GetDelimitedText: string;
  592. Procedure SetDelimitedText(Const AValue: string);
  593. Function GetValueFromIndex(Index: Integer): string;
  594. Procedure SetValueFromIndex(Index: Integer; const Value: string);
  595. Procedure CheckSpecialChars;
  596. Class Function GetNextLine (Const Value : String; Var S : String; Var P : SizeInt) : Boolean;
  597. Function GetNextLinebreak (Const Value : String; Var S : String; Var P : SizeInt) : Boolean;
  598. {$IF (SizeOf(Integer) < SizeOf(SizeInt)) }
  599. class function GetNextLine(const Value: string; var S: string; var P: Integer) : Boolean; deprecated;
  600. function GetNextLineBreak(const Value: string; var S: string; var P: Integer) : Boolean; deprecated;
  601. {$IFEND}
  602. public
  603. constructor Create;
  604. destructor Destroy; override;
  605. function ToObjectArray(aStart,aEnd : Integer) : TObjectDynArray; overload;
  606. function ToObjectArray: TObjectDynArray; overload;
  607. function ToStringArray(aStart,aEnd : Integer) : TStringDynArray; overload;
  608. function ToStringArray: TStringDynArray; overload;
  609. function Add(const S: string): Integer; virtual; overload;
  610. function AddObject(const S: string; AObject: TObject): Integer; virtual; overload;
  611. function Add(const Fmt : string; const Args : Array of const): Integer; overload;
  612. function AddObject(const Fmt: string; Args : Array of const; AObject: TObject): Integer; overload;
  613. function AddPair(const AName, AValue: string): TStrings; overload; {$IFDEF CLASSESINLINE}inline;{$ENDIF}
  614. function AddPair(const AName, AValue: string; AObject: TObject): TStrings; overload;
  615. procedure AddStrings(TheStrings: TStrings); overload; virtual;
  616. procedure AddStrings(TheStrings: TStrings; ClearFirst : Boolean); overload;
  617. procedure AddStrings(const TheStrings: array of string); overload; virtual;
  618. procedure AddStrings(const TheStrings: array of string; ClearFirst : Boolean); overload;
  619. procedure SetStrings(TheStrings: TStrings); overload; virtual;
  620. procedure SetStrings(TheStrings: array of string); overload; virtual;
  621. Procedure AddText(Const S : String); virtual;
  622. procedure AddCommaText(const S: String);
  623. procedure AddDelimitedText(const S: String; ADelimiter: AnsiChar; AStrictDelimiter: Boolean); overload;
  624. procedure AddDelimitedtext(const S: String); overload;
  625. procedure Append(const S: string);
  626. procedure Assign(Source: TPersistent); override;
  627. procedure BeginUpdate;
  628. procedure Clear; virtual; abstract;
  629. procedure Delete(Index: Integer); virtual; abstract;
  630. procedure EndUpdate;
  631. function Equals(Obj: TObject): Boolean; override; overload;
  632. function Equals(TheStrings: TStrings): Boolean; overload;
  633. procedure Exchange(Index1, Index2: Integer); virtual;
  634. function ExtractName(Const S:String):String;
  635. Procedure Filter(aFilter: TStringsFilterMethod; aList : TStrings);
  636. Function Filter(aFilter: TStringsFilterMethod) : TStrings;
  637. Procedure Fill(const aValue : String; aStart,aEnd : Integer);
  638. procedure ForEach(aCallback: TStringsForeachMethod);
  639. procedure ForEach(aCallback: TStringsForeachMethodEx);
  640. procedure ForEach(aCallback: TStringsForeachMethodExObj);
  641. function GetEnumerator: TStringsEnumerator;
  642. procedure GetNameValue(Index : Integer; Out AName,AValue : String);
  643. function GetText: PAnsiChar; virtual;
  644. function IndexOf(const S: string): Integer; virtual;
  645. function IndexOf(const S: string; aStart : Integer): Integer; virtual;
  646. function IndexOfName(const Name: string): Integer; virtual;
  647. function IndexOfObject(AObject: TObject): Integer; virtual;
  648. procedure Insert(Index: Integer; const S: string); virtual; abstract;
  649. procedure InsertObject(Index: Integer; const S: string; AObject: TObject);
  650. function LastIndexOf(const S: string; aStart : Integer): Integer; virtual;
  651. function LastIndexOf(const S: string): Integer;
  652. procedure LoadFromFile(const FileName: string); overload; virtual;
  653. procedure LoadFromFile(const FileName: string; IgnoreEncoding : Boolean);
  654. procedure LoadFromFile(const FileName: string; AEncoding: TEncoding); overload; virtual;
  655. procedure LoadFromStream(Stream: TStream); overload; virtual;
  656. procedure LoadFromStream(Stream: TStream; IgnoreEncoding : Boolean); overload;
  657. procedure LoadFromStream(Stream: TStream; AEncoding: TEncoding); overload; virtual;
  658. Procedure Map(aMap: TStringsMapMethod; aList : TStrings);
  659. Function Map(aMap: TStringsMapMethod) : TStrings;
  660. procedure Move(CurIndex, NewIndex: Integer); virtual;
  661. Function Pop : String;
  662. function Reduce(aReduceMethod: TStringsReduceMethod; const startingValue: string): string;
  663. Function Reverse : TStrings;
  664. Procedure Reverse(aList : TStrings);
  665. procedure SaveToFile(const FileName: string); overload; virtual;
  666. procedure SaveToFile(const FileName: string; IgnoreEncoding : Boolean); overload;
  667. procedure SaveToFile(const FileName: string; AEncoding: TEncoding); overload; virtual;
  668. procedure SaveToStream(Stream: TStream); overload; virtual;
  669. procedure SaveToStream(Stream: TStream; IgnoreEncoding : Boolean); overload;
  670. procedure SaveToStream(Stream: TStream; AEncoding: TEncoding); overload; virtual;
  671. function Shift : String;
  672. Procedure Slice(fromIndex: integer; aList : TStrings);
  673. Function Slice(fromIndex: integer) : TStrings;
  674. procedure SetText(TheText: PAnsiChar); virtual;
  675. property AlwaysQuote: Boolean read FAlwaysQuote write FAlwaysQuote;
  676. property Capacity: Integer read GetCapacity write SetCapacity;
  677. property CommaText: string read GetCommaText write SetCommaText;
  678. property Count: Integer read GetCount;
  679. property DefaultEncoding: TEncoding read FDefaultEncoding write SetDefaultEncoding;
  680. property DelimitedText: string read GetDelimitedText write SetDelimitedText;
  681. property Delimiter: AnsiChar read GetDelimiter write SetDelimiter;
  682. property Encoding: TEncoding read FEncoding;
  683. property LineBreak : string Read GetLineBreak write SetLineBreak;
  684. Property MissingNameValueSeparatorAction : TMissingNameValueSeparatorAction Read GetMissingNameValueSeparatorAction Write SetMissingNameValueSeparatorAction;
  685. property Names[Index: Integer]: string read GetName;
  686. Property NameValueSeparator : AnsiChar Read GetNameValueSeparator Write SetNameValueSeparator;
  687. property Objects[Index: Integer]: TObject read GetObject write PutObject;
  688. property Options: TStringsOptions read FOptions write FOptions;
  689. property QuoteChar: AnsiChar read GetQuoteChar write SetQuoteChar;
  690. Property SkipLastLineBreak : Boolean Read GetSkipLastLineBreak Write SetSkipLastLineBreak;
  691. // Same as SkipLastLineBreak but for Delphi compatibility. Note it has opposite meaning.
  692. Property TrailingLineBreak : Boolean Read GetTrailingLineBreak Write SetTrailingLineBreak;
  693. Property StrictDelimiter : Boolean Read GetStrictDelimiter Write SetStrictDelimiter;
  694. property Strings[Index: Integer]: string read Get write Put; default;
  695. property StringsAdapter: IStringsAdapter read FAdapter write SetStringsAdapter;
  696. property Text: string read GetTextStr write SetTextStr;
  697. Property TextLineBreakStyle : TTextLineBreakStyle Read GetLBS Write SetLBS;
  698. Property UseLocale : Boolean Read GetUseLocale Write SetUseLocale;
  699. property ValueFromIndex[Index: Integer]: string read GetValueFromIndex write SetValueFromIndex;
  700. property Values[const Name: string]: string read GetValue write SetValue;
  701. property WriteBOM: Boolean read GetWriteBOM write SetWriteBOM;
  702. end;
  703. TStringsClass = Class of TStrings;
  704. { TStringList class }
  705. TStringList = class;
  706. TStringListSortCompare = function(List: TStringList; Index1, Index2: Integer): Integer;
  707. {$IFNDEF FPC_TESTGENERICS}
  708. PStringItem = ^TStringItem;
  709. TStringItem = record
  710. FString: string;
  711. FObject: TObject;
  712. end;
  713. PStringItemList = ^TStringItemList;
  714. TStringItemList = array[0..MaxListSize] of TStringItem;
  715. TStringsSortStyle = (sslNone,sslUser,sslAuto);
  716. TStringsSortStyles = Set of TStringsSortStyle;
  717. TStringList = class(TStrings)
  718. private
  719. FList: PStringItemList;
  720. FCount: Integer;
  721. FCapacity: Integer;
  722. FOnChange: TNotifyEvent;
  723. FOnChanging: TNotifyEvent;
  724. FDuplicates: TDuplicates;
  725. FCaseSensitive : Boolean;
  726. FForceSort : Boolean;
  727. FOwnsObjects : Boolean;
  728. FSortStyle: TStringsSortStyle;
  729. procedure ExchangeItemsInt(Index1, Index2: Integer); inline;
  730. function GetSorted: Boolean;
  731. procedure Grow;
  732. procedure InternalClear(FromIndex : Integer = 0; ClearOnly : Boolean = False);
  733. procedure SetSorted(Value: Boolean);
  734. procedure SetCaseSensitive(b : boolean);
  735. procedure SetSortStyle(AValue: TStringsSortStyle);
  736. protected
  737. Procedure CheckIndex(AIndex : Integer); inline;
  738. procedure ExchangeItems(Index1, Index2: Integer); virtual;
  739. procedure Changed; virtual;
  740. procedure Changing; virtual;
  741. function Get(Index: Integer): string; override;
  742. function GetCapacity: Integer; override;
  743. function GetCount: Integer; override;
  744. function GetObject(Index: Integer): TObject; override;
  745. procedure Put(Index: Integer; const S: string); override;
  746. procedure PutObject(Index: Integer; AObject: TObject); override;
  747. procedure SetCapacity(NewCapacity: Integer); override;
  748. procedure SetUpdateState(Updating: Boolean); override;
  749. procedure InsertItem(Index: Integer; const S: string); virtual;
  750. procedure InsertItem(Index: Integer; const S: string; O: TObject); virtual;
  751. Function DoCompareText(const s1,s2 : string) : PtrInt; override;
  752. public
  753. Constructor Create;
  754. Constructor Create(anOwnsObjects : Boolean);
  755. destructor Destroy; override;
  756. function Add(const S: string): Integer; override;
  757. procedure Clear; override;
  758. procedure Delete(Index: Integer); override;
  759. procedure Exchange(Index1, Index2: Integer); override;
  760. function Find(const S: string; Out Index: Integer): Boolean; virtual;
  761. function IndexOf(const S: string): Integer; override;
  762. procedure Insert(Index: Integer; const S: string); override;
  763. procedure Sort; virtual;
  764. procedure Sort(SortingAlgorithm: PSortingAlgorithm); virtual;
  765. procedure CustomSort(CompareFn: TStringListSortCompare); virtual;
  766. procedure CustomSort(CompareFn: TStringListSortCompare; SortingAlgorithm: PSortingAlgorithm); virtual;
  767. property Duplicates: TDuplicates read FDuplicates write FDuplicates;
  768. property Sorted: Boolean read GetSorted write SetSorted;
  769. property CaseSensitive: Boolean read FCaseSensitive write SetCaseSensitive;
  770. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  771. property OnChanging: TNotifyEvent read FOnChanging write FOnChanging;
  772. property OwnsObjects : boolean read FOwnsObjects write FOwnsObjects;
  773. Property SortStyle : TStringsSortStyle Read FSortStyle Write SetSortStyle;
  774. end;
  775. {$else}
  776. TFPStrObjMap = specialize TFPGMap<string, TObject>;
  777. TStringListTextCompare = function(const s1, s2: string): PtrInt of object;
  778. TStringList = class(TStrings)
  779. private
  780. FMap: TFPStrObjMap;
  781. FCaseSensitive: Boolean;
  782. FOnChange: TNotifyEvent;
  783. FOnChanging: TNotifyEvent;
  784. FOnCompareText: TStringListTextCompare;
  785. FOwnsObjects : Boolean;
  786. procedure SetCaseSensitive(NewSensitive: Boolean);
  787. protected
  788. procedure Changed; virtual;
  789. procedure Changing; virtual;
  790. function DefaultCompareText(const s1, s2: string): PtrInt;
  791. function DoCompareText(const s1, s2: string): PtrInt; override;
  792. function Get(Index: Integer): string; override;
  793. function GetCapacity: Integer; override;
  794. function GetDuplicates: TDuplicates;
  795. function GetCount: Integer; override;
  796. function GetObject(Index: Integer): TObject; override;
  797. function GetSorted: Boolean; {$ifdef CLASSESINLINE} inline; {$endif}
  798. function MapPtrCompare(Key1, Key2: Pointer): Integer;
  799. procedure Put(Index: Integer; const S: string); override;
  800. procedure PutObject(Index: Integer; AObject: TObject); override;
  801. procedure QuickSort(L, R: Integer; CompareFn: TStringListSortCompare);
  802. procedure SetCapacity(NewCapacity: Integer); override;
  803. procedure SetDuplicates(NewDuplicates: TDuplicates);
  804. procedure SetSorted(NewSorted: Boolean); {$ifdef CLASSESINLINE} inline; {$endif}
  805. procedure SetUpdateState(Updating: Boolean); override;
  806. public
  807. constructor Create;
  808. destructor Destroy; override;
  809. function Add(const S: string): Integer; override;
  810. procedure Clear; override;
  811. procedure Delete(Index: Integer); override;
  812. procedure Exchange(Index1, Index2: Integer); override;
  813. function Find(const S: string; var Index: Integer): Boolean; virtual;
  814. function IndexOf(const S: string): Integer; override;
  815. procedure Insert(Index: Integer; const S: string); override;
  816. procedure Sort; virtual;
  817. procedure Sort(SortingAlgorithm: PSortingAlgorithm); virtual;
  818. procedure CustomSort(CompareFn: TStringListSortCompare);
  819. procedure CustomSort(CompareFn: TStringListSortCompare; SortingAlgorithm: PSortingAlgorithm);
  820. property Duplicates: TDuplicates read GetDuplicates write SetDuplicates;
  821. property Sorted: Boolean read GetSorted write SetSorted;
  822. property CaseSensitive: Boolean read FCaseSensitive write SetCaseSensitive;
  823. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  824. property OnChanging: TNotifyEvent read FOnChanging write FOnChanging;
  825. property OnCompareText: TStringListTextCompare read FOnCompareText write FOnCompareText;
  826. property OwnsObjects : boolean read FOwnsObjects write FOwnsObjects;
  827. end;
  828. {$endif}
  829. { TStream abstract class }
  830. TStream = class(TObject)
  831. private
  832. protected
  833. procedure InvalidSeek; virtual;
  834. procedure Discard(const Count: Int64);
  835. procedure DiscardLarge(Count: int64; const MaxBufferSize: Longint);
  836. procedure FakeSeekForward(Offset: Int64; const Origin: TSeekOrigin; const Pos: Int64);
  837. function GetPosition: Int64; virtual;
  838. procedure SetPosition(const Pos: Int64); virtual;
  839. function GetSize: Int64; virtual;
  840. procedure SetSize64(const NewSize: Int64); virtual;
  841. procedure SetSize(NewSize: Longint); virtual;overload;
  842. procedure SetSize(const NewSize: Int64); virtual;overload;
  843. procedure ReadNotImplemented;
  844. procedure WriteNotImplemented;
  845. function ReadMaxSizeData(Var Buffer; aSize,aCount : NativeInt) : NativeInt;
  846. Procedure ReadExactSizeData(Var Buffer; aSize,aCount : NativeInt);
  847. function WriteMaxSizeData(Const Buffer; aSize,aCount : NativeInt) : NativeInt;
  848. Procedure WriteExactSizeData(Const Buffer; aSize,aCount : NativeInt);
  849. public
  850. const DefaultWriteUnitname : Boolean = false;
  851. function Read(var Buffer; Count: Longint): Longint; virtual; overload;
  852. function Read(var Buffer: TBytes; Count: Longint): Longint; overload;
  853. function Read( Buffer : TBytes; aOffset, Count: Longint): Longint; overload;
  854. function Read64( Buffer : TBytes; aOffset, Count: Int64): Int64; overload;
  855. function Write(const Buffer: TBytes; Offset, Count: Longint): Longint; overload;
  856. function Write(const Buffer: TBytes; Count: Longint): Longint; overload;
  857. function Write(const Buffer; Count: Longint): Longint; virtual; overload;
  858. function Write64(const Buffer: TBytes; Offset, Count: Int64): Int64;
  859. function Seek(Offset: Longint; Origin: Word): Longint; virtual; overload;
  860. function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; virtual; overload;
  861. function ReadData(Buffer: Pointer; Count: NativeInt): NativeInt; overload;
  862. function ReadData(Buffer: TBytes; Count: NativeInt): NativeInt; overload;
  863. function ReadData(var Buffer: Boolean): NativeInt; overload;
  864. function ReadData(var Buffer: Boolean; Count: NativeInt): NativeInt; overload;
  865. function ReadData(var Buffer: AnsiChar): NativeInt; overload;
  866. function ReadData(var Buffer: AnsiChar; Count: NativeInt): NativeInt; overload;
  867. function ReadData(var Buffer: WideChar): NativeInt; overload;
  868. function ReadData(var Buffer: WideChar; Count: NativeInt): NativeInt; overload;
  869. function ReadData(var Buffer: Int8): NativeInt; overload;
  870. function ReadData(var Buffer: Int8; Count: NativeInt): NativeInt; overload;
  871. function ReadData(var Buffer: UInt8): NativeInt; overload;
  872. function ReadData(var Buffer: UInt8; Count: NativeInt): NativeInt; overload;
  873. function ReadData(var Buffer: Int16): NativeInt; overload;
  874. function ReadData(var Buffer: Int16; Count: NativeInt): NativeInt; overload;
  875. function ReadData(var Buffer: UInt16): NativeInt; overload;
  876. function ReadData(var Buffer: UInt16; Count: NativeInt): NativeInt; overload;
  877. function ReadData(var Buffer: Int32): NativeInt; overload;
  878. function ReadData(var Buffer: Int32; Count: NativeInt): NativeInt; overload;
  879. function ReadData(var Buffer: UInt32): NativeInt; overload;
  880. function ReadData(var Buffer: UInt32; Count: NativeInt): NativeInt; overload;
  881. function ReadData(var Buffer: Int64): NativeInt; overload;
  882. function ReadData(var Buffer: Int64; Count: NativeInt): NativeInt; overload;
  883. function ReadData(var Buffer: UInt64): NativeInt; overload;
  884. function ReadData(var Buffer: UInt64; Count: NativeInt): NativeInt; overload;
  885. function ReadData(var Buffer: Single): NativeInt; overload;
  886. function ReadData(var Buffer: Single; Count: NativeInt): NativeInt; overload;
  887. function ReadData(var Buffer: Double): NativeInt; overload;
  888. function ReadData(var Buffer: Double; Count: NativeInt): NativeInt; overload;
  889. {$IFDEF FPC_HAS_TYPE_EXTENDED}
  890. function ReadData(var Buffer: Extended): NativeInt; overload;
  891. function ReadData(var Buffer: Extended; Count: NativeInt): NativeInt; overload;
  892. function ReadData(var Buffer: TExtended80Rec): NativeInt; overload;
  893. function ReadData(var Buffer: TExtended80Rec; Count: NativeInt): NativeInt; overload;
  894. {$ENDIF}
  895. procedure ReadBuffer(var Buffer; Count: NativeInt);
  896. procedure ReadBuffer(var Buffer: TBytes; Count: NativeInt); overload;
  897. procedure ReadBuffer(var Buffer: TBytes; Offset, Count: NativeInt); overload;
  898. procedure ReadBufferData(var Buffer: Boolean); overload;
  899. procedure ReadBufferData(var Buffer: Boolean; Count: NativeInt); overload;
  900. procedure ReadBufferData(var Buffer: AnsiChar); overload;
  901. procedure ReadBufferData(var Buffer: AnsiChar; Count: NativeInt); overload;
  902. procedure ReadBufferData(var Buffer: WideChar); overload;
  903. procedure ReadBufferData(var Buffer: WideChar; Count: NativeInt); overload;
  904. procedure ReadBufferData(var Buffer: Int8); overload;
  905. procedure ReadBufferData(var Buffer: Int8; Count: NativeInt); overload;
  906. procedure ReadBufferData(var Buffer: UInt8); overload;
  907. procedure ReadBufferData(var Buffer: UInt8; Count: NativeInt); overload;
  908. procedure ReadBufferData(var Buffer: Int16); overload;
  909. procedure ReadBufferData(var Buffer: Int16; Count: NativeInt); overload;
  910. procedure ReadBufferData(var Buffer: UInt16); overload;
  911. procedure ReadBufferData(var Buffer: UInt16; Count: NativeInt); overload;
  912. procedure ReadBufferData(var Buffer: Int32); overload;
  913. procedure ReadBufferData(var Buffer: Int32; Count: NativeInt); overload;
  914. procedure ReadBufferData(var Buffer: UInt32); overload;
  915. procedure ReadBufferData(var Buffer: UInt32; Count: NativeInt); overload;
  916. procedure ReadBufferData(var Buffer: Int64); overload;
  917. procedure ReadBufferData(var Buffer: Int64; Count: NativeInt); overload;
  918. procedure ReadBufferData(var Buffer: UInt64); overload;
  919. procedure ReadBufferData(var Buffer: UInt64; Count: NativeInt); overload;
  920. procedure ReadBufferData(var Buffer: Single); overload;
  921. procedure ReadBufferData(var Buffer: Single; Count: NativeInt); overload;
  922. procedure ReadBufferData(var Buffer: Double); overload;
  923. procedure ReadBufferData(var Buffer: Double; Count: NativeInt); overload;
  924. {$IFDEF FPC_HAS_TYPE_EXTENDED}
  925. procedure ReadBufferData(var Buffer: Extended); overload;
  926. procedure ReadBufferData(var Buffer: Extended; Count: NativeInt); overload;
  927. procedure ReadBufferData(var Buffer: TExtended80Rec); overload;
  928. procedure ReadBufferData(var Buffer: TExtended80Rec; Count: NativeInt); overload;
  929. {$ENDIF}
  930. procedure WriteBuffer(const Buffer; Count: NativeInt);
  931. procedure WriteBuffer(const Buffer: TBytes; Count: NativeInt); overload;
  932. procedure WriteBuffer(const Buffer: TBytes; Offset, Count: NativeInt); overload;
  933. function WriteData(const Buffer: TBytes; Count: NativeInt): NativeInt; overload;
  934. function WriteData(const Buffer: Pointer; Count: NativeInt): NativeInt; overload;
  935. function WriteData(const Buffer: Boolean): NativeInt; overload;
  936. function WriteData(const Buffer: Boolean; Count: NativeInt): NativeInt; overload;
  937. function WriteData(const Buffer: AnsiChar): NativeInt; overload;
  938. function WriteData(const Buffer: AnsiChar; Count: NativeInt): NativeInt; overload;
  939. function WriteData(const Buffer: WideChar): NativeInt; overload;
  940. function WriteData(const Buffer: WideChar; Count: NativeInt): NativeInt; overload;
  941. function WriteData(const Buffer: Int8): NativeInt; overload;
  942. function WriteData(const Buffer: Int8; Count: NativeInt): NativeInt; overload;
  943. function WriteData(const Buffer: UInt8): NativeInt; overload;
  944. function WriteData(const Buffer: UInt8; Count: NativeInt): NativeInt; overload;
  945. function WriteData(const Buffer: Int16): NativeInt; overload;
  946. function WriteData(const Buffer: Int16; Count: NativeInt): NativeInt; overload;
  947. function WriteData(const Buffer: UInt16): NativeInt; overload;
  948. function WriteData(const Buffer: UInt16; Count: NativeInt): NativeInt; overload;
  949. function WriteData(const Buffer: Int32): NativeInt; overload;
  950. function WriteData(const Buffer: Int32; Count: NativeInt): NativeInt; overload;
  951. function WriteData(const Buffer: UInt32): NativeInt; overload;
  952. function WriteData(const Buffer: UInt32; Count: NativeInt): NativeInt; overload;
  953. function WriteData(const Buffer: Int64): NativeInt; overload;
  954. function WriteData(const Buffer: Int64; Count: NativeInt): NativeInt; overload;
  955. function WriteData(const Buffer: UInt64): NativeInt; overload;
  956. function WriteData(const Buffer: UInt64; Count: NativeInt): NativeInt; overload;
  957. function WriteData(const Buffer: Single): NativeInt; overload;
  958. function WriteData(const Buffer: Single; Count: NativeInt): NativeInt; overload;
  959. function WriteData(const Buffer: Double): NativeInt; overload;
  960. function WriteData(const Buffer: Double; Count: NativeInt): NativeInt; overload;
  961. {$IFDEF FPC_HAS_TYPE_EXTENDED}
  962. function WriteData(const Buffer: Extended): NativeInt; overload;
  963. function WriteData(const Buffer: Extended; Count: NativeInt): NativeInt; overload;
  964. function WriteData(const Buffer: TExtended80Rec): NativeInt; overload;
  965. function WriteData(const Buffer: TExtended80Rec; Count: NativeInt): NativeInt; overload;
  966. {$ENDIF}
  967. procedure WriteBufferData(Buffer: Int32); overload;
  968. procedure WriteBufferData(Buffer: Int32; Count: NativeInt); overload;
  969. procedure WriteBufferData(Buffer: Boolean); overload;
  970. procedure WriteBufferData(Buffer: Boolean; Count: NativeInt); overload;
  971. procedure WriteBufferData(Buffer: AnsiChar); overload;
  972. procedure WriteBufferData(Buffer: AnsiChar; Count: NativeInt); overload;
  973. procedure WriteBufferData(Buffer: WideChar); overload;
  974. procedure WriteBufferData(Buffer: WideChar; Count: NativeInt); overload;
  975. procedure WriteBufferData(Buffer: Int8); overload;
  976. procedure WriteBufferData(Buffer: Int8; Count: NativeInt); overload;
  977. procedure WriteBufferData(Buffer: UInt8); overload;
  978. procedure WriteBufferData(Buffer: UInt8; Count: NativeInt); overload;
  979. procedure WriteBufferData(Buffer: Int16); overload;
  980. procedure WriteBufferData(Buffer: Int16; Count: NativeInt); overload;
  981. procedure WriteBufferData(Buffer: UInt16); overload;
  982. procedure WriteBufferData(Buffer: UInt16; Count: NativeInt); overload;
  983. procedure WriteBufferData(Buffer: UInt32); overload;
  984. procedure WriteBufferData(Buffer: UInt32; Count: NativeInt); overload;
  985. procedure WriteBufferData(Buffer: Int64); overload;
  986. procedure WriteBufferData(Buffer: Int64; Count: NativeInt); overload;
  987. procedure WriteBufferData(Buffer: UInt64); overload;
  988. procedure WriteBufferData(Buffer: UInt64; Count: NativeInt); overload;
  989. procedure WriteBufferData(Buffer: Single); overload;
  990. procedure WriteBufferData(Buffer: Single; Count: NativeInt); overload;
  991. procedure WriteBufferData(Buffer: Double); overload;
  992. procedure WriteBufferData(Buffer: Double; Count: NativeInt); overload;
  993. {$IFDEF FPC_HAS_TYPE_EXTENDED}
  994. procedure WriteBufferData(Buffer: Extended); overload;
  995. procedure WriteBufferData(Buffer: Extended; Count: NativeInt); overload;
  996. procedure WriteBufferData(Buffer: TExtended80Rec); overload;
  997. procedure WriteBufferData(Buffer: TExtended80Rec; Count: NativeInt); overload;
  998. {$ENDIF}
  999. function CopyFrom(Source: TStream; Count: Int64): Int64;
  1000. function ReadComponent(Instance: TComponent): TComponent;
  1001. function ReadComponentRes(Instance: TComponent): TComponent;
  1002. procedure WriteComponent(Instance: TComponent); overload;
  1003. procedure WriteComponent(Instance: TComponent; aWriteUnitname: boolean); overload;
  1004. procedure WriteComponentRes(const ResName: string; Instance: TComponent); overload;
  1005. procedure WriteComponentRes(const ResName: string; Instance: TComponent; aWriteUnitname: boolean); overload;
  1006. procedure WriteDescendent(Instance, Ancestor: TComponent); overload;
  1007. procedure WriteDescendent(Instance, Ancestor: TComponent; aWriteUnitname: boolean); overload;
  1008. procedure WriteDescendentRes(const ResName: string; Instance, Ancestor: TComponent); overload;
  1009. procedure WriteDescendentRes(const ResName: string; Instance, Ancestor: TComponent; aWriteUnitname: boolean); overload;
  1010. procedure WriteResourceHeader(const ResName: string; {!!!:out} var FixupInfo: Longint);
  1011. procedure FixupResourceHeader(FixupInfo: Longint);
  1012. procedure ReadResHeader;
  1013. function ReadByte : Byte;
  1014. function ReadWord : Word;
  1015. function ReadDWord : Cardinal;
  1016. function ReadQWord : QWord;
  1017. function ReadAnsiString : String;
  1018. procedure WriteByte(b : Byte);
  1019. procedure WriteWord(w : Word);
  1020. procedure WriteDWord(d : Cardinal);
  1021. procedure WriteQWord(q : QWord);
  1022. Procedure WriteAnsiString (const S : String); virtual;
  1023. property Position: Int64 read GetPosition write SetPosition;
  1024. property Size: Int64 read GetSize write SetSize64;
  1025. end;
  1026. TProxyStream = class(TStream)
  1027. private
  1028. FStream: IStream;
  1029. protected
  1030. function GetIStream: IStream;
  1031. public
  1032. constructor Create(const Stream: IStream);
  1033. function Read(var Buffer; Count: Longint): Longint; override;
  1034. function Write(const Buffer; Count: Longint): Longint; override;
  1035. function Seek(const Offset: int64; Origin: TSeekOrigin): int64; override;
  1036. procedure Check(err:integer); virtual; abstract;
  1037. end;
  1038. { TOwnerStream }
  1039. TOwnerStream = Class(TStream)
  1040. Protected
  1041. FOwner : Boolean;
  1042. FSource : TStream;
  1043. Public
  1044. Constructor Create(ASource : TStream);
  1045. Destructor Destroy; override;
  1046. Property Source : TStream Read FSource;
  1047. Property SourceOwner : Boolean Read Fowner Write FOwner;
  1048. end;
  1049. IStreamPersist = interface ['{B8CD12A3-267A-11D4-83DA-00C04F60B2DD}']
  1050. procedure LoadFromStream(Stream: TStream);
  1051. procedure SaveToStream(Stream: TStream);
  1052. end;
  1053. { THandleStream class }
  1054. THandleStream = class(TStream)
  1055. private
  1056. FHandle: THandle;
  1057. protected
  1058. procedure SetSize(NewSize: Longint); override;
  1059. procedure SetSize(const NewSize: Int64); override;
  1060. public
  1061. constructor Create(AHandle: THandle);
  1062. function Read(var Buffer; Count: Longint): Longint; override;
  1063. function Write(const Buffer; Count: Longint): Longint; override;
  1064. function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
  1065. property Handle: THandle read FHandle;
  1066. end;
  1067. { TFileStream class }
  1068. TFileStream = class(THandleStream)
  1069. Private
  1070. FFileName : String;
  1071. public
  1072. constructor Create(const AFileName: string; Mode: Word);
  1073. constructor Create(const AFileName: string; Mode: Word; Rights: Cardinal);
  1074. destructor Destroy; override;
  1075. Function Flush : Boolean;
  1076. property FileName : String Read FFilename;
  1077. end;
  1078. { TCustomMemoryStream abstract class }
  1079. TCustomMemoryStream = class(TStream)
  1080. private
  1081. FMemory: Pointer;
  1082. FSize, FPosition: PtrInt;
  1083. FSizeBoundsSeek : Boolean;
  1084. protected
  1085. Function GetSize : Int64; Override;
  1086. function GetPosition: Int64; Override;
  1087. procedure SetPointer(Ptr: Pointer; ASize: PtrInt);
  1088. public
  1089. function Read(var Buffer; Count: LongInt): LongInt; override;
  1090. function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
  1091. procedure SaveToStream(Stream: TStream);
  1092. procedure SaveToFile(const FileName: string);
  1093. property Memory: Pointer read FMemory;
  1094. Property SizeBoundsSeek : Boolean Read FSizeBoundsSeek Write FSizeBoundsSeek;
  1095. end;
  1096. { TMemoryStream }
  1097. TMemoryStream = class(TCustomMemoryStream)
  1098. private
  1099. FCapacity: PtrInt;
  1100. procedure SetCapacity(NewCapacity: PtrInt);
  1101. protected
  1102. function Realloc(var NewCapacity: PtrInt): Pointer; virtual;
  1103. property Capacity: PtrInt read FCapacity write SetCapacity;
  1104. public
  1105. destructor Destroy; override;
  1106. procedure Clear;
  1107. procedure LoadFromStream(Stream: TStream);
  1108. procedure LoadFromFile(const FileName: string);
  1109. procedure SetSize({$ifdef CPU64}const NewSize: Int64{$else}NewSize: LongInt{$endif}); override;
  1110. function Write(const Buffer; Count: LongInt): LongInt; override;
  1111. end;
  1112. { TBytesStream }
  1113. TBytesStream = class(TMemoryStream)
  1114. private
  1115. FBytes: TBytes;
  1116. protected
  1117. function Realloc(var NewCapacity: PtrInt): Pointer; override;
  1118. public
  1119. constructor Create(const ABytes: TBytes); virtual; overload;
  1120. property Bytes: TBytes read FBytes;
  1121. end;
  1122. { TStringStream }
  1123. TStringStream = class(TBytesStream)
  1124. private
  1125. FEncoding: TEncoding;
  1126. FOwnsEncoding : Boolean;
  1127. function GetDataString: string;
  1128. function GetUnicodeDataString: UnicodeString;
  1129. protected
  1130. public
  1131. constructor Create(const ABytes: TBytes); override; overload;
  1132. constructor Create(const AString: string = ''); overload;
  1133. constructor CreateRaw(const AString: RawByteString); overload;
  1134. constructor Create(const AString: string; AEncoding: TEncoding; AOwnsEncoding: Boolean = True); overload;
  1135. constructor Create(const AString: string; ACodePage: Integer); overload;
  1136. // UnicodeString versions
  1137. constructor Create(const AString: UnicodeString); overload;
  1138. constructor Create(const AString: UnicodeString; AEncoding: TEncoding; AOwnsEncoding: Boolean = True); overload;
  1139. constructor Create(const AString: UnicodeString; ACodePage: Integer); overload;
  1140. Destructor Destroy; override;
  1141. function ReadUnicodeString(Count: Longint): UnicodeString;
  1142. procedure WriteUnicodeString(const AString: UnicodeString);
  1143. function ReadAnsiString(Count: Longint): AnsiString; overload;
  1144. procedure WriteAnsiString(const AString: AnsiString); override;
  1145. function ReadString(Count: Longint): string;
  1146. procedure WriteString(const AString: string);
  1147. property DataString: string read GetDataString;
  1148. Property UnicodeDataString : UnicodeString Read GetUnicodeDataString;
  1149. Property OwnsEncoding : Boolean Read FOwnsEncoding;
  1150. Property Encoding : TEncoding Read FEncoding;
  1151. end;
  1152. { TRawByteStringStream }
  1153. TRawByteStringStream = Class(TBytesStream)
  1154. public
  1155. Constructor Create (const aData : RawByteString); overload;
  1156. function DataString: RawByteString;
  1157. function ReadString(Count: Longint): RawByteString;
  1158. procedure WriteString(const AString: RawByteString);
  1159. end;
  1160. { TResourceStream }
  1161. {$ifdef FPC_OS_UNICODE}
  1162. TResourceStream = class(TCustomMemoryStream)
  1163. private
  1164. Res: TFPResourceHandle;
  1165. Handle: TFPResourceHGLOBAL;
  1166. procedure Initialize(Instance: TFPResourceHMODULE; Name, ResType: PWideChar; NameIsID: Boolean);
  1167. public
  1168. constructor Create(Instance: TFPResourceHMODULE; const ResName: WideString; ResType: PWideChar);
  1169. constructor CreateFromID(Instance: TFPResourceHMODULE; ResID: Integer; ResType: PWideChar);
  1170. destructor Destroy; override;
  1171. end;
  1172. {$else}
  1173. TResourceStream = class(TCustomMemoryStream)
  1174. private
  1175. Res: TFPResourceHandle;
  1176. Handle: TFPResourceHGLOBAL;
  1177. procedure Initialize(Instance: TFPResourceHMODULE; Name, ResType: PAnsiChar; NameIsID: Boolean);
  1178. public
  1179. constructor Create(Instance: TFPResourceHMODULE; const ResName: string; ResType: PAnsiChar);
  1180. constructor CreateFromID(Instance: TFPResourceHMODULE; ResID: Integer; ResType: PAnsiChar);
  1181. destructor Destroy; override;
  1182. end;
  1183. {$endif FPC_OS_UNICODE}
  1184. { TStreamAdapter }
  1185. TStreamOwnership = (soReference, soOwned);
  1186. { Implements OLE IStream on TStream }
  1187. TStreamAdapter = class(TInterfacedObject, IStream)
  1188. private
  1189. FStream : TStream;
  1190. FOwnership : TStreamOwnership;
  1191. m_bReverted: Boolean;
  1192. public
  1193. constructor Create(Stream: TStream; Ownership: TStreamOwnership = soReference);
  1194. destructor Destroy; override;
  1195. function Read(pv: Pointer; cb: DWORD; pcbRead: PDWORD): HResult; virtual; stdcall;
  1196. function Write(pv: Pointer; cb: DWORD; pcbWritten: PDWORD): HResult; virtual; stdcall;
  1197. function Seek(dlibMove: LargeInt; dwOrigin: DWORD; out libNewPosition: LargeUint): HResult; virtual; stdcall;
  1198. function SetSize(libNewSize: LargeUint): HResult; virtual; stdcall;
  1199. function CopyTo(stm: IStream; cb: LargeUint; out cbRead: LargeUint; out cbWritten: LargeUint): HResult; virtual; stdcall;
  1200. function Commit(grfCommitFlags: DWORD): HResult; virtual; stdcall;
  1201. function Revert: HResult; virtual; stdcall;
  1202. function LockRegion(libOffset: LargeUint; cb: LargeUint; dwLockType: DWORD): HResult; virtual; stdcall;
  1203. function UnlockRegion(libOffset: LargeUint; cb: LargeUint; dwLockType: DWORD): HResult; virtual; stdcall;
  1204. function Stat(out statstg: TStatStg; grfStatFlag: DWORD): HResult; virtual; stdcall;
  1205. function Clone(out stm: IStream): HResult; virtual; stdcall;
  1206. property Stream: TStream read FStream;
  1207. property StreamOwnership: TStreamOwnership read FOwnership write FOwnership;
  1208. end;
  1209. { TFiler }
  1210. TValueType = (vaNull, vaList, vaInt8, vaInt16, vaInt32, vaExtended,
  1211. vaString, vaIdent, vaFalse, vaTrue, vaBinary, vaSet, vaLString,
  1212. vaNil, vaCollection, vaSingle, vaCurrency, vaDate, vaWString, vaInt64,
  1213. vaUTF8String, vaUString, vaQWord);
  1214. TFilerFlag = (ffInherited, ffChildPos, ffInline);
  1215. TFilerFlags = set of TFilerFlag;
  1216. TReaderProc = procedure(Reader: TReader) of object;
  1217. TWriterProc = procedure(Writer: TWriter) of object;
  1218. TStreamProc = procedure(Stream: TStream) of object;
  1219. TFiler = class(TObject)
  1220. private
  1221. FRoot: TComponent;
  1222. FLookupRoot: TComponent;
  1223. FAncestor: TPersistent;
  1224. FIgnoreChildren: Boolean;
  1225. protected
  1226. procedure SetRoot(ARoot: TComponent); virtual;
  1227. public
  1228. procedure DefineProperty(const Name: string;
  1229. ReadData: TReaderProc; WriteData: TWriterProc;
  1230. HasData: Boolean); virtual; abstract;
  1231. procedure DefineBinaryProperty(const Name: string;
  1232. ReadData, WriteData: TStreamProc;
  1233. HasData: Boolean); virtual; abstract;
  1234. Procedure FlushBuffer; virtual; abstract;
  1235. property Root: TComponent read FRoot write SetRoot;
  1236. property LookupRoot: TComponent read FLookupRoot;
  1237. property Ancestor: TPersistent read FAncestor write FAncestor;
  1238. property IgnoreChildren: Boolean read FIgnoreChildren write FIgnoreChildren;
  1239. end;
  1240. { TComponent class reference type }
  1241. TComponentClass = class of TComponent;
  1242. { TReader }
  1243. { TAbstractObjectReader }
  1244. TAbstractObjectReader = class
  1245. public
  1246. Procedure FlushBuffer; virtual;
  1247. function NextValue: TValueType; virtual; abstract;
  1248. function ReadValue: TValueType; virtual; abstract;
  1249. procedure BeginRootComponent; virtual; abstract;
  1250. procedure BeginComponent(var Flags: TFilerFlags; var AChildPos: Integer;
  1251. var CompClassName, CompName: String); virtual; abstract; overload;
  1252. procedure BeginComponent(var Flags: TFilerFlags; var AChildPos: Integer;
  1253. var CompUnitName, CompClassName, CompName: String); virtual; overload;
  1254. function BeginProperty: String; virtual; abstract;
  1255. //Please don't use read, better use ReadBinary whenever possible
  1256. procedure Read(var Buf; Count: LongInt); virtual; abstract;
  1257. { All ReadXXX methods are called _after_ the value type has been read! }
  1258. procedure ReadBinary(const DestData: TMemoryStream); virtual; abstract;
  1259. {$ifndef FPUNONE}
  1260. function ReadFloat: Extended; virtual; abstract;
  1261. function ReadSingle: Single; virtual; abstract;
  1262. function ReadDate: TDateTime; virtual; abstract;
  1263. {$endif}
  1264. function ReadCurrency: Currency; virtual; abstract;
  1265. function ReadIdent(ValueType: TValueType): String; virtual; abstract;
  1266. function ReadInt8: ShortInt; virtual; abstract;
  1267. function ReadInt16: SmallInt; virtual; abstract;
  1268. function ReadInt32: LongInt; virtual; abstract;
  1269. function ReadInt64: Int64; virtual; abstract;
  1270. function ReadSet(EnumType: Pointer): Integer; virtual; abstract;
  1271. procedure ReadSignature; virtual; abstract;
  1272. function ReadStr: String; virtual; abstract;
  1273. function ReadString(StringType: TValueType): String; virtual; abstract;
  1274. function ReadWideString: WideString;virtual;abstract;
  1275. function ReadUnicodeString: UnicodeString;virtual;abstract;
  1276. procedure SkipComponent(SkipComponentInfos: Boolean); virtual; abstract;
  1277. procedure SkipValue; virtual; abstract;
  1278. end;
  1279. { TBinaryObjectReader }
  1280. TBinaryObjectReader = class(TAbstractObjectReader)
  1281. public
  1282. {$ScopedEnums on}
  1283. type
  1284. TBOVersion = (
  1285. boVersion0,
  1286. boVersion1
  1287. );
  1288. {$ScopedEnums off}
  1289. const
  1290. UnitnameSeparator = '/';
  1291. protected
  1292. FStream: TStream;
  1293. FBuffer: Pointer;
  1294. FBufSize: Integer;
  1295. FBufPos: Integer;
  1296. FBufEnd: Integer;
  1297. FVersion: TBOVersion;
  1298. function ReadWord : word; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1299. function ReadDWord : longword; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1300. function ReadQWord : qword; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1301. {$ifndef FPUNONE}
  1302. function ReadExtended : extended; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1303. {$endif}
  1304. procedure SkipProperty;
  1305. procedure SkipSetBody;
  1306. public
  1307. constructor Create(Stream: TStream; BufSize: Integer);
  1308. destructor Destroy; override;
  1309. function NextValue: TValueType; override;
  1310. function ReadValue: TValueType; override;
  1311. procedure BeginRootComponent; override;
  1312. procedure BeginComponent(var Flags: TFilerFlags; var AChildPos: Integer;
  1313. var CompClassName, CompName: String); override; overload;
  1314. procedure BeginComponent(var Flags: TFilerFlags; var AChildPos: Integer;
  1315. var CompUnitName, CompClassName, CompName: String); override; overload;
  1316. function BeginProperty: String; override;
  1317. //Please don't use read, better use ReadBinary whenever possible
  1318. procedure Read(var Buf; Count: LongInt); override;
  1319. procedure ReadBinary(const DestData: TMemoryStream); override;
  1320. {$ifndef FPUNONE}
  1321. function ReadFloat: Extended; override;
  1322. function ReadSingle: Single; override;
  1323. function ReadDate: TDateTime; override;
  1324. {$endif}
  1325. function ReadCurrency: Currency; override;
  1326. function ReadIdent(ValueType: TValueType): String; override;
  1327. function ReadInt8: ShortInt; override;
  1328. function ReadInt16: SmallInt; override;
  1329. function ReadInt32: LongInt; override;
  1330. function ReadInt64: Int64; override;
  1331. function ReadSet(EnumType: Pointer): Integer; override;
  1332. procedure ReadSignature; override;
  1333. function ReadStr: String; override;
  1334. function ReadString(StringType: TValueType): String; override;
  1335. function ReadWideString: WideString;override;
  1336. function ReadUnicodeString: UnicodeString;override;
  1337. procedure SkipComponent(SkipComponentInfos: Boolean); override;
  1338. procedure SkipValue; override;
  1339. property Version: TBOVersion read FVersion;
  1340. end;
  1341. TFindMethodEvent = procedure(Reader: TReader; const MethodName: string;
  1342. var Address: CodePointer; var Error: Boolean) of object;
  1343. TSetMethodPropertyEvent = procedure(Reader: TReader; Instance: TPersistent;
  1344. PropInfo: PPropInfo; const TheMethodName: string;
  1345. var Handled: boolean) of object;
  1346. TSetNameEvent = procedure(Reader: TReader; Component: TComponent;
  1347. var aName: string) of object;
  1348. TReferenceNameEvent = procedure(Reader: TReader; var aName: string) of object;
  1349. TAncestorNotFoundEvent = procedure(Reader: TReader; const ComponentName: string;
  1350. ComponentClass: TPersistentClass; var Component: TComponent) of object;
  1351. TReadComponentsProc = procedure(Component: TComponent) of object;
  1352. TReaderError = procedure(Reader: TReader; const Message: string;
  1353. var Handled: Boolean) of object;
  1354. TPropertyNotFoundEvent = procedure(Reader: TReader; Instance: TPersistent;
  1355. var PropName: string; IsPath: boolean; var Handled, Skip: Boolean) of object;
  1356. TFindComponentClassEvent = procedure(Reader: TReader; const aClassName: string;
  1357. var ComponentClass: TComponentClass) of object;
  1358. TFindComponentClassExEvent = procedure(Reader: TReader;
  1359. const aName, anUnitname, aClassName: string;
  1360. var ComponentClass: TComponentClass) of object;
  1361. TCreateComponentEvent = procedure(Reader: TReader;
  1362. ComponentClass: TComponentClass; var Component: TComponent) of object;
  1363. TReadWriteStringPropertyEvent = procedure(Sender:TObject;
  1364. const Instance: TPersistent; PropInfo: PPropInfo;
  1365. var Content:string) of object;
  1366. { TReader }
  1367. TReader = class(TFiler)
  1368. private
  1369. FDriver: TAbstractObjectReader;
  1370. FOnFindComponentClassEx: TFindComponentClassExEvent;
  1371. FOwner: TComponent;
  1372. FParent: TComponent;
  1373. FFixups: TObject;
  1374. FLoaded: TFpList;
  1375. FLock: TRTLCriticalSection;
  1376. FOnFindMethod: TFindMethodEvent;
  1377. FOnSetMethodProperty: TSetMethodPropertyEvent;
  1378. FOnSetName: TSetNameEvent;
  1379. FOnReferenceName: TReferenceNameEvent;
  1380. FOnAncestorNotFound: TAncestorNotFoundEvent;
  1381. FOnError: TReaderError;
  1382. FOnPropertyNotFound: TPropertyNotFoundEvent;
  1383. FOnFindComponentClass: TFindComponentClassEvent;
  1384. FOnCreateComponent: TCreateComponentEvent;
  1385. FPropName: string;
  1386. FCanHandleExcepts: Boolean;
  1387. FOnReadStringProperty:TReadWriteStringPropertyEvent;
  1388. procedure DoFixupReferences;
  1389. function FindComponentClass(const AName, anUnitName, AClassName: string): TComponentClass;
  1390. procedure Lock;
  1391. procedure Unlock;
  1392. protected
  1393. function Error(const Message: string): Boolean; virtual;
  1394. function FindMethod(ARoot: TComponent; const AMethodName: string): CodePointer; virtual;
  1395. procedure ReadProperty(AInstance: TPersistent);
  1396. procedure ReadPropValue(Instance: TPersistent; PropInfo: Pointer);
  1397. procedure PropertyError;
  1398. procedure ReadData(Instance: TComponent);
  1399. property PropName: string read FPropName;
  1400. property CanHandleExceptions: Boolean read FCanHandleExcepts;
  1401. function CreateDriver(Stream: TStream; BufSize: Integer): TAbstractObjectReader; virtual;
  1402. public
  1403. constructor Create(Stream: TStream; BufSize: Integer);
  1404. destructor Destroy; override;
  1405. Procedure FlushBuffer; override;
  1406. procedure BeginReferences;
  1407. procedure CheckValue(Value: TValueType);
  1408. procedure DefineProperty(const Name: string;
  1409. AReadData: TReaderProc; WriteData: TWriterProc;
  1410. HasData: Boolean); override;
  1411. procedure DefineBinaryProperty(const Name: string;
  1412. AReadData, WriteData: TStreamProc;
  1413. HasData: Boolean); override;
  1414. function EndOfList: Boolean;
  1415. procedure EndReferences;
  1416. procedure FixupReferences;
  1417. function NextValue: TValueType;
  1418. //Please don't use read, better use ReadBinary whenever possible
  1419. //uuups, ReadBinary is protected ..
  1420. procedure Read(var Buf; Count: LongInt); virtual;
  1421. function ReadBoolean: Boolean;
  1422. function ReadChar: AnsiChar;
  1423. function ReadWideChar: WideChar;
  1424. function ReadUnicodeChar: UnicodeChar;
  1425. procedure ReadCollection(Collection: TCollection);
  1426. function ReadComponent(Component: TComponent): TComponent;
  1427. procedure ReadComponents(AOwner, AParent: TComponent;
  1428. Proc: TReadComponentsProc);
  1429. {$ifndef FPUNONE}
  1430. function ReadFloat: Extended;
  1431. function ReadSingle: Single;
  1432. function ReadDate: TDateTime;
  1433. {$endif}
  1434. function ReadCurrency: Currency;
  1435. function ReadIdent: string;
  1436. function ReadInteger: Longint;
  1437. function ReadInt64: Int64;
  1438. function ReadSet(EnumType: Pointer): Integer;
  1439. procedure ReadListBegin;
  1440. procedure ReadListEnd;
  1441. function ReadRootComponent(ARoot: TComponent): TComponent;
  1442. function ReadVariant: Variant;
  1443. procedure ReadSignature;
  1444. function ReadString: string;
  1445. function ReadWideString: WideString;
  1446. function ReadUnicodeString: UnicodeString;
  1447. function ReadValue: TValueType;
  1448. procedure CopyValue(Writer: TWriter);
  1449. property Driver: TAbstractObjectReader read FDriver;
  1450. property Owner: TComponent read FOwner write FOwner;
  1451. property Parent: TComponent read FParent write FParent;
  1452. property OnError: TReaderError read FOnError write FOnError;
  1453. property OnPropertyNotFound: TPropertyNotFoundEvent read FOnPropertyNotFound write FOnPropertyNotFound;
  1454. property OnFindMethod: TFindMethodEvent read FOnFindMethod write FOnFindMethod;
  1455. property OnSetMethodProperty: TSetMethodPropertyEvent read FOnSetMethodProperty write FOnSetMethodProperty;
  1456. property OnSetName: TSetNameEvent read FOnSetName write FOnSetName;
  1457. property OnReferenceName: TReferenceNameEvent read FOnReferenceName write FOnReferenceName;
  1458. property OnAncestorNotFound: TAncestorNotFoundEvent read FOnAncestorNotFound write FOnAncestorNotFound;
  1459. property OnCreateComponent: TCreateComponentEvent read FOnCreateComponent write FOnCreateComponent;
  1460. property OnFindComponentClass: TFindComponentClassEvent read FOnFindComponentClass write FOnFindComponentClass;
  1461. property OnFindComponentClassEx: TFindComponentClassExEvent read FOnFindComponentClassEx write FOnFindComponentClassEx;
  1462. property OnReadStringProperty: TReadWriteStringPropertyEvent read FOnReadStringProperty write FOnReadStringProperty;
  1463. end;
  1464. { TWriter }
  1465. { TAbstractObjectWriter }
  1466. TAbstractObjectWriter = class
  1467. public
  1468. { Begin/End markers. Those ones who don't have an end indicator, use
  1469. "EndList", after the occurrence named in the comment. Note that this
  1470. only counts for "EndList" calls on the same level; each BeginXXX call
  1471. increases the current level. }
  1472. procedure BeginCollection; virtual; abstract; { Ends with the next "EndList" }
  1473. procedure BeginComponent(Component: TComponent; Flags: TFilerFlags;
  1474. ChildPos: Integer); virtual; abstract; { Ends after the second "EndList" }
  1475. procedure WriteSignature; virtual; abstract;
  1476. procedure BeginList; virtual; abstract;
  1477. procedure EndList; virtual; abstract;
  1478. procedure BeginProperty(const PropName: String); virtual; abstract;
  1479. procedure EndProperty; virtual; abstract;
  1480. Procedure FlushBuffer; virtual;
  1481. //Please don't use write, better use WriteBinary whenever possible
  1482. procedure Write(const Buffer; Count: Longint); virtual;abstract;
  1483. procedure WriteBinary(const Buffer; Count: Longint); virtual; abstract;
  1484. procedure WriteBoolean(Value: Boolean); virtual; abstract;
  1485. // procedure WriteChar(Value: AnsiChar);
  1486. {$ifndef FPUNONE}
  1487. procedure WriteFloat(const Value: Extended); virtual; abstract;
  1488. procedure WriteSingle(const Value: Single); virtual; abstract;
  1489. procedure WriteDate(const Value: TDateTime); virtual; abstract;
  1490. {$endif}
  1491. procedure WriteCurrency(const Value: Currency); virtual; abstract;
  1492. procedure WriteIdent(const Ident: string); virtual; abstract;
  1493. procedure WriteInteger(Value: Int64); virtual; abstract;
  1494. procedure WriteUInt64(Value: QWord); virtual; abstract;
  1495. procedure WriteVariant(const Value: Variant); virtual; abstract;
  1496. procedure WriteMethodName(const Name: String); virtual; abstract;
  1497. procedure WriteSet(Value: LongInt; SetType: Pointer); virtual; abstract;
  1498. procedure WriteString(const Value: String); virtual; abstract;
  1499. procedure WriteWideString(const Value: WideString);virtual;abstract;
  1500. procedure WriteUnicodeString(const Value: UnicodeString);virtual;abstract;
  1501. end;
  1502. { TBinaryObjectWriter }
  1503. TBinaryObjectWriter = class(TAbstractObjectWriter)
  1504. protected
  1505. FStream: TStream;
  1506. FBuffer: Pointer;
  1507. FBufSize: Integer;
  1508. FBufPos: Integer;
  1509. FBufEnd: Integer;
  1510. FVersion: TBinaryObjectReader.TBOVersion;
  1511. procedure WriteWord(w : word); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1512. procedure WriteDWord(lw : longword); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1513. procedure WriteQWord(qw : qword); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1514. {$ifndef FPUNONE}
  1515. procedure WriteExtended(e : extended); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1516. {$endif}
  1517. procedure WriteValue(Value: TValueType);
  1518. public
  1519. constructor Create(Stream: TStream; BufSize: Integer);
  1520. destructor Destroy; override;
  1521. procedure WriteSignature; override;
  1522. procedure FlushBuffer; override;
  1523. procedure BeginCollection; override;
  1524. procedure BeginComponent(Component: TComponent; Flags: TFilerFlags;
  1525. ChildPos: Integer); override;
  1526. procedure BeginList; override;
  1527. procedure EndList; override;
  1528. procedure BeginProperty(const PropName: String); override;
  1529. procedure EndProperty; override;
  1530. //Please don't use write, better use WriteBinary whenever possible
  1531. procedure Write(const Buffer; Count: Longint); override;
  1532. procedure WriteBinary(const Buffer; Count: LongInt); override;
  1533. procedure WriteBoolean(Value: Boolean); override;
  1534. {$ifndef FPUNONE}
  1535. procedure WriteFloat(const Value: Extended); override;
  1536. procedure WriteSingle(const Value: Single); override;
  1537. procedure WriteDate(const Value: TDateTime); override;
  1538. {$endif}
  1539. procedure WriteCurrency(const Value: Currency); override;
  1540. procedure WriteIdent(const Ident: string); override;
  1541. procedure WriteInteger(Value: Int64); override;
  1542. procedure WriteUInt64(Value: QWord); override;
  1543. procedure WriteMethodName(const Name: String); override;
  1544. procedure WriteSet(Value: LongInt; SetType: Pointer); override;
  1545. procedure WriteStr(const Value: String); // write shortstring
  1546. procedure WriteString(const Value: String); override;
  1547. procedure WriteWideString(const Value: WideString); override;
  1548. procedure WriteUnicodeString(const Value: UnicodeString); override;
  1549. procedure WriteVariant(const VarValue: Variant);override;
  1550. property Version: TBinaryObjectReader.TBOVersion read FVersion write FVersion;
  1551. end;
  1552. TTextObjectWriter = class(TAbstractObjectWriter)
  1553. end;
  1554. TFindAncestorEvent = procedure (Writer: TWriter; Component: TComponent;
  1555. const Name: string; var Ancestor, RootAncestor: TComponent) of object;
  1556. TWriteMethodPropertyEvent = procedure (Writer: TWriter; Instance: TPersistent;
  1557. PropInfo: PPropInfo;
  1558. const MethodValue, DefMethodValue: TMethod;
  1559. var Handled: boolean) of object;
  1560. TWriter = class(TFiler)
  1561. private
  1562. FDriver: TAbstractObjectWriter;
  1563. FDestroyDriver: Boolean;
  1564. FRootAncestor: TComponent;
  1565. FPropPath: String;
  1566. FAncestors: TStringList;
  1567. FAncestorPos: Integer;
  1568. FCurrentPos: Integer;
  1569. FOnFindAncestor: TFindAncestorEvent;
  1570. FOnWriteMethodProperty: TWriteMethodPropertyEvent;
  1571. FOnWriteStringProperty:TReadWriteStringPropertyEvent;
  1572. procedure AddToAncestorList(Component: TComponent);
  1573. procedure WriteComponentData(Instance: TComponent);
  1574. Procedure DetermineAncestor(Component: TComponent);
  1575. procedure DoFindAncestor(Component : TComponent);
  1576. protected
  1577. procedure SetRoot(ARoot: TComponent); override;
  1578. procedure WriteBinary(AWriteData: TStreamProc);
  1579. procedure WriteProperty(Instance: TPersistent; PropInfo: Pointer);
  1580. procedure WriteProperties(Instance: TPersistent);
  1581. procedure WriteChildren(Component: TComponent);
  1582. function CreateDriver(Stream: TStream; BufSize: Integer): TAbstractObjectWriter; virtual;
  1583. public
  1584. constructor Create(ADriver: TAbstractObjectWriter);
  1585. constructor Create(Stream: TStream; BufSize: Integer);
  1586. destructor Destroy; override;
  1587. Procedure FlushBuffer; override;
  1588. procedure DefineProperty(const Name: string;
  1589. ReadData: TReaderProc; AWriteData: TWriterProc;
  1590. HasData: Boolean); override;
  1591. procedure DefineBinaryProperty(const Name: string;
  1592. ReadData, AWriteData: TStreamProc;
  1593. HasData: Boolean); override;
  1594. //Please don't use write, better use WriteBinary whenever possible
  1595. //uuups, WriteBinary is protected ..
  1596. procedure Write(const Buffer; Count: Longint); virtual;
  1597. procedure WriteBoolean(Value: Boolean);
  1598. procedure WriteCollection(Value: TCollection);
  1599. procedure WriteComponent(Component: TComponent);
  1600. procedure WriteChar(Value: AnsiChar);
  1601. procedure WriteWideChar(Value: WideChar);
  1602. procedure WriteDescendent(ARoot: TComponent; AAncestor: TComponent);
  1603. {$ifndef FPUNONE}
  1604. procedure WriteFloat(const Value: Extended);
  1605. procedure WriteSingle(const Value: Single);
  1606. procedure WriteDate(const Value: TDateTime);
  1607. {$endif}
  1608. procedure WriteCurrency(const Value: Currency);
  1609. procedure WriteIdent(const Ident: string);
  1610. procedure WriteInteger(Value: Longint); overload;
  1611. procedure WriteInteger(Value: Int64); overload;
  1612. procedure WriteSet(Value: Longint; SetType: Pointer);
  1613. procedure WriteListBegin;
  1614. procedure WriteListEnd;
  1615. Procedure WriteSignature;
  1616. procedure WriteRootComponent(ARoot: TComponent);
  1617. procedure WriteString(const Value: String);
  1618. procedure WriteWideString(const Value: WideString);
  1619. procedure WriteUnicodeString(const Value: UnicodeString);
  1620. procedure WriteVariant(const VarValue: Variant);
  1621. property RootAncestor: TComponent read FRootAncestor write FRootAncestor;
  1622. property OnFindAncestor: TFindAncestorEvent read FOnFindAncestor write FOnFindAncestor;
  1623. property OnWriteMethodProperty: TWriteMethodPropertyEvent read FOnWriteMethodProperty write FOnWriteMethodProperty;
  1624. property OnWriteStringProperty: TReadWriteStringPropertyEvent read FOnWriteStringProperty write FOnWriteStringProperty;
  1625. property Driver: TAbstractObjectWriter read FDriver;
  1626. property PropertyPath: string read FPropPath;
  1627. end;
  1628. { TParser }
  1629. TParser = class(TObject)
  1630. private
  1631. fStream : TStream;
  1632. fBuf : PAnsiChar;
  1633. fBufLen : integer;
  1634. fPos : integer;
  1635. fDeltaPos : integer;
  1636. fFloatType : AnsiChar;
  1637. fSourceLine : integer;
  1638. fToken : AnsiChar;
  1639. fEofReached : boolean;
  1640. fLastTokenStr : string;
  1641. fLastTokenWStr : widestring;
  1642. function GetTokenName(aTok : AnsiChar) : string;
  1643. procedure LoadBuffer;
  1644. procedure CheckLoadBuffer; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  1645. procedure ProcessChar; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  1646. function IsNumber : boolean; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  1647. function IsHexNum : boolean; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  1648. function IsAlpha : boolean; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  1649. function IsAlphaNum : boolean; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  1650. function GetHexValue(c : AnsiChar) : byte; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  1651. function GetAlphaNum : string;
  1652. procedure HandleNewLine;
  1653. procedure SkipBOM;
  1654. procedure SkipSpaces;
  1655. procedure SkipWhitespace;
  1656. procedure HandleEof;
  1657. procedure HandleAlphaNum;
  1658. procedure HandleNumber;
  1659. procedure HandleHexNumber;
  1660. function HandleQuotedString : string;
  1661. procedure HandleDecimalCharacter(var ascii : boolean;
  1662. out WideChr: widechar; out StringChr: AnsiChar);
  1663. procedure HandleString;
  1664. procedure HandleMinus;
  1665. procedure HandleUnknown;
  1666. public
  1667. constructor Create(Stream: TStream);
  1668. destructor Destroy; override;
  1669. procedure CheckToken(T: AnsiChar);
  1670. procedure CheckTokenSymbol(const S: string);
  1671. procedure Error(const Ident: string);
  1672. procedure ErrorFmt(const Ident: string; const Args: array of const);
  1673. procedure ErrorStr(const Message: string);
  1674. procedure HexToBinary(Stream: TStream);
  1675. function NextToken: AnsiChar;
  1676. function SourcePos: Longint;
  1677. function TokenComponentIdent: string;
  1678. {$ifndef FPUNONE}
  1679. function TokenFloat: Extended;
  1680. {$endif}
  1681. function TokenInt: Int64;
  1682. function TokenString: string;
  1683. function TokenWideString: WideString;
  1684. function TokenSymbolIs(const S: string): Boolean;
  1685. property FloatType: AnsiChar read fFloatType;
  1686. property SourceLine: Integer read fSourceLine;
  1687. property Token: AnsiChar read fToken;
  1688. end;
  1689. { TThread }
  1690. TThread = Class;
  1691. EThread = class(Exception);
  1692. EThreadExternalException = class(EThread);
  1693. EThreadDestroyCalled = class(EThread);
  1694. TSynchronizeProcVar = procedure;
  1695. TThreadMethod = procedure of object;
  1696. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  1697. TThreadProcedure = reference to procedure;
  1698. {$endif}
  1699. TThreadReportStatus = Procedure(Const status : String) of Object;
  1700. TThreadStatusNotifyEvent = Procedure(Sender : TThread; Const status : String) of Object;
  1701. TThreadExecuteHandler = TThreadMethod;
  1702. TThreadExecuteStatusHandler = Procedure(ReportStatus : TThreadReportStatus) of object;
  1703. TNotifyCallBack = Procedure(Sender : TObject; AData : Pointer);
  1704. TThreadStatusNotifyCallBack = Procedure(Sender : TThread; AData : Pointer; Const status : String);
  1705. TThreadExecuteCallBack = Procedure(AData : Pointer);
  1706. TThreadExecuteStatusCallBack = Procedure(AData : Pointer; ReportStatus : TThreadReportStatus);
  1707. TThreadPriority = (tpIdle, tpLowest, tpLower, tpNormal, tpHigher, tpHighest,
  1708. tpTimeCritical);
  1709. TThread = class
  1710. private type
  1711. PThreadQueueEntry = ^TThreadQueueEntry;
  1712. TThreadQueueEntry = record
  1713. Method: TThreadMethod;
  1714. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  1715. ThreadProc: TThreadProcedure;
  1716. {$endif}
  1717. Thread: TThread;
  1718. ThreadID: TThreadID;
  1719. Exception: TObject;
  1720. SyncEvent: PRtlEvent;
  1721. Next: PThreadQueueEntry;
  1722. end;
  1723. public type
  1724. TSystemTimes = record
  1725. IdleTime: QWord;
  1726. UserTime: QWord;
  1727. KernelTime: QWord;
  1728. NiceTime: QWord;
  1729. end;
  1730. private
  1731. class var FProcessorCount: LongWord;
  1732. private
  1733. FHandle: TThreadID;
  1734. FTerminated: Boolean;
  1735. FFreeOnTerminate: Boolean;
  1736. FFinished: Boolean;
  1737. FSuspended: LongBool;
  1738. FReturnValue: Integer;
  1739. FOnTerminate: TNotifyEvent;
  1740. FFatalException: TObject;
  1741. FExternalThread: Boolean;
  1742. FSynchronizeEntry: PThreadQueueEntry;
  1743. class function GetCurrentThread: TThread; static;
  1744. class function GetIsSingleProcessor: Boolean; static; inline;
  1745. class procedure InternalQueue(aThread: TThread; aMethod: TThreadMethod; aQueueIfMain: Boolean); static;
  1746. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  1747. class procedure InternalQueue(aThread: TThread; aProcedure: TThreadProcedure; aQueueIfMain: Boolean); static;
  1748. {$endif}
  1749. procedure CallOnTerminate;
  1750. function GetPriority: TThreadPriority;
  1751. procedure SetPriority(Value: TThreadPriority);
  1752. procedure SetSuspended(Value: Boolean);
  1753. function GetSuspended: Boolean;
  1754. procedure InitSynchronizeEvent;
  1755. procedure DoneSynchronizeEvent;
  1756. { these two need to be implemented per platform }
  1757. procedure SysCreate(CreateSuspended: Boolean;
  1758. const StackSize: SizeUInt);
  1759. procedure SysDestroy;
  1760. protected
  1761. FThreadID: TThreadID; // someone might need it for pthread_* calls
  1762. procedure DoTerminate; virtual;
  1763. procedure TerminatedSet; virtual;
  1764. procedure Execute; virtual; abstract;
  1765. procedure Synchronize(AMethod: TThreadMethod);
  1766. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  1767. procedure Synchronize(AProcedure : TThreadProcedure);
  1768. {$endif}
  1769. procedure Queue(aMethod: TThreadMethod);
  1770. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  1771. procedure Queue(aProcedure: TThreadProcedure);
  1772. {$endif}
  1773. procedure ForceQueue(aMethod: TThreadMethod); inline;
  1774. property ReturnValue: Integer read FReturnValue write FReturnValue;
  1775. property Terminated: Boolean read FTerminated;
  1776. {$if defined(windows) or defined(OS2)}
  1777. private
  1778. FInitialSuspended: boolean;
  1779. {$endif}
  1780. {$ifdef Unix}
  1781. private
  1782. // see tthread.inc, ThreadFunc and TThread.Resume
  1783. FSuspendEvent: PRTLEvent;
  1784. FInitialSuspended: boolean;
  1785. FSuspendedInternal: longbool;
  1786. FThreadReaped: boolean;
  1787. {$endif}
  1788. {$ifdef netwlibc}
  1789. private
  1790. // see tthread.inc, ThreadFunc and TThread.Resume
  1791. FSem: Pointer;
  1792. FInitialSuspended: boolean;
  1793. FSuspendedExternal: boolean;
  1794. FPid: LongInt;
  1795. {$endif}
  1796. {$if defined(hasamiga)}
  1797. private
  1798. FInitialSuspended: boolean;
  1799. {$endif}
  1800. {$ifdef beos}
  1801. FSem : pointer;
  1802. FSuspendedExternal: boolean;
  1803. {$endif}
  1804. public
  1805. constructor Create(CreateSuspended: Boolean;
  1806. const StackSize: SizeUInt = DefaultStackSize);
  1807. destructor Destroy; override;
  1808. { Note: Once closures are supported aProc will be changed to TProc }
  1809. class function CreateAnonymousThread(aProc: TProcedure): TThread; static;
  1810. class procedure NameThreadForDebugging(aThreadName: UnicodeString; aThreadID: TThreadID = TThreadID(-1)); static; inline;
  1811. class procedure NameThreadForDebugging(aThreadName: AnsiString; aThreadID: TThreadID = TThreadID(-1)); static; inline;
  1812. class procedure SetReturnValue(aValue: Integer); static;
  1813. class function CheckTerminated: Boolean; static;
  1814. class procedure Synchronize(AThread: TThread; AMethod: TThreadMethod);
  1815. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  1816. class procedure Synchronize(AThread: TThread; AProcedure : TThreadProcedure);
  1817. {$endif}
  1818. class procedure Queue(aThread: TThread; aMethod: TThreadMethod); static;
  1819. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  1820. class procedure Queue(aThread: TThread; AProcedure : TThreadProcedure); static;
  1821. {$endif}
  1822. class procedure ForceQueue(aThread: TThread; aMethod: TThreadMethod); inline; static;
  1823. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  1824. class procedure ForceQueue(aThread: TThread; aMethod: TThreadProcedure); inline; static;
  1825. {$endif}
  1826. class procedure RemoveQueuedEvents(aThread: TThread; aMethod: TThreadMethod); static;
  1827. class procedure RemoveQueuedEvents(aMethod: TThreadMethod); static;
  1828. class procedure RemoveQueuedEvents(aThread: TThread); static;
  1829. class procedure SpinWait(aIterations: LongWord); static;
  1830. class procedure Sleep(aMilliseconds: Cardinal); static;
  1831. class procedure Yield; static;
  1832. { use HAS_TTHREAD_GETSYSTEMTIMES to implement a platform specific variant
  1833. which does not return a zeroed record }
  1834. class procedure GetSystemTimes(out aSystemTimes: TSystemTimes); static;
  1835. class function GetTickCount: LongWord; static; deprecated 'Use TThread.GetTickCount64 instead';
  1836. class function GetTickCount64: QWord; static;
  1837. // Object based
  1838. Class Function ExecuteInThread(AMethod : TThreadExecuteHandler; AOnTerminate : TNotifyEvent = Nil) : TThread; overload; static;
  1839. Class Function ExecuteInThread(AMethod : TThreadExecuteStatusHandler; AOnStatus : TThreadStatusNotifyEvent; AOnTerminate : TNotifyEvent = Nil) : TThread; overload;static;
  1840. // Plain methods.
  1841. Class Function ExecuteInThread(AMethod : TThreadExecuteCallback; AData : Pointer = Nil; AOnTerminate: TNotifyCallBack = Nil) : TThread; overload;static;
  1842. Class Function ExecuteInThread(AMethod : TThreadExecuteStatusCallback; AOnStatus : TThreadStatusNotifyCallback; AData : Pointer = Nil; AOnTerminate : TNotifyCallBack = Nil) : TThread; overload;static;
  1843. procedure AfterConstruction; override;
  1844. procedure Start;
  1845. procedure Resume; deprecated;
  1846. procedure Suspend; deprecated;
  1847. procedure Terminate;
  1848. function WaitFor: Integer;
  1849. class property CurrentThread: TThread read GetCurrentThread;
  1850. class property ProcessorCount: LongWord read FProcessorCount;
  1851. class property IsSingleProcessor: Boolean read GetIsSingleProcessor;
  1852. property FreeOnTerminate: Boolean read FFreeOnTerminate write FFreeOnTerminate;
  1853. property Handle: TThreadID read FHandle;
  1854. property ExternalThread: Boolean read FExternalThread;
  1855. property Priority: TThreadPriority read GetPriority write SetPriority;
  1856. property Suspended: Boolean read GetSuspended write SetSuspended;
  1857. property Finished: Boolean read FFinished;
  1858. property ThreadID: TThreadID read FThreadID;
  1859. property OnTerminate: TNotifyEvent read FOnTerminate write FOnTerminate;
  1860. property FatalException: TObject read FFatalException;
  1861. end;
  1862. { TComponent class }
  1863. TOperation = (opInsert, opRemove);
  1864. TComponentState = set of (csLoading, csReading, csWriting, csDestroying,
  1865. csDesigning, csAncestor, csUpdating, csFixups, csFreeNotification,
  1866. csInline, csDesignInstance);
  1867. TComponentStyle = set of (csInheritable, csCheckPropAvail, csSubComponent,
  1868. csTransient);
  1869. TGetChildProc = procedure (Child: TComponent) of object;
  1870. IVCLComObject = interface
  1871. ['{E07892A0-F52F-11CF-BD2F-0020AF0E5B81}']
  1872. function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
  1873. function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
  1874. function GetIDsOfNames(const IID: TGUID; Names: Pointer;
  1875. NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
  1876. function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
  1877. Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
  1878. function SafeCallException(ExceptObject: TObject; ExceptAddr: CodePointer): HResult;
  1879. procedure FreeOnRelease;
  1880. end;
  1881. IInterfaceComponentReference = interface
  1882. ['{3FEEC8E1-E400-4A24-BCAC-1F01476439B1}']
  1883. function GetComponent:TComponent;
  1884. end;
  1885. IDesignerNotify = interface
  1886. ['{B971E807-E3A6-11D1-AAB1-00C04FB16FBC}']
  1887. procedure Modified;
  1888. procedure Notification(AnObject: TPersistent; Operation: TOperation);
  1889. end;
  1890. TComponentEnumerator = class
  1891. private
  1892. FComponent: TComponent;
  1893. FPosition: Integer;
  1894. public
  1895. constructor Create(AComponent: TComponent);
  1896. function GetCurrent: TComponent;
  1897. function MoveNext: Boolean;
  1898. property Current: TComponent read GetCurrent;
  1899. end;
  1900. TBasicAction = class;
  1901. { TComponent }
  1902. TComponent = class(TPersistent,IUnknown,IInterfaceComponentReference)
  1903. private
  1904. FOwner: TComponent;
  1905. FName: TComponentName;
  1906. FTag: Ptrint;
  1907. FComponents: TFpList;
  1908. FFreeNotifies: TFpList;
  1909. FDesignInfo: Longint;
  1910. FVCLComObject: Pointer;
  1911. FComponentState: TComponentState;
  1912. function GetComObject: IUnknown;
  1913. function GetComponent(AIndex: Integer): TComponent;
  1914. function GetComponentCount: Integer;
  1915. function GetComponentIndex: Integer;
  1916. procedure Insert(AComponent: TComponent);
  1917. procedure ReadLeft(Reader: TReader);
  1918. procedure ReadTop(Reader: TReader);
  1919. procedure Remove(AComponent: TComponent);
  1920. procedure RemoveNotification(AComponent: TComponent);
  1921. procedure SetComponentIndex(Value: Integer);
  1922. procedure SetReference(Enable: Boolean);
  1923. procedure WriteLeft(Writer: TWriter);
  1924. procedure WriteTop(Writer: TWriter);
  1925. protected
  1926. FComponentStyle: TComponentStyle;
  1927. procedure ChangeName(const NewName: TComponentName);
  1928. procedure DefineProperties(Filer: TFiler); override;
  1929. procedure GetChildren(Proc: TGetChildProc; Root: TComponent); dynamic;
  1930. function GetChildOwner: TComponent; dynamic;
  1931. function GetChildParent: TComponent; dynamic;
  1932. function GetOwner: TPersistent; override;
  1933. procedure Loaded; virtual;
  1934. procedure Loading; virtual;
  1935. procedure Notification(AComponent: TComponent;
  1936. Operation: TOperation); virtual;
  1937. procedure PaletteCreated; dynamic;
  1938. procedure ReadState(Reader: TReader); virtual;
  1939. procedure SetAncestor(Value: Boolean);
  1940. procedure SetDesigning(Value: Boolean; SetChildren : Boolean = True);
  1941. procedure SetDesignInstance(Value: Boolean);
  1942. procedure SetInline(Value: Boolean);
  1943. procedure SetName(const NewName: TComponentName); virtual;
  1944. procedure SetChildOrder(Child: TComponent; Order: Integer); dynamic;
  1945. procedure SetParentComponent(Value: TComponent); dynamic;
  1946. procedure Updating; dynamic;
  1947. procedure Updated; dynamic;
  1948. class procedure UpdateRegistry(Register: Boolean; const ClassID, ProgID: string); dynamic;
  1949. procedure ValidateRename(AComponent: TComponent;
  1950. const CurName, NewName: string); virtual;
  1951. procedure ValidateContainer(AComponent: TComponent); dynamic;
  1952. procedure ValidateInsert(AComponent: TComponent); dynamic;
  1953. { IUnknown }
  1954. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} IID: TGUID; out Obj): Hresult; virtual; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  1955. function _AddRef: Longint; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  1956. function _Release: Longint; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  1957. function iicrGetComponent: TComponent;
  1958. { IDispatch }
  1959. function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
  1960. function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
  1961. function GetIDsOfNames(const IID: TGUID; Names: Pointer;
  1962. NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
  1963. function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
  1964. Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
  1965. public
  1966. //!! Moved temporary
  1967. // fpdoc doesn't handle this yet :(
  1968. {$ifndef fpdocsystem}
  1969. function IInterfaceComponentReference.GetComponent=iicrgetcomponent;
  1970. {$endif}
  1971. procedure WriteState(Writer: TWriter); virtual;
  1972. constructor Create(AOwner: TComponent); virtual;
  1973. destructor Destroy; override;
  1974. procedure BeforeDestruction; override;
  1975. procedure DestroyComponents;
  1976. procedure Destroying;
  1977. function ExecuteAction(Action: TBasicAction): Boolean; dynamic;
  1978. function FindComponent(const AName: string): TComponent;
  1979. procedure FreeNotification(AComponent: TComponent);
  1980. procedure RemoveFreeNotification(AComponent: TComponent);
  1981. procedure FreeOnRelease;
  1982. function GetEnumerator: TComponentEnumerator;
  1983. function GetNamePath: string; override;
  1984. function GetParentComponent: TComponent; dynamic;
  1985. function HasParent: Boolean; dynamic;
  1986. procedure InsertComponent(AComponent: TComponent);
  1987. procedure RemoveComponent(AComponent: TComponent);
  1988. function SafeCallException(ExceptObject: TObject;
  1989. ExceptAddr: CodePointer): HResult; override;
  1990. procedure SetSubComponent(ASubComponent: Boolean);
  1991. function UpdateAction(Action: TBasicAction): Boolean; dynamic;
  1992. property ComObject: IUnknown read GetComObject;
  1993. function IsImplementorOf (const Intf:IInterface):boolean;
  1994. procedure ReferenceInterface(const intf:IInterface;op:TOperation);
  1995. property Components[Index: Integer]: TComponent read GetComponent;
  1996. property ComponentCount: Integer read GetComponentCount;
  1997. property ComponentIndex: Integer read GetComponentIndex write SetComponentIndex;
  1998. property ComponentState: TComponentState read FComponentState;
  1999. property ComponentStyle: TComponentStyle read FComponentStyle;
  2000. property DesignInfo: Longint read FDesignInfo write FDesignInfo;
  2001. property Owner: TComponent read FOwner;
  2002. property VCLComObject: Pointer read FVCLComObject write FVCLComObject;
  2003. published
  2004. property Name: TComponentName read FName write SetName stored False;
  2005. property Tag: PtrInt read FTag write FTag default 0;
  2006. end;
  2007. { TBasicActionLink }
  2008. TBasicActionLink = class(TObject)
  2009. private
  2010. FOnChange: TNotifyEvent;
  2011. protected
  2012. FAction: TBasicAction;
  2013. procedure AssignClient(AClient: TObject); virtual;
  2014. procedure Change; virtual;
  2015. function IsOnExecuteLinked: Boolean; virtual;
  2016. procedure SetAction(Value: TBasicAction); virtual;
  2017. procedure SetOnExecute(Value: TNotifyEvent); virtual;
  2018. public
  2019. constructor Create(AClient: TObject); virtual;
  2020. destructor Destroy; override;
  2021. function Execute(AComponent: TComponent = nil): Boolean; virtual;
  2022. function Update: Boolean; virtual;
  2023. property Action: TBasicAction read FAction write SetAction;
  2024. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  2025. end;
  2026. TBasicActionLinkClass = class of TBasicActionLink;
  2027. { TBasicAction }
  2028. TBasicAction = class(TComponent)
  2029. private
  2030. FActionComponent: TComponent;
  2031. FOnChange: TNotifyEvent;
  2032. FOnExecute: TNotifyEvent;
  2033. FOnUpdate: TNotifyEvent;
  2034. protected
  2035. FClients: TFpList;
  2036. procedure Change; virtual;
  2037. procedure SetOnExecute(Value: TNotifyEvent); virtual;
  2038. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  2039. public
  2040. constructor Create(AOwner: TComponent); override;
  2041. destructor Destroy; override;
  2042. function HandlesTarget(Target: TObject): Boolean; virtual;
  2043. procedure UpdateTarget(Target: TObject); virtual;
  2044. procedure ExecuteTarget(Target: TObject); virtual;
  2045. function Execute: Boolean; dynamic;
  2046. procedure RegisterChanges(Value: TBasicActionLink);
  2047. procedure UnRegisterChanges(Value: TBasicActionLink);
  2048. function Update: Boolean; virtual;
  2049. property ActionComponent: TComponent read FActionComponent write FActionComponent;
  2050. property OnExecute: TNotifyEvent read FOnExecute write SetOnExecute;
  2051. property OnUpdate: TNotifyEvent read FOnUpdate write FOnUpdate;
  2052. end;
  2053. { TBasicAction class reference type }
  2054. TBasicActionClass = class of TBasicAction;
  2055. { Component registration handlers }
  2056. TActiveXRegType = (axrComponentOnly, axrIncludeDescendants);
  2057. IInterfaceList = interface ['{285DEA8A-B865-11D1-AAA7-00C04FB17A72}']
  2058. function Get(i : Integer) : IUnknown;
  2059. function GetCapacity : Integer;
  2060. function GetCount : Integer;
  2061. procedure Put(i : Integer;item : IUnknown);
  2062. procedure SetCapacity(NewCapacity : Integer);
  2063. procedure SetCount(NewCount : Integer);
  2064. procedure Clear;
  2065. procedure Delete(index : Integer);
  2066. procedure Exchange(index1,index2 : Integer);
  2067. function First : IUnknown;
  2068. function IndexOf(const item : IUnknown) : Integer;
  2069. function Add(item : IUnknown) : Integer;
  2070. procedure Insert(i : Integer;item : IUnknown);
  2071. function Last : IUnknown;
  2072. function Remove(item : IUnknown): Integer;
  2073. procedure Lock;
  2074. procedure Unlock;
  2075. property Capacity : Integer read GetCapacity write SetCapacity;
  2076. property Count : Integer read GetCount write SetCount;
  2077. property Items[index : Integer] : IUnknown read Get write Put;default;
  2078. end;
  2079. TInterfaceList = class;
  2080. TInterfaceListEnumerator = class
  2081. private
  2082. FList: TInterfaceList;
  2083. FPosition: Integer;
  2084. public
  2085. constructor Create(AList: TInterfaceList);
  2086. function GetCurrent: IUnknown;
  2087. function MoveNext: Boolean;
  2088. property Current: IUnknown read GetCurrent;
  2089. end;
  2090. TInterfaceList = class(TInterfacedObject,IInterfaceList)
  2091. private
  2092. FList : TThreadList;
  2093. protected
  2094. function Get(i : Integer) : IUnknown;
  2095. function GetCapacity : Integer;
  2096. function GetCount : Integer;
  2097. procedure Put(i : Integer;item : IUnknown);
  2098. procedure SetCapacity(NewCapacity : Integer);
  2099. procedure SetCount(NewCount : Integer);
  2100. public
  2101. constructor Create;
  2102. destructor Destroy; override;
  2103. procedure Clear;
  2104. procedure Delete(index : Integer);
  2105. procedure Exchange(index1,index2 : Integer);
  2106. function First : IUnknown;
  2107. function GetEnumerator: TInterfaceListEnumerator;
  2108. function IndexOf(const item : IUnknown) : Integer;
  2109. function Add(item : IUnknown) : Integer;
  2110. procedure Insert(i : Integer;item : IUnknown);
  2111. function Last : IUnknown;
  2112. function Remove(item : IUnknown): Integer;
  2113. procedure Lock;
  2114. procedure Unlock;
  2115. function Expand : TInterfaceList;
  2116. property Capacity : Integer read GetCapacity write SetCapacity;
  2117. property Count : Integer read GetCount write SetCount;
  2118. property Items[Index : Integer] : IUnknown read Get write Put;default;
  2119. end;
  2120. { ---------------------------------------------------------------------
  2121. TDatamodule support
  2122. ---------------------------------------------------------------------}
  2123. TDataModule = class(TComponent)
  2124. private
  2125. FDPos: TPoint;
  2126. FDSize: TPoint;
  2127. FDPPI: Integer;
  2128. FOnCreate: TNotifyEvent;
  2129. FOnDestroy: TNotifyEvent;
  2130. FOldOrder : Boolean;
  2131. Procedure ReadP(Reader: TReader);
  2132. Procedure WriteP(Writer: TWriter);
  2133. Procedure ReadT(Reader: TReader);
  2134. Procedure WriteT(Writer: TWriter);
  2135. Procedure ReadL(Reader: TReader);
  2136. Procedure WriteL(Writer: TWriter);
  2137. Procedure ReadW(Reader: TReader);
  2138. Procedure WriteW(Writer: TWriter);
  2139. Procedure ReadH(Reader: TReader);
  2140. Procedure WriteH(Writer: TWriter);
  2141. protected
  2142. Procedure DoCreate; virtual;
  2143. Procedure DoDestroy; virtual;
  2144. Procedure DefineProperties(Filer: TFiler); override;
  2145. Procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
  2146. Function HandleCreateException: Boolean; virtual;
  2147. Procedure ReadState(Reader: TReader); override;
  2148. public
  2149. constructor Create(AOwner: TComponent); override;
  2150. Constructor CreateNew(AOwner: TComponent);
  2151. Constructor CreateNew(AOwner: TComponent; CreateMode: Integer); virtual;
  2152. destructor Destroy; override;
  2153. Procedure AfterConstruction; override;
  2154. Procedure BeforeDestruction; override;
  2155. property DesignOffset: TPoint read FDPos write FDPos;
  2156. property DesignSize: TPoint read FDSize write FDSize;
  2157. property DesignPPI: Integer read FDPPI write FDPPI;
  2158. published
  2159. property OnCreate: TNotifyEvent read FOnCreate write FOnCreate;
  2160. property OnDestroy: TNotifyEvent read FOnDestroy write FOnDestroy;
  2161. property OldCreateOrder: Boolean read FOldOrder write FOldOrder;
  2162. end;
  2163. TDataModuleClass = Class of TDataModule;
  2164. var
  2165. // IDE hooks for TDatamodule support.
  2166. AddDataModule : procedure (DataModule: TDataModule) of object;
  2167. RemoveDataModule : procedure (DataModule: TDataModule) of object;
  2168. ApplicationHandleException : procedure (Sender: TObject) of object;
  2169. ApplicationShowException : procedure (E: Exception) of object;
  2170. { ---------------------------------------------------------------------
  2171. tthread helpers
  2172. ---------------------------------------------------------------------}
  2173. { function to be called when gui thread is ready to execute method
  2174. result is true if a method has been executed
  2175. }
  2176. function CheckSynchronize(timeout : longint=0) : boolean;
  2177. var
  2178. { method proc that is called to trigger gui thread to execute a
  2179. method }
  2180. WakeMainThread : TNotifyEvent = nil;
  2181. { ---------------------------------------------------------------------
  2182. General streaming and registration routines
  2183. ---------------------------------------------------------------------}
  2184. var
  2185. RegisterComponentsProc: procedure(const Page: string;
  2186. ComponentClasses: array of TComponentClass);
  2187. RegisterNoIconProc: procedure(ComponentClasses: array of TComponentClass);
  2188. {!!!! RegisterNonActiveXProc: procedure(ComponentClasses: array of TComponentClass;
  2189. AxRegType: TActiveXRegType) = nil;
  2190. CurrentGroup: Integer = -1;}
  2191. CreateVCLComObjectProc: procedure(Component: TComponent) = nil;
  2192. { Point and rectangle constructors }
  2193. function Point(AX, AY: Integer): TPoint;
  2194. function SmallPoint(AX, AY: SmallInt): TSmallPoint;
  2195. function Rect(ALeft, ATop, ARight, ABottom: Integer): TRect;
  2196. function Bounds(ALeft, ATop, AWidth, AHeight: Integer): TRect;
  2197. function PointsEqual(const P1, P2: TPoint): Boolean; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  2198. function PointsEqual(const P1, P2: TSmallPoint): Boolean; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  2199. function InvalidPoint(X, Y: Integer): Boolean;
  2200. function InvalidPoint(const At: TPoint): Boolean;
  2201. function InvalidPoint(const At: TSmallPoint): Boolean;
  2202. { Class registration routines }
  2203. procedure RegisterClass(AClass: TPersistentClass);
  2204. procedure RegisterClasses(AClasses: array of TPersistentClass);
  2205. procedure RegisterClassAlias(AClass: TPersistentClass; const Alias: string);
  2206. procedure UnRegisterClass(AClass: TPersistentClass);
  2207. procedure UnRegisterClasses(const AClasses: array of TPersistentClass);
  2208. procedure UnRegisterModuleClasses(Module: HMODULE);
  2209. function FindClass(const AClassName: string): TPersistentClass; overload;
  2210. function FindClass(const anUnitname, aClassName: string): TPersistentClass; overload;
  2211. function GetClass(const aClassName: string): TPersistentClass; overload;
  2212. function GetClass(const anUnitname, aClassName: string): TPersistentClass; overload;
  2213. procedure StartClassGroup(AClass: TPersistentClass);
  2214. procedure GroupDescendentsWith(AClass, AClassGroup: TPersistentClass);
  2215. function ActivateClassGroup(AClass: TPersistentClass): TPersistentClass;
  2216. function ClassGroupOf(AClass: TPersistentClass): TPersistentClass;
  2217. function ClassGroupOf(Instance: TPersistent): TPersistentClass;
  2218. { Component registration routines }
  2219. procedure RegisterComponents(const Page: string;
  2220. ComponentClasses: array of TComponentClass);
  2221. procedure RegisterNoIcon(ComponentClasses: array of TComponentClass);
  2222. procedure RegisterNonActiveX(ComponentClasses: array of TComponentClass;
  2223. AxRegType: TActiveXRegType);
  2224. var
  2225. GlobalNameSpace: IReadWriteSync;
  2226. { Object filing routines }
  2227. type
  2228. TIdentMapEntry = record
  2229. Value: Integer;
  2230. Name: String;
  2231. end;
  2232. TIdentToInt = function(const Ident: string; var Int: Longint): Boolean;
  2233. TIntToIdent = function(Int: Longint; var Ident: string): Boolean;
  2234. TFindGlobalComponent = function(const Name: string): TComponent;
  2235. TInitComponentHandler = function(Instance: TComponent; RootAncestor : TClass): boolean;
  2236. var
  2237. MainThreadID: TThreadID;
  2238. procedure RegisterIntegerConsts(IntegerType: Pointer; IdentToIntFn: TIdentToInt;
  2239. IntToIdentFn: TIntToIdent);
  2240. function IdentToInt(const Ident: string; out Int: Longint; const Map: array of TIdentMapEntry): Boolean;
  2241. function IntToIdent(Int: Longint; var Ident: string; const Map: array of TIdentMapEntry): Boolean;
  2242. function FindIntToIdent(AIntegerType: Pointer): TIntToIdent;
  2243. function FindIdentToInt(AIntegerType: Pointer): TIdentToInt;
  2244. procedure RegisterFindGlobalComponentProc(AFindGlobalComponent: TFindGlobalComponent);
  2245. procedure UnregisterFindGlobalComponentProc(AFindGlobalComponent: TFindGlobalComponent);
  2246. function FindGlobalComponent(const Name: string): TComponent;
  2247. function InitInheritedComponent(Instance: TComponent; RootAncestor: TClass): Boolean;
  2248. function InitComponentRes(const ResName: string; Instance: TComponent): Boolean;
  2249. function ReadComponentRes(const ResName: string; Instance: TComponent): TComponent;
  2250. function ReadComponentResEx(HInstance: THandle; const ResName: string): TComponent;
  2251. function ReadComponentResFile(const FileName: string; Instance: TComponent): TComponent;
  2252. procedure WriteComponentResFile(const FileName: string; Instance: TComponent);
  2253. procedure RegisterInitComponentHandler(ComponentClass: TComponentClass; Handler: TInitComponentHandler);
  2254. procedure GlobalFixupReferences;
  2255. procedure GetFixupReferenceNames(Root: TComponent; Names: TStrings);
  2256. procedure GetFixupInstanceNames(Root: TComponent;
  2257. const ReferenceRootName: string; Names: TStrings);
  2258. procedure RedirectFixupReferences(Root: TComponent; const OldRootName,
  2259. NewRootName: string);
  2260. procedure RemoveFixupReferences(Root: TComponent; const RootName: string);
  2261. procedure RemoveFixups(Instance: TPersistent);
  2262. Function FindNestedComponent(Root : TComponent; APath : String; CStyle : Boolean = True) : TComponent;
  2263. procedure BeginGlobalLoading;
  2264. procedure NotifyGlobalLoading;
  2265. procedure EndGlobalLoading;
  2266. function CollectionsEqual(C1, C2: TCollection): Boolean;
  2267. function CollectionsEqual(C1, C2: TCollection; Owner1, Owner2: TComponent): Boolean;
  2268. { Object conversion routines }
  2269. type
  2270. TObjectTextEncoding = (
  2271. oteDFM,
  2272. oteLFM
  2273. );
  2274. procedure ObjectBinaryToText(Input, Output: TStream; Encoding: TObjectTextEncoding);
  2275. procedure ObjectBinaryToText(Input, Output: TStream);
  2276. procedure ObjectTextToBinary(Input, Output: TStream);
  2277. procedure ObjectResourceToText(Input, Output: TStream);
  2278. procedure ObjectTextToResource(Input, Output: TStream);
  2279. { Utility routines }
  2280. function LineStart(Buffer, BufPos: PAnsiChar): PAnsiChar;
  2281. procedure BinToHex(BinValue, HexValue: PAnsiChar; BinBufSize: Integer); deprecated 'use procedures from unit StrUtils';
  2282. function HexToBin(HexValue, BinValue: PAnsiChar; BinBufSize: Integer): Integer;
  2283. function ExtractStrings(Separators, WhiteSpace: TSysCharSet; Content: PAnsiChar; Strings: TStrings; AddEmptyStrings : Boolean = False): Integer;
  2284. Function IfThen(AValue: Boolean; const ATrue: TStringList; const AFalse: TStringList = nil): TStringList; overload;