Results 1 to 5 of 5

Thread: Working with void/pointer types

  1. #1

    Working with void/pointer types

    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:
    Code:
    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:
    Code:
    [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...
    Code:
    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.
    Code:
    [Pascal Error] Project1.dpr(12): E2017 Pointer type required
    WTH?? :? What the heck is that supposed to mean!?

  2. #2

    Working with void/pointer types

    [pascal]
    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.[/pascal]

    This seems ok to me

    Edit: BTW I'm not sure if it is what you need :scratch:
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  3. #3

    Working with void/pointer types

    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.

  4. #4

    Working with void/pointer types

    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....

  5. #5

    Working with void/pointer types

    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?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •