OOP in Object Pascal

FACULTY of
COMPUTER SCIENCE
Multi-platform App
Development
Course 3:
Deployment and debugging
Agenda
• IDE:
– Deploying an app to different platforms
– Debugging local and remote applications
• Language:
– OOP in Object Pascal
2
Deploying to Android
Android Deployment:
•
•
•
•
3
Android SDK and NDK must be installed (default option in Appmethod setup)
An Android device accessible by ADB (connected directly to the development
machine through an USB cable or wireless network) is required
– command for listing connected devices: adb devices
The development machine must be trusted by the device (enable USB debugging)
The device has to be development enabled
http://docwiki.appmethod.com/appmethod/1.15/topics/en/Enabling_USB_Debugging_on_an_Android_Device
Deploying to Android
Android Deployment:
4
•
In the Project Manager the selected target
platform must be Android
•
Under Target the available devices are
displayed
•
Select one of the devices and click the run
button to deploy
http://docwiki.appmethod.com/appmethod/1.15/topics/en/Deploying_Multi-Device_Applications
Deploying to different platforms
Other platforms:
• The Platform Assistant Server (PAServer) is a command-line tool that allows you to
deploy multi-device applications (Win64, Mac OS X, iOS).
• You install the Platform Assistant on a remote system, in the same network as your
development system, to let Appmethod interact with that remote system.
• The Platform Assistant needs to be running and connected to the IDE in order for
you to do the following:
• PA Server enable user to
– Run and debug multi-device applications remotely (Win64, Mac OS X, iOS).
– Deploy multi-device applications (Win64, Mac OS X, iOS).
– Add to Appmethod a local copy of a Mac OS X or iOS SDK, which is a prerequisite for
developing Object Pascal or C++ applications for Mac OS X, iOS or iOS Simulator (Object
Pascal only).
5
http://docwiki.appmethod.com/appmethod/1.15/topics/en/PAServer,_the_Platform_Assistant_Server_Application
Deploying to different platforms
• A connection profile is a named set of properties that define a connection
to an instance of the Platform Assistant (PA) server running on a remote
machine.
• Connection profiles are required for
– running or debugging applications remotely. This allows you to run and debug
applications that target 64-bit Windows (when on a 32-bit Windows
development PC), Mac OS X or iOS.
– use Deployment Manager to deploy applications that target 64-bit Windows
(when on a 32-bit Windows development PC), Mac OS X or iOS.
• Connection profiles are not required for:
•
•
•
6
running or debugging Android applications.
use the Deployment Manager to deploy applications for Android.
deploying applications without Deployment Manager.
http://docwiki.appmethod.com/appmethod/1.15/topics/en/Connection_Profile_Manager
Deploying to different platforms
Deploying to iOS Devices
• Limitations imposed by Apple:
– A Mac is required (to which the device is attached)
– You need to be enrolled in the iOS Developer Program ($99/year)
– Setting up the Mac by means of Provisioning Procedures
• SSL Certificate and Keys
• Provisioning profile
7
Deploying to different platforms
Deploying to iOS Devices
• An iOS device connected through an USB cable to a Mac is required
• Correct Xcode version installed with regards to the iOS version the device is
running
• Xcode must be configured for iOS device deployment (iOS Developer profile,
Provisioning profile)
• PAServer must be running on the Mac
8
Deploying to different platforms
Deploying to iOS Devices
Using Appmethod:
• Development is done on Windows
- Connection profile with Mac IP
• iOS Device is connected to the Mac
• PAServer is installed and running on the Mac
• The built app is sent by Appmethod to the
Mac and then to the iOS device using
the PAServer
9
http://docwiki.appmethod.com/appmethod/1.15/topics/en/Deploying_Multi-Device_Applications
Deployment Manager
• In most of the cases when you create a project, everything is automated
when the project is deployed to the remote machine so there is no need
for user to interact with Deployment Manager.
• Deployment Manager gives you control over special cases like:
– View and control the exact list(name, location) of files deployed for each platform
– Add all "featured files”, such as database drivers for the target platform.
– Add user custom files.
10
http://docwiki.appmethod.com/appmethod/1.15/topics/en/Deployment_Manager
Deployment Manager actions
• Enable or disable the files that are to be deployed to the target
machine (check or uncheck ).
• Edit properties of the files that are to be deployed. For each
platform you can specify the Remote Path and Remote Name
• Delete files from the deployment list (click ).
• Select those files that you do not want overwritten during
deployment (Overwrite).
11
Deployment Manager actions
• Add featured files from a generated list of ready-to-deploy files.
– IDE automaticaly copies the files currently enabled in the deployment list
into the deployed application for each run, debug, or deploy of your
application
– For multi-device database applications, you use Deployment Manager to
enable the necessary database drivers for your specific target machine.
– You enable the database drivers using the Add Featured Files dialog box.
• Add custom/user files to your deployment list (click ). This are file
you want to be transferred on device and be accessible from the
program.
– Resource files (images, text, etc)
– Data files, etc
12
http://docwiki.appmethod.com/appmethod/1.15/topics/en/Deployment_Manager_-_Add_Featured_Files
Deployment specifics
•
When deploying custom files or features files
the deployment path will vary from one
platform to another.
• Usual paths like home directory, temp path,
document path, pictures, can be queried using
standard RTL functions that are generic for all
platforms.
• On windows only when using remote
deployment and debugging you get the
benefits of deployment manager.
In this example GetDocumentsPath for win32/64
platform and OSx platform would return user
documents directory.
This is not a preferred location , so a hardcoded
relative location within bundle is used instead.
13
var
ImageFile,
ImagesPath: string;
begin
…
{$IF DEFINED(IOS) OR DEFINED(ANDROID)}
ImagesPath := TPath.GetDocumentsPath;
{$ELSE}
ImagesPath := '../../images/';
{$ENDIF}
for ImageFile in TDirectory.GetFiles(ImagesPath) do
begin
…
end;
Deployment specifics
Example paths for TPath.GetHomePath from System.IOUtils.pas.
For iOS and Android path is bound to app ID, while for Win and OSX is bound to user.
XP
Windows
’C:\Documents and Settings\<username>\Application Data'
Vista or later
'C:\Users\<username>\AppData\Roaming'
OSX
A typical path is
'/Users/<username>'
Device
iOS
’/private/var/mobile/Applications/<application ID>'
Simulator
'/Users/<username>/Library/Application Support/iPhone Simulator/<SDK
version>/Applications/<application ID>'
Android
A typical path is
'/data/data/<application ID>/files'
14 More on http://docwiki.appmethod.com/appmethod/1.15/topics/en/Standard_RTL_Path_Functions_across_the_Supported_Target_Platforms
Debugging Multi-Device Applications
Debugger features:
- Stepping Through Code
- Evaluate/Modify
- Breakpoints
- Watches
- Debug Windows (Call Stack, Threads, Breakpoints etc)
15
http://docwiki.appmethod.com/appmethod/1.15/topics/en/Debugging_Multi-Device_Applications
OOP in Object Pascal
Highlights:
• Complete OOP support
• Classes & Interfaces
• Single inheritance
• Single root hierarchy (TObject for classes; IInterface for
Interfaces)
16
OOP in Object Pascal
Class declaration:
type
TShape = class(TInterfacedObject, IShape)
private
FPosition: TPoint;
function GetPosition: TPoint;
procedure SetPosition(APosition: TPoint);
public
17
property Position: TPoint read GetPosition write SetPosition;
procedure Draw; virtual; abstract;
end;
OOP in Object Pascal
Class declaration:
type
{ Class name } { Parent class, Implemented interface(s) }
TShape = class(TInterfacedObject, IShape)
{ Access modifier section }
private
FPosition: TPoint; { Fields must be listed before methods and properties }
{ Private methods }
function GetPosition: TPoint;
procedure SetPosition(APosition: TPoint);
18
public
{ Public methods and properties }
property Position: TPoint read GetPosition write SetPosition;
procedure Draw; virtual; abstract; { Abstract method }
end;
OOP in Object Pascal
Method implementation:
{ TShape }
function TShape.GetPosition: TPoint;
begin
Result := FPosition; // Result is a predefined variable that stores the return value
end;
19
OOP in Object Pascal
Access modifiers:
•
strict private
Can be accessed only by the class's methods
•
private
Can be accessed only by the class's methods or by methods in the same unit
•
protected
Can be accessed from the class's methods or from child classes
•
public
Unrestricted access
•
published
Similar to public but with additional RTTI (runtime type information)
20
OOP in Object Pascal
• Overloading a method:
type
TShape = class(TInterfacedObject, IShape)
private
FPosition: TPoint;
public
function GetPosition: TPoint;
{ Overloaded methods MUST be marked with the overload directive }
procedure SetPosition(X, Y: Integer);
overload;
procedure SetPosition(APosition: TPoint); overload;
end;
21
OOP in Object Pascal
• Inheritance
– Single inheritance; single root hierarchy
• Similar to Java / C#
• TObject is automatically inherited from if no parent class is specified
– A class can implement many interfaces
22
OOP in Object Pascal
• Polymorphism
type
TShape = class
public
{ For polymorphism the “virtual” directive is required, similar to C++ and C# }
procedure Draw; virtual; abstract;
end;
TRectangle = class(TShape)
public
{ In the child class the “override” directive is required, similar to C# but unlike C++ }
procedure Draw; override;
end;
23
OOP in Object Pascal
• Creating, using and freeing an object:
var
MyShape: TShape; { Declare a reference (MyShape) that can “point” to an object of type
TShape or one of its descendants (if we want to use polymorphism). }
begin
MyShape := TRectangle.Create; { Create a new TRectangle object and point MyShape to it. }
try
MyShape.Draw; { Perform operations on the object via MyShape reference; in this case
there will be polymorphic behavior: Draw from TRectangle will be called. }
finally
MyShape.Free; // De-allocate the memory. DO NOT use this for platforms with ARC
end;
24
end;
OOP in Object Pascal
• Creating, using and freeing an object:
– Memory allocation is only dynamic (on the heap)
– Object creation is done using the class name and the constructor name (by convention it’s
named “Create”)
– The object is accessed using a reference (similar to a pointer but with automatic
dereferencing)
– Operations on the object should be performed inside a try-finally block
– De-allocation is done by calling the inherited “Free” method
•
25
In case of using ARC manual de-allocation is not necessary (this is described later on)
OOP in Object Pascal
• Notable differences from other languages:
– Constructors and destructors are inherited (implicit ones come from TObject)
– There are no new / delete keywords (call ClassName.ConstructorName and
ReferenceName.Free instead)
– Constructors and destructors can have arbitrary names (although the
recommendation is to be named “Create” / “Destroy” respectively)
– Object destruction is done by calling the inherited “Free” method on the reference
• In case of using ARC manual de-allocation is not necessary (this is described later on)
26
OOP in Object Pascal
• Our TRectangle class with constructors and destructors:
type
TRectangle = class(TShape)
public
procedure Draw; override;
constructor Create; { Constructor declaration; by convention it’s named “Create”. }
destructor Destroy; override; { We need to mark it as override because Destroy
inherited from TObject is virtual; otherwise we won’t have polymorphic behavior
and TRectangle’s destructor will never be called. }
end;
27
OOP in Object Pascal
• Our TRectangle class with constructors and destructors:
constructor TRectangle.Create;
begin
inherited Create; { Call base class constructor - this must be the first statement
to ensure proper object initialization. }
// Do some initializations here.
end;
destructor TRectangle.Destroy;
begin
// Do some cleanup operations here.
28
inherited; // In the end call the inherited Destroy.
end;
OOP in Object Pascal
• More differences from other languages:
– Because Destroy is inherited from TObject where it’s marked as
virtual we create an overridden version of it and we must
declare it with “override” directive in order for it to actually be
called;
– Constructors / destructors for parent classes are not
automatically called; one must explicitly call “inherited Create”
as first statement in the constructor and “inherited Destroy” as
last statement in the destructor.
29
OOP in Object Pascal
• Automatic Reference Counting:
– Objects’ lifetime is managed using the number of existing references to it (when the number
of references reaches zero, the object is destroyed)
– Desktop compilers have ARC for interfaces, dynamic arrays and strings. Mobile compilers also
have for classes
– Weak References are used on mobile compilers to have a reference to an object without
increasing its reference count. The [weak] attribute is used to achieve this behavior
– To forcefully destroy a component the “DisposeOf” method must be used that will put the
object in the disposed state until the memory is freed
30
http://docwiki.appmethod.com/appmethod/1.15/topics/en/Automatic_Reference_Counting_in_Object_Pascal_Mobile_Compilers
OOP in Object Pascal
• More information about Classes and Objects:
– http://docwiki.appmethod.com/appmethod/1.15/topics/en/Classes_and_Objects
– http://docwiki.appmethod.com/appmethod/1.15/topics/en/Fields
– http://docwiki.appmethod.com/appmethod/1.15/topics/en/Methods
– http://docwiki.appmethod.com/appmethod/1.15/topics/en/Properties
31
Q&A
32
Thank you!
33