Sign in Join

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

Mailing lists / lazarus / 2013-12

Pointer type handling

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Leonardo M. Ramé

Hi, I'm passing a pointer buffer as void * to a C library that returns Uint8 or Uint16 (and signed types).

If I explicitly declare the buffer as PByte, PWord, etc. I get exactly what I need, but, I would like to work with an abstract type (for example Pointer) and cast it depending on a flag returned by the library.

If I pass a Pointer (instead of a PWord or PByte) and cast it to PWord, for example. I get the same result if I have passed a PByte.

How can I handle a situation like this without explicitly define each type?.

Regards,

Reply by email

Hans-Peter Diettrich
to Leonardo M. Ramé
Show quoted text 13 lines
Hi, I'm passing a pointer buffer as void * to a C library that returns Uint8 or Uint16 (and signed types). If I explicitly declare the buffer as PByte, PWord, etc. I get exactly what I need, but, I would like to work with an abstract type (for example Pointer) and cast it depending on a flag returned by the library. If I pass a Pointer (instead of a PWord or PByte) and cast it to PWord, for example. I get the same result if I have passed a PByte. How can I handle a situation like this without explicitly define each type?.

I'm not sure what you mean. How does your code look like now, and how do you want it to look?

You can modify the function declarations in your units, to accept the properly typed pointer or an Var parameter.

DoDi

Reply by email

Leonardo M. Ramé
to Hans-Peter Diettrich
Hans-Peter Diettrich wrote 23 lines
Leonardo M. Ram? schrieb: >Hi, I'm passing a pointer buffer as void * to a C library that returns >Uint8 or Uint16 (and signed types). > >If I explicitly declare the buffer as PByte, PWord, etc. I get exactly >what I need, but, I would like to work with an abstract type (for >example Pointer) and cast it depending on a flag returned by the >library. > >If I pass a Pointer (instead of a PWord or PByte) and cast it to PWord, >for example. I get the same result if I have passed a PByte. > >How can I handle a situation like this without explicitly define each >type?. I'm not sure what you mean. How does your code look like now, and how do you want it to look? You can modify the function declarations in your units, to accept the properly typed pointer or an Var parameter. DoDi

If you mean overloading functions, that is exactly what I don't want to do.

My methods are similar to this:

procedure TImageDrawer.Draw(ABuffer: PWord);
begin
  ...
end;

As you can see, I'm passing a PWord, this works as expected, the same if I modify the funcion passing a PByte. But, as I cannot create a new function for each pointer type, I would like to pass a generic pointer, and typecast it inside the function, for example:

procedure TImageDrawer.Draw(ABuffer: Pointer);
begin
  ...
  if MyImage.DataType = "Uint16" then
    data = PWord(ABuffer)^
  else
  if MyImage.DataType = "Uint8" then
    data = PByte(ABuffer)^;
end;

If I do this, the result of PWord(ABuffer) is the same as PByte(ABuffer). I'm starting to think PByte is equal to Pointer type.

Reply by email

Hans-Peter Diettrich
to Leonardo M. Ramé
Show quoted text 12 lines
procedure TImageDrawer.Draw(ABuffer: Pointer); begin ... if MyImage.DataType = "Uint16" then data = PWord(ABuffer)^ else if MyImage.DataType = "Uint8" then data = PByte(ABuffer)^; end; If I do this, the result of PWord(ABuffer) is the same as PByte(ABuffer). I'm starting to think PByte is equal to Pointer type.

The address of ABuffer stays the same, the typecasts only affect the element type (pointed to).

I cannot provide a simpler solution than you found already. You have to find an type for "data" that allows for both signed and unsigned values in the range of the values, too, e.g. Int64. When ABuffer contains multiple values, typecasts also are required when incrementing the pointer.

DoDi

Reply by email

Anton Kavalenka
to Leonardo M. Ramé: Hi, I'm passing a pointer buffer as void * to a C library that returns Uint8 or Uint16…
Leonardo M. Ram? wrote 15 lines
Hi, I'm passing a pointer buffer as void * to a C library that returns Uint8 or Uint16 (and signed types). If I explicitly declare the buffer as PByte, PWord, etc. I get exactly what I need, but, I would like to work with an abstract type (for example Pointer) and cast it depending on a flag returned by the library. If I pass a Pointer (instead of a PWord or PByte) and cast it to PWord, for example. I get the same result if I have passed a PByte. How can I handle a situation like this without explicitly define each type?. Regards,
PPointerCast=^TPointerCast
TpointerCast=record
case integer of
0:(pb:PByte);
1:(pw:PWord);
2:(pd:PDword);
3:(pu:PtrUint);
4:(pi:PtrInt);
end;
function some_function(ptr:TPointerCast):boolean;

regards,
Anton

Reply by email