Browse Source

* Added possibility to force use of setter/getter for properties.
* Added possibility to add a line of text in the property setter. (%PROPNAME%)
* Fixed TCodeOptions.Assign, missing a couple of properties.
* Fixed name of setter routine

git-svn-id: trunk@23439 -

michael 12 years ago
parent
commit
49e3d2734e
1 changed files with 33 additions and 8 deletions
  1. 33 8
      packages/fcl-db/src/codegen/fpddcodegen.pp

+ 33 - 8
packages/fcl-db/src/codegen/fpddcodegen.pp

@@ -38,6 +38,8 @@ Type
   TVisibility = (vPrivate,vProtected,vPublic,vPublished);
   TVisibility = (vPrivate,vProtected,vPublic,vPublished);
   TVisibilities = Set of TVisibility;
   TVisibilities = Set of TVisibility;
   TPropAccess = (paReadWrite,paReadonly,paWriteonly);
   TPropAccess = (paReadWrite,paReadonly,paWriteonly);
+  TPropSetter = (psRead,psWrite);
+  TPropSetters = set of TPropSetter;
 
 
 
 
   TFieldPropDefs = Class;
   TFieldPropDefs = Class;
@@ -51,6 +53,7 @@ Type
     FFieldType: TFieldType;
     FFieldType: TFieldType;
     FPropAccess: TPropAccess;
     FPropAccess: TPropAccess;
     FPropDef: String;
     FPropDef: String;
+    FPropSetters: TPropSetters;
     FPropType : TPropType;
     FPropType : TPropType;
     FPRopSize: Integer;
     FPRopSize: Integer;
     FPropName : String;
     FPropName : String;
@@ -66,8 +69,8 @@ Type
     Constructor Create(ACollection : TCollection) ; override;
     Constructor Create(ACollection : TCollection) ; override;
     Procedure Assign(ASource : TPersistent); override;
     Procedure Assign(ASource : TPersistent); override;
     Function FieldPropDefs : TFieldPropDefs;
     Function FieldPropDefs : TFieldPropDefs;
-    Function HasGetter : Boolean; Virtual; // Always false.
-    Function HasSetter : Boolean; Virtual; // True for streams/strings
+    Function HasGetter : Boolean; Virtual; // Checks Propsetters for psRead
+    Function HasSetter : Boolean; Virtual; // True for streams/strings or if Propsetters has pswrite
     Function ObjPasTypeDef : String; virtual; // Object pascal definition of type
     Function ObjPasTypeDef : String; virtual; // Object pascal definition of type
     Function ObjPasReadDef : String; virtual; // Object pascal definition of getter
     Function ObjPasReadDef : String; virtual; // Object pascal definition of getter
     Function ObjPasWriteDef : String; virtual; // Object pascal definition of setter
     Function ObjPasWriteDef : String; virtual; // Object pascal definition of setter
@@ -81,6 +84,7 @@ Type
     Property PropertyDef : String Read FPropDef Write FPropDef;
     Property PropertyDef : String Read FPropDef Write FPropDef;
     Property PropertyVisibility : TVisibility Read FPropVis Write FPropVis;
     Property PropertyVisibility : TVisibility Read FPropVis Write FPropVis;
     Property PropertyAccess : TPropAccess Read FPropAccess Write FPropAccess;
     Property PropertyAccess : TPropAccess Read FPropAccess Write FPropAccess;
+    Property PropSetters : TPropSetters Read FPropSetters Write FPropSetters;
   end;
   end;
   
   
   { TFieldPropDefs }
   { TFieldPropDefs }
@@ -113,6 +117,7 @@ Type
     FInterfaceUnits: String;
     FInterfaceUnits: String;
     FOptions: TCodeOptions;
     FOptions: TCodeOptions;
     FUnitName: String;
     FUnitName: String;
+    FExtraSetterLine : string;
     procedure SetImplementationUnits(const AValue: String);
     procedure SetImplementationUnits(const AValue: String);
     procedure SetInterfaceUnits(const AValue: String);
     procedure SetInterfaceUnits(const AValue: String);
     procedure SetUnitname(const AValue: String);
     procedure SetUnitname(const AValue: String);
@@ -122,9 +127,15 @@ Type
     Constructor create; virtual;
     Constructor create; virtual;
     Procedure Assign(ASource : TPersistent); override;
     Procedure Assign(ASource : TPersistent); override;
   Published
   Published
+    // Line of code that will be added to each property setter. Use %PROPNAME% to include property name in the line.
+    Property ExtraSetterLine : String Read FExtraSetterLine Write FExtraSetterLine;
+    // options
     Property Options : TCodeOptions Read FOptions Write SetOPtions;
     Property Options : TCodeOptions Read FOptions Write SetOPtions;
+    // Name of unit if a unit is generated.
     Property UnitName : String Read FUnitName Write SetUnitname;
     Property UnitName : String Read FUnitName Write SetUnitname;
+    // Comma-separated list of  units that will be put in the interface units clause
     Property InterfaceUnits : String Read FInterfaceUnits Write SetInterfaceUnits;
     Property InterfaceUnits : String Read FInterfaceUnits Write SetInterfaceUnits;
