Forráskód Böngészése

Libraries: added object support in records

Herman Schoenfeld 7 éve
szülő
commit
262f08794f

+ 53 - 13
src/common/UAutoScope.pas → src/libraries/sphere10/UAutoScope.pas

@@ -75,7 +75,7 @@ type
     private
       FScopedRec: PScoped;
     public
-      constructor Create(ScopedRec: PScoped);
+      constructor Create(ScopedRec: PScoped; ACapacity:Integer);
       destructor Destroy; override;
     end;
   {$ENDIF}
@@ -87,10 +87,10 @@ type
     FLastIndex: {$IFDEF FPC}TDynArrayIndex{$ELSE}Integer{$ENDIF};
     {$IFDEF USE_OPERATORS}
     class operator Initialize(var AScope: TScoped);
-    class operator Finalize(var AScope: TScoped);
+    class operator Finalize(var AScope: TScoped; ACapacity:Integer);
     {$ENDIF}
     {$IFDEF USE_INTERFACE}
-    class procedure Initialize(var AScope: TScoped); static;
+    class procedure Initialize(var AScope: TScoped; ACapacity:Integer); static;
     class procedure Finalize(var AScope: TScoped); static;
     {$ENDIF}
     procedure RegisterPointer(Ptr: Pointer; IsObject: Boolean);
@@ -127,6 +127,13 @@ type
     ///   </para>
     /// </remarks>
     procedure RemoveObject(const AnObject: TObject);
+
+    // HS: added
+    procedure InitCapacity(ACapacity : Integer);
+    function Count : Integer;
+    function ItemAt(AIndex : Integer) : TObject;
+    function ScopedPtrAt(AIndex : Integer) : TScopedPtr;
+
     /// <summary>Allocates an automatically releasing memory block.</summary>
     /// <param name="P">Returns a pointer to allocated memory block.</param>
     /// <param name="Size">Is a size in bytes of required memory.</param>
@@ -210,13 +217,15 @@ var
 
 implementation
 
+uses sysutils;
+
 { TScoped }
 
 {$IFDEF USE_INTERFACE}
-constructor TScoped.TScopedGuardian.Create(ScopedRec: PScoped);
+constructor TScoped.TScopedGuardian.Create(ScopedRec: PScoped; ACapacity:Integer);
 begin
   FScopedRec := ScopedRec;
-  TScoped.Initialize(FScopedRec^);
+  TScoped.Initialize(FScopedRec^, ACapacity);
 end;
 
 destructor TScoped.TScopedGuardian.Destroy;
@@ -235,14 +244,14 @@ end;
 class operator TScoped.Initialize(var AScope: TScoped);
 {$ENDIF}
 {$IFDEF USE_INTERFACE}
-class procedure TScoped.Initialize(var AScope: TScoped);
+class procedure TScoped.Initialize(var AScope: TScoped; ACapacity:Integer);
 {$ENDIF}
 begin
   {$IFDEF WITH_PARANOIA}
    __no_use_ptr := @AScope;
   {$ENDIF}
   AScope.FLastIndex := -1;
-  SetLength(AScope.FPointers, 16);
+  SetLength(AScope.FPointers, ACapacity);
 end;
 
 {$IFDEF USE_OPERATORS}
@@ -318,7 +327,7 @@ begin
   {$IFNDEF NEXTGEN}
     {$IFDEF USE_INTERFACE}
     if not Assigned(FGuardian) then
-      FGuardian := TScopedGuardian.Create(@Self);
+      FGuardian := TScopedGuardian.Create(@Self, 16);
     {$ENDIF}
 
   RegisterPointer(Pointer(AnObject), True);
@@ -331,13 +340,44 @@ begin
   {$IFNDEF NEXTGEN}
     {$IFDEF USE_INTERFACE}
     if not Assigned(FGuardian) then
-      FGuardian := TScopedGuardian.Create(@Self);
+      FGuardian := TScopedGuardian.Create(@Self, 16);
     {$ENDIF}
 
   UnregisterPointer(Pointer(AnObject));
   {$ENDIF}
 end;
 
+procedure TScoped.InitCapacity(ACapacity : Integer);
+begin
+  {$IFNDEF NEXTGEN}
+    {$IFDEF USE_INTERFACE}
+    if Assigned(FGuardian) then
+      raise Exception.Create('Already initialized');
+    FGuardian := TScopedGuardian.Create(@Self, ACapacity);
+    {$ENDIF}
+  {$ENDIF}
+end;
+
+function TScoped.Count : Integer;
+begin
+  if not Assigned(FGuardian) then
+    Exit(0);
+  Result := FLastIndex + 1;
+end;
+
+function TScoped.ItemAt(AIndex : Integer) : TObject;
+var sp : TScopedPtr;
+begin
+  sp := FPointers[AIndex];
+  Result := TObject(sp.Ptr);
+end;
+
+function TScoped.ScopedPtrAt(AIndex : Integer) : TScopedPtr;
+var sp : TScopedPtr;
+begin
+  Result := FPointers[AIndex];
+end;
+
 procedure TScoped.GetMem(out P: Pointer; Size:
   {$IFDEF FPC}PtrUInt{$ELSE}Integer{$ENDIF});
 begin
@@ -349,7 +389,7 @@ procedure TScoped.FreeMem(var P: Pointer);
 begin
   {$IFDEF USE_INTERFACE}
   if not Assigned(FGuardian) then
-    FGuardian := TScopedGuardian.Create(@Self);
+    FGuardian := TScopedGuardian.Create(@Self, 16);
   {$ENDIF}
 
   UnregisterPointer(P);
@@ -360,7 +400,7 @@ procedure TScoped.AddMem(const P: Pointer);
 begin
   {$IFDEF USE_INTERFACE}
   if not Assigned(FGuardian) then
-    FGuardian := TScopedGuardian.Create(@Self);
+    FGuardian := TScopedGuardian.Create(@Self, 16);
   {$ENDIF}
 
   RegisterPointer(P, False);
@@ -373,7 +413,7 @@ var
 begin
   {$IFDEF USE_INTERFACE}
   if not Assigned(FGuardian) then
-    FGuardian := TScopedGuardian.Create(@Self);
+    FGuardian := TScopedGuardian.Create(@Self, 16);
   {$ENDIF}
 
   for i := FLastIndex downto 0 do
@@ -389,7 +429,7 @@ procedure TScoped.RemoveMem(const P: Pointer);
 begin
   {$IFDEF USE_INTERFACE}
   if not Assigned(FGuardian) then
-    FGuardian := TScopedGuardian.Create(@Self);
+    FGuardian := TScopedGuardian.Create(@Self, 16);
   {$ENDIF}
 
   UnregisterPointer(P);

+ 42 - 42
src/libraries/sphere10/UCommon.pas

@@ -20,7 +20,7 @@ interface
 
 uses
   Classes, SysUtils, Generics.Collections, Generics.Defaults,
-  Variants, LazUTF8, math, typinfo;
+  Variants, LazUTF8, math, typinfo, UAutoScope;
 
 { CONSTANTS }
 
@@ -131,26 +131,18 @@ type
     end;
   {$ENDIF}
 
-  { TBox - a generic wrapper class for wrappying any type, mainly strings and primtives }
+  { TAuto }
 
-  TBox<T> = class(TObject)
-    type
-      TDestroyItemDelegate = procedure (constref val : T) of object;
-    strict private
-      FValue: T;
-      FDestroyFunc : TDestroyItemDelegate;
-      class procedure NoOpDestroyItem(constref val : T);
+  TAuto<T> = record
+    private
+      FGC : TScoped;
+      function GetItem : T;
+      procedure SetItem(const AItem: T);
     public
-      constructor Create(Value: T); overload;
-      constructor Create(Value: T; destroyItemFunc: TDestroyItemDelegate); overload;
-      destructor Destroy; override;
-      property Value: T read FValue;
+      constructor Create(const AItem: T);
+      property Item : T read GetItem write SetItem;
   end;
 
-  { A TObject-wrapped string }
-
-  TStringObject = TBox<AnsiString>;
-
   { TDateTimeHelper }
 
   TDateTimeHelper = record helper for TDateTime
