Skip to content

Commit 1e7f818

Browse files
committed
Adjust some examples to Delphi, add Makefile commands to test using Delphi
1 parent 23b697d commit 1e7f818

26 files changed

+96
-56
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ modern_pascal_introduction_ukrainian.pdf
1616
modern_pascal_introduction_ukrainian.xml
1717

1818
/code-samples*/*.ppu
19+
/code-samples*/*.dcu
1920
/code-samples*/*.o
2021
/code-samples*/*.exe
2122
/code-samples*/anonymous_functions

code-samples/Makefile

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
1-
UNIT_SOURCES:=$(wildcard *.pas)
2-
PROGRAM_SOURCES:=$(wildcard *.lpr) $(wildcard *.dpr)
3-
4-
PROGRAM_UNIX_BINARIES:=$(PROGRAM_SOURCES:.lpr=)
5-
PROGRAM_UNIX_BINARIES:=$(PROGRAM_UNIX_BINARIES:.dpr=)
6-
7-
PROGRAM_WINDOWS_BINARIES:=$(PROGRAM_SOURCES:.lpr=.exe)
8-
PROGRAM_WINDOWS_BINARIES:=$(PROGRAM_WINDOWS_BINARIES:.dpr=.exe)
9-
10-
.PHONY: all
11-
all:
12-
$(foreach UNIT_NAME,$(UNIT_SOURCES),fpc $(UNIT_NAME) && ) true
13-
$(foreach PROGRAM_NAME,$(PROGRAM_SOURCES),fpc $(PROGRAM_NAME) && ) true
14-
15-
.PHONY: clean
16-
clean:
17-
rm -Rf *.o *.ppu $(PROGRAM_UNIX_BINARIES) $(PROGRAM_WINDOWS_BINARIES)
1+
UNIT_SOURCES:=$(wildcard *.pas)
2+
PROGRAM_SOURCES:=$(wildcard *.lpr) $(wildcard *.dpr)
3+
4+
PROGRAM_UNIX_BINARIES:=$(PROGRAM_SOURCES:.lpr=)
5+
PROGRAM_UNIX_BINARIES:=$(PROGRAM_UNIX_BINARIES:.dpr=)
6+
7+
PROGRAM_WINDOWS_BINARIES:=$(PROGRAM_SOURCES:.lpr=.exe)
8+
PROGRAM_WINDOWS_BINARIES:=$(PROGRAM_WINDOWS_BINARIES:.dpr=.exe)
9+
10+
.PHONY: all
11+
all:
12+
$(foreach UNIT_NAME,$(UNIT_SOURCES),fpc $(UNIT_NAME) && ) true
13+
$(foreach PROGRAM_NAME,$(PROGRAM_SOURCES),fpc $(PROGRAM_NAME) && ) true
14+
15+
# Delphi (dcc) command line options.
16+
# See https://github.com/castle-engine/castle-engine/blob/master/tools/build-tool/code/toolcompile.pas#L858
17+
# for how Castle Game Engine build tool invokes dcc for some hints.
18+
# -NS below follows the DPROJ settings generated by Delphi for new projects.
19+
DELPHI_OPTIONS_COMMON:='-NSSystem;Xml;Data;Datasnap;Web;Soap'
20+
DELPHI_WIN32_COMMAND:=dcc32 $(DELPHI_OPTIONS_COMMON)
21+
DELPHI_WIN64_COMMAND:=dcc64 $(DELPHI_OPTIONS_COMMON)
22+
23+
.PHONY: all-delphi-win32
24+
all-delphi-win32:
25+
$(foreach UNIT_NAME,$(UNIT_SOURCES),$(DELPHI_WIN32_COMMAND) $(UNIT_NAME) && ) true
26+
$(foreach PROGRAM_NAME,$(PROGRAM_SOURCES),$(DELPHI_WIN32_COMMAND) $(PROGRAM_NAME) && ) true
27+
28+
.PHONY: all-delphi-win64
29+
all-delphi-win64:
30+
$(foreach UNIT_NAME,$(UNIT_SOURCES),$(DELPHI_WIN64_COMMAND) $(UNIT_NAME) && ) true
31+
$(foreach PROGRAM_NAME,$(PROGRAM_SOURCES),$(DELPHI_WIN64_COMMAND) $(PROGRAM_NAME) && ) true
32+
33+
.PHONY: clean
34+
clean:
35+
rm -Rf *.o *.ppu $(PROGRAM_UNIX_BINARIES) $(PROGRAM_WINDOWS_BINARIES)

code-samples/anon_functions_assignment_test.dpr

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,41 @@
1111
uses Classes;
1212

1313
type
14+
{$ifdef FPC}
1415
TFunc = function: LongInt;
16+
{$else}
17+
TFunc = reference to function: LongInt;
18+
TProcedure = reference to procedure;
19+
TNotifyEvent = reference to procedure(aSender: TObject);
20+
{$endif}
1521

