[fpc-pascal]Using UInt32 {LongWords} as Indexes for Properties.

2004-04-06 Thread 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.


___
fpc-pascal maillist  -  [EMAIL PROTECTED]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal]QWORD error ?

2004-04-06 Thread Jonas Maebe
On 5 apr 2004, at 12:12, [EMAIL PROTECTED] wrote:

  L:=-1;
  qw:=qw+(-L);{ qw=99 O.K. }
(*
  qw:=qw-L;   { run time error 201 }
  qw:=qw+L;   { run time error 201 }
*)
[snip]
Could somebody help me what's wrong ?
The problem is that the compiler has to convert both operands to the 
same type when you do an operation. When you are working with the 
largest supported unsigned type (qword currently), this means that both 
operands are first converted to qword. -1 is an invalid value for a 
qword, so you get a range check error.

That's also the reason why Turbo Pascal only supported longint, and not 
cardinal: it breaks range checking.

Jonas

___
fpc-pascal maillist  -  [EMAIL PROTECTED]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal]Using UInt32 {LongWords} as Indexes for Properties.

2004-04-06 Thread Matt Emson
> 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  -  [EMAIL PROTECTED]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal]Using UInt32 {LongWords} as Indexes for Properties.

2004-04-06 Thread Jon D. Sawyer
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.

On Tue, 2004-04-06 at 06:29, Matt Emson wrote:
> > 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  -  [EMAIL PROTECTED]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal


___
fpc-pascal maillist  -  [EMAIL PROTECTED]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal]Using UInt32 {LongWords} as Indexes for Properties.

2004-04-06 Thread 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();

PropertyHWSurface : Boolean Index SDL_HWSurface
Read GetFlag
Write   SetFlag;
PropertySRCAlpha  : Boolean Index SDL_SrcAlpha
ReadGetFlag
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.
  


___
fpc-pascal maillist  -  [EMAIL PROTECTED]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal]Using UInt32 {LongWords} as Indexes for Properties.

2004-04-06 Thread Peter Vreman
> 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();
>
> PropertyHWSurface : Boolean Index SDL_HWSurface
>   Read GetFlag
> Write   SetFlag;
> PropertySRCAlpha  : Boolean Index SDL_SrcAlpha
> ReadGetFlag
> 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



___
fpc-pascal maillist  -  [EMAIL PROTECTED]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal