Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2004-04

Using UInt32 {LongWords} as Indexes for Properties.

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jon D. Sawyer

Ran into a problem of where I'm not sure how the code will work once I'm finished and I'd rather not write all this if its not going to work:

I want to provide access to Bit-Flags that control a class of mine:

ig:

Property SWSurface : Boolean Index SDL_SWSurface
Read GetFlags Write SetFlags;
If I declare Get/Set Flags as the following:
Function GetFlags(aBit: UInt32): Boolean;
Procedure SetFlags(aBit: UInt32; aBool: Boolean);
I of course get a bunch of:
cqsdl_surface.pp(17,6) Error: Illegal symbol for property access
So I set it to read:
Function GetFlags(aBit: Integer): Boolean;
Procedure SetFlags(aBit: Integer; aBool: Boolean);

And it compiles fine. What I'm worried about is that if aBit is a Signed Integer and the constant SDL_SWSurface was meant to be treated as an Unsigned Integer can this code work the same?

This works:

Property Flags[aBit: UInt32]: Boolean Read GetFlag Write SetFlag;
Function GetFlags(aBit: UInt32): Boolean;
Procedure SetFlags(aBit: UInt32; aBool: Boolean);

BUT it means that the programmer has to supply the Bit mask and THAT means yet-another-uses statement just for a few damned constants. The entire point of me providing these specifically named properties was to stop that.

Any Suggestions? Feel free to smack me around for being an idiot.

Reply by email

Matt Emson
to Jon D. Sawyer
Show quoted text 6 lines
Property SWSurface : Boolean Index SDL_SWSurface Read GetFlags Write SetFlags; If I declare Get/Set Flags as the following: Function GetFlags(aBit: UInt32): Boolean; Procedure SetFlags(aBit: UInt32; aBool: Boolean);

Looks like you're tying to use an indexed property. Dunno what mode you're using, but in Delphi mode:

unit Example;
interface
type
 t = class
 private
    FSurface: SDL_SWSurface;
    function GetSWSurfaceFlags(index: integer): Boolean;
    procedure SetSWSurfaceFlags(index: integer; const Value: Boolean);
 public
   property SWSurfaceFlags[index: integer] : Boolean read GetSWSurfaceFlags
write SetSWSurfaceFlags;
 end;
implementation

{ t }

function t.GetSWSurfaceFlags(index: integer): Boolean;
begin
  /// do stuff with FSurface
end;
procedure t.SetSWSurfaceFlags(index: integer; const Value: Boolean);
begin
  /// do stuff with FSurface
end;
end.

You would then implement the get/set by implementing read/writes to SDL_SWSurface...

I would have thought that this is quite limiting though. I would also have a:

property SWSurfaceColor[index: uint32] : TColor
  read GetSWSurfaceColor write SetSWSurfaceColor;
function t.GetSWSurfaceColor(index: uint32): TColor;
procedure t.SetSWSurfaceColor(index: uint32; const value: TColor);

The params are not me being pedantic, by the was, but the way in which Delphi implements properties. Other FPC modes may be a little more forgiving.

Hope that helps,

Matt

Reply by email

Jon D. Sawyer
to Matt Emson

I appreciate the help but I'm not sure if you understand what it is I'm trying to do.

SetFlag() and GetFlag() do binary operations on the flags controling an SDL Surface.

Passing the BitMask to the GetFlag() will return weither or not the bit is on in the flags variable.

Passing the BitMask and a boolean will turn on / off the bit in the flags variable.

I wanted to provide shortcuts to GetFlag(SDL_SWSurface) and
SetFlag(SDL_Surface, TRUE) so you could say: SWSurface := TRUE;

If I understand correctly Indexed properties have to be signed interger type... which would ruin the bitwise comparison because the Bit Mask is a Unsigned integer type.

