Sign in Join

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

Mailing lists / fpc-pascal / 2010-08

Something like g_object_get() in Pascal?

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Matthias Klumpp

Hello!
I have a record containing a set of variables. This record should now be accessed from an application developed in C (through a shared library). Because Pascal records are incompatible with C structs, I could write a get_value() function for each value in the record, e.g. if I have:

TTestRec = record
 val1: String;
 val2: Integer;
end;
I would have to write functions like:
function testrec_get_val1(rec: TTestRec): String;
function testrec_get_val2(rec: TTestRec): Integer;

Now the record consists of a lot more than just two properties. The GObject type system knows a much better way for accessing properties: (refer to
http://library.gnome.org/devel/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-get )
I just write the property name as string and add a pointer to the result type:

int i;
g_object_get(testrec, "val2", &i, NULL);

Is there a possibility to get the same function in Pascal to? That I just define one get_value() function which receives the property name as string and outputs the value of it?
Thanks & kind regards
Matthias

Bernd Kreuss
to Matthias Klumpp
Matthias Klumpp wrote
Because Pascal records are incompatible with C structs

how are they incompatible? It should be possible to define the same record as a struct in C and for every struct in C it should be possible to define the same as a record in Pascal. Then you could work with pointers to these structs (or records).

The difficulty in your example might come from the fact that it involves the usage of strings and pascal will handle strings a bit differently than this is usually done in C.

Matthias Klumpp
to Bernd Kreuss
Bernd Kreuss wrote 8 lines
On 01.08.2010 14:45, Matthias Klumpp wrote: > Because Pascal records are incompatible with C structs how are they incompatible? It should be possible to define the same record as a struct in C and for every struct in C it should be possible to define the same as a record in Pascal. Then you could work with pointers to these structs (or records).

It does not work here... If I access a PChar in C, I just get an empty string or a wrong pointer.

The difficulty in your example might come from the fact that it involves the usage of strings and pascal will handle strings a bit differently than this is usually done in C.

I use PChars instead of strings, so this can't cause the problems. Anyway, if you say it works I'll do some tests with it to get it working. Thanks!

Matthias

Matthias Klumpp
to Matthias Klumpp
Matthias Klumpp wrote 15 lines
On Sun, 01 Aug 2010 19:27:20 +0200, Bernd Kreuss wrote: > On 01.08.2010 14:45, Matthias Klumpp wrote: > >> Because Pascal records are incompatible with C structs > > how are they incompatible? It should be possible to define the same > record as a struct in C and for every struct in C it should be possible > to define the same as a record in Pascal. Then you could work with > pointers to these structs (or records). It does not work here... If I access a PChar in C, I just get an empty string or a wrong pointer. > The difficulty in your example might come from the fact that it

involves

> the usage of strings and pascal will handle strings a bit differently > than this is usually done in C. I use PChars instead of strings, so this can't cause the problems. Anyway, if you say it works I'll do some tests with it to get it

working.

Thanks!

Aah, I finally found the problem... It was a small bug in my C-code generator.
Now everything is fine.

Matthias
Felipe Monteiro de Carvalho
to Matthias Klumpp: Hello! I have a record containing a set of variables. This record should now be accessed…
Matthias Klumpp wrote
Is there a possibility to get the same function in Pascal to? That I just define one get_value() function which receives the property name as string and outputs the value of it?

I don't know about getting the value of a property, but you can get the type of a property using RTTI:

Read more here:
http://delphi.about.com/gi/o.htm?zi=1/XJ&zTi=1&sdn=delphi&cdn=compute&tm=47&f=11&su=p284.9.336.ip_p504.1.336.ip_&tt=2&bt=1&bts=1&zu=http%3A//www.blong.com/Conferences/BorConUK98/DelphiRTTI/CB140.htm

Specifically:

function GetPropInfo(TypeInfo: PTypeInfo;
  const PropName: string): PPropInfo;

You would need to change your record into a class:

TTestClass = class
private
 Fval1: String;
 Fval2: Integer;
published
 property val1: String read Fval1 write Fval1;
 property val2: Integer read Fval2 write Fval2;
end;

Probably this mechanism also allows to get the value of the property, although I am not sure ...

Felipe Monteiro de Carvalho