Discussion:
[fpc-pascal] Type helper for JNI pointers
Benito van der Zander
2018-08-12 11:42:10 UTC
Permalink
Hi,

when you use JNI functions you always need to repeat "env" a lot:

var
  javaClass: jclass;
  javaObj: jobject;
begin
  //javaClass := ...
  javaObj := env^^.NewObject(env, javaClass)
  env^^.DeleteLocalRef(env, javaObj);
end;

So I thought you could declare type helpers for jobject and jclass,

  TjobjectHelper = type helper for jobject
     procedure DeleteLocalRef;
  end;
  TjclassHelper = type helper for jclass
     procedure NewObject;
  end;

And then just write

var
  javaClass: jclass;
  javaObj: jobject;
begin
  //javaClass := ...
  javaObj := javaClass.NewObject()
  javaObj.DeleteLocalRef;
end;


But this does not work, because fpc thinks jclass and jobject are the
same type, so there is only one type helper for both of the types allowed.

Because it is declared as

type
     jobject=pointer;
     jclass=jobject;


What can we do about this?

Bye,

Benito
Andrew Haines via fpc-pascal
2018-08-23 02:23:13 UTC
Permalink
Post by Benito van der Zander
But this does not work, because fpc thinks jclass and jobject are the
same type, so there is only one type helper for both of the types allowed.
Because it is declared as
type
     jobject=pointer;
     jclass=jobject;
What can we do about this?
I haven't used type helpers but why not change to defines like

type
  jObjectRec = record end;
  jObject= ^jObjectRec;
  jClassRec = record end;
  jClass = ^jClassRec;

or possibly simpler you could try
  jclass = type(jobject); // I believe this forces a new type and is
not just an alias

Regards,

Andrew
Benito van der Zander
2018-08-26 23:50:03 UTC
Permalink
Hi,
Post by Andrew Haines via fpc-pascal
I haven't used type helpers but why not change to defines like
type
  jObjectRec = record end;
  jObject= ^jObjectRec;
  jClassRec = record end;
  jClass = ^jClassRec;
or possibly simpler you could try
  jclass = type(jobject); // I believe this forces a new type and is
not just an alias
that looks better.

Who can  change them? These jobject/jclass type definitions are part of
fpc...





Although type is weird
Post by Andrew Haines via fpc-pascal
type a = type (pointer);
type b = type (a);
does not compile, duplicate identifier a
Post by Andrew Haines via fpc-pascal
type a = type (pointer);
type b = type a;
compiles, but a type helper for cannot use self as pointer. self <> nil
=> operator is not overloaded. But you can do writeln(self);
Post by Andrew Haines via fpc-pascal
type a = type pointer;
type b = type (a);
does not compile, duplicate identifier a
Post by Andrew Haines via fpc-pascal
type a = type pointer;
type b = type a;
compiles, and is like a pointer, but unlike case 2, you cannot do
writeln(self) in the type helper
Post by Andrew Haines via fpc-pascal
type a = type (pointer);
type b = type (pointer);
does not compile, duplicate identifier
Post by Andrew Haines via fpc-pascal
type a = type (pointer);
type b = type pointer;
does not compile, error in type definition
Post by Andrew Haines via fpc-pascal
type a = type pointer;
type b = type (pointer);
compiles
Post by Andrew Haines via fpc-pascal
type a = type pointer;
type b = type pointer;
compiles




Cheers,
Benito
Post by Andrew Haines via fpc-pascal
Post by Benito van der Zander
But this does not work, because fpc thinks jclass and jobject are the
same type, so there is only one type helper for both of the types allowed.
Because it is declared as
type
     jobject=pointer;
     jclass=jobject;
What can we do about this?
I haven't used type helpers but why not change to defines like
type
  jObjectRec = record end;
  jObject= ^jObjectRec;
  jClassRec = record end;
  jClass = ^jClassRec;
or possibly simpler you could try
  jclass = type(jobject); // I believe this forces a new type and is
not just an alias
Regards,
Andrew
_______________________________________________
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Loading...