classes.inc 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 1999-2000 by Michael Van Canneyt and Florian Klaempfl
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {**********************************************************************
  11. * Class implementations are in separate files. *
  12. **********************************************************************}
  13. type
  14. {$ifdef CPU16}
  15. TFilerFlagsInt = Byte;
  16. {$else CPU16}
  17. TFilerFlagsInt = LongInt;
  18. {$endif CPU16}
  19. var
  20. ClassList : TThreadlist;
  21. ClassAliasList : TStringList;
  22. {
  23. Include all message strings
  24. Add a language with IFDEF LANG_NAME
  25. just befor the final ELSE. This way English will always be the default.
  26. }
  27. {$IFDEF LANG_GERMAN}
  28. {$i constsg.inc}
  29. {$ELSE}
  30. {$IFDEF LANG_SPANISH}
  31. {$i constss.inc}
  32. {$ENDIF}
  33. {$ENDIF}
  34. { Utility routines }
  35. {$i util.inc}
  36. { TBits implementation }
  37. {$i bits.inc}
  38. { All streams implementations: }
  39. { Tstreams THandleStream TFileStream TResourcseStreams TStringStream }
  40. { TCustomMemoryStream TMemoryStream }
  41. {$i streams.inc}
  42. { TParser implementation}
  43. {$i parser.inc}
  44. { TCollection and TCollectionItem implementations }
  45. {$i collect.inc}
  46. { TList and TThreadList implementations }
  47. {$i lists.inc}
  48. { TStrings and TStringList implementations }
  49. {$i stringl.inc}
  50. { TThread implementation }
  51. { system independend threading code }
  52. var
  53. { event executed by SychronizeInternal to wake main thread if it sleeps in
  54. CheckSynchronize }
  55. SynchronizeTimeoutEvent: PRtlEvent;
  56. { the head of the queue containing the entries to be Synchronized - Nil if the
  57. queue is empty }
  58. ThreadQueueHead: TThread.PThreadQueueEntry;
  59. { the tail of the queue containing the entries to be Synchronized - Nil if the
  60. queue is empty }
  61. ThreadQueueTail: TThread.PThreadQueueEntry;
  62. { used for serialized access to the queue }
  63. ThreadQueueLock: TRtlCriticalSection;
  64. { usage counter for ThreadQueueLock }
  65. ThreadQueueLockCounter : longint;
  66. { this list holds all instances of external threads that need to be freed at
  67. the end of the program }
  68. ExternalThreads: TThreadList;
  69. { this list signals that the ExternalThreads list is cleared and thus the
  70. thread instances don't need to remove themselves }
  71. ExternalThreadsCleanup: Boolean = False;
  72. { this must be a global var, otherwise unwanted optimizations might happen in
  73. TThread.SpinWait() }
  74. SpinWaitDummy: LongWord;
  75. {$ifdef FPC_HAS_FEATURE_THREADING}
  76. threadvar
  77. {$else}
  78. var
  79. {$endif}
  80. { the instance of the current thread; in case of an external thread this is
  81. Nil until TThread.GetCurrentThread was called once (the RTLs need to ensure
  82. that threadvars are initialized with 0!) }
  83. CurrentThreadVar: TThread;
  84. type
  85. { this type is used if a thread is created using
  86. TThread.CreateAnonymousThread }
  87. TAnonymousThread = class(TThread)
  88. private
  89. fProc: TProcedure;
  90. protected
  91. procedure Execute; override;
  92. public
  93. { as in TThread aProc needs to be changed to TProc once closures are
  94. supported }
  95. constructor Create(aProc: TProcedure);
  96. end;
  97. procedure TAnonymousThread.Execute;
  98. begin
  99. fProc();
  100. end;
  101. constructor TAnonymousThread.Create(aProc: TProcedure);
  102. begin
  103. { an anonymous thread is created suspended and with FreeOnTerminate set }
  104. inherited Create(True);
  105. FreeOnTerminate := True;
  106. fProc := aProc;
  107. end;
  108. type
  109. { this type is used by TThread.GetCurrentThread if the thread does not yet
  110. have a value in CurrentThreadVar (Note: the main thread is also created as
  111. a TExternalThread) }
  112. TExternalThread = class(TThread)
  113. protected
  114. { dummy method to remove the warning }
  115. procedure Execute; override;
  116. public
  117. constructor Create;
  118. destructor Destroy; override;
  119. end;
  120. procedure TExternalThread.Execute;
  121. begin
  122. { empty }
  123. end;
  124. constructor TExternalThread.Create;
  125. begin
  126. FExternalThread := True;
  127. { the parameter is unimportant if FExternalThread is True }
  128. inherited Create(False);
  129. with ExternalThreads.LockList do
  130. try
  131. Add(Self);
  132. finally
  133. ExternalThreads.UnlockList;
  134. end;
  135. end;
  136. destructor TExternalThread.Destroy;
  137. begin
  138. inherited;
  139. if not ExternalThreadsCleanup then
  140. with ExternalThreads.LockList do
  141. try
  142. Extract(Self);
  143. finally
  144. ExternalThreads.UnlockList;
  145. end;
  146. end;
  147. function ThreadProc(ThreadObjPtr: Pointer): PtrInt;
  148. var
  149. FreeThread: Boolean;
  150. Thread: TThread absolute ThreadObjPtr;
  151. begin
  152. { if Suspend checks FSuspended before doing anything, make sure it }
  153. { knows we're currently not suspended (this flag may have been set }
  154. { to true if CreateSuspended was true) }
  155. // Thread.FSuspended:=false;
  156. // wait until AfterConstruction has been called, so we cannot
  157. // free ourselves before TThread.Create has finished
  158. // (since that one may check our VTM in case of $R+, and
  159. // will call the AfterConstruction method in all cases)
  160. // Thread.Suspend;
  161. try
  162. { The thread may be already terminated at this point, e.g. if it was intially
  163. suspended, or if it wasn't ever scheduled for execution for whatever reason.
  164. So bypass user code if terminated. }
  165. if not Thread.Terminated then begin
  166. CurrentThreadVar := Thread;
  167. Thread.Execute;
  168. end;
  169. except
  170. Thread.FFatalException := TObject(AcquireExceptionObject);
  171. end;
  172. FreeThread := Thread.FFreeOnTerminate;
  173. Result := Thread.FReturnValue;
  174. Thread.FFinished := True;
  175. Thread.DoTerminate;
  176. if FreeThread then
  177. Thread.Free;
  178. {$ifdef FPC_HAS_FEATURE_THREADING}
  179. EndThread(Result);
  180. {$endif}
  181. end;
  182. { system-dependent code }
  183. {$i tthread.inc}
  184. constructor TThread.Create(CreateSuspended: Boolean;
  185. const StackSize: SizeUInt);
  186. begin
  187. inherited Create;
  188. {$ifdef FPC_HAS_FEATURE_THREADING}
  189. InterlockedIncrement(ThreadQueueLockCounter);
  190. {$endif}
  191. if FExternalThread then
  192. {$ifdef FPC_HAS_FEATURE_THREADING}
  193. FThreadID := GetCurrentThreadID
  194. {$else}
  195. FThreadID := 0{GetCurrentThreadID}
  196. {$endif}
  197. else
  198. SysCreate(CreateSuspended, StackSize);
  199. end;
  200. destructor TThread.Destroy;
  201. begin
  202. if not FExternalThread then begin
  203. SysDestroy;
  204. {$ifdef FPC_HAS_FEATURE_THREADING}
  205. if FHandle <> TThreadID(0) then
  206. CloseThread(FHandle);
  207. {$endif}
  208. end;
  209. RemoveQueuedEvents(Self);
  210. DoneSynchronizeEvent;
  211. {$ifdef FPC_HAS_FEATURE_THREADING}
  212. if InterlockedDecrement(ThreadQueueLockCounter)=0 then
  213. DoneCriticalSection(ThreadQueueLock);
  214. {$endif}
  215. { set CurrentThreadVar to Nil? }
  216. inherited Destroy;
  217. end;
  218. procedure TThread.Start;
  219. begin
  220. { suspend/resume are now deprecated in Delphi (they also don't work
  221. on most platforms in FPC), so a different method was required
  222. to start a thread if it's create with fSuspended=true -> that's
  223. what this method is for. }
  224. Resume;
  225. end;
  226. function TThread.GetSuspended: Boolean;
  227. begin
  228. GetSuspended:=FSuspended;
  229. end;
  230. procedure TThread.Terminate;
  231. begin
  232. FTerminated := True;
  233. TerminatedSet;
  234. end;
  235. Procedure TThread.TerminatedSet;
  236. begin
  237. // Empty, must be overridden.
  238. end;
  239. procedure TThread.AfterConstruction;
  240. begin
  241. inherited AfterConstruction;
  242. // enable for all platforms once http://bugs.freepascal.org/view.php?id=16884
  243. // is fixed for all platforms (in case the fix for non-unix platforms also
  244. // requires this field at least)
  245. {$if defined(unix) or defined(windows) or defined(os2) or defined(hasamiga)}
  246. if not FExternalThread and not FInitialSuspended then
  247. Resume;
  248. {$endif}
  249. end;
  250. procedure ExecuteThreadQueueEntry(aEntry: TThread.PThreadQueueEntry);
  251. begin
  252. if Assigned(aEntry^.Method) then
  253. aEntry^.Method()
  254. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  255. else
  256. if Assigned(aEntry^.ThreadProc) then
  257. aEntry^.ThreadProc
  258. {$endif}
  259. end;
  260. procedure ThreadQueueAppend(aEntry: TThread.PThreadQueueEntry; aQueueIfMain: Boolean);
  261. var
  262. thd: TThread;
  263. issync: Boolean;
  264. begin
  265. { do we really need a synchronized call? }
  266. {$ifdef FPC_HAS_FEATURE_THREADING}
  267. if (GetCurrentThreadID = MainThreadID) and (not aQueueIfMain or not IsMultiThread) then
  268. {$endif}
  269. begin
  270. try
  271. ExecuteThreadQueueEntry(aEntry);
  272. finally
  273. if not Assigned(aEntry^.SyncEvent) then
  274. Dispose(aEntry);
  275. end;
  276. {$ifdef FPC_HAS_FEATURE_THREADING}
  277. end else begin
  278. { store thread and whether we're dealing with a synchronized event; the
  279. event record itself might already be freed after the ThreadQueueLock is
  280. released (in case of a Queue() call; for a Synchronize() call the record
  281. will stay valid, thus accessing SyncEvent later on (if issync is true) is
  282. okay) }
  283. thd := aEntry^.Thread;
  284. issync := Assigned(aEntry^.SyncEvent);
  285. System.EnterCriticalSection(ThreadQueueLock);
  286. try
  287. { add the entry to the thread queue }
  288. if Assigned(ThreadQueueTail) then begin
  289. ThreadQueueTail^.Next := aEntry;
  290. end else
  291. ThreadQueueHead := aEntry;
  292. ThreadQueueTail := aEntry;
  293. finally
  294. System.LeaveCriticalSection(ThreadQueueLock);
  295. end;
  296. { ensure that the main thread knows that something awaits }
  297. RtlEventSetEvent(SynchronizeTimeoutEvent);
  298. if assigned(WakeMainThread) then
  299. WakeMainThread(thd);
  300. { is this a Synchronize or Queue entry? }
  301. if issync then begin
  302. RtlEventWaitFor(aEntry^.SyncEvent);
  303. if Assigned(aEntry^.Exception) then
  304. raise aEntry^.Exception;
  305. end;
  306. {$endif def FPC_HAS_FEATURE_THREADING}
  307. end;
  308. end;
  309. procedure TThread.InitSynchronizeEvent;
  310. begin
  311. if Assigned(FSynchronizeEntry) then
  312. Exit;
  313. New(FSynchronizeEntry);
  314. FillChar(FSynchronizeEntry^, SizeOf(TThreadQueueEntry), 0);
  315. FSynchronizeEntry^.Thread := Self;
  316. FSynchronizeEntry^.ThreadID := ThreadID;
  317. {$ifdef FPC_HAS_FEATURE_THREADING}
  318. FSynchronizeEntry^.SyncEvent := RtlEventCreate;
  319. {$else}
  320. FSynchronizeEntry^.SyncEvent := nil{RtlEventCreate};
  321. {$endif}
  322. end;
  323. procedure TThread.DoneSynchronizeEvent;
  324. begin
  325. if not Assigned(FSynchronizeEntry) then
  326. Exit;
  327. {$ifdef FPC_HAS_FEATURE_THREADING}
  328. RtlEventDestroy(FSynchronizeEntry^.SyncEvent);
  329. {$endif}
  330. Dispose(FSynchronizeEntry);
  331. FSynchronizeEntry := Nil;
  332. end;
  333. class procedure TThread.Synchronize(AThread: TThread; AMethod: TThreadMethod);
  334. var
  335. syncentry: PThreadQueueEntry;
  336. thread: TThread;
  337. begin
  338. {$ifdef FPC_HAS_FEATURE_THREADING}
  339. if Assigned(AThread) and (AThread.ThreadID = GetCurrentThreadID) then
  340. {$else}
  341. if Assigned(AThread) then
  342. {$endif}
  343. thread := AThread
  344. else if Assigned(CurrentThreadVar) then
  345. thread := CurrentThreadVar
  346. else begin
  347. thread := Nil;
  348. { use a local synchronize event }
  349. New(syncentry);
  350. FillChar(syncentry^, SizeOf(TThreadQueueEntry), 0);
  351. {$ifdef FPC_HAS_FEATURE_THREADING}
  352. syncentry^.ThreadID := GetCurrentThreadID;
  353. syncentry^.SyncEvent := RtlEventCreate;
  354. {$else}
  355. syncentry^.ThreadID := 0{GetCurrentThreadID};
  356. syncentry^.SyncEvent := nil{RtlEventCreate};
  357. {$endif}
  358. end;
  359. if Assigned(thread) then begin
  360. { the Synchronize event is instantiated on demand }
  361. thread.InitSynchronizeEvent;
  362. syncentry := thread.FSynchronizeEntry;
  363. end;
  364. syncentry^.Exception := Nil;
  365. syncentry^.Method := AMethod;
  366. try
  367. ThreadQueueAppend(syncentry, False);
  368. finally
  369. syncentry^.Method := Nil;
  370. syncentry^.Next := Nil;
  371. if not Assigned(thread) then begin
  372. { clean up again }
  373. {$ifdef FPC_HAS_FEATURE_THREADING}
  374. RtlEventDestroy(syncentry^.SyncEvent);
  375. {$endif}
  376. Dispose(syncentry);
  377. end;
  378. end;
  379. end;
  380. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  381. class procedure TThread.Synchronize(AThread: TThread; AProcedure: TThreadProcedure);
  382. var
  383. syncentry: PThreadQueueEntry;
  384. thread: TThread;
  385. begin
  386. {$ifdef FPC_HAS_FEATURE_THREADING}
  387. if Assigned(AThread) and (AThread.ThreadID = GetCurrentThreadID) then
  388. {$else}
  389. if Assigned(AThread) then
  390. {$endif}
  391. thread := AThread
  392. else if Assigned(CurrentThreadVar) then
  393. thread := CurrentThreadVar
  394. else begin
  395. thread := Nil;
  396. { use a local synchronize event }
  397. New(syncentry);
  398. FillChar(syncentry^, SizeOf(TThreadQueueEntry), 0);
  399. {$ifdef FPC_HAS_FEATURE_THREADING}
  400. syncentry^.ThreadID := GetCurrentThreadID;
  401. syncentry^.SyncEvent := RtlEventCreate;
  402. {$else}
  403. syncentry^.ThreadID := 0{GetCurrentThreadID};
  404. syncentry^.SyncEvent := nil{RtlEventCreate};
  405. {$endif}
  406. end;
  407. if Assigned(thread) then begin
  408. { the Synchronize event is instantiated on demand }
  409. thread.InitSynchronizeEvent;
  410. syncentry := thread.FSynchronizeEntry;
  411. end;
  412. syncentry^.Exception := Nil;
  413. syncentry^.ThreadProc := AProcedure;
  414. try
  415. ThreadQueueAppend(syncentry, False);
  416. finally
  417. syncentry^.ThreadProc := Nil;
  418. syncentry^.Next := Nil;
  419. if not Assigned(thread) then begin
  420. { clean up again }
  421. {$ifdef FPC_HAS_FEATURE_THREADING}
  422. RtlEventDestroy(syncentry^.SyncEvent);
  423. {$endif}
  424. Dispose(syncentry);
  425. end;
  426. end;
  427. end;
  428. {$endif}
  429. procedure TThread.Synchronize(AMethod: TThreadMethod);
  430. begin
  431. TThread.Synchronize(self,AMethod);
  432. end;
  433. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  434. procedure TThread.Synchronize(AProcedure: TThreadProcedure);
  435. begin
  436. TThread.Synchronize(self,AProcedure);
  437. end;
  438. {$endif}
  439. Function PopThreadQueueHead : TThread.PThreadQueueEntry;
  440. begin
  441. Result:=ThreadQueueHead;
  442. if (Result<>Nil) then
  443. begin
  444. {$ifdef FPC_HAS_FEATURE_THREADING}
  445. System.EnterCriticalSection(ThreadQueueLock);
  446. try
  447. {$endif}
  448. Result:=ThreadQueueHead;
  449. if Result<>Nil then
  450. ThreadQueueHead:=ThreadQueueHead^.Next;
  451. if Not Assigned(ThreadQueueHead) then
  452. ThreadQueueTail := Nil;
  453. {$ifdef FPC_HAS_FEATURE_THREADING}
  454. finally
  455. System.LeaveCriticalSection(ThreadQueueLock);
  456. end;
  457. {$endif}
  458. end;
  459. end;
  460. function CheckSynchronize(timeout : longint=0) : boolean;
  461. { assumes being called from GUI thread }
  462. var
  463. ExceptObj: TObject;
  464. tmpentry: TThread.PThreadQueueEntry;
  465. begin
  466. result:=false;
  467. { first sanity check }
  468. if Not IsMultiThread then
  469. Exit
  470. {$ifdef FPC_HAS_FEATURE_THREADING}
  471. { second sanity check }
  472. else if GetCurrentThreadID<>MainThreadID then
  473. raise EThread.CreateFmt(SCheckSynchronizeError,[GetCurrentThreadID]);
  474. if timeout>0 then
  475. RtlEventWaitFor(SynchronizeTimeoutEvent,timeout)
  476. else
  477. RtlEventResetEvent(SynchronizeTimeoutEvent);
  478. tmpentry := PopThreadQueueHead;
  479. while Assigned(tmpentry) do
  480. begin
  481. { step 2: execute the method }
  482. exceptobj := Nil;
  483. try
  484. ExecuteThreadQueueEntry(tmpentry);
  485. except
  486. exceptobj := TObject(AcquireExceptionObject);
  487. end;
  488. { step 3: error handling and cleanup }
  489. if Assigned(tmpentry^.SyncEvent) then
  490. begin
  491. { for Synchronize entries we pass back the Exception and trigger
  492. the event that Synchronize waits in }
  493. tmpentry^.Exception := exceptobj;
  494. RtlEventSetEvent(tmpentry^.SyncEvent)
  495. end
  496. else
  497. begin
  498. { for Queue entries we dispose the entry and raise the exception }
  499. Dispose(tmpentry);
  500. if Assigned(exceptobj) then
  501. raise exceptobj;
  502. end;
  503. tmpentry := PopThreadQueueHead;
  504. end
  505. {$endif};
  506. end;
  507. class function TThread.GetCurrentThread: TThread;
  508. begin
  509. { if this is the first time GetCurrentThread is called for an external thread
  510. we need to create a corresponding TExternalThread instance }
  511. Result := CurrentThreadVar;
  512. if not Assigned(Result) then begin
  513. Result := TExternalThread.Create;
  514. CurrentThreadVar := Result;
  515. end;
  516. end;
  517. class function TThread.GetIsSingleProcessor: Boolean;
  518. begin
  519. Result := FProcessorCount <= 1;
  520. end;
  521. procedure TThread.Queue(aMethod: TThreadMethod);
  522. begin
  523. Queue(Self, aMethod);
  524. end;
  525. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  526. procedure TThread.Queue(aProcedure: TThreadProcedure);
  527. begin
  528. Queue(Self, aProcedure);
  529. end;
  530. {$endif}
  531. class procedure TThread.Queue(aThread: TThread; aMethod: TThreadMethod); static;
  532. begin
  533. InternalQueue(aThread, aMethod, False);
  534. end;
  535. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  536. class procedure TThread.Queue(aThread: TThread; aProcedure: TThreadProcedure); static;
  537. begin
  538. InternalQueue(aThread, aProcedure, False);
  539. end;
  540. {$endif}
  541. class procedure TThread.InternalQueue(aThread: TThread; aMethod: TThreadMethod; aQueueIfMain: Boolean); static;
  542. var
  543. queueentry: PThreadQueueEntry;
  544. begin
  545. New(queueentry);
  546. FillChar(queueentry^, SizeOf(TThreadQueueEntry), 0);
  547. queueentry^.Thread := aThread;
  548. {$ifdef FPC_HAS_FEATURE_THREADING}
  549. queueentry^.ThreadID := GetCurrentThreadID;
  550. {$else}
  551. queueentry^.ThreadID := 0{GetCurrentThreadID};
  552. {$endif}
  553. queueentry^.Method := aMethod;
  554. { the queueentry is freed by CheckSynchronize (or by RemoveQueuedEvents) }
  555. ThreadQueueAppend(queueentry, aQueueIfMain);
  556. end;
  557. {$ifdef FPC_HAS_REFERENCE_PROCEDURE}
  558. class procedure TThread.InternalQueue(aThread: TThread; aProcedure: TThreadProcedure; aQueueIfMain: Boolean); static;
  559. var
  560. queueentry: PThreadQueueEntry;
  561. begin
  562. New(queueentry);
  563. FillChar(queueentry^, SizeOf(TThreadQueueEntry), 0);
  564. queueentry^.Thread := aThread;
  565. {$ifdef FPC_HAS_FEATURE_THREADING}
  566. queueentry^.ThreadID := GetCurrentThreadID;
  567. {$else}
  568. queueentry^.ThreadID := 0{GetCurrentThreadID};
  569. {$endif}
  570. queueentry^.ThreadProc := aProcedure;
  571. { the queueentry is freed by CheckSynchronize (or by RemoveQueuedEvents) }
  572. ThreadQueueAppend(queueentry, aQueueIfMain);
  573. end;
  574. {$endif}
  575. procedure TThread.ForceQueue(aMethod: TThreadMethod);
  576. begin
  577. ForceQueue(Self, aMethod);
  578. end;
  579. class procedure TThread.ForceQueue(aThread: TThread; aMethod: TThreadMethod); static;
  580. begin
  581. InternalQueue(aThread, aMethod, True);
  582. end;
  583. class procedure TThread.RemoveQueuedEvents(aThread: TThread; aMethod: TThreadMethod);
  584. var
  585. entry, tmpentry, lastentry: PThreadQueueEntry;
  586. begin
  587. { anything to do at all? }
  588. if not Assigned(aThread) and not Assigned(aMethod) then
  589. Exit;
  590. {$ifdef FPC_HAS_FEATURE_THREADING}
  591. System.EnterCriticalSection(ThreadQueueLock);
  592. try
  593. {$endif}
  594. lastentry := Nil;
  595. entry := ThreadQueueHead;
  596. while Assigned(entry) do begin
  597. if
  598. { only entries not added by Synchronize }
  599. not Assigned(entry^.SyncEvent)
  600. { check for the thread }
  601. and (not Assigned(aThread) or (entry^.Thread = aThread) or (entry^.ThreadID = aThread.ThreadID))
  602. { check for the method }
  603. and (not Assigned(aMethod) or
  604. (
  605. (TMethod(entry^.Method).Code = TMethod(aMethod).Code) and
  606. (TMethod(entry^.Method).Data = TMethod(aMethod).Data)
  607. ))
  608. then begin
  609. { ok, we need to remove this entry }
  610. tmpentry := entry;
  611. if Assigned(lastentry) then
  612. lastentry^.Next := entry^.Next;
  613. entry := entry^.Next;
  614. if ThreadQueueHead = tmpentry then
  615. ThreadQueueHead := entry;
  616. if ThreadQueueTail = tmpentry then
  617. ThreadQueueTail := lastentry;
  618. { only dispose events added by Queue }
  619. if not Assigned(tmpentry^.SyncEvent) then
  620. Dispose(tmpentry);
  621. end else begin
  622. { leave this entry }
  623. lastentry := entry;
  624. entry := entry^.Next;
  625. end;
  626. end;
  627. {$ifdef FPC_HAS_FEATURE_THREADING}
  628. finally
  629. System.LeaveCriticalSection(ThreadQueueLock);
  630. end;
  631. {$endif}
  632. end;
  633. class procedure TThread.RemoveQueuedEvents(aMethod: TThreadMethod);
  634. begin
  635. RemoveQueuedEvents(Nil, aMethod);
  636. end;
  637. class procedure TThread.RemoveQueuedEvents(aThread: TThread);
  638. begin
  639. RemoveQueuedEvents(aThread, Nil);
  640. end;
  641. class function TThread.CheckTerminated: Boolean;
  642. begin
  643. { this method only works with threads created by TThread, so we can make a
  644. shortcut here }
  645. if not Assigned(CurrentThreadVar) then
  646. raise EThreadExternalException.Create(SThreadExternal);
  647. Result := CurrentThreadVar.FTerminated;
  648. end;
  649. class procedure TThread.SetReturnValue(aValue: Integer);
  650. begin
  651. { this method only works with threads created by TThread, so we can make a
  652. shortcut here }
  653. if not Assigned(CurrentThreadVar) then
  654. raise EThreadExternalException.Create(SThreadExternal);
  655. CurrentThreadVar.FReturnValue := aValue;
  656. end;
  657. class function TThread.CreateAnonymousThread(aProc: TProcedure): TThread;
  658. begin
  659. if not Assigned(aProc) then
  660. raise Exception.Create(SNoProcGiven);
  661. Result := TAnonymousThread.Create(aProc);
  662. end;
  663. class procedure TThread.NameThreadForDebugging(aThreadName: UnicodeString; aThreadID: TThreadID);
  664. begin
  665. {$ifdef FPC_HAS_FEATURE_THREADING}
  666. SetThreadDebugName(aThreadID, aThreadName);
  667. {$endif}
  668. end;
  669. class procedure TThread.NameThreadForDebugging(aThreadName: AnsiString; aThreadID: TThreadID);
  670. begin
  671. {$ifdef FPC_HAS_FEATURE_THREADING}
  672. SetThreadDebugName(aThreadID, aThreadName);
  673. {$endif}
  674. end;
  675. class procedure TThread.Yield;
  676. begin
  677. {$ifdef FPC_HAS_FEATURE_THREADING}
  678. ThreadSwitch;
  679. {$endif}
  680. end;
  681. class procedure TThread.Sleep(aMilliseconds: Cardinal);
  682. begin
  683. SysUtils.Sleep(aMilliseconds);
  684. end;
  685. class procedure TThread.SpinWait(aIterations: LongWord);
  686. var
  687. i: LongWord;
  688. begin
  689. { yes, it's just a simple busy wait to burn some cpu cycles... and as the job
  690. of this loop is to burn CPU cycles we switch off any optimizations that
  691. could interfere with this (e.g. loop unrolling) }
  692. { Do *NOT* do $PUSH, $OPTIMIZATIONS OFF, <code>, $POP because optimization is
  693. not a local switch, which means $PUSH/POP doesn't affect it, so that turns
  694. off *ALL* optimizations for code below this point. Thanks to this we shipped
  695. large parts of the classes unit with optimizations off between 2012-12-27
  696. and 2014-06-06.
  697. Instead, use a global var for the spinlock, because that is always handled
  698. as volatile, so the access won't be optimized away by the compiler. (KB) }
  699. for i:=1 to aIterations do
  700. begin
  701. Inc(SpinWaitDummy); // SpinWaitDummy *MUST* be global
  702. end;
  703. end;
  704. {$ifndef HAS_TTHREAD_GETSYSTEMTIMES}
  705. class procedure TThread.GetSystemTimes(out aSystemTimes: TSystemTimes);
  706. begin
  707. { by default we just return a zeroed out record }
  708. FillChar(aSystemTimes, SizeOf(aSystemTimes), 0);
  709. end;
  710. {$endif}
  711. class function TThread.GetTickCount: LongWord;
  712. begin
  713. Result := SysUtils.GetTickCount;
  714. end;
  715. class function TThread.GetTickCount64: QWord;
  716. begin
  717. Result := SysUtils.GetTickCount64;
  718. end;
  719. { TSimpleThread allows objects to create a threading method without defining
  720. a new thread class }
  721. Type
  722. TSimpleThread = class(TThread)
  723. private
  724. FExecuteMethod: TThreadExecuteHandler;
  725. protected
  726. procedure Execute; override;
  727. public
  728. constructor Create(ExecuteMethod: TThreadExecuteHandler; AOnterminate : TNotifyEvent);
  729. end;
  730. TSimpleStatusThread = class(TThread)
  731. private
  732. FExecuteMethod: TThreadExecuteStatusHandler;
  733. FStatus : String;
  734. FOnStatus : TThreadStatusNotifyEvent;
  735. protected
  736. procedure Execute; override;
  737. Procedure DoStatus;
  738. Procedure SetStatus(Const AStatus : String);
  739. public
  740. constructor Create(ExecuteMethod: TThreadExecuteStatusHandler; AOnStatus : TThreadStatusNotifyEvent; AOnterminate : TNotifyEvent);
  741. end;
  742. TSimpleProcThread = class(TThread)
  743. private
  744. FExecuteMethod: TThreadExecuteCallBack;
  745. FCallOnTerminate : TNotifyCallBack;
  746. FData : Pointer;
  747. protected
  748. Procedure TerminateCallBack(Sender : TObject);
  749. procedure Execute; override;
  750. public
  751. constructor Create(ExecuteMethod: TThreadExecuteCallBack; AData : Pointer; AOnterminate : TNotifyCallBack);
  752. end;
  753. TSimpleStatusProcThread = class(TThread)
  754. private
  755. FExecuteMethod: TThreadExecuteStatusCallBack;
  756. FCallOnTerminate : TNotifyCallBack;
  757. FStatus : String;
  758. FOnStatus : TThreadStatusNotifyCallBack;
  759. FData : Pointer;
  760. protected
  761. procedure Execute; override;
  762. Procedure DoStatus;
  763. Procedure SetStatus(Const AStatus : String);
  764. Procedure TerminateCallBack(Sender : TObject);
  765. public
  766. constructor Create(ExecuteMethod: TThreadExecuteStatusCallBack; AData : Pointer; AOnStatus : TThreadStatusNotifyCallBack; AOnterminate : TNotifyCallBack);
  767. end;
  768. { TSimpleThread }
  769. constructor TSimpleThread.Create(ExecuteMethod: TThreadExecuteHandler; AOnTerminate: TNotifyEvent);
  770. begin
  771. FExecuteMethod := ExecuteMethod;
  772. OnTerminate := AOnTerminate;
  773. inherited Create(False);
  774. end;
  775. procedure TSimpleThread.Execute;
  776. begin
  777. FreeOnTerminate := True;
  778. FExecuteMethod;
  779. end;
  780. { TSimpleStatusThread }
  781. constructor TSimpleStatusThread.Create(ExecuteMethod: TThreadExecuteStatusHandler;AOnStatus : TThreadStatusNotifyEvent; AOnTerminate: TNotifyEvent);
  782. begin
  783. FExecuteMethod := ExecuteMethod;
  784. OnTerminate := AOnTerminate;
  785. FOnStatus:=AOnStatus;
  786. FStatus:='';
  787. inherited Create(False);
  788. end;
  789. procedure TSimpleStatusThread.Execute;
  790. begin
  791. FreeOnTerminate := True;
  792. FExecuteMethod(@SetStatus);
  793. end;
  794. procedure TSimpleStatusThread.SetStatus(Const AStatus : String);
  795. begin
  796. If (AStatus=FStatus) then
  797. exit;
  798. FStatus:=AStatus;
  799. If Assigned(FOnStatus) then
  800. Synchronize(@DoStatus);
  801. end;
  802. procedure TSimpleStatusThread.DoStatus;
  803. begin
  804. FOnStatus(Self,FStatus);
  805. end;
  806. { TSimpleProcThread }
  807. constructor TSimpleProcThread.Create(ExecuteMethod: TThreadExecuteCallBack; AData : Pointer; AOnTerminate: TNotifyCallBack);
  808. begin
  809. FExecuteMethod := ExecuteMethod;
  810. FCallOnTerminate := AOnTerminate;
  811. FData:=AData;
  812. If Assigned(FCallOnTerminate) then
  813. OnTerminate:=@TerminateCallBack;
  814. inherited Create(False);
  815. end;
  816. procedure TSimpleProcThread.Execute;
  817. begin
  818. FreeOnTerminate := True;
  819. FExecuteMethod(FData);
  820. end;
  821. procedure TSimpleProcThread.TerminateCallBack(Sender : TObject);
  822. begin
  823. if Assigned(FCallOnTerminate) then
  824. FCallOnTerminate(Sender,FData);
  825. end;
  826. { TSimpleStatusProcThread }
  827. constructor TSimpleStatusProcThread.Create(ExecuteMethod: TThreadExecuteStatusCallback; AData : Pointer; AOnStatus : TThreadStatusNotifyCallBack; AOnTerminate: TNotifyCallBack);
  828. begin
  829. FExecuteMethod := ExecuteMethod;
  830. FCallOnTerminate := AOnTerminate;
  831. FData:=AData;
  832. If Assigned(FCallOnTerminate) then
  833. OnTerminate:=@TerminateCallBack;
  834. FOnStatus:=AOnStatus;
  835. FStatus:='';
  836. inherited Create(False);
  837. end;
  838. procedure TSimpleStatusProcThread.Execute;
  839. begin
  840. FreeOnTerminate := True;
  841. FExecuteMethod(FData,@SetStatus);
  842. end;
  843. procedure TSimpleStatusProcThread.SetStatus(Const AStatus : String);
  844. begin
  845. If (AStatus=FStatus) then
  846. exit;
  847. FStatus:=AStatus;
  848. If Assigned(FOnStatus) then
  849. Synchronize(@DoStatus);
  850. end;
  851. procedure TSimpleStatusProcThread.DoStatus;
  852. begin
  853. FOnStatus(Self,FData,FStatus);
  854. end;
  855. procedure TSimpleStatusProcThread.TerminateCallBack(Sender : TObject);
  856. begin
  857. if Assigned(FCallOnTerminate) then
  858. FCallOnTerminate(Sender,FData);
  859. end;
  860. Class Function TThread.ExecuteInThread(AMethod : TThreadExecuteHandler; AOnTerminate : TNotifyEvent = Nil) : TThread;
  861. begin
  862. Result:=TSimpleThread.Create(AMethod,AOnTerminate);
  863. end;
  864. Class Function TThread.ExecuteInThread(AMethod : TThreadExecuteCallback; AData : Pointer; AOnTerminate : TNotifyCallback = Nil) : TThread;
  865. begin
  866. Result:=TSimpleProcThread.Create(AMethod,AData,AOnTerminate);
  867. end;
  868. Class Function TThread.ExecuteInThread(AMethod : TThreadExecuteStatusHandler; AOnStatus : TThreadStatusNotifyEvent; AOnTerminate : TNotifyEvent = Nil) : TThread;
  869. begin
  870. If Not Assigned(AOnStatus) then
  871. Raise EThread.Create(SErrStatusCallBackRequired);
  872. Result:=TSimpleStatusThread.Create(AMethod,AOnStatus,AOnTerminate);
  873. end;
  874. Class Function TThread.ExecuteInThread(AMethod : TThreadExecuteStatusCallback; AOnStatus : TThreadStatusNotifyCallback;AData : Pointer = Nil; AOnTerminate : TNotifyCallBack = Nil) : TThread;
  875. begin
  876. If Not Assigned(AOnStatus) then
  877. Raise EThread.Create(SErrStatusCallBackRequired);
  878. Result:=TSimpleStatusProcThread.Create(AMethod,AData,AOnStatus,AOnTerminate);
  879. end;
  880. { TPersistent implementation }
  881. {$i persist.inc }
  882. {$i sllist.inc}
  883. {$i resref.inc}
  884. { TComponent implementation }
  885. {$i compon.inc}
  886. { TBasicAction implementation }
  887. {$i action.inc}
  888. { TDataModule implementation }
  889. {$i dm.inc}
  890. { Class and component registration routines }
  891. {$I cregist.inc}
  892. { Interface related stuff }
  893. {$I intf.inc}
  894. {**********************************************************************
  895. * Miscellaneous procedures and functions *
  896. **********************************************************************}
  897. function ExtractStrings(Separators, WhiteSpace: TSysCharSet; Content: PChar; Strings: TStrings; AddEmptyStrings : Boolean = False): Integer;
  898. var
  899. b, c : pchar;
  900. procedure SkipWhitespace;
  901. begin
  902. while (c^ in Whitespace) do
  903. inc (c);
  904. end;
  905. procedure AddString;
  906. var
  907. l : integer;
  908. s : string;
  909. begin
  910. l := c-b;
  911. if (l > 0) or AddEmptyStrings then
  912. begin
  913. if assigned(Strings) then
  914. begin
  915. setlength(s, l);
  916. if l>0 then
  917. move (b^, s[1],l*SizeOf(char));
  918. Strings.Add (s);
  919. end;
  920. inc (result);
  921. end;
  922. end;
  923. var
  924. quoted : char;
  925. begin
  926. result := 0;
  927. c := Content;
  928. Quoted := #0;
  929. Separators := Separators + [#13, #10] - ['''','"'];
  930. SkipWhitespace;
  931. b := c;
  932. while (c^ <> #0) do
  933. begin
  934. if (c^ = Quoted) then
  935. begin
  936. if ((c+1)^ = Quoted) then
  937. inc (c)
  938. else
  939. Quoted := #0
  940. end
  941. else if (Quoted = #0) and (c^ in ['''','"']) then
  942. Quoted := c^;
  943. if (Quoted = #0) and (c^ in Separators) then
  944. begin
  945. AddString;
  946. inc (c);
  947. SkipWhitespace;
  948. b := c;
  949. end
  950. else
  951. inc (c);
  952. end;
  953. if (c <> b) then
  954. AddString;
  955. end;
  956. { Point and rectangle constructors }
  957. function Point(AX, AY: Integer): TPoint;
  958. begin
  959. with Result do
  960. begin
  961. X := AX;
  962. Y := AY;
  963. end;
  964. end;
  965. function SmallPoint(AX, AY: SmallInt): TSmallPoint;
  966. begin
  967. with Result do
  968. begin
  969. X := AX;
  970. Y := AY;
  971. end;
  972. end;
  973. function Rect(ALeft, ATop, ARight, ABottom: Integer): TRect;
  974. begin
  975. with Result do
  976. begin
  977. Left := ALeft;
  978. Top := ATop;
  979. Right := ARight;
  980. Bottom := ABottom;
  981. end;
  982. end;
  983. function Bounds(ALeft, ATop, AWidth, AHeight: Integer): TRect;
  984. begin
  985. with Result do
  986. begin
  987. Left := ALeft;
  988. Top := ATop;
  989. Right := ALeft + AWidth;
  990. Bottom := ATop + AHeight;
  991. end;
  992. end;
  993. function PointsEqual(const P1, P2: TPoint): Boolean; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  994. begin
  995. { lazy, but should work }
  996. result:=QWord(P1)=QWord(P2);
  997. end;
  998. function PointsEqual(const P1, P2: TSmallPoint): Boolean; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  999. begin
  1000. { lazy, but should work }
  1001. result:=DWord(P1)=DWord(P2);
  1002. end;
  1003. function InvalidPoint(X, Y: Integer): Boolean;
  1004. begin
  1005. result:=(X=-1) and (Y=-1);
  1006. end;
  1007. function InvalidPoint(const At: TPoint): Boolean;
  1008. begin
  1009. result:=(At.x=-1) and (At.y=-1);
  1010. end;
  1011. function InvalidPoint(const At: TSmallPoint): Boolean;
  1012. begin
  1013. result:=(At.x=-1) and (At.y=-1);
  1014. end;
  1015. { Object filing routines }
  1016. var
  1017. IntConstList: TThreadList;
  1018. type
  1019. TIntConst = class
  1020. IntegerType: PTypeInfo; // The integer type RTTI pointer
  1021. IdentToIntFn: TIdentToInt; // Identifier to Integer conversion
  1022. IntToIdentFn: TIntToIdent; // Integer to Identifier conversion
  1023. constructor Create(AIntegerType: PTypeInfo; AIdentToInt: TIdentToInt;
  1024. AIntToIdent: TIntToIdent);
  1025. end;
  1026. constructor TIntConst.Create(AIntegerType: PTypeInfo; AIdentToInt: TIdentToInt;
  1027. AIntToIdent: TIntToIdent);
  1028. begin
  1029. IntegerType := AIntegerType;
  1030. IdentToIntFn := AIdentToInt;
  1031. IntToIdentFn := AIntToIdent;
  1032. end;
  1033. procedure RegisterIntegerConsts(IntegerType: Pointer; IdentToIntFn: TIdentToInt;
  1034. IntToIdentFn: TIntToIdent);
  1035. begin
  1036. IntConstList.Add(TIntConst.Create(IntegerType, IdentToIntFn, IntToIdentFn));
  1037. end;
  1038. function FindIntToIdent(AIntegerType: Pointer): TIntToIdent;
  1039. var
  1040. i: Integer;
  1041. begin
  1042. with IntConstList.LockList do
  1043. try
  1044. for i := 0 to Count - 1 do
  1045. if TIntConst(Items[i]).IntegerType = AIntegerType then
  1046. exit(TIntConst(Items[i]).IntToIdentFn);
  1047. Result := nil;
  1048. finally
  1049. IntConstList.UnlockList;
  1050. end;
  1051. end;
  1052. function FindIdentToInt(AIntegerType: Pointer): TIdentToInt;
  1053. var
  1054. i: Integer;
  1055. begin
  1056. with IntConstList.LockList do
  1057. try
  1058. for i := 0 to Count - 1 do
  1059. with TIntConst(Items[I]) do
  1060. if TIntConst(Items[I]).IntegerType = AIntegerType then
  1061. exit(IdentToIntFn);
  1062. Result := nil;
  1063. finally
  1064. IntConstList.UnlockList;
  1065. end;
  1066. end;
  1067. function IdentToInt(const Ident: String; out Int: LongInt;
  1068. const Map: array of TIdentMapEntry): Boolean;
  1069. var
  1070. i: Integer;
  1071. begin
  1072. for i := Low(Map) to High(Map) do
  1073. if CompareText(Map[i].Name, Ident) = 0 then
  1074. begin
  1075. Int := Map[i].Value;
  1076. exit(True);
  1077. end;
  1078. Result := False;
  1079. end;
  1080. function IntToIdent(Int: LongInt; var Ident: String;
  1081. const Map: array of TIdentMapEntry): Boolean;
  1082. var
  1083. i: Integer;
  1084. begin
  1085. for i := Low(Map) to High(Map) do
  1086. if Map[i].Value = Int then
  1087. begin
  1088. Ident := Map[i].Name;
  1089. exit(True);
  1090. end;
  1091. Result := False;
  1092. end;
  1093. function GlobalIdentToInt(const Ident: String; var Int: LongInt):boolean;
  1094. var
  1095. i : Integer;
  1096. begin
  1097. with IntConstList.LockList do
  1098. try
  1099. for i := 0 to Count - 1 do
  1100. if TIntConst(Items[I]).IdentToIntFn(Ident, Int) then
  1101. Exit(True);
  1102. Result := false;
  1103. finally
  1104. IntConstList.UnlockList;
  1105. end;
  1106. end;
  1107. { TPropFixup }
  1108. // Tainted. TPropFixup is being removed.
  1109. Type
  1110. TInitHandler = Class(TObject)
  1111. AHandler : TInitComponentHandler;
  1112. AClass : TComponentClass;
  1113. end;
  1114. {$ifndef i8086}
  1115. type
  1116. TCodePtrList = TList;
  1117. {$endif i8086}
  1118. Var
  1119. InitHandlerList : TList;
  1120. FindGlobalComponentList : TCodePtrList;
  1121. procedure RegisterFindGlobalComponentProc(AFindGlobalComponent: TFindGlobalComponent);
  1122. begin
  1123. if not(assigned(FindGlobalComponentList)) then
  1124. FindGlobalComponentList:=TCodePtrList.Create;
  1125. if FindGlobalComponentList.IndexOf(CodePointer(AFindGlobalComponent))<0 then
  1126. FindGlobalComponentList.Add(CodePointer(AFindGlobalComponent));
  1127. end;
  1128. procedure UnregisterFindGlobalComponentProc(AFindGlobalComponent: TFindGlobalComponent);
  1129. begin
  1130. if assigned(FindGlobalComponentList) then
  1131. FindGlobalComponentList.Remove(CodePointer(AFindGlobalComponent));
  1132. end;
  1133. function FindGlobalComponent(const Name: string): TComponent;
  1134. var
  1135. i : sizeint;
  1136. begin
  1137. FindGlobalComponent:=nil;
  1138. if assigned(FindGlobalComponentList) then
  1139. begin
  1140. for i:=FindGlobalComponentList.Count-1 downto 0 do
  1141. begin
  1142. FindGlobalComponent:=TFindGlobalComponent(FindGlobalComponentList[i])(name);
  1143. if assigned(FindGlobalComponent) then
  1144. break;
  1145. end;
  1146. end;
  1147. end;
  1148. procedure RegisterInitComponentHandler(ComponentClass: TComponentClass; Handler: TInitComponentHandler);
  1149. Var
  1150. I : Integer;
  1151. H: TInitHandler;
  1152. begin
  1153. If (InitHandlerList=Nil) then
  1154. InitHandlerList:=TList.Create;
  1155. H:=TInitHandler.Create;
  1156. H.Aclass:=ComponentClass;
  1157. H.AHandler:=Handler;
  1158. try
  1159. With InitHandlerList do
  1160. begin
  1161. I:=0;
  1162. While (I<Count) and not H.AClass.InheritsFrom(TInitHandler(Items[I]).AClass) do
  1163. Inc(I);
  1164. { override? }
  1165. if (I<Count) and (TInitHandler(Items[I]).AClass=H.AClass) then
  1166. begin
  1167. TInitHandler(Items[I]).AHandler:=Handler;
  1168. H.Free;
  1169. end
  1170. else
  1171. InitHandlerList.Insert(I,H);
  1172. end;
  1173. except
  1174. H.Free;
  1175. raise;
  1176. end;
  1177. end;
  1178. { all targets should at least include the sysres.inc dummy in the system unit to compile this }
  1179. function CreateComponentfromRes(const res : string;Inst : THandle;var Component : TComponent) : Boolean;
  1180. var
  1181. ResStream : TResourceStream;
  1182. begin
  1183. result:=true;
  1184. if Inst=0 then
  1185. Inst:=HInstance;
  1186. try
  1187. ResStream:=TResourceStream.Create(Inst,res,RT_RCDATA);
  1188. try
  1189. Component:=ResStream.ReadComponent(Component);
  1190. finally
  1191. ResStream.Free;
  1192. end;
  1193. except
  1194. on EResNotFound do
  1195. result:=false;
  1196. end;
  1197. end;
  1198. function DefaultInitHandler(Instance: TComponent; RootAncestor: TClass): Boolean;
  1199. function doinit(_class : TClass) : boolean;
  1200. begin
  1201. result:=false;
  1202. if (_class.ClassType=TComponent) or (_class.ClassType=RootAncestor) then
  1203. exit;
  1204. result:=doinit(_class.ClassParent);
  1205. result:=CreateComponentfromRes(_class.ClassName,0,Instance) or result;
  1206. end;
  1207. begin
  1208. {$ifdef FPC_HAS_FEATURE_THREADING}
  1209. GlobalNameSpace.BeginWrite;
  1210. try
  1211. {$endif}
  1212. result:=doinit(Instance.ClassType);
  1213. {$ifdef FPC_HAS_FEATURE_THREADING}
  1214. finally
  1215. GlobalNameSpace.EndWrite;
  1216. end;
  1217. {$endif}
  1218. end;
  1219. function InitInheritedComponent(Instance: TComponent; RootAncestor: TClass): Boolean;
  1220. Var
  1221. I : Integer;
  1222. begin
  1223. I:=0;
  1224. if not Assigned(InitHandlerList) then begin
  1225. Result := True;
  1226. Exit;
  1227. end;
  1228. Result:=False;
  1229. With InitHandlerList do
  1230. begin
  1231. I:=0;
  1232. // Instance is the normally the lowest one, so that one should be used when searching.
  1233. While Not result and (I<Count) do
  1234. begin
  1235. If (Instance.InheritsFrom(TInitHandler(Items[i]).AClass)) then
  1236. Result:=TInitHandler(Items[i]).AHandler(Instance,RootAncestor);
  1237. Inc(I);
  1238. end;
  1239. end;
  1240. end;
  1241. function InitComponentRes(const ResName: String; Instance: TComponent): Boolean;
  1242. begin
  1243. Result:=ReadComponentRes(ResName,Instance)=Instance;
  1244. end;
  1245. function SysReadComponentRes(HInstance : THandle; const ResName: String; Instance: TComponent): TComponent;
  1246. Var
  1247. H : TFPResourceHandle;
  1248. begin
  1249. { Windows unit also has a FindResource function, use the one from
  1250. system unit here. }
  1251. H:=system.FindResource(HInstance,ResName,RT_RCDATA);
  1252. if (PtrInt(H)=0) then
  1253. Result:=Nil
  1254. else
  1255. With TResourceStream.Create(HInstance,ResName,RT_RCDATA) do
  1256. try
  1257. Result:=ReadComponent(Instance);
  1258. Finally
  1259. Free;
  1260. end;
  1261. end;
  1262. function ReadComponentRes(const ResName: String; Instance: TComponent): TComponent;
  1263. begin
  1264. Result:=SysReadComponentRes(Hinstance,Resname,Instance);
  1265. end;
  1266. function ReadComponentResEx(HInstance: THandle; const ResName: String): TComponent;
  1267. begin
  1268. Result:=SysReadComponentRes(Hinstance,ResName,Nil);
  1269. end;
  1270. function ReadComponentResFile(const FileName: String; Instance: TComponent): TComponent;
  1271. var
  1272. FileStream: TStream;
  1273. begin
  1274. FileStream := TFileStream.Create(FileName, fmOpenRead {!!!:or fmShareDenyWrite});
  1275. try
  1276. Result := FileStream.ReadComponentRes(Instance);
  1277. finally
  1278. FileStream.Free;
  1279. end;
  1280. end;
  1281. procedure WriteComponentResFile(const FileName: String; Instance: TComponent);
  1282. var
  1283. FileStream: TStream;
  1284. begin
  1285. FileStream := TFileStream.Create(FileName, fmCreate);
  1286. try
  1287. FileStream.WriteComponentRes(Instance.ClassName, Instance);
  1288. finally
  1289. FileStream.Free;
  1290. end;
  1291. end;
  1292. Function FindNestedComponent(Root : TComponent; APath : String; CStyle : Boolean = True) : TComponent;
  1293. Function GetNextName : String; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  1294. Var
  1295. P : Integer;
  1296. CM : Boolean;
  1297. begin
  1298. P:=Pos('.',APath);
  1299. CM:=False;
  1300. If (P=0) then
  1301. begin
  1302. If CStyle then
  1303. begin
  1304. P:=Pos('->',APath);
  1305. CM:=P<>0;
  1306. end;
  1307. If (P=0) Then
  1308. P:=Length(APath)+1;
  1309. end;
  1310. Result:=Copy(APath,1,P-1);
  1311. Delete(APath,1,P+Ord(CM));
  1312. end;
  1313. Var
  1314. C : TComponent;
  1315. S : String;
  1316. begin
  1317. If (APath='') then
  1318. Result:=Nil
  1319. else
  1320. begin
  1321. Result:=Root;
  1322. While (APath<>'') And (Result<>Nil) do
  1323. begin
  1324. C:=Result;
  1325. S:=Uppercase(GetNextName);
  1326. Result:=C.FindComponent(S);
  1327. If (Result=Nil) And (S='OWNER') then
  1328. Result:=C;
  1329. end;
  1330. end;
  1331. end;
  1332. {$ifdef FPC_HAS_FEATURE_THREADING}
  1333. threadvar
  1334. {$else}
  1335. var
  1336. {$endif}
  1337. GlobalLoaded, GlobalLists: TFpList;
  1338. procedure BeginGlobalLoading;
  1339. begin
  1340. if not Assigned(GlobalLists) then
  1341. GlobalLists := TFpList.Create;
  1342. GlobalLists.Add(GlobalLoaded);
  1343. GlobalLoaded := TFpList.Create;
  1344. end;
  1345. { Notify all global components that they have been loaded completely }
  1346. procedure NotifyGlobalLoading;
  1347. var
  1348. i: Integer;
  1349. begin
  1350. for i := 0 to GlobalLoaded.Count - 1 do
  1351. TComponent(GlobalLoaded[i]).Loaded;
  1352. end;
  1353. procedure EndGlobalLoading;
  1354. begin
  1355. { Free the memory occupied by BeginGlobalLoading }
  1356. GlobalLoaded.Free;
  1357. GlobalLoaded := TFpList(GlobalLists.Last);
  1358. GlobalLists.Delete(GlobalLists.Count - 1);
  1359. if GlobalLists.Count = 0 then
  1360. begin
  1361. GlobalLists.Free;
  1362. GlobalLists := nil;
  1363. end;
  1364. end;
  1365. function CollectionsEqual(C1, C2: TCollection): Boolean;
  1366. begin
  1367. // !!!: Implement this
  1368. CollectionsEqual:=false;
  1369. end;
  1370. function CollectionsEqual(C1, C2: TCollection; Owner1, Owner2: TComponent): Boolean;
  1371. procedure stream_collection(s : tstream;c : tcollection;o : tcomponent);
  1372. var
  1373. w : twriter;
  1374. begin
  1375. w:=twriter.create(s,4096);
  1376. try
  1377. w.root:=o;
  1378. w.flookuproot:=o;
  1379. w.writecollection(c);
  1380. finally
  1381. w.free;
  1382. end;
  1383. end;
  1384. var
  1385. s1,s2 : tmemorystream;
  1386. begin
  1387. result:=false;
  1388. if (c1.classtype<>c2.classtype) or
  1389. (c1.count<>c2.count) then
  1390. exit;
  1391. if c1.count = 0 then
  1392. begin
  1393. result:= true;
  1394. exit;
  1395. end;
  1396. s1:=tmemorystream.create;
  1397. try
  1398. s2:=tmemorystream.create;
  1399. try
  1400. stream_collection(s1,c1,owner1);
  1401. stream_collection(s2,c2,owner2);
  1402. result:=(s1.size=s2.size) and (CompareChar(s1.memory^,s2.memory^,s1.size)=0);
  1403. finally
  1404. s2.free;
  1405. end;
  1406. finally
  1407. s1.free;
  1408. end;
  1409. end;
  1410. { Object conversion routines }
  1411. type
  1412. CharToOrdFuncty = Function(var charpo: Pointer): Cardinal;
  1413. function CharToOrd(var P: Pointer): Cardinal;
  1414. begin
  1415. result:= ord(pchar(P)^);
  1416. inc(pchar(P));
  1417. end;
  1418. function WideCharToOrd(var P: Pointer): Cardinal;
  1419. begin
  1420. result:= ord(pwidechar(P)^);
  1421. inc(pwidechar(P));
  1422. end;
  1423. function Utf8ToOrd(var P:Pointer): Cardinal;
  1424. begin
  1425. // Should also check for illegal utf8 combinations
  1426. Result := Ord(PChar(P)^);
  1427. Inc(P);
  1428. if (Result and $80) <> 0 then
  1429. if (Ord(Result) and %11100000) = %11000000 then begin
  1430. Result := ((Result and %00011111) shl 6)
  1431. or (ord(PChar(P)^) and %00111111);
  1432. Inc(P);
  1433. end else if (Ord(Result) and %11110000) = %11100000 then begin
  1434. Result := ((Result and %00011111) shl 12)
  1435. or ((ord(PChar(P)^) and %00111111) shl 6)
  1436. or (ord((PChar(P)+1)^) and %00111111);
  1437. Inc(P,2);
  1438. end else begin
  1439. Result := ((ord(Result) and %00011111) shl 18)
  1440. or ((ord(PChar(P)^) and %00111111) shl 12)
  1441. or ((ord((PChar(P)+1)^) and %00111111) shl 6)
  1442. or (ord((PChar(P)+2)^) and %00111111);
  1443. Inc(P,3);
  1444. end;
  1445. end;
  1446. procedure ObjectBinaryToText(Input, Output: TStream; Encoding: TObjectTextEncoding);
  1447. var
  1448. Version: TBinaryObjectReader.TBOVersion;
  1449. procedure OutStr(s: String);
  1450. begin
  1451. if Length(s) > 0 then
  1452. Output.Write(s[1], Length(s));
  1453. end;
  1454. procedure OutLn(s: String);
  1455. begin
  1456. OutStr(s + LineEnding);
  1457. end;
  1458. procedure Outchars(P, LastP : Pointer; CharToOrdFunc: CharToOrdFuncty;
  1459. UseBytes: boolean = false);
  1460. var
  1461. res, NewStr: String;
  1462. w: Cardinal;
  1463. InString, NewInString: Boolean;
  1464. begin
  1465. if p = nil then begin
  1466. res:= '''''';
  1467. end
  1468. else
  1469. begin
  1470. res := '';
  1471. InString := False;
  1472. while P < LastP do
  1473. begin
  1474. NewInString := InString;
  1475. w := CharToOrdfunc(P);
  1476. if w = ord('''') then
  1477. begin //quote char
  1478. if not InString then
  1479. NewInString := True;
  1480. NewStr := '''''';
  1481. end
  1482. else if (Ord(w) >= 32) and ((Ord(w) < 127) or (UseBytes and (Ord(w)<256))) then
  1483. begin //printable ascii or bytes
  1484. if not InString then
  1485. NewInString := True;
  1486. NewStr := char(w);
  1487. end
  1488. else
  1489. begin //ascii control chars, non ascii
  1490. if InString then
  1491. NewInString := False;
  1492. NewStr := '#' + IntToStr(w);
  1493. end;
  1494. if NewInString <> InString then
  1495. begin
  1496. NewStr := '''' + NewStr;
  1497. InString := NewInString;
  1498. end;
  1499. res := res + NewStr;
  1500. end;
  1501. if InString then
  1502. res := res + '''';
  1503. end;
  1504. OutStr(res);
  1505. end;
  1506. procedure OutString(s: String);
  1507. begin
  1508. OutChars(Pointer(S),PChar(S)+Length(S),@CharToOrd,Encoding=oteLFM);
  1509. end;
  1510. procedure OutWString(W: WideString);
  1511. begin
  1512. OutChars(Pointer(W),pwidechar(W)+Length(W),@WideCharToOrd);
  1513. end;
  1514. procedure OutUString(W: UnicodeString);
  1515. begin
  1516. OutChars(Pointer(W),pwidechar(W)+Length(W),@WideCharToOrd);
  1517. end;
  1518. procedure OutUtf8Str(s: String);
  1519. begin
  1520. if Encoding=oteLFM then
  1521. OutChars(Pointer(S),PChar(S)+Length(S),@CharToOrd)
  1522. else
  1523. OutChars(Pointer(S),PChar(S)+Length(S),@Utf8ToOrd);
  1524. end;
  1525. function ReadWord : word; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1526. begin
  1527. Result:=Input.ReadWord;
  1528. Result:=LEtoN(Result);
  1529. end;
  1530. function ReadDWord : longword; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1531. begin
  1532. Result:=Input.ReadDWord;
  1533. Result:=LEtoN(Result);
  1534. end;
  1535. function ReadQWord : qword; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1536. begin
  1537. Input.ReadBuffer(Result,sizeof(Result));
  1538. Result:=LEtoN(Result);
  1539. end;
  1540. {$ifndef FPUNONE}
  1541. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1542. function ExtendedToDouble(e : pointer) : double;
  1543. var mant : qword;
  1544. exp : smallint;
  1545. sign : boolean;
  1546. d : qword;
  1547. begin
  1548. move(pbyte(e)[0],mant,8); //mantissa : bytes 0..7
  1549. move(pbyte(e)[8],exp,2); //exponent and sign: bytes 8..9
  1550. mant:=LEtoN(mant);
  1551. exp:=LetoN(word(exp));
  1552. sign:=(exp and $8000)<>0;
  1553. if sign then exp:=exp and $7FFF;
  1554. case exp of
  1555. 0 : mant:=0; //if denormalized, value is too small for double,
  1556. //so it's always zero
  1557. $7FFF : exp:=2047 //either infinity or NaN
  1558. else
  1559. begin
  1560. dec(exp,16383-1023);
  1561. if (exp>=-51) and (exp<=0) then //can be denormalized
  1562. begin
  1563. mant:=mant shr (-exp);
  1564. exp:=0;
  1565. end
  1566. else
  1567. if (exp<-51) or (exp>2046) then //exponent too large.
  1568. begin
  1569. Result:=0;
  1570. exit;
  1571. end
  1572. else //normalized value
  1573. mant:=mant shl 1; //hide most significant bit
  1574. end;
  1575. end;
  1576. d:=word(exp);
  1577. d:=d shl 52;
  1578. mant:=mant shr 12;
  1579. d:=d or mant;
  1580. if sign then d:=d or $8000000000000000;
  1581. Result:=pdouble(@d)^;
  1582. end;
  1583. {$ENDIF}
  1584. {$endif}
  1585. function ReadInt(ValueType: TValueType): Int64;
  1586. begin
  1587. case ValueType of
  1588. vaInt8: Result := ShortInt(Input.ReadByte);
  1589. vaInt16: Result := SmallInt(ReadWord);
  1590. vaInt32: Result := LongInt(ReadDWord);
  1591. vaInt64: Result := Int64(ReadQWord);
  1592. end;
  1593. end;
  1594. function ReadInt: Int64;
  1595. begin
  1596. Result := ReadInt(TValueType(Input.ReadByte));
  1597. end;
  1598. {$ifndef FPUNONE}
  1599. function ReadExtended : extended;
  1600. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1601. var ext : array[0..9] of byte;
  1602. {$ENDIF}
  1603. begin
  1604. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1605. Input.ReadBuffer(ext[0],10);
  1606. Result:=ExtendedToDouble(@(ext[0]));
  1607. {$ELSE}
  1608. Input.ReadBuffer(Result,sizeof(Result));
  1609. {$ENDIF}
  1610. end;
  1611. {$endif}
  1612. function ReadSStr: String;
  1613. var
  1614. len: Byte;
  1615. begin
  1616. len := Input.ReadByte;
  1617. SetLength(Result, len);
  1618. if (len > 0) then
  1619. Input.ReadBuffer(Result[1], len);
  1620. end;
  1621. function ReadLStr: String;
  1622. var
  1623. len: DWord;
  1624. begin
  1625. len := ReadDWord;
  1626. SetLength(Result, len);
  1627. if (len > 0) then
  1628. Input.ReadBuffer(Result[1], len);
  1629. end;
  1630. function ReadWStr: WideString;
  1631. var
  1632. len: DWord;
  1633. {$IFDEF ENDIAN_BIG}
  1634. i : integer;
  1635. {$ENDIF}
  1636. begin
  1637. len := ReadDWord;
  1638. SetLength(Result, len);
  1639. if (len > 0) then
  1640. begin
  1641. Input.ReadBuffer(Pointer(@Result[1])^, len*2);
  1642. {$IFDEF ENDIAN_BIG}
  1643. for i:=1 to len do
  1644. Result[i]:=widechar(SwapEndian(word(Result[i])));
  1645. {$ENDIF}
  1646. end;
  1647. end;
  1648. function ReadUStr: UnicodeString;
  1649. var
  1650. len: DWord;
  1651. {$IFDEF ENDIAN_BIG}
  1652. i : integer;
  1653. {$ENDIF}
  1654. begin
  1655. len := ReadDWord;
  1656. SetLength(Result, len);
  1657. if (len > 0) then
  1658. begin
  1659. Input.ReadBuffer(Pointer(@Result[1])^, len*2);
  1660. {$IFDEF ENDIAN_BIG}
  1661. for i:=1 to len do
  1662. Result[i]:=widechar(SwapEndian(word(Result[i])));
  1663. {$ENDIF}
  1664. end;
  1665. end;
  1666. procedure ReadPropList(indent: String);
  1667. procedure ProcessValue(ValueType: TValueType; Indent: String);
  1668. procedure ProcessBinary;
  1669. var
  1670. ToDo, DoNow, i: LongInt;
  1671. lbuf: array[0..31] of Byte;
  1672. s: String;
  1673. begin
  1674. ToDo := ReadDWord;
  1675. OutLn('{');
  1676. while ToDo > 0 do begin
  1677. DoNow := ToDo;
  1678. if DoNow > 32 then DoNow := 32;
  1679. Dec(ToDo, DoNow);
  1680. s := Indent + ' ';
  1681. Input.ReadBuffer(lbuf, DoNow);
  1682. for i := 0 to DoNow - 1 do
  1683. s := s + IntToHex(lbuf[i], 2);
  1684. OutLn(s);
  1685. end;
  1686. OutLn(indent + '}');
  1687. end;
  1688. var
  1689. s: String;
  1690. { len: LongInt; }
  1691. IsFirst: Boolean;
  1692. {$ifndef FPUNONE}
  1693. ext: Extended;
  1694. {$endif}
  1695. begin
  1696. case ValueType of
  1697. vaList: begin
  1698. OutStr('(');
  1699. IsFirst := True;
  1700. while True do begin
  1701. ValueType := TValueType(Input.ReadByte);
  1702. if ValueType = vaNull then break;
  1703. if IsFirst then begin
  1704. OutLn('');
  1705. IsFirst := False;
  1706. end;
  1707. OutStr(Indent + ' ');
  1708. ProcessValue(ValueType, Indent + ' ');
  1709. end;
  1710. OutLn(Indent + ')');
  1711. end;
  1712. vaInt8: OutLn(IntToStr(ShortInt(Input.ReadByte)));
  1713. vaInt16: OutLn( IntToStr(SmallInt(ReadWord)));
  1714. vaInt32: OutLn(IntToStr(LongInt(ReadDWord)));
  1715. vaInt64: OutLn(IntToStr(Int64(ReadQWord)));
  1716. {$ifndef FPUNONE}
  1717. vaExtended: begin
  1718. ext:=ReadExtended;
  1719. Str(ext,S);// Do not use localized strings.
  1720. OutLn(S);
  1721. end;
  1722. {$endif}
  1723. vaString: begin
  1724. OutString(ReadSStr);
  1725. OutLn('');
  1726. end;
  1727. vaIdent: OutLn(ReadSStr);
  1728. vaFalse: OutLn('False');
  1729. vaTrue: OutLn('True');
  1730. vaBinary: ProcessBinary;
  1731. vaSet: begin
  1732. OutStr('[');
  1733. IsFirst := True;
  1734. while True do begin
  1735. s := ReadSStr;
  1736. if Length(s) = 0 then break;
  1737. if not IsFirst then OutStr(', ');
  1738. IsFirst := False;
  1739. OutStr(s);
  1740. end;
  1741. OutLn(']');
  1742. end;
  1743. vaLString:
  1744. begin
  1745. OutString(ReadLStr);
  1746. OutLn('');
  1747. end;
  1748. vaWString:
  1749. begin
  1750. OutWString(ReadWStr);
  1751. OutLn('');
  1752. end;
  1753. vaUString:
  1754. begin
  1755. OutWString(ReadWStr);
  1756. OutLn('');
  1757. end;
  1758. vaNil:
  1759. OutLn('nil');
  1760. vaCollection: begin
  1761. OutStr('<');
  1762. while Input.ReadByte <> 0 do begin
  1763. OutLn(Indent);
  1764. Input.Seek(-1, soFromCurrent);
  1765. OutStr(indent + ' item');
  1766. ValueType := TValueType(Input.ReadByte);
  1767. if ValueType <> vaList then
  1768. OutStr('[' + IntToStr(ReadInt(ValueType)) + ']');
  1769. OutLn('');
  1770. ReadPropList(indent + ' ');
  1771. OutStr(indent + ' end');
  1772. end;
  1773. OutLn('>');
  1774. end;
  1775. {vaSingle: begin OutLn('!!Single!!'); exit end;
  1776. vaCurrency: begin OutLn('!!Currency!!'); exit end;
  1777. vaDate: begin OutLn('!!Date!!'); exit end;}
  1778. vaUTF8String: begin
  1779. OutUtf8Str(ReadLStr);
  1780. OutLn('');
  1781. end;
  1782. else
  1783. Raise EReadError.CreateFmt(SErrInvalidPropertyType,[Ord(ValueType)]);
  1784. end;
  1785. end;
  1786. begin
  1787. while Input.ReadByte <> 0 do begin
  1788. Input.Seek(-1, soFromCurrent);
  1789. OutStr(indent + ReadSStr + ' = ');
  1790. ProcessValue(TValueType(Input.ReadByte), Indent);
  1791. end;
  1792. end;
  1793. procedure ReadObject(indent: String);
  1794. var
  1795. b: Byte;
  1796. ObjUnitName, ObjClassName, ObjName: String;
  1797. ChildPos: LongInt;
  1798. ValueType: TValueType;
  1799. p: SizeInt;
  1800. begin
  1801. // Check for FilerFlags
  1802. b := Input.ReadByte;
  1803. if (b and $f0) = $f0 then begin
  1804. if (b and 2) <> 0 then ChildPos := ReadInt;
  1805. end else begin
  1806. b := 0;
  1807. Input.Seek(-1, soFromCurrent);
  1808. end;
  1809. ObjUnitName:='';
  1810. if Version=TBinaryObjectReader.TBOVersion.boVersion1 then
  1811. begin
  1812. ValueType := TValueType(Input.ReadByte);
  1813. if ValueType=vaString then
  1814. ObjClassName := ReadSStr
  1815. else
  1816. ObjClassName := ReadLStr;
  1817. p:=Pos(TBinaryObjectReader.UnitnameSeparator,ObjClassName);
  1818. if p>0 then
  1819. begin
  1820. ObjUnitName:=copy(ObjClassName,1,p-1);
  1821. System.Delete(ObjClassName,1,p);
  1822. end;
  1823. end else
  1824. ObjClassName := ReadSStr;
  1825. ObjName := ReadSStr;
  1826. OutStr(Indent);
  1827. if (b and 1) <> 0 then
  1828. OutStr('inherited')
  1829. else if (b and 4) <> 0 then
  1830. OutStr('inline')
  1831. else
  1832. OutStr('object');
  1833. OutStr(' ');
  1834. if ObjName <> '' then
  1835. OutStr(ObjName + ': ');
  1836. if Version=TBinaryObjectReader.TBOVersion.boVersion1 then
  1837. begin
  1838. OutStr(ObjUnitName);
  1839. OutStr('/');
  1840. end;
  1841. OutStr(ObjClassName);
  1842. if (b and 2) <> 0 then
  1843. OutStr('[' + IntToStr(ChildPos) + ']');
  1844. OutLn('');
  1845. ReadPropList(indent + ' ');
  1846. while Input.ReadByte <> 0 do begin
  1847. Input.Seek(-1, soFromCurrent);
  1848. ReadObject(indent + ' ');
  1849. end;
  1850. OutLn(indent + 'end');
  1851. end;
  1852. var
  1853. Signature: DWord;
  1854. begin
  1855. Signature:=Input.ReadDWord;
  1856. if Signature = DWord(unaligned(FilerSignature1)) then
  1857. Version:=TBinaryObjectReader.TBOVersion.boVersion1
  1858. else if Signature = DWord(unaligned(FilerSignature)) then
  1859. Version:=TBinaryObjectReader.TBOVersion.boVersion0
  1860. else
  1861. raise EReadError.Create('Illegal stream image' {###SInvalidImage});
  1862. ReadObject('');
  1863. end;
  1864. procedure ObjectBinaryToText(Input, Output: TStream);
  1865. begin
  1866. ObjectBinaryToText(Input,Output,oteDFM);
  1867. end;
  1868. procedure ObjectTextToBinary(Input, Output: TStream);
  1869. var
  1870. parser: TParser;
  1871. Version: TBinaryObjectReader.TBOVersion;
  1872. StartPos: Int64;
  1873. procedure WriteWord(w : word); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1874. begin
  1875. w:=NtoLE(w);
  1876. Output.WriteWord(w);
  1877. end;
  1878. procedure WriteDWord(lw : longword); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1879. begin
  1880. lw:=NtoLE(lw);
  1881. Output.WriteDWord(lw);
  1882. end;
  1883. procedure WriteQWord(qw : qword); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1884. begin
  1885. qw:=NtoLE(qw);
  1886. Output.WriteBuffer(qw,sizeof(qword));
  1887. end;
  1888. {$ifndef FPUNONE}
  1889. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1890. procedure DoubleToExtended(d : double; e : pointer);
  1891. var mant : qword;
  1892. exp : smallint;
  1893. sign : boolean;
  1894. begin
  1895. mant:=(qword(d) and $000FFFFFFFFFFFFF) shl 12;
  1896. exp :=(qword(d) shr 52) and $7FF;
  1897. sign:=(qword(d) and $8000000000000000)<>0;
  1898. case exp of
  1899. 0 : begin
  1900. if mant<>0 then //denormalized value: hidden bit is 0. normalize it
  1901. begin
  1902. exp:=16383-1022;
  1903. while (mant and $8000000000000000)=0 do
  1904. begin
  1905. dec(exp);
  1906. mant:=mant shl 1;
  1907. end;
  1908. dec(exp); //don't shift, most significant bit is not hidden in extended
  1909. end;
  1910. end;
  1911. 2047 : exp:=$7FFF //either infinity or NaN
  1912. else
  1913. begin
  1914. inc(exp,16383-1023);
  1915. mant:=(mant shr 1) or $8000000000000000; //unhide hidden bit
  1916. end;
  1917. end;
  1918. if sign then exp:=exp or $8000;
  1919. mant:=NtoLE(mant);
  1920. exp:=NtoLE(word(exp));
  1921. move(mant,pbyte(e)[0],8); //mantissa : bytes 0..7
  1922. move(exp,pbyte(e)[8],2); //exponent and sign: bytes 8..9
  1923. end;
  1924. {$ENDIF}
  1925. procedure WriteExtended(const e : extended);
  1926. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1927. var ext : array[0..9] of byte;
  1928. {$ENDIF}
  1929. begin
  1930. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1931. DoubleToExtended(e,@(ext[0]));
  1932. Output.WriteBuffer(ext[0],10);
  1933. {$ELSE}
  1934. Output.WriteBuffer(e,sizeof(e));
  1935. {$ENDIF}
  1936. end;
  1937. {$endif}
  1938. procedure WriteSString(const s: String);
  1939. var size : byte;
  1940. begin
  1941. if length(s)>255 then size:=255
  1942. else size:=length(s);
  1943. Output.WriteByte(size);
  1944. if Length(s) > 0 then
  1945. Output.WriteBuffer(s[1], size);
  1946. end;
  1947. procedure WriteLString(Const s: String);
  1948. begin
  1949. WriteDWord(Length(s));
  1950. if Length(s) > 0 then
  1951. Output.WriteBuffer(s[1], Length(s));
  1952. end;
  1953. procedure WriteSorLString(Const s: String);
  1954. begin
  1955. if length(s)<256 then
  1956. begin
  1957. Output.WriteByte(Ord(vaString));
  1958. WriteSString(s);
  1959. end else begin
  1960. Output.WriteByte(Ord(vaLString));
  1961. WriteSString(s);
  1962. end;
  1963. end;
  1964. procedure WriteWString(Const s: WideString);
  1965. var len : longword;
  1966. {$IFDEF ENDIAN_BIG}
  1967. i : integer;
  1968. ws : widestring;
  1969. {$ENDIF}
  1970. begin
  1971. len:=Length(s);
  1972. WriteDWord(len);
  1973. if len > 0 then
  1974. begin
  1975. {$IFDEF ENDIAN_BIG}
  1976. setlength(ws,len);
  1977. for i:=1 to len do
  1978. ws[i]:=widechar(SwapEndian(word(s[i])));
  1979. Output.WriteBuffer(ws[1], len*sizeof(widechar));
  1980. {$ELSE}
  1981. Output.WriteBuffer(s[1], len*sizeof(widechar));
  1982. {$ENDIF}
  1983. end;
  1984. end;
  1985. procedure WriteInteger(value: Int64);
  1986. begin
  1987. if (value >= -128) and (value <= 127) then begin
  1988. Output.WriteByte(Ord(vaInt8));
  1989. Output.WriteByte(byte(value));
  1990. end else if (value >= -32768) and (value <= 32767) then begin
  1991. Output.WriteByte(Ord(vaInt16));
  1992. WriteWord(word(value));
  1993. end else if (value >= -2147483648) and (value <= 2147483647) then begin
  1994. Output.WriteByte(Ord(vaInt32));
  1995. WriteDWord(longword(value));
  1996. end else begin
  1997. Output.WriteByte(ord(vaInt64));
  1998. WriteQWord(qword(value));
  1999. end;
  2000. end;
  2001. procedure ProcessWideString(const left : widestring);
  2002. var ws : widestring;
  2003. begin
  2004. ws:=left+parser.TokenWideString;
  2005. while parser.NextToken = '+' do
  2006. begin
  2007. parser.NextToken; // Get next string fragment
  2008. if not (parser.Token in [toString,toWString]) then
  2009. parser.CheckToken(toWString);
  2010. ws:=ws+parser.TokenWideString;
  2011. end;
  2012. Output.WriteByte(Ord(vaWstring));
  2013. WriteWString(ws);
  2014. end;
  2015. procedure ProcessProperty; forward;
  2016. procedure ProcessValue;
  2017. var
  2018. {$ifndef FPUNONE}
  2019. flt: Extended;
  2020. {$endif}
  2021. s: String;
  2022. stream: TMemoryStream;
  2023. begin
  2024. case parser.Token of
  2025. toInteger:
  2026. begin
  2027. WriteInteger(parser.TokenInt);
  2028. parser.NextToken;
  2029. end;
  2030. {$ifndef FPUNONE}
  2031. toFloat:
  2032. begin
  2033. Output.WriteByte(Ord(vaExtended));
  2034. flt := Parser.TokenFloat;
  2035. WriteExtended(flt);
  2036. parser.NextToken;
  2037. end;
  2038. {$endif}
  2039. toString:
  2040. begin
  2041. s := parser.TokenString;
  2042. while parser.NextToken = '+' do
  2043. begin
  2044. parser.NextToken; // Get next string fragment
  2045. case parser.Token of
  2046. toString : s:=s+parser.TokenString;
  2047. toWString : begin
  2048. ProcessWideString(WideString(s));
  2049. exit;
  2050. end
  2051. else parser.CheckToken(toString);
  2052. end;
  2053. end;
  2054. if (length(S)>255) then
  2055. begin
  2056. Output.WriteByte(Ord(vaLString));
  2057. WriteLString(S);
  2058. end
  2059. else
  2060. begin
  2061. Output.WriteByte(Ord(vaString));
  2062. WriteSString(s);
  2063. end;
  2064. end;
  2065. toWString:
  2066. ProcessWideString('');
  2067. toSymbol:
  2068. begin
  2069. if CompareText(parser.TokenString, 'True') = 0 then
  2070. Output.WriteByte(Ord(vaTrue))
  2071. else if CompareText(parser.TokenString, 'False') = 0 then
  2072. Output.WriteByte(Ord(vaFalse))
  2073. else if CompareText(parser.TokenString, 'nil') = 0 then
  2074. Output.WriteByte(Ord(vaNil))
  2075. else
  2076. begin
  2077. Output.WriteByte(Ord(vaIdent));
  2078. WriteSString(parser.TokenComponentIdent);
  2079. end;
  2080. Parser.NextToken;
  2081. end;
  2082. // Set
  2083. '[':
  2084. begin
  2085. parser.NextToken;
  2086. Output.WriteByte(Ord(vaSet));
  2087. if parser.Token <> ']' then
  2088. while True do
  2089. begin
  2090. parser.CheckToken(toSymbol);
  2091. WriteSString(parser.TokenString);
  2092. parser.NextToken;
  2093. if parser.Token = ']' then
  2094. break;
  2095. parser.CheckToken(',');
  2096. parser.NextToken;
  2097. end;
  2098. Output.WriteByte(0);
  2099. parser.NextToken;
  2100. end;
  2101. // List
  2102. '(':
  2103. begin
  2104. parser.NextToken;
  2105. Output.WriteByte(Ord(vaList));
  2106. while parser.Token <> ')' do
  2107. ProcessValue;
  2108. Output.WriteByte(0);
  2109. parser.NextToken;
  2110. end;
  2111. // Collection
  2112. '<':
  2113. begin
  2114. parser.NextToken;
  2115. Output.WriteByte(Ord(vaCollection));
  2116. while parser.Token <> '>' do
  2117. begin
  2118. parser.CheckTokenSymbol('item');
  2119. parser.NextToken;
  2120. // ConvertOrder
  2121. Output.WriteByte(Ord(vaList));
  2122. while not parser.TokenSymbolIs('end') do
  2123. ProcessProperty;
  2124. parser.NextToken; // Skip 'end'
  2125. Output.WriteByte(0);
  2126. end;
  2127. Output.WriteByte(0);
  2128. parser.NextToken;
  2129. end;
  2130. // Binary data
  2131. '{':
  2132. begin
  2133. Output.WriteByte(Ord(vaBinary));
  2134. stream := TMemoryStream.Create;
  2135. try
  2136. parser.HexToBinary(stream);
  2137. WriteDWord(stream.Size);
  2138. Output.WriteBuffer(Stream.Memory^, stream.Size);
  2139. finally
  2140. stream.Free;
  2141. end;
  2142. parser.NextToken;
  2143. end;
  2144. else
  2145. parser.Error(SInvalidProperty);
  2146. end;
  2147. end;
  2148. procedure ProcessProperty;
  2149. var
  2150. name: String;
  2151. begin
  2152. // Get name of property
  2153. parser.CheckToken(toSymbol);
  2154. name := parser.TokenString;
  2155. while True do begin
  2156. parser.NextToken;
  2157. if parser.Token <> '.' then break;
  2158. parser.NextToken;
  2159. parser.CheckToken(toSymbol);
  2160. name := name + '.' + parser.TokenString;
  2161. end;
  2162. WriteSString(name);
  2163. parser.CheckToken('=');
  2164. parser.NextToken;
  2165. ProcessValue;
  2166. end;
  2167. procedure ProcessObject(Root: boolean);
  2168. var
  2169. Flags: Byte;
  2170. ObjectName, ObjUnitName, ObjClassName: String;
  2171. ChildPos: Integer;
  2172. begin
  2173. if parser.TokenSymbolIs('OBJECT') then
  2174. Flags :=0 { IsInherited := False }
  2175. else begin
  2176. if parser.TokenSymbolIs('INHERITED') then
  2177. Flags := 1 { IsInherited := True; }
  2178. else begin
  2179. parser.CheckTokenSymbol('INLINE');
  2180. Flags := 4;
  2181. end;
  2182. end;
  2183. parser.NextToken;
  2184. parser.CheckToken(toSymbol);
  2185. ObjectName := '';
  2186. ObjUnitName := '';
  2187. ObjClassName := parser.TokenString;
  2188. parser.NextToken;
  2189. if parser.Token = '/' then begin
  2190. ObjUnitName := ObjClassName;
  2191. parser.NextToken;
  2192. parser.CheckToken(toSymbol);
  2193. ObjClassName := parser.TokenString;
  2194. parser.NextToken;
  2195. end else if parser.Token = ':' then begin
  2196. parser.NextToken;
  2197. parser.CheckToken(toSymbol);
  2198. ObjectName := ObjClassName;
  2199. ObjClassName := parser.TokenString;
  2200. parser.NextToken;
  2201. if parser.Token = '/' then begin
  2202. ObjUnitName := ObjClassName;
  2203. parser.NextToken;
  2204. parser.CheckToken(toSymbol);
  2205. ObjClassName := parser.TokenString;
  2206. parser.NextToken;
  2207. end;
  2208. if parser.Token = '[' then begin
  2209. parser.NextToken;
  2210. ChildPos := parser.TokenInt;
  2211. parser.NextToken;
  2212. parser.CheckToken(']');
  2213. parser.NextToken;
  2214. Flags := Flags or 2;
  2215. end;
  2216. end;
  2217. if Root then
  2218. begin
  2219. if (ObjUnitName<>'') then
  2220. Version:=TBinaryObjectReader.TBOVersion.boVersion1;
  2221. if Version=TBinaryObjectReader.TBOVersion.boVersion1 then
  2222. Output.WriteBuffer(FilerSignature1[1], length(FilerSignature1))
  2223. else
  2224. Output.WriteBuffer(FilerSignature[1], length(FilerSignature));
  2225. end;
  2226. if Flags <> 0 then begin
  2227. Output.WriteByte($f0 or Flags);
  2228. if (Flags and 2) <> 0 then
  2229. WriteInteger(ChildPos);
  2230. end;
  2231. if Version=TBinaryObjectReader.TBOVersion.boVersion1 then
  2232. WriteSorLString(ObjUnitName+TBinaryObjectReader.UnitnameSeparator+ObjClassName)
  2233. else
  2234. WriteSString(ObjClassName);
  2235. WriteSString(ObjectName);
  2236. // Convert property list
  2237. while not (parser.TokenSymbolIs('END') or
  2238. parser.TokenSymbolIs('OBJECT') or
  2239. parser.TokenSymbolIs('INHERITED') or
  2240. parser.TokenSymbolIs('INLINE')) do
  2241. ProcessProperty;
  2242. Output.WriteByte(0); // Terminate property list
  2243. // Convert child objects
  2244. while not parser.TokenSymbolIs('END') do ProcessObject(false);
  2245. parser.NextToken; // Skip end token
  2246. Output.WriteByte(0); // Terminate property list
  2247. end;
  2248. begin
  2249. Version:=TBinaryObjectReader.TBOVersion.boVersion0;
  2250. parser := TParser.Create(Input);
  2251. try
  2252. StartPos:=Output.Position;
  2253. ProcessObject(true);
  2254. finally
  2255. parser.Free;
  2256. end;
  2257. end;
  2258. procedure ObjectResourceToText(Input, Output: TStream);
  2259. begin
  2260. Input.ReadResHeader;
  2261. ObjectBinaryToText(Input, Output);
  2262. end;
  2263. procedure ObjectTextToResource(Input, Output: TStream);
  2264. var
  2265. StartPos, FixupInfo: LongInt;
  2266. parser: TParser;
  2267. name: String;
  2268. begin
  2269. // Get form type name
  2270. StartPos := Input.Position;
  2271. parser := TParser.Create(Input);
  2272. try
  2273. if not parser.TokenSymbolIs('OBJECT') then parser.CheckTokenSymbol('INHERITED');
  2274. parser.NextToken;
  2275. parser.CheckToken(toSymbol);
  2276. parser.NextToken;
  2277. parser.CheckToken(':');
  2278. parser.NextToken;
  2279. parser.CheckToken(toSymbol);
  2280. name := parser.TokenString;
  2281. finally
  2282. parser.Free;
  2283. Input.Position := StartPos;
  2284. end;
  2285. name := UpperCase(name);
  2286. Output.WriteResourceHeader(name,FixupInfo); // Write resource header
  2287. ObjectTextToBinary(Input, Output); // Convert the stuff!
  2288. Output.FixupResourceHeader(FixupInfo); // Insert real resource data size
  2289. end;
  2290. { Utility routines }
  2291. Function IfThen(AValue: Boolean; const ATrue: TStringList; const AFalse: TStringList = nil): TStringList; overload;
  2292. begin
  2293. if avalue then
  2294. result:=atrue
  2295. else
  2296. result:=afalse;
  2297. end;
  2298. function LineStart(Buffer, BufPos: PChar): PChar;
  2299. begin
  2300. Result := BufPos;
  2301. while Result > Buffer do begin
  2302. Dec(Result);
  2303. if Result[0] = #10 then break;
  2304. end;
  2305. end;
  2306. procedure CommonInit;
  2307. begin
  2308. {$ifdef FPC_HAS_FEATURE_THREADING}
  2309. SynchronizeTimeoutEvent:=RtlEventCreate;
  2310. InterlockedIncrement(ThreadQueueLockCounter);
  2311. InitCriticalSection(ThreadQueueLock);
  2312. MainThreadID:=GetCurrentThreadID;
  2313. {$else}
  2314. MainThreadID:=0{GetCurrentThreadID};
  2315. {$endif}
  2316. ExternalThreads := TThreadList.Create;
  2317. {$ifdef FPC_HAS_FEATURE_THREADING}
  2318. InitCriticalsection(ResolveSection);
  2319. TThread.FProcessorCount := CPUCount;
  2320. {$else}
  2321. TThread.FProcessorCount := 1{CPUCount};
  2322. {$endif}
  2323. InitHandlerList:=Nil;
  2324. FindGlobalComponentList:=nil;
  2325. IntConstList := TThreadList.Create;
  2326. ClassList := TThreadList.Create;
  2327. ClassAliasList := nil;
  2328. { on unix this maps to a simple rw synchornizer }
  2329. GlobalNameSpace := TMultiReadExclusiveWriteSynchronizer.Create;
  2330. RegisterInitComponentHandler(TComponent,@DefaultInitHandler);
  2331. end;
  2332. procedure CommonCleanup;
  2333. var
  2334. i: Integer;
  2335. tmpentry: TThread.PThreadQueueEntry;
  2336. begin
  2337. {$ifdef FPC_HAS_FEATURE_THREADING}
  2338. GlobalNameSpace.BeginWrite;
  2339. {$endif}
  2340. with IntConstList.LockList do
  2341. try
  2342. for i := 0 to Count - 1 do
  2343. TIntConst(Items[I]).Free;
  2344. finally
  2345. IntConstList.UnlockList;
  2346. end;
  2347. IntConstList.Free;
  2348. ClassList.Free;
  2349. ClassAliasList.Free;
  2350. RemoveFixupReferences(nil, '');
  2351. {$ifdef FPC_HAS_FEATURE_THREADING}
  2352. DoneCriticalsection(ResolveSection);
  2353. {$endif}
  2354. GlobalLists.Free;
  2355. ComponentPages.Free;
  2356. FreeAndNil(NeedResolving);
  2357. {$ifdef FPC_HAS_FEATURE_THREADING}
  2358. GlobalNameSpace.EndWrite;
  2359. {$endif}
  2360. { GlobalNameSpace is an interface so this is enough }
  2361. GlobalNameSpace:=nil;
  2362. if (InitHandlerList<>Nil) then
  2363. for i := 0 to InitHandlerList.Count - 1 do
  2364. TInitHandler(InitHandlerList.Items[I]).Free;
  2365. InitHandlerList.Free;
  2366. InitHandlerList:=Nil;
  2367. FindGlobalComponentList.Free;
  2368. FindGlobalComponentList:=nil;
  2369. ExternalThreadsCleanup:=True;
  2370. with ExternalThreads.LockList do
  2371. try
  2372. for i := 0 to Count - 1 do
  2373. TThread(Items[i]).Free;
  2374. finally
  2375. ExternalThreads.UnlockList;
  2376. end;
  2377. FreeAndNil(ExternalThreads);
  2378. {$ifdef FPC_HAS_FEATURE_THREADING}
  2379. RtlEventDestroy(SynchronizeTimeoutEvent);
  2380. try
  2381. System.EnterCriticalSection(ThreadQueueLock);
  2382. {$endif}
  2383. { clean up the queue, but keep in mind that the entries used for Synchronize
  2384. are owned by the corresponding TThread }
  2385. while Assigned(ThreadQueueHead) do begin
  2386. tmpentry := ThreadQueueHead;
  2387. ThreadQueueHead := tmpentry^.Next;
  2388. if not Assigned(tmpentry^.SyncEvent) then
  2389. Dispose(tmpentry);
  2390. end;
  2391. { We also need to reset ThreadQueueTail }
  2392. ThreadQueueTail := nil;
  2393. {$ifdef FPC_HAS_FEATURE_THREADING}
  2394. finally
  2395. System.LeaveCriticalSection(ThreadQueueLock);
  2396. end;
  2397. if InterlockedDecrement(ThreadQueueLockCounter)=0 then
  2398. DoneCriticalSection(ThreadQueueLock);
  2399. {$endif}
  2400. end;
  2401. { TFiler implementation }
  2402. {$i filer.inc}
  2403. { TReader implementation }
  2404. {$i reader.inc}
  2405. { TWriter implementations }
  2406. {$i writer.inc}
  2407. {$i twriter.inc}