Class AnApplication

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type AnApplication = class(APrintingObject)

Description

This class represents a single process (an application) in an object-oriented fashion. It is somewhat similar to the old Borland Turbo Pascal TApplication class and somewhat more simplistic than the TCustomApplication class provided in Free Component Library for Free Pascal.

The base implementation of this class supports printing a string representation of the class to an arbitrary stream; it also provides object-oriented access to command-line parameters and environment variables made available to the process. This functionality is designed to provide support for descendant classes, as you will likely not directly instantiate AnApplication, but will instead derive a custom class from it or one of its descendants (such as AConsoleApplication.

Hierarchy

Overview

Fields

Protected MyEnvironment: ADictionary;
Protected MyParameters: AStringList;
Protected MyVersionInfo: ADictionary;

Methods

Protected procedure parseEnvironment; virtual;
Protected procedure storeCommandLine; virtual;
Public function init: boolean; override;
Public destructor destroy; override;
Public function run: integer; virtual;
Public procedure sleep(const count: longword); virtual;
Public function executablePath: string; virtual;
Public function executableName(const withoutExtension: boolean = false): string; virtual;
Public function currentDirectory: string; virtual;
Public function setCurrentDirectory(const newPath: string): string; virtual;
Public function temporaryDirectory(const forAllUsers: boolean = false): string; virtual;
Public function configurationDirectory(const forAllUsers: boolean = false): string; virtual;
Public function userDirectory: string; virtual;
Public function toString: AnsiString; override;
Public function Environment: ADictionary; virtual;
Public function Parameters: AStringList; virtual;
Public function VersionInfo: ADictionary; virtual;

Description

Fields

Protected MyEnvironment: ADictionary;

Stores the environment variables available to the process

Protected MyParameters: AStringList;

Stores the command-line parameters available to the process

Protected MyVersionInfo: ADictionary;

Stores the version information available to the process

Methods

Protected procedure parseEnvironment; virtual;

Parse any and all environment variables made available to the process. This method separates the name of the variable from its value and stores both in AnApplication.Environment. It is called automatically by AnApplication.init.

Protected procedure storeCommandLine; virtual;

Parse the command line arguments made available to the process. This method collects all parameters except the zeroth one (the name of the application being executed) into AnApplication.Parameters. It is called automatically by AnApplication.init.

Public function init: boolean; override;

Initializer

Public destructor destroy; override;

Destroy the application instance.

This method is called automatically by TObject.free when it is called on an instance of AnApplication. It frees AnApplication.Environment and AnApplication.Parameters before calling the inherited routine.

Public function run: integer; virtual;

Execute the application.

This method should be overridden by descendant classes. In the base implementation, it does nothing.

Returns

A value indicating whether the application executed successfully. This value may be passed back to the underlying operating system. Generally, a return value of zero (0) means no errors occurred. A value greater than zero indicates some kind of error condition, with the exact meaning dependent on the application. A value less than zero indicates a condition which is not an error, but which can nonetheless prevent normal execution from proceeding.

Public procedure sleep(const count: longword); virtual;

Suspend execution of the application for at least count milliseconds. Depending on the underlying operating system, the actual amount of time that the process is suspended may be greater than count.

This method simply calls SysUtils.sleep. It is provided for convenience.

Public function executablePath: string; virtual;

Retrieve the absolute path of the executable; this does NOT include the name of the executable file itself. To obtain the name of the executable file without its path, call AnApplication.executableName.

Public function executableName(const withoutExtension: boolean = false): string; virtual;

Retrieve the name of the executable file that contains the application. This is the name of the file without any path information; to obtain the full path to the executable, call AnApplication.executablePath.

To obtain the name of the executable file without its extension, set withoutExtension to True.

Public function currentDirectory: string; virtual;

Get the current working directory of the appliction.

This method simply calls SysUtils.getCurrentDir and returns the result. It is provided for convenience.

Public function setCurrentDirectory(const newPath: string): string; virtual;

Set the current working directory of the application.

This method simply calls SysUtils.setCurrentDir. It is provided for convenience.

Returns

The previous value of AnApplication.currentDirectory.

Public function temporaryDirectory(const forAllUsers: boolean = false): string; virtual;

Get the name of the directory where temporary files may be stored. Depending on the underlying operating system, this directory may be specific to the user that is running the application; to obtain the name of a temporary directory that is accessible to all users, set forAllUsers to True.

This method simply calls SysUtils.getTempDir and returns the result. Note that the resulting string will contain a path delimiter.

Public function configurationDirectory(const forAllUsers: boolean = false): string; virtual;

Get the name of the directory where application configuration files may be stored. Depending on the underlying operating system, this directory may be specific to the user that is running the application; to obtain the name of a temporary directory that is accessible to all users, set forAllUsers to True.

This method simply calls SysUtils.getAppConfigDir and returns the result. Note that the resulting string will contain a path delimiter.

Public function userDirectory: string; virtual;

Get the name of the directory where data for the current user is kept.

This method simply calls SysUtils.getUserDir and returns the result. Note that the resulting string will contain a path delimiter.

Public function toString: AnsiString; override;

Construct a string representation of the application, suitable for printing to a text-based device, such as a console.

This method overrides the behavior inherted from APrintingObject.toString; it returns the value of AnApplication.executableName.

Public function Environment: ADictionary; virtual;

Retrieve a reference to the dictionary of environment variables available to the process. This reference should NOT be freed by the caller; that will be done when the AnApplication instance is destroyed.

The dictionary reference returned by this method will contain any and all environment variables made available to the process.

Public function Parameters: AStringList; virtual;

Retrieve a reference to the dictionary of command-line parameters available to the process. This reference should NOT be freed by the caller; that will be done when the AnApplication instance is destroyed.

The dictionary reference returned by this method will contain any and all parameters passed to the process. It will NOT contain the name of the application itself (the "zeroth" argument); this is instead returned by AnApplication.executablePath.

Public function VersionInfo: ADictionary; virtual;

Retrieve a reference to the dictionary of version information available to the process. This reference should NOT be freed by the caller; that will be done with the AnApplication instance is destroyed.

The dictionary reference returned by this method will contain any and all version information key/value pairs that were compiled as a resource into the object file which represents the main executable of the current process. This dictionary is cached, as these values will not change throughout the lifetime of the program. The dictionary is constructed the first time this method is called.


Generated by PasDoc 0.13.0 on 2015-06-23 19:40:11