1622
var
1723
p: TProcedure;
1824
f: TFunc;
1925
n: TNotifyEvent;
2026
begin
27+
{$ifdef FPC} // Delphi doesn't allow to invoke like this
2128
procedure(const aArg: String)
2229
begin
2330
Writeln(aArg);
2431
end('Hello World');
32+
{$endif}
2533

2634
p := procedure
2735
begin
2836
Writeln('Foobar');
2937
end;
3038
p();
3139

32-
n := procedure(aSender: TObject)
40+
n := procedure(Sender: TObject)
3341
begin
34-
Writeln(HexStr(Pointer(aSender)));
42+
Writeln(Sender.ClassName);
3543
end;
3644
n(Nil);
3745

38-
f := function MyRes : LongInt
46+
f := function: LongInt
3947
begin
40-
MyRes := 42;
48+
Result := 42;
4149
end;
4250
Writeln(f());
4351
end.

code-samples/callbacks_of_object.dpr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ var
3838
begin
3939
C := TMyClass.Create;
4040
try
41-
C.ProcessTheList(@C.Add);
41+
C.ProcessTheList({$ifdef FPC}@{$endif} C.Add);
4242
WriteLn('1 + 2 + 3 ... + 10 = ', C.CurrentValue);
4343

44-
C.ProcessTheList(@C.Multiply);
44+
C.ProcessTheList({$ifdef FPC}@{$endif} C.Multiply);
4545
WriteLn('1 * 2 * 3 ... * 10 = ', C.CurrentValue);
4646
finally
4747
FreeAndNil(C);

code-samples/for_in_list.dpr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
{$ifdef FPC} {$mode objfpc}{$H+}{$J-} {$endif}
22
{$ifdef MSWINDOWS} {$apptype CONSOLE} {$endif}
33

4+
{$ifndef FPC}
5+
{$message warn 'Delphi does not have FGL unit'}
6+
begin end.
7+
{$endif}
8+
49
uses
510
SysUtils, FGL;
611

712
type
813
TMyClass = class
914
I, Square: Integer;
1015
end;
11-
TMyClassList = specialize TFPGObjectList<TMyClass>;
16+
TMyClassList = {$ifdef FPC}specialize{$endif} TFPGObjectList<TMyClass>;
1217

1318
var
1419
List: TMyClassList;

code-samples/generic_functions.dpr

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
{$ifdef FPC} {$mode objfpc}{$H+}{$J-} {$endif}
22
{$ifdef MSWINDOWS} {$apptype CONSOLE} {$endif}
33

4-
uses
5-
SysUtils;
4+
{$ifndef FPC}
5+
{$message warn 'Delphi does not support global generic functions'}
6+
begin end.
7+
{$endif}
8+
9+
uses SysUtils;
610

711
{ Note: this example requires FPC 3.1.1 (will not compile with FPC 3.0.0 or older). }
812

code-samples/generics.dpr

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
{$ifdef FPC} {$mode objfpc}{$H+}{$J-} {$endif}
22
{$ifdef MSWINDOWS} {$apptype CONSOLE} {$endif}
33

4-
uses
5-
SysUtils;
4+
{$ifndef FPC}
5+
{$message warn 'Delphi does not allow addition on types that are generic parameters'}
6+
begin end.
7+
{$endif}
8+
9+
uses SysUtils;
610

711
type
812
generic TMyCalculator<T> = class
@@ -16,8 +20,8 @@ begin
1620
end;
1721

1822
type
19-
TMyFloatCalculator = specialize TMyCalculator<Single>;
20-
TMyStringCalculator = specialize TMyCalculator<string>;
23+
TMyFloatCalculator = {$ifdef FPC}specialize{$endif} TMyCalculator<Single>;
24+
TMyStringCalculator = {$ifdef FPC}specialize{$endif} TMyCalculator<string>;
2125

2226
var
2327
FloatCalc: TMyFloatCalculator;

code-samples/generics_dictionary.dpr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type
88
Name: string;
99
end;
1010

11-
TAppleDictionary = specialize TDictionary<string, TApple>;
11+
TAppleDictionary = {$ifdef FPC}specialize{$endif} TDictionary<string, TApple>;
1212

1313
var
1414
Apples: TAppleDictionary;

code-samples/generics_lists.dpr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type
88
Name: string;
99
end;
1010

11-
TAppleList = {$ifdef FPC_OBJFPC}specialize{$endif} TObjectList<TApple>;
11+
TAppleList = {$ifdef FPC}specialize{$endif} TObjectList<TApple>;
1212

1313
var
1414
A: TApple;

code-samples/generics_object_dictionary.dpr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type
88
Name: string;
99
end;
1010

11-
TAppleDictionary = specialize TObjectDictionary<string, TApple>;
11+
TAppleDictionary = {$ifdef FPC}specialize{$endif} TObjectDictionary<string, TApple>;
1212

1313
var
1414
Apples: TAppleDictionary;

0 commit comments

Comments
 (0)