From 5451a52cd221b861c1d091514b0c9d2ab0e0e60b Mon Sep 17 00:00:00 2001 From: Bert Herngreen Date: Fri, 1 Sep 2017 11:48:05 +0200 Subject: [PATCH] Support of 'Def' getter method for optional elements --- Units/DelphiXMLDataBindingGenerator.pas | 7 ++++ Units/DelphiXMLDataBindingResources.pas | 47 +++++++++++++++---------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/Units/DelphiXMLDataBindingGenerator.pas b/Units/DelphiXMLDataBindingGenerator.pas index 974f9ad..c02bbb0 100644 --- a/Units/DelphiXMLDataBindingGenerator.pas +++ b/Units/DelphiXMLDataBindingGenerator.pas @@ -1223,7 +1223,11 @@ begin WriteNewLine; if writeOptional then + begin sourceCode.Add(PropertyIntfMethodGetOptional); + if AProperty.PropertyType = ptSimple then + sourceCode.Add(PropertyIntfMethodGetOptionalOrDefault); + end; if writeNil then sourceCode.Add(PropertyIntfMethodGetNil); @@ -1311,6 +1315,9 @@ begin else sourceCode.Add(PropertyImplMethodGetOptional[GetDelphiElementType(nodeType)]); end; + + if AProperty.PropertyType = ptSimple then + sourceCode.Add(PropertyImplMethodGetOptionalOrDefault); end; if writeNil then diff --git a/Units/DelphiXMLDataBindingResources.pas b/Units/DelphiXMLDataBindingResources.pas index 8c999e7..2f093ae 100644 --- a/Units/DelphiXMLDataBindingResources.pas +++ b/Units/DelphiXMLDataBindingResources.pas @@ -155,25 +155,36 @@ const ' Result := (inherited GetCurrent as %:s);' + CrLf + 'end;' + CrLf; - PropertyIntfMethodGetOptional = ' function GetHas%:s: Boolean;'; - PropertyIntfMethodGetNil = ' function Get%:sIsNil: Boolean;'; - PropertyIntfMethodGetText = ' function Get%:sText: WideString;'; - PropertyIntfMethodGet = ' function Get%:s: %:s;'; - PropertyIntfMethodSetNil = ' procedure Set%:sIsNil(const Value: Boolean);'; - PropertyIntfMethodSetText = ' procedure Set%:sText(const Value: WideString);'; - PropertyIntfMethodSet = ' procedure Set%:s(const Value: %:s);'; - PropertyIntfMethodLoadFromStream = ' procedure Load%:sFromStream(AStream: TStream);'; - PropertyIntfMethodLoadFromFile = ' procedure Load%:sFromFile(const AFileName: string);'; - PropertyIntfMethodSaveToStream = ' procedure Save%:sToStream(AStream: TStream);'; - PropertyIntfMethodSaveToFile = ' procedure Save%:sToFile(const AFileName: string);'; + PropertyIntfMethodGetOptional = ' function GetHas%:s: Boolean;'; + PropertyIntfMethodGetOptionalOrDefault = ' function %:sDef(const ADefaultValue: %:s = Default(%:s)): %:s;'; + PropertyIntfMethodGetNil = ' function Get%:sIsNil: Boolean;'; + PropertyIntfMethodGetText = ' function Get%:sText: WideString;'; + PropertyIntfMethodGet = ' function Get%:s: %:s;'; + PropertyIntfMethodSetNil = ' procedure Set%:sIsNil(const Value: Boolean);'; + PropertyIntfMethodSetText = ' procedure Set%:sText(const Value: WideString);'; + PropertyIntfMethodSet = ' procedure Set%:s(const Value: %:s);'; + PropertyIntfMethodLoadFromStream = ' procedure Load%:sFromStream(AStream: TStream);'; + PropertyIntfMethodLoadFromFile = ' procedure Load%:sFromFile(const AFileName: string);'; + PropertyIntfMethodSaveToStream = ' procedure Save%:sToStream(AStream: TStream);'; + PropertyIntfMethodSaveToFile = ' procedure Save%:sToFile(const AFileName: string);'; - PropertyInterfaceOptional = ' property Has%:s: Boolean read GetHas%:s;'; - PropertyInterfaceNilReadOnly = ' property %:sIsNil: Boolean read Get%:sIsNil;'; - PropertyInterfaceNil = ' property %:sIsNil: Boolean read Get%:sIsNil write Set%:sIsNil;'; - PropertyInterfaceTextReadOnly = ' property %:sText: WideString read Get%:sText;'; - PropertyInterfaceReadOnly = ' property %:s: %:s read Get%:s;'; - PropertyInterfaceText = ' property %:sText: WideString read Get%:sText write Set%:sText;'; - PropertyInterface = ' property %:s: %:s read Get%:s write Set%:s;'; + PropertyInterfaceOptional = ' property Has%:s: Boolean read GetHas%:s;'; + PropertyInterfaceNilReadOnly = ' property %:sIsNil: Boolean read Get%:sIsNil;'; + PropertyInterfaceNil = ' property %:sIsNil: Boolean read Get%:sIsNil write Set%:sIsNil;'; + PropertyInterfaceTextReadOnly = ' property %:sText: WideString read Get%:sText;'; + PropertyInterfaceReadOnly = ' property %:s: %:s read Get%:s;'; + PropertyInterfaceText = ' property %:sText: WideString read Get%:sText write Set%:sText;'; + PropertyInterface = ' property %:s: %:s read Get%:s write Set%:s;'; + + + PropertyImplMethodGetOptionalOrDefault = 'function TXML%:s.%:sDef(const ADefaultValue: %:s): %:s;' + CrLf + + 'begin' + CrLf + + ' if GetHas%:s then' + CrLf + + ' Result := Get%:s' + CrLf + + ' else' + CrLf + + ' Result := ADefaultValue;' + CrLf + + 'end;' + CrLf + + '' + CrLf; PropertyImplMethodGetOptional: array[TDelphiElementType] of string = (