Results 1 to 9 of 9

Thread: Variant parts record problem

  1. #1

    Variant parts record problem

    Using variant parts in records makes a lot of things easier. For example, I wrote a color structure that looks like this:
    [pascal] TMyColor = record
    case boolean of
    false: (color: cardinal);
    true: (rgba: packed array[1..4] of byte);
    end;[/pascal]
    It works great! Any changes made to the one interpretation get applied automatically to the other, because they're mapped to the same memory. But trying to set up a bitfield for easy bitwise operations... doesn't seem to work.
    [pascal] TBitfield16 = record
    case boolean of
    false: (num: word);
    true: (bits: packed array[1..16] of boolean);
    end;[/pascal]
    In this one, changes made to the one value don't seem to affect the other one. Anyone know why, and how I can fix it?

  2. #2
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Variant parts record problem

    A boolean takes 1 byte of memory, not 1 bit


    A Boolean variable occupies one byte of memory, a ByteBool variable also occupies one byte, a WordBool variable occupies two bytes (one word), and a LongBool variable occupies four bytes (two words).
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  3. #3

    Variant parts record problem

    Aww crap. I could have sworn that used to work in other versions...

  4. #4

    Variant parts record problem

    If you use FPC then you could do it with the bitpacked keyword:

    [pascal]
    TBitfield16 = bitpacked record
    case boolean of
    false: (num: word);
    true: (bits: packed array[1..16] of boolean);
    end;
    [/pascal]
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  5. #5

    Variant parts record problem

    Unfortunately, my code won't compile under Lazarus, so that's not an option for the moment.

    I rewrote the whole thing using operator overloads, and most of them work... except for one that doesn't. Here's what I've got so far:
    [pascal]interface
    type
    TBitset16 = record
    bits: array[1..16] of boolean;
    class operator Implicit(a: word): TBitset16; inline; //works
    class operator implicit(a: TBitset16): word; inline; //doesn't work
    end;

    implementation
    {$R-}
    class operator TBitset16.Implicit(a: word): TBitset16; //works
    var
    dummy: word;
    i: integer;
    begin
    dummy := 1;
    for I := 1 to 16 do
    begin
    result.bits[i] := (a and dummy > 0);
    dummy := dummy shl 1;
    end;
    end;

    class operator TBitset16.implicit(a: TBitset16): word; //doesn't work
    var
    dummy: word;
    i: integer;
    begin
    result := 0;
    dummy := 1;
    for I := 1 to 16 do
    begin
    if a.bits[i] then
    inc(result, dummy);
    dummy := dummy shl 1;
    end;
    end;
    {$R+}[/pascal]
    This compiles, and I can assign a word to a TBitset16 just fine, but if I try to actually assign a TBitset16 to a word, the compiler says they're incompatible. How do I fix this?

  6. #6

    Variant parts record problem

    If you are noto using FPC, then how class operators are working under delphi? What version it is?

  7. #7

    Variant parts record problem

    I'm running BDS 2006, which supports class operators for records. But for some reason it doesn't like my assignment operator. Anyone with experience in this know what I'm doing wrong?

  8. #8

    Variant parts record problem

    Are you assigning it from a variable or from a constant?

    I think most constant numeric arguments evaluate to longints
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  9. #9

    Variant parts record problem

    Assigning it *from* numbers works fine. (All integer types can be implicitly converted to each other anyway.) It's trying to assign it *to* a Word that doesn't work.

    [pascal]
    var
    bitset: TBitset16;
    number: word;
    begin
    bitset := number; //compiler likes this
    bitset := 5; //this works just fine too
    number := bitset; //compiler chokes on this one
    end;
    [/pascal]

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
  •