summaryrefslogtreecommitdiff
path: root/tests/webtbs/tw4950.pp
blob: ed65272b72726d47befa4212e5b1467644cb4a1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// This code is provided bt Leandro Conde [leandor@gmail.com]
// to demonstrate a problem with function redirection in interfaces
// in the FPC compiler version 2.0.2
//
// In the real implementation is used to provide a set of data type
// conversion functions: From<Type> and To<Type>.
// In that scenario, the IBase contains functions specific por <Type>,
// and the Derived are the <From> and <To> parts.

{$mode delphi}

program FunctionRedirectionProblem;
{$APPTYPE CONSOLE}

var
  cnt : longint;

type
  // It really doesn't matters what this interface contains,
  // is just represet some functionality set that is provided
  // with two complementary sets of implementations.
  IBase = interface
    ['{6CEA50E0-1844-405F-9B6F-2E9D50BE6EFC}']
    function Base1(const Arg1: string; const Arg2: Double): Boolean;
    function Base2(const Arg1: TDateTime; const Arg2: Single): Boolean;
    function Base3(const Arg1: WideString; const Arg2: Integer): Boolean;
  end;

  // Derived 1 to provide an IBase with a specific funtionality when used from IDerived1
  IDerived1 = interface (IBase)
    ['{3DC49E1B-9A52-499E-8BCD-E1BA160329C9}']
    function Derived1Specific(Arg1: Integer): Integer;
  end;

  // Derived 2 to provide an IBase with a complementary funtionality when used from IDerived2
  IDerived2 = interface (IBase)
    ['{E44DF3BC-75C8-4284-928B-DE3162347C21}']
    function Derived2Specific(Arg1: Integer): Integer;
  end;

  // Just for runtime testing purposes
  ITestCase = interface
    ['{C608134F-2F9A-44A4-8932-F169EF40E177}']
    function Derived1: IDerived1;
    function Derived2: IDerived2;
  end;

type
  // Implementing all the above stuff
  TTestCase = class(TInterfacedObject, IDerived1, IDerived2, ITestCase )
  protected
    // methods from IBase in IDerived1 redirected to one function set:
    function IDerived1.Base1 = Derived1_Base1;
    function IDerived1.Base2 = Derived1_Base2;
    function IDerived1.Base3 = Derived1_Base3;
    // this is the function set for IBase in IDerived1
    function Derived1_Base1(const Arg1: string; const Arg2: Double): Boolean;
    function Derived1_Base2(const Arg1: TDateTime; const Arg2: Single): Boolean;
    function Derived1_Base3(const Arg1: WideString; const Arg2: Integer): Boolean;
    // specifics from IDerived1
    function Derived1Specific(Arg1: Integer): Integer;
  protected
    // methods from IBase in IDerived2 redirected to the other function set:
    function IDerived2.Base1 = Derived2_Base1;
    function IDerived2.Base2 = Derived2_Base2;
    function IDerived2.Base3 = Derived2_Base3;
    // this is the function set for IBase in IDerived2
    function Derived2_Base1(const Arg1: string; const Arg2: Double): Boolean;
    function Derived2_Base2(const Arg1: TDateTime; const Arg2: Single): Boolean;
    function Derived2_Base3(const Arg1: WideString; const Arg2: Integer): Boolean;
    // specifics from IDerived2
    function Derived2Specific(Arg1: Integer): Integer;
  protected // this is not essential, just for testing in runtime ITestCase
    function Derived1: IDerived1;
    function Derived2: IDerived2;
  end;

{ TTestCase }

function TTestCase.Derived1_Base1(const Arg1: string; const Arg2: Double): Boolean;
begin
  Result := True;
  writeln('1:',arg1,arg2);
  inc(cnt);
end;

function TTestCase.Derived1_Base2(const Arg1: TDateTime; const Arg2: Single): Boolean;
begin
  Result := True;
end;

function TTestCase.Derived1_Base3(const Arg1: WideString; const Arg2: Integer): Boolean;
begin
  Result := True;
end;

function TTestCase.Derived1Specific(Arg1: Integer): Integer;
begin
  Result := Arg1;
end;

function TTestCase.Derived2_Base1(const Arg1: string; const Arg2: Double): Boolean;
begin
  Result := True;
  writeln('2:',arg1,arg2);
  inc(cnt);
end;

function TTestCase.Derived2_Base2(const Arg1: TDateTime; const Arg2: Single): Boolean;
begin
  Result := True;
end;

function TTestCase.Derived2_Base3(const Arg1: WideString; const Arg2: Integer): Boolean;
begin
  Result := True;
end;

function TTestCase.Derived2Specific(Arg1: Integer): Integer;
begin
  Result := Arg1;
end;

function TTestCase.Derived1: IDerived1;
begin
  Result := Self;
end;

function TTestCase.Derived2: IDerived2;
begin
  Result := Self;
end;

var
  TestCase: ITestCase;
begin
  TestCase := TTestCase.Create;

  TestCase.Derived1.Base1('called from derived1', 3.14159);
  TestCase.Derived2.Base1('called from derived2', 2.7178);
  if cnt<>2 then
    halt(1);
end.