There are 2 techniques
{ Printer function - Replaces the Printer global variable of previous versions,
to improve smart linking (reduce exe size by 2.5k in projects that don't use
the printer). Code which assigned to the Printer global variable
must call SetPrinter instead. SetPrinter returns current printer object
and makes the new printer object the current printer. It is the caller's
responsibility to free the old printer, if appropriate. (This allows
toggling between different printer objects without destroying configuration
settings.) }
|
Overview
| Unit | Variable | Function | |
|---|---|---|---|
| graphics.pas | PatternManager BitmapCanvasList CanvasList FontManager PenManager BrushManager | GetFileFormats GetClipboardFormats | These variables are defined as global to the unit,
created at runtime, and can be accessed by
all instances of the various classes defined in the unit.
However, they are not visible to any code outside the unit.
Perhaps only a part of the class was converted to the new - function driven - style. The supported File and Clipboard image formats can be extended at runtime. |
| clipbrd.pas | Clipboard | Provides access to the windows clipboard. | |
| printers.pas | Printer | Allows additional printer types to be added at run time. | |
| menus.pas | ShortCutItems PopupList CommandPool | Two of these are global, the other is global to just the unit. |
Examples
Clipboard
unit Clipbrd;
interface
function Clipboard: TClipboard; // this is the global function
implementation
var
FClipboard: TClipboard; // this is a private variable to hold the object
function Clipboard: TClipboard;
begin
if FClipboard = nil then
FClipboard := TClipboard.Create;
Result := FClipboard;
end;
initialization
FClipboard := nil; // this should not be necessary
finalization
FClipboard.Free;
end.
|
FClipboard := nil; |
unit Printers;
interface
{ Printer function - Replaces the Printer global variable of previous versions,
to improve smart linking (reduce exe size by 2.5k in projects that don't use
the printer). Code which assigned to the Printer global variable
must call SetPrinter instead. SetPrinter returns current printer object
and makes the new printer object the current printer. It is the caller's
responsibility to free the old printer, if appropriate. (This allows
toggling between different printer objects without destroying configuration
settings.) }
function Printer: TPrinter; // this is the global function
implementation
var
FPrinter: TPrinter = nil; // not sure why it is assigned to nil
function Printer: TPrinter;
begin
if FPrinter = nil then FPrinter := TPrinter.Create;
Result := FPrinter;
end;
initialization
finalization
FPrinter.Free;
end.
|
TGraphics
unit Graphics;
interface
TPicture = class(TPersistent)
public
class procedure RegisterFileFormat(const AExtension, ADescription: string;
AGraphicClass: TGraphicClass);
end;
implementation
var
FileFormats: TFileFormatsList = nil;
function GetFileFormats: TFileFormatsList;
begin
if FileFormats = nil then FileFormats := TFileFormatsList.Create;
Result := FileFormats;
end;
class procedure TPicture.RegisterFileFormat(const AExtension,
ADescription: string; AGraphicClass: TGraphicClass);
begin
GetFileFormats.Add(AExtension, ADescription, 0, AGraphicClass);
end;
|
This is how the JPEG unit adds itself to the list of supported TGraphics types.
unit jpeg;
interface
implementation
initialization
InitDefaults;
TPicture.RegisterFileFormat('jpeg', sJPEGImageFile, TJPEGImage);
TPicture.RegisterFileFormat('jpg' , sJPEGImageFile, TJPEGImage);
finalization
TPicture.UnregisterGraphicClass(TJPEGImage);
end.
|
Menus
unit Menus; interface var PopupList : TPopupList; // global to the application ShortCutItems: TMenuItemStack; implementation var CommandPool: TBits; // global to the unit initialization CommandPool := TBits .Create; PopupList := TPopupList .Create; ShortCutItems := TMenuItemStack.Create; finalization ShortCutItems.Free; PopupList .Free; CommandPool .Free; end. |
Naming Conventions
From unit mcExtras,
type Tmc_File_Version_Info = class
private
public
constructor Create; virtual;
// the details don't matter on this page
end;
function mc_File_Version_Info: Tmc_File_Version_Info;
implementation
var
Fmc_File_Version_Info : Tmc_File_Version_Info;
function mc_File_Version_Info: Tmc_File_Version_Info;
begin
if Fmc_File_Version_Info=nil then begin
Fmc_File_Version_Info := Tmc_File_Version_Info.Create;
end;
result := Fmc_File_Version_Info;
end;
{ end mc_File_Version_Info }
initialization // required so that *finalization* can be defined
finalization
Fmc_File_Version_Info.Free;
|
unit mc_LogFiles;
type
Tmc_LogFiles = class(TObject)
// the details don't matter on this page
end;
var
mcLogFiles: Tmc_LogFiles;
implementation
initialization
mcLogFiles := Tmc_LogFiles.Create ;
finalization
mcLogFiles.Free;
|