Hello,
For my double linked list I want to pass any type of pointer to it, since
all pointers will basically point to the same kind of memory structure.
For example:
PsomeNode
TsomeNode = packed record
Next
Prev
SomeData
end;
PotherNode
TotherNode = packed record
Next
Prev
OtherData
end;
Notice how both first two fields are the same.
So the list only needs to know about
Pnode
Tnode = packed record
Next
Prev
Data : record end; // empty record
end;
But I don't want to have to write code like this all the time !:
AddBegin( Pnode(SomeNode) );
Notice the nasty typecast.
I would just be happy if AddBegin would just accept any pointer.
However that seems not possible.
See example below:
// *** Begin of Demonstration ***
program Project1;
{$APPTYPE CONSOLE}
{
Tested typed pointer assignment to untyped pointer via parameter.
Version 0.01 created by Skybuck Flying.
I want to write a general function which will accept all pointer types.
All pointer types will point to the same structure heading,
I know what I am doing and I want to get rid of the external typecasts.
Unfortuantely it does not seems possible with Delphi 2007.
Possible work around could be to use untyped data... but this is a bit too
dangerous
for my taste because then there won't be any length checking anymore.
}
uses
SysUtils;
var
P1 : pointer;
P2 : pointer;
P3 : pointer;
procedure Test( var A : pointer );
begin
P2 := A;
end;
// ok better.
procedure DangerousWorkAroundNoLengthChecking( var A );
var
P : pointer absolute A; // no typecast required this way and no extra
variables.
begin
P3 := P;
end;
var
IntP : Pinteger;
MyInt : integer;
Anything : array[0..10] of byte;
begin
try
MyInt := 10;
IntP := @[EMAIL PROTECTED]
P1 := IntP; // ok
// Test( IntP ); // compile failure.
Test( Pointer(IntP) ); // nasty typecast needed
DangerousWorkAroundNoLengthChecking( IntP );
writeln( longword(P1) );
writeln( longword(P2) );
writeln( longword(P3) );
// no pointer length checking, dangerous !
DangerousWorkAroundNoLengthChecking( Anything );
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
readln;
end.
// *** End of Demonstration ***
Bye,
Skybuck.


|