2
0

classesh.inc 88 KB

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