BrookLibraryLoader.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2020 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. { Dynamic library loader. }
  26. unit BrookLibraryLoader;
  27. {$I BrookDefines.inc}
  28. interface
  29. uses
  30. SysUtils,
  31. Classes,
  32. libsagui,
  33. BrookHandledClasses,
  34. BrookUtility;
  35. resourcestring
  36. { Indicates not allowed operation when the library loader is loaded. }
  37. SBrookActiveLibLoader = 'Active library loader.';
  38. type
  39. { Class for dynamic library loading. }
  40. TBrookLibraryLoader = class(TBrookHandledComponent)
  41. public const
  42. { Default library name. }
  43. LIB_NAME = SG_LIB_NAME;
  44. private
  45. FActive: Boolean;
  46. FVersion: string;
  47. FHandle: TLibHandle;
  48. FLibraryName: TFileName;
  49. FStreamedActive: Boolean;
  50. FOnLoad: TNotifyEvent;
  51. FOnUnload: TNotifyEvent;
  52. function IsActiveStored: Boolean;
  53. function IsLibraryNameStored: Boolean;
  54. procedure SetActive(AValue: Boolean);
  55. procedure SetLibraryName(const AValue: TFileName);
  56. procedure InternalOpen; inline;
  57. procedure InternalLibUnloadEvent(ASender: TObject);
  58. protected
  59. procedure Loaded; override;
  60. procedure CheckInactive; inline;
  61. function GetHandle: Pointer; override;
  62. public
  63. { Creates an instance of @code(TBrookLibraryLoader).
  64. @param(AOwner[in] Owner component.) }
  65. constructor Create(AOwner: TComponent); override;
  66. { Destroys an instance of @code(TBrookLibraryLoader). }
  67. destructor Destroy; override;
  68. { Loads the library dynamically.
  69. @param(ALibraryName Library name.) }
  70. class procedure Load(const ALibraryName: TFileName); overload; static;
  71. { Loads the library dynamically. }
  72. class procedure Load; overload; static;
  73. { Unloads the library dynamically. }
  74. class procedure Unload; static;
  75. { Checks if the library is already loaded. }
  76. class function IsLoaded: Boolean; static;
  77. { Loads the library dynamically. }
  78. procedure Open; virtual;
  79. { Unloads the library dynamically. }
  80. procedure Close; virtual;
  81. { @exclude }
  82. procedure DefineProperties(AFiler: TFiler); override;
  83. published
  84. { Loads/Unloads the library dynamically. }
  85. property Active: Boolean read FActive write SetActive stored IsActiveStored;
  86. { Specifies the library to be loaded dynamically. }
  87. property LibraryName: TFileName read FLibraryName write SetLibraryName
  88. stored IsLibraryNameStored;
  89. { Version of the loaded library. }
  90. property Version: string read FVersion stored False;
  91. { Notifies that the library is loaded. }
  92. property OnLoad: TNotifyEvent read FOnLoad write FOnLoad;
  93. { Notifies that the library is unloaded. }
  94. property OnUnload: TNotifyEvent read FOnUnload write FOnUnload;
  95. end;
  96. implementation
  97. constructor TBrookLibraryLoader.Create(AOwner: TComponent);
  98. begin
  99. inherited Create(AOwner);
  100. SgLib.UnloadEvents.Add(InternalLibUnloadEvent, Self);
  101. FLibraryName := SG_LIB_NAME;
  102. end;
  103. destructor TBrookLibraryLoader.Destroy;
  104. begin
  105. Close;
  106. SgLib.UnloadEvents.Remove(InternalLibUnloadEvent);
  107. inherited Destroy;
  108. end;
  109. procedure TBrookLibraryLoader.InternalLibUnloadEvent(ASender: TObject);
  110. begin
  111. if Assigned(ASender) then
  112. begin
  113. FActive := False;
  114. FVersion := '';
  115. end;
  116. end;
  117. procedure TBrookLibraryLoader.CheckInactive;
  118. begin
  119. if not (csLoading in ComponentState) and Active then
  120. raise EInvalidOpException.Create(SBrookActiveLibLoader);
  121. end;
  122. procedure TBrookLibraryLoader.InternalOpen;
  123. begin
  124. FHandle := SgLib.Load(FLibraryName);
  125. FActive := FHandle <> NilHandle;
  126. if FActive then
  127. FVersion := Sagui.VersionStr
  128. else
  129. FVersion := '';
  130. if Assigned(FOnLoad) then
  131. FOnLoad(Self);
  132. end;
  133. procedure TBrookLibraryLoader.Loaded;
  134. begin
  135. inherited Loaded;
  136. if FActive then
  137. Open;
  138. end;
  139. procedure TBrookLibraryLoader.DefineProperties(AFiler: TFiler);
  140. begin
  141. inherited DefineProperties(AFiler);
  142. if FActive and not FStreamedActive then
  143. begin
  144. FStreamedActive := True;
  145. InternalOpen;
  146. end;
  147. end;
  148. function TBrookLibraryLoader.GetHandle: Pointer;
  149. begin
  150. Result := @FHandle;
  151. end;
  152. class procedure TBrookLibraryLoader.Load(const ALibraryName: TFileName);
  153. begin
  154. SgLib.Load(ALibraryName);
  155. end;
  156. class procedure TBrookLibraryLoader.Load;
  157. begin
  158. SgLib.Load(LIB_NAME);
  159. end;
  160. class procedure TBrookLibraryLoader.Unload;
  161. begin
  162. SgLib.Unload;
  163. end;
  164. class function TBrookLibraryLoader.IsLoaded: Boolean;
  165. begin
  166. Result := SgLib.IsLoaded;
  167. end;
  168. procedure TBrookLibraryLoader.SetActive(AValue: Boolean);
  169. begin
  170. if AValue = FActive then
  171. Exit;
  172. if csLoading in ComponentState then
  173. begin
  174. SgLib.Unload;
  175. FActive := AValue;
  176. end
  177. else
  178. if AValue then
  179. Open
  180. else
  181. Close;
  182. end;
  183. function TBrookLibraryLoader.IsActiveStored: Boolean;
  184. begin
  185. Result := FActive;
  186. end;
  187. function TBrookLibraryLoader.IsLibraryNameStored: Boolean;
  188. begin
  189. Result := CompareText(FLibraryName, SG_LIB_NAME) <> 0;
  190. end;
  191. procedure TBrookLibraryLoader.SetLibraryName(const AValue: TFileName);
  192. begin
  193. if AValue = FLibraryName then
  194. Exit;
  195. CheckInactive;
  196. FLibraryName := AValue;
  197. if FLibraryName = '' then
  198. FLibraryName := SG_LIB_NAME;
  199. end;
  200. procedure TBrookLibraryLoader.Open;
  201. begin
  202. if FActive then
  203. Exit;
  204. SgLib.Unload;
  205. InternalOpen;
  206. end;
  207. procedure TBrookLibraryLoader.Close;
  208. begin
  209. if not FActive then
  210. Exit;
  211. FHandle := SgLib.Unload;
  212. FActive := FHandle <> NilHandle;
  213. if not FActive then
  214. FVersion := '';
  215. if Assigned(FOnUnload) then
  216. FOnUnload(Self);
  217. end;
  218. end.