Matt Emson wrote 72 lines
> Property SWSurface : Boolean Index SDL_SWSurface > Read GetFlags Write SetFlags; > > If I declare Get/Set Flags as the following: > Function GetFlags(aBit: UInt32): Boolean; > Procedure SetFlags(aBit: UInt32; aBool: Boolean); Looks like you're tying to use an indexed property. Dunno what mode you're using, but in Delphi mode: unit Example; interface type t = class private FSurface: SDL_SWSurface; function GetSWSurfaceFlags(index: integer): Boolean; procedure SetSWSurfaceFlags(index: integer; const Value: Boolean); public property SWSurfaceFlags[index: integer] : Boolean read GetSWSurfaceFlags write SetSWSurfaceFlags; end; implementation { t } function t.GetSWSurfaceFlags(index: integer): Boolean; begin /// do stuff with FSurface end; procedure t.SetSWSurfaceFlags(index: integer; const Value: Boolean); begin /// do stuff with FSurface end; end. You would then implement the get/set by implementing read/writes to SDL_SWSurface... I would have thought that this is quite limiting though. I would also have a: property SWSurfaceColor[index: uint32] : TColor read GetSWSurfaceColor write SetSWSurfaceColor; function t.GetSWSurfaceColor(index: uint32): TColor; procedure t.SetSWSurfaceColor(index: uint32; const value: TColor); The params are not me being pedantic, by the was, but the way in which Delphi implements properties. Other FPC modes may be a little more forgiving. Hope that helps, Matt _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Jon D. Sawyer
to Jon D. Sawyer

To give a better picture of my problem:

Type
tQSDL_Surface
= Class (SomeClass)

      Protected
        fFlags : UInt32; {LongWord}
        Function GetFlag(aBit: UInt32): Boolean;
        Function SetFlag(aBig: UInt32; aBool: Boolean);
      Public
        Constructor Create();
        Destructor  Destroy();
        Property    HWSurface : Boolean Index SDL_HWSurface
	    Read     GetFlag
            Write   SetFlag;
        Property    SRCAlpha  : Boolean Index SDL_SrcAlpha
            Read    GetFlag
            Write   SetFlag;
      end;

SDL_HWSurface and SDL_SRCAlpha are constants defined in the SDL_Video unit. They are meant for Boolean operations. More specificly they are meant to be ORed togeather to create a flag list and ANDed with fFlags to check for there state.

I'm trying to do two things with these properties: 1# Not force a programmer to include the SDL Unit to get these constants.
2# Improve the readability of my code by using these properties to control flags.

For instance a child object, SDL_VideoSurface, would have extra flags such as FullScreen. Calling the following:

MyVideoSurface.FullScreen := TRUE;

would cause the fullscreen bit to be set to true and when the screen is allocated it will fill the entire screen. The Problem is that if I define a constant index {$Mode ObjFPC} requires that index to be an Signed Integer value which it cannot be because that would change its binary value and the bitwise operations would be based on the wrong values.

My solution is to simply create methods for each flag individualy that calls GetFlag/SetFlag with the correct paramaters. I was just wondering if there was a way to get around this.

If not, thanks anyways. I just learned something new about indexes and and making sure I understand there limitation correctly.

Reply by email

Peter Vreman
to Jon D. Sawyer
Show quoted text 52 lines
To give a better picture of my problem: Type tQSDL_Surface = Class (SomeClass) Protected fFlags : UInt32; {LongWord} Function GetFlag(aBit: UInt32): Boolean; Function SetFlag(aBig: UInt32; aBool: Boolean); Public Constructor Create(); Destructor Destroy(); Property HWSurface : Boolean Index SDL_HWSurface Read GetFlag Write SetFlag; Property SRCAlpha : Boolean Index SDL_SrcAlpha Read GetFlag Write SetFlag; end; SDL_HWSurface and SDL_SRCAlpha are constants defined in the SDL_Video unit. They are meant for Boolean operations. More specificly they are meant to be ORed togeather to create a flag list and ANDed with fFlags to check for there state. I'm trying to do two things with these properties: 1# Not force a programmer to include the SDL Unit to get these constants. 2# Improve the readability of my code by using these properties to control flags. For instance a child object, SDL_VideoSurface, would have extra flags such as FullScreen. Calling the following: MyVideoSurface.FullScreen := TRUE; would cause the fullscreen bit to be set to true and when the screen is allocated it will fill the entire screen. The Problem is that if I define a constant index {$Mode ObjFPC} requires that index to be an Signed Integer value which it cannot be because that would change its binary value and the bitwise operations would be based on the wrong values. My solution is to simply create methods for each flag individualy that calls GetFlag/SetFlag with the correct paramaters. I was just wondering if there was a way to get around this. If not, thanks anyways. I just learned something new about indexes and and making sure I understand there limitation correctly.

Indexes are forced to 32bit signed, because the index type is not know yet when the index keyword is parsed.

But i don't see any problems with 32bit signed/unsigned wrt bitwise operations. The only problem can be range checking.

Which compiler version do you use? If it doesn't work with 1.9.x then
submit a bug with a complete example.

Peter

Reply by email