function IsValidFilePath(const FileName: String): Boolean;var
S: String;
I: Integer;begin
Result :=False;
S := FileName;repeat
I := LastDelimiter('\/', S);
MoveFile(nil, PChar(S));if(GetLastError = ERROR_ALREADY_EXISTS)or((GetFileAttributes(PChar(Copy(S, I +1, MaxInt)))= INVALID_FILE_ATTRIBUTES)and(GetLastError=ERROR_INVALID_NAME))then
Exit;if I>0then
S := Copy(S,1,I-1);until I =0;
Result :=True;end;
What is the proper way for pooling of TCustomConnection instances in Delphi, that allows to distinguish between instances that have effectively equal connection properties and the ones that are effectively unequal?
I’ve tried searching the RTL and VCL sources and didn’t find a generic way.
I could copy either of the specific ones I found (see list below) and adapt them to a more generic solution or adapt one of the answers in #16404051 to be for TCustomConnection, but I wonder if there is an existing solution for TCustomConnection in the first place.
The below came in really useful in an old project I took over that was full of bugs having to do with improper casts and FreeAndNil usage.
EDIT 20181010: WordPress.com keeps mangling angle-brackets in pre and code sections, so I added the code to a gist; see link below.
First the examples.
procedure TMyServer.UnbindFromIdTcpServerStatusContext(const aContext: TIdContext);
var
lClientSession: TClientSession;
begin
lClientSession := TObjectHelper.Cast<TClientSession>(aContext.Data);
...
end;
type
TBaseDataInterface = class(TObject)
strict private
FDatabase: TIBDatabase;
FTransaction: TIBTransaction;
...
end;
destructor TBaseDataInterface.Destroy();
begin
TObjectHelper.FreeAndNil(FDatabase);
TObjectHelper.FreeAndNil(FTransaction);
...
inherited Destroy();
end;
And the implementation.
unit ObjectHelperUnit;
interface
type
TObjectHelper = record
class function Cast<T: class>(const aValue: TObject): T; static;
class procedure FreeAndNil<T: class>(var Value: T); static;
end;
implementation
uses
System.SysConst,
System.SysUtils;
class function TObjectHelper.Cast<T>(const aValue: TObject): T;
var
lException: Exception;
begin
if Assigned(aValue) then
begin
if aValue is T then
Result := T(aValue)
else
begin
lException := EInvalidCast.CreateFmt('%s; actual type %s but expected %s.',
[SInvalidCast, aValue.QualifiedClassName, T.QualifiedClassName]);
raise lException;
end;
end
else
Result := nil;
end;
class procedure TObjectHelper.FreeAndNil<T>(var Value: T);
begin
System.SysUtils.FreeAndNil(Value);
end;
end.
–jeroen
Gist:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Blast from the past, but still relevant, this article by Peter Below:
This article follows the path of a keystroke message through the VCL. You will learn how the key processing is implemented, how the OnKey events work and what intervention points for the programmer can be found in the whole process. In addition, things like message processing are explained, and you will learn how to trace messages in the debugger from the message loop to their eventual destination.
With fresh libraries – like new Delphi and Windows versions – I usually take a pause to see if any major updates have been published to stabalise things.