* Make getters/setter protected, add flag to make them private methods

This commit is contained in:
Michaël Van Canneyt 2024-06-13 15:55:13 +02:00
parent c9260edb4f
commit 558704f91c
4 changed files with 51 additions and 27 deletions

View File

@ -86,7 +86,8 @@ Type
coExpandUnionTypeArgs,
coDictionaryAsClass,
coChromeWindow,
coOnlyUsed
coOnlyUsed,
coPrivateMethods
);
TBaseConversionOptions = Set of TBaseConversionOption;
@ -96,7 +97,8 @@ const
'ExpandUnionTypeArgs',
'DictionaryAsClass',
'ChromeWindow',
'OnlyUsed'
'OnlyUsed',
'PrivateMethods'
);
NativeTypeNames : Array [TPascalNativeType] of String = (
'',
@ -265,13 +267,13 @@ type
function WritePlainFields(aParent: TIDLDefinition; aList: TIDLDefinitionList): Integer; virtual;
function WriteDictionaryFields(aDict: TIDLDictionaryDefinition; aList: TIDLDefinitionList): Integer; virtual;
function WritePrivateReadOnlyFields(aParent: TIDLDefinition; aList: TIDLDefinitionList): Integer; virtual;
function WritePrivateGetters(aParent: TIDLStructuredDefinition; aList: TIDLDefinitionList): Integer; virtual;
function WritePrivateSetters(aParent: TIDLStructuredDefinition; aList: TIDLDefinitionList): Integer; virtual;
function WriteGetters(aParent: TIDLStructuredDefinition; aList: TIDLDefinitionList): Integer; virtual;
function WriteSetters(aParent: TIDLStructuredDefinition; aList: TIDLDefinitionList): Integer; virtual;
// Maplike-specific methods
function WriteMapLikePrivateReadOnlyFields(aParent: TIDLDefinition; aMap: TIDLMapLikeDefinition): Integer; virtual;
function WriteMapLikeMethodDefinitions(aParent: TIDLStructuredDefinition; aMap: TIDLMapLikeDefinition): integer; virtual;
function WriteMapLikeProperties(aParent: TIDLDefinition; aMap: TIDLMapLikeDefinition): Integer; virtual;
function WriteMapLikePrivateGetters(aParent: TIDLStructuredDefinition; aMap: TIDLMapLikeDefinition): Integer; virtual;
function WriteMapLikeGetters(aParent: TIDLStructuredDefinition; aMap: TIDLMapLikeDefinition): Integer; virtual;
// Implementations. For webidl2pas, these are empty
procedure WriteDefinitionImplementation(D: TIDLDefinition); virtual;
procedure WriteTypeDefsAndCallbackImplementations(aList: TIDLDefinitionList); virtual;
@ -601,7 +603,7 @@ begin
Result:=Result+WriteMapLikePrivateReadOnlyFields(aParent,MD);
end;
function TBaseWebIDLToPas.WritePrivateGetters(
function TBaseWebIDLToPas.WriteGetters(
aParent: TIDLStructuredDefinition; aList: TIDLDefinitionList): Integer;
var
D : TIDLDefinition;
@ -614,10 +616,10 @@ begin
for D in aList do
if D is TIDLMapLikeDefinition then
if ConvertDef(D) then
Result:=Result+WriteMapLikePrivateGetters(aParent,MD);
Result:=Result+WriteMapLikeGetters(aParent,MD);
end;
function TBaseWebIDLToPas.WritePrivateSetters(
function TBaseWebIDLToPas.WriteSetters(
aParent: TIDLStructuredDefinition; aList: TIDLDefinitionList): Integer;
begin
Result:=0;
@ -657,7 +659,7 @@ begin
Result:=1;
end;
function TBaseWebIDLToPas.WriteMapLikePrivateGetters(aParent: TIDLStructuredDefinition; aMap: TIDLMapLikeDefinition): Integer;
function TBaseWebIDLToPas.WriteMapLikeGetters(aParent: TIDLStructuredDefinition; aMap: TIDLMapLikeDefinition): Integer;
begin
if (aParent<>Nil) and (aMap<>Nil) then;
Result:=0;
@ -1101,8 +1103,14 @@ begin
AddLn('Private');
Indent;
WritePrivateReadOnlyFields(Intf,ML);
WritePrivateGetters(Intf,ML);
WritePrivateSetters(Intf,ML);
if Not (coPrivateMethods in BaseOptions) then
begin
Undent;
AddLn('Protected');
Indent;
end;
WriteGetters(Intf,ML);
WriteSetters(Intf,ML);
Undent;
// write public section
AddLn('Public');
@ -1149,8 +1157,14 @@ begin
AddLn('Private');
Indent;
WritePrivateReadOnlyFields(aNamespace,ML);
WritePrivateGetters(aNamespace,ML);
WritePrivateSetters(aNamespace,ML);
if not (coPrivateMethods in BaseOptions) then
begin
Undent;
AddLn('Protected');
Indent;
end;
WriteGetters(aNamespace,ML);
WriteSetters(aNamespace,ML);
Undent;
// write public section
AddLn('Public');

View File

@ -137,14 +137,14 @@ type
function WriteOtherImplicitTypes(Intf: TIDLStructuredDefinition; aMemberList: TIDLDefinitionList): Integer; override;
// Code generation routines. Return the number of actually written defs.
function WriteDictionaryPrivateFields(aParent: TIDLStructuredDefinition; aList: TIDLDefinitionList): Integer; virtual;
function WritePrivateGetters(aParent: TIDLStructuredDefinition; aList: TIDLDefinitionList): Integer; override;
function WritePrivateSetters(aParent: TIDLStructuredDefinition; aList: TIDLDefinitionList): Integer; override;
function WriteGetters(aParent: TIDLStructuredDefinition; aList: TIDLDefinitionList): Integer; override;
function WriteSetters(aParent: TIDLStructuredDefinition; aList: TIDLDefinitionList): Integer; override;
function WriteProperties(aParent: TIDLDefinition; aList: TIDLDefinitionList): Integer; override;
function WriteUtilityMethods(Intf: TIDLStructuredDefinition): Integer; override;
// Maplike
function WriteMapLikeProperties(aParent: TIDLDefinition; aMap: TIDLMapLikeDefinition): Integer; override;
function WriteMapLikePrivateReadOnlyFields(aParent: TIDLDefinition; aMap: TIDLMapLikeDefinition): Integer; override;
function WriteMapLikePrivateGetters(aParent: TIDLStructuredDefinition; aMap: TIDLMapLikeDefinition): Integer; override;
function WriteMapLikeGetters(aParent: TIDLStructuredDefinition; aMap: TIDLMapLikeDefinition): Integer; override;
// Definitions. Return true if a definition was written.
function WriteEnumDef(aDef: TIDLEnumDefinition): Boolean; override;
function WriteDictionaryDef(aDict: TIDLDictionaryDefinition): Boolean; override;
@ -528,8 +528,8 @@ begin
AddLn('['''+ComputeGUID(Decl,aMemberList)+''']');
// private members
WritePrivateGetters(Intf,aMemberList);
WritePrivateSetters(Intf,aMemberList);
WriteGetters(Intf,aMemberList);
WriteSetters(Intf,aMemberList);
// public members
if StructType<>sdDictionary then
@ -554,12 +554,12 @@ begin
// Do nothing, used in stub
end;
function TWebIDLToPasWasmJob.WritePrivateGetters(aParent: TIDLStructuredDefinition;
function TWebIDLToPasWasmJob.WriteGetters(aParent: TIDLStructuredDefinition;
aList: TIDLDefinitionList): Integer;
var
D: TIDLDefinition;
begin
Result:=Inherited WritePrivateGetters(aParent,aList);
Result:=Inherited WriteGetters(aParent,aList);
for D in aList do
if D is TIDLPropertyDefinition then
if ConvertDef(D) then
@ -567,12 +567,12 @@ begin
inc(Result);
end;
function TWebIDLToPasWasmJob.WritePrivateSetters(
function TWebIDLToPasWasmJob.WriteSetters(
aParent: TIDLStructuredDefinition; aList: TIDLDefinitionList): Integer;
var
D: TIDLDefinition;
begin
Result:=Inherited WritePrivateSetters(aParent,aList);
Result:=Inherited WriteSetters(aParent,aList);
for D in aList do
if D is TIDLPropertyDefinition then
if ConvertDef(D) then
@ -618,7 +618,7 @@ begin
Result:=0;
end;
function TWebIDLToPasWasmJob.WriteMapLikePrivateGetters(aParent: TIDLStructuredDefinition; aMap: TIDLMapLikeDefinition): Integer;
function TWebIDLToPasWasmJob.WriteMapLikeGetters(aParent: TIDLStructuredDefinition; aMap: TIDLMapLikeDefinition): Integer;
begin
if (aParent=Nil) and (aMap=Nil) then ; // Silence compiler warning
@ -651,8 +651,14 @@ begin
AddLn('Private');
Indent;
WriteDictionaryPrivateFields(aDict,DefList);
WritePrivateGetters(aDict,DefList);
WritePrivateSetters(aDict,DefList);
if not (coPrivateMethods in BaseOptions) then
begin
Undent;
AddLn('Protected');
Indent;
end;
WriteGetters(aDict,DefList);
WriteSetters(aDict,DefList);
Undent;
AddLn('Public');
Indent;

View File

@ -46,6 +46,7 @@
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value="../../packages/webidl/src"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Linking>

View File

@ -148,6 +148,7 @@ begin
CheckBaseOption(coExpandUnionTypeArgs,'e','expandunionargs');
CheckBaseOption(coChromeWindow,'r','chrome');
CheckBaseOption(coPrivateMethods,'a','private');
// -f ?
A:=GetOptionValue('g','globals');
@ -230,7 +231,7 @@ end;
procedure TWebIDLToPasApplication.DoRun;
const
Short = 'ced::f:g:hi:m:n:o:pt:u:vw:x:rl:';
Short = 'ced::f:g:hi:m:n:o:pt:u:vw:x:rl:a';
Long : Array of string = (
'help',
'constexternal',
@ -249,7 +250,8 @@ const
'webidlversion:',
'extra:',
'chrome',
'list:'
'list:',
'private'
);
@ -332,6 +334,7 @@ begin
writeln(StdErr,'Usage: ', ExeName, ' [options]');
Writeln(StdErr,'Where option is one or more of');
Writeln(StdErr,'-h --help This help text.');
Writeln(StdErr,'-a --private Write getters/setters as private methods. Default is protected.');
Writeln(StdErr,'-c --constexternal Write consts as external const (no value).');
Writeln(StdErr,'-d --dicttoclass[=Parent] Write dictionaries as classes.');
Writeln(StdErr,'-e --expandunionargs Add overloads for all Union typed function arguments.');