@@ -612,6 +604,39 @@ end;
 
 {$ENDIF}
 
+{%region TAuto }
+
+constructor TAuto<T>.Create(const AItem: T);
+begin
+  FGC.InitCapacity(1);
+  FGC.AddObject(AItem);
+end;
+
+function TAuto<T>.GetItem : T;
+begin
+  if FGC.Count = 1 then
+    Result := T(FGC.ItemAt(0))
+  else
+    Result := Default(T)
+end;
+
+procedure TAuto<T>.SetItem(const AItem: T);
+var
+  oldsp : TScopedPtr;
+  old : TObject;
+begin
+  while FGC.Count > 0 do begin
+    oldsp := FGC.ScopedPtrAt(0);
+    old := FGC.ItemAt(0);
+    FGC.RemoveObject(old);
+    if (oldsp.IsObject) then
+      FreeAndNil(Pointer(old));
+  end;
+  FGC.AddObject(AItem);
+end;
+
+{%endregion}
+
 {%region Language-level tools }
 function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: Cardinal): Cardinal;
 begin
@@ -740,31 +765,6 @@ begin
   end;
 end;
 
-{%region TBox }
-
-constructor TBox<T>.Create(Value: T);
-begin
-  Create(Value, NoOpDestroyItem);
-end;
-
-constructor TBox<T>.Create(Value: T; destroyItemFunc: TDestroyItemDelegate);
-begin
-  inherited Create;
-  FValue := Value;
-  FDestroyFunc := destroyItemFunc;
-end;
-
-destructor TBox<T>.Destroy;
-begin
-  FDestroyFunc(FValue);
-  inherited;
-end;
-
-class procedure TBox<T>.NoOpDestroyItem(constref val : T);
-begin
-  // No op
-end;
-
 {%endregion}
 
 {%region Date/Time Support }

+ 1 - 1
src/libraries/sphere10/UWizard.pas

@@ -155,7 +155,7 @@ type
   end;
 
   { TGenericWizardBag - a generic bag for use within wizard }
-  TGenericWizardBag = TDictionary<AnsiString, TObject>;
+  TGenericWizardBag = TDictionary<utf8string, Variant>;
 
 implementation
 

+ 31 - 31
src/pascalcoin_wallet.lpi

@@ -289,106 +289,106 @@
         <ResourceBaseClass Value="Form"/>
       </Unit50>
       <Unit51>
-        <Filename Value="common\UAutoScope.pas"/>
-        <IsPartOfProject Value="True"/>
-      </Unit51>
-      <Unit52>
         <Filename Value="gui\wizards\UWIZAddKey_Start.pas"/>
         <IsPartOfProject Value="True"/>
         <ComponentName Value="WIZAddKey_Start"/>
         <HasResources Value="True"/>
         <ResourceBaseClass Value="Form"/>
-      </Unit52>
-      <Unit53>
+      </Unit51>
+      <Unit52>
         <Filename Value="gui\wizards\UWIZAddKey_GenerateOrImport.pas"/>
         <IsPartOfProject Value="True"/>
         <ComponentName Value="WIZAddKey_GenerateOrImport"/>
         <HasResources Value="True"/>
         <ResourceBaseClass Value="Form"/>
-      </Unit53>
-      <Unit54>
+      </Unit52>
+      <Unit53>
         <Filename Value="gui\wizards\UWIZAddKey_ImportPubKey.pas"/>
         <IsPartOfProject Value="True"/>
         <ComponentName Value="WIZAddKey_ImportPubKey"/>
         <HasResources Value="True"/>
         <ResourceBaseClass Value="Form"/>
-      </Unit54>
-      <Unit55>
+      </Unit53>
+      <Unit54>
         <Filename Value="gui\wizards\UWIZAddKey_ImportPrivKey.pas"/>
         <IsPartOfProject Value="True"/>
         <ComponentName Value="WIZAddKey_ImportPrivKey"/>
         <HasResources Value="True"/>
         <ResourceBaseClass Value="Form"/>
