classesh.inc 93 KB

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