PDA

View Full Version : Working with void/pointer types



Robert Kosek
31-05-2007, 07:23 PM
System: Win32 (XP SP2), AMD 64 X2
Compiler/IDE: Turbo Delphi + Hotfix Rollup
Libraries/API: N/A


Alrighty, I'll get down to it quickly. I'm trying to create a "stack" of potentially variant types to use in functions, but I also am aware that variants can take a bit of size. So, I figured that I would use a void type. Usually I can do something like:
function HandleThis(const thisvar): boolean;
begin
TObject(thisvar).Free;
result := true;
end;(And yes I am aware the function is bogus.) But, if I were to create a record of these types Delphi chokes, stating that:
[Pascal Error] Project1.dpr(8): E2029 Type expected but ';' found. There is no void type and I am reluctant to use a variant right now.

So, the question boils down to how do I treat pointers as voids, find their types, and then handle them in a function without breaking anything? System.TypeOf() returns a pointer, I assume to a string passed by the C/C++ function, but what do I do with it?

EDIT:

Trying this program gives the following error...

program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;

var a: TObject;

begin
a := TObject.Create;
if TypeOf(a) = TObject then
writeln('a is an object!');
readln;
end.

[Pascal Error] Project1.dpr(12): E2017 Pointer type required

WTH?? :? What the heck is that supposed to mean!?

Legolas
31-05-2007, 07:48 PM
program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;

var
a: TObject;

begin
a := TObject.Create;
if a.ClassType = TObject then
writeln('a is an object!');
readln;
end.

This seems ok to me :)

Edit: BTW I'm not sure if it is what you need :scratch:

Robert Kosek
31-05-2007, 07:54 PM
Okay, think of it this way. I'm trying to assemble a "stack" of void types, potentially anything, in a dynamic array that is passed to a given function. This function needs to have a way to distinguish between the types and validate the passed types to ensure it is correct. It could be a string, an integer, or even an object.

So how do I pull things apart? I know it is possible in C, I've seen them do it, but aside from variants I have no clue how. And in all honesty, I'm loathe to use variants because they're a bit memory hungry.

Setharian
31-05-2007, 08:13 PM
TypeOf is probably considered obsolete - it is not in help and it works only on old object types with a VMT in which case it returns pointer to the VMT....how about open variant arrays (array of const)? you cannot get the type of an untyped pointer argument with any function (it's just a memory address), you have to pass some additional param identifing its type....however this is what variant open arrays actually do - they pass a parameter and a type identifier....then you'll branch on the type of the argument passed - you can get the type of an element by accessing array (when the parameter is called Args) like Args[Index].VType....

Robert Kosek
31-05-2007, 08:27 PM
For one reason, variants take 16 bytes of memory! 2 bytes are a word consisting of the type, never mind that there are only 16 types in a variant, followed by 6 unused bytes, followed by the 8 bytes for the pointer or basic type. If I were to do something similar it would literally be a 3 byte variation of variants.

But then another problem arises: type conversion. Delphi only permits conversion (inline) for a type/object of to and from 1 type for each implicit and explicit typecasts. Adding more types doesn't work.

This might be a better idea in the long run, but it seems circuitous. Surely someone has had use for something similar! Delphi auto-magically controls variants and their lifetimes, but writing something similar yet compact will require a good bit of effort on my part.

And what I will be using this for will use quite a few variants in dynamic (de)allocation in function calls. To do this could slow things down a bit. Maybe it is better to pass strings explicitly and force conversion of the parameters in the various functions?

What do you guys think?