-      </Unit55>
-      <Unit56>
+      </Unit54>
+      <Unit55>
         <Filename Value="gui\wizards\UWIZAddKey_EnterName.pas"/>
         <IsPartOfProject Value="True"/>
         <ComponentName Value="WIZAddKey_EnterName"/>
         <HasResources Value="True"/>
         <ResourceBaseClass Value="Form"/>
-      </Unit56>
-      <Unit57>
+      </Unit55>
+      <Unit56>
         <Filename Value="gui\UFRMWalletKeys.pas"/>
         <IsPartOfProject Value="True"/>
         <ComponentName Value="FRMWalletKeys"/>
         <HasResources Value="True"/>
         <ResourceBaseClass Value="Form"/>
+      </Unit56>
+      <Unit57>
+        <Filename Value="gui\wizards\UWIZAddKey.pas"/>
+        <IsPartOfProject Value="True"/>
       </Unit57>
       <Unit58>
-        <Filename Value="gui\wizards\UWIZAddKey.pas"/>
+        <Filename Value="core\USettings.pas"/>
         <IsPartOfProject Value="True"/>
       </Unit58>
       <Unit59>
-        <Filename Value="core\USettings.pas"/>
-        <IsPartOfProject Value="True"/>
-      </Unit59>
-      <Unit60>
         <Filename Value="gui\wizards\UWIZAddKey_SelectEncryption.pas"/>
         <IsPartOfProject Value="True"/>
         <ComponentName Value="WIZAddKey_SelectEncryption"/>
         <HasResources Value="True"/>
         <ResourceBaseClass Value="Form"/>
-      </Unit60>
-      <Unit61>
+      </Unit59>
+      <Unit60>
         <Filename Value="gui\UCTRLWallet.pas"/>
         <IsPartOfProject Value="True"/>
         <ComponentName Value="CTRLWallet"/>
         <HasResources Value="True"/>
         <ResourceBaseClass Value="Form"/>
+      </Unit60>
+      <Unit61>
+        <Filename Value="core\UDataSources.pas"/>
+        <IsPartOfProject Value="True"/>
       </Unit61>
       <Unit62>
-        <Filename Value="core\UDataSources.pas"/>
+        <Filename Value="libraries\sphere10\UCommon.Collections.pas"/>
         <IsPartOfProject Value="True"/>
       </Unit62>
       <Unit63>
-        <Filename Value="libraries\sphere10\UCommon.Collections.pas"/>
+        <Filename Value="libraries\sphere10\UCommon.Data.pas"/>
         <IsPartOfProject Value="True"/>
       </Unit63>
       <Unit64>
-        <Filename Value="libraries\sphere10\UCommon.Data.pas"/>
+        <Filename Value="libraries\sphere10\UCommon.pas"/>
         <IsPartOfProject Value="True"/>
       </Unit64>
       <Unit65>
-        <Filename Value="libraries\sphere10\UCommon.pas"/>
+        <Filename Value="libraries\sphere10\UCommon.UI.pas"/>
         <IsPartOfProject Value="True"/>
       </Unit65>
       <Unit66>
-        <Filename Value="libraries\sphere10\UCommon.UI.pas"/>
+        <Filename Value="libraries\sphere10\UVisualGrid.pas"/>
         <IsPartOfProject Value="True"/>
       </Unit66>
       <Unit67>
-        <Filename Value="libraries\sphere10\UVisualGrid.pas"/>
-        <IsPartOfProject Value="True"/>
-      </Unit67>
-      <Unit68>
         <Filename Value="libraries\sphere10\UWizard.pas"/>
         <IsPartOfProject Value="True"/>
         <ComponentName Value="WizardHostForm"/>
         <HasResources Value="True"/>
         <ResourceBaseClass Value="Form"/>
+      </Unit67>
+      <Unit68>
+        <Filename Value="config.inc"/>
+        <IsPartOfProject Value="True"/>
       </Unit68>
       <Unit69>
-        <Filename Value="config.inc"/>
+        <Filename Value="libraries\sphere10\UAutoScope.pas"/>
         <IsPartOfProject Value="True"/>
       </Unit69>
     </Units>