+    //  Comma-separated list of  units that will be put in the implementation units clause
     Property ImplementationUnits : String Read FImplementationUnits Write SetImplementationUnits;
     Property ImplementationUnits : String Read FImplementationUnits Write SetImplementationUnits;
   end;
   end;
   TCodeGeneratorOptionsClass = Class of TCodeGeneratorOptions;
   TCodeGeneratorOptionsClass = Class of TCodeGeneratorOptions;
@@ -539,13 +550,13 @@ end;
 
 
 function TFieldPropDef.HasGetter: Boolean;
 function TFieldPropDef.HasGetter: Boolean;
 begin
 begin
-  Result:=False;
+  Result:=psRead in PropSetters;
 end;
 end;
 
 
 function TFieldPropDef.HasSetter: Boolean;
 function TFieldPropDef.HasSetter: Boolean;
 begin
 begin
   Result:=(PropertyAccess in [paReadWrite,paWriteOnly])
   Result:=(PropertyAccess in [paReadWrite,paWriteOnly])
-          and (PropertyType in [ptStream,ptTStrings]);
+          and ((PropertyType in [ptStream,ptTStrings]) or (psWrite in Propsetters));
 end;
 end;
 
 
 function TFieldPropDef.ObjPasTypeDef: String;
 function TFieldPropDef.ObjPasTypeDef: String;
@@ -832,7 +843,7 @@ begin
   For I:=0 to Fields.Count-1 do
   For I:=0 to Fields.Count-1 do
     begin
     begin
     F:=Fields[i];
     F:=Fields[i];
-    if AllowPropertyDeclaration(F,[]) and F.HasGetter then
+    if AllowPropertyDeclaration(F,[]) and F.HasSetter then
       begin
       begin
       If not B then
       If not B then
         begin
         begin
@@ -867,22 +878,33 @@ Procedure TDDClassCodeGenerator.WritePropertySetterImpl(Strings : TStrings; F :
 
 
 Var
 Var
   S : String;
   S : String;
+  L : Integer;
 
 
 begin
 begin
-  S:=PropertyGetterDeclaration(F,True);
+  S:=PropertySetterDeclaration(F,True);
   BeginMethod(Strings,S);
   BeginMethod(Strings,S);
   AddLn(Strings,'begin');
   AddLn(Strings,'begin');
   IncIndent;
   IncIndent;
   Try
   Try
+    AddLn(Strings,Format('if (F%s=AValue) then exit;',[F.PropertyName]));
     Case F.PropertyType of
     Case F.PropertyType of
       ptTStrings :
       ptTStrings :
         S:=Format('F%s.Assign(AValue);',[F.PropertyName]);
         S:=Format('F%s.Assign(AValue);',[F.PropertyName]);
       ptStream :
       ptStream :
         S:=Format('F%s.CopyFrom(AValue,0);',[F.PropertyName]);
         S:=Format('F%s.CopyFrom(AValue,0);',[F.PropertyName]);
     else
     else
-       S:=Format('F%s:=AValue',[F.PropertyName]);
+       S:=Format('F%s:=AValue;',[F.PropertyName]);
     end;
     end;
     AddLn(Strings,S);
     AddLn(Strings,S);
+    S:=CodeOptions.ExtraSetterLine;
+    L:=Length(S);
+    if (L>0) then
+      begin
+      S:=StringReplace(S,'%PROPNAME%',F.PropertyName,[rfReplaceAll,rfIgnoreCase]);
+      if (S[L]<>';') then
+        S:=S+';';
+      AddLn(Strings,S);  
+      end;
   Finally
   Finally
     DecIndent;
     DecIndent;
   end;
   end;
@@ -1093,7 +1115,7 @@ begin
   Result:='Procedure ';
   Result:='Procedure ';
   If Impl then
   If Impl then
     Result:=Result+ClassOptions.ObjectClassName+'.';
     Result:=Result+ClassOptions.ObjectClassName+'.';
-  Result:=Result+Def.ObjPasReadDef+' (AValue  : '+Def.ObjPasTypeDef+');';
+  Result:=Result+Def.ObjPasWriteDef+' (AValue  : '+Def.ObjPasTypeDef+');';
 end;
 end;
 
 
 function TDDClassCodeGenerator.NeedsConstructor: Boolean;
 function TDDClassCodeGenerator.NeedsConstructor: Boolean;
@@ -1478,8 +1500,11 @@ begin
   If ASource is TCodeGeneratorOptions then
   If ASource is TCodeGeneratorOptions then
     begin
     begin
     CG:=ASource as TCodeGeneratorOptions;
     CG:=ASource as TCodeGeneratorOptions;
+    FInterfaceUnits:=CG.InterfaceUnits;
+    FImplementationUnits:=CG.ImplementationUnits;
     FOptions:=CG.FOptions;
     FOptions:=CG.FOptions;
     FUnitName:=CG.UnitName;
     FUnitName:=CG.UnitName;
+    FExtraSetterLine:=CG.ExtraSetterLine;
     end
     end
   else
   else
     inherited Assign(ASource);
     inherited Assign(ASource);