Pointers to procedures as structure fields?

Just starting out? Need help? Post your questions and find answers here.
Quin
Enthusiast
Enthusiast
Posts: 279
Joined: Thu Mar 31, 2022 7:03 pm
Location: United States
Contact:

Pointers to procedures as structure fields?

Post by Quin »

Hi,
In C, you can do the following:

Code: Select all

typedef struct string {
char *chars;
int (*length)(string *self);
} string;

int length(string *self) {
return strlen(self->chars);
}
Then it can be called like the following:

Code: Select all

string *str = malloc(sizeof(string))
str->chars = malloc(sizeof(char) * 30); // 30 bytes, just for example.
str->length = length;

printf("%d", str->length());
Is it possible to do this kind of thing in PB? For example, to be able to call a procedure like: struct\proc(...)
PB v5.40/6.10, Windows 10 64-bit.
16-core AMD Ryzen 9 5950X, 128 GB DDR5.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Pointers to procedures as structure fields?

Post by mk-soft »

Without Interface ...

Code: Select all

;-TOP

Prototype Invoke()
Prototype Invoke1(*Self)

Structure udtMyData
  chars.s
  Length.Invoke1
EndStructure

Procedure _Lenght(*Self.udtMyData)
  ProcedureReturn Len(*Self\chars)
EndProcedure

; Init

Global data1.udtMyData
data1\chars = "Hello World"
data1\Length = @_Lenght() ; Set Procedure Address


len = data1\Length(data1) ; <- need parameter to self
Debug Len
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Pointers to procedures as structure fields?

Post by NicTheQuick »

With Interface it is way more complicated.

Code: Select all

Interface my_string
	length.i()
EndInterface
Structure my_string_data
	*vTable
	chars.s
EndStructure

Procedure.i length(*this.my_string_data)
	ProcedureReturn Len(*this\chars)
EndProcedure

DataSection
	vTable_my_string:
		Data.i @length()
EndDataSection

Define data1.my_string_data
Define data1i.my_string = data1
PokeI(data1, ?vTable_my_string)

data1\chars = "Hello"
Debug data1i\length()
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Quin
Enthusiast
Enthusiast
Posts: 279
Joined: Thu Mar 31, 2022 7:03 pm
Location: United States
Contact:

Re: Pointers to procedures as structure fields?

Post by Quin »

Thanks both of you! MKSoft's way was exactly what I'm looking for! :)
PB v5.40/6.10, Windows 10 64-bit.
16-core AMD Ryzen 9 5950X, 128 GB DDR5.
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Pointers to procedures as structure fields?

Post by NicTheQuick »

The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Pointers to procedures as structure fields?

Post by NicTheQuick »

Converted from your C example it would look like this:

Code: Select all

Prototype.i length_proc()

Structure my_string
	chars.s
	length.length_proc
EndStructure

Procedure.i length_()
	Protected *this.my_string
	CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
		!MOV [p.p_this],Rbp
	CompilerElse
		!MOV [p.p_this],Ebp
	CompilerEndIf
	ProcedureReturn Len(*this\chars)
EndProcedure

Define data1.my_string
data1\chars = Space(30)
data1\length = @length_()

Debug data1\length()
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Pointers to procedures as structure fields?

Post by StarBootics »

NicTheQuick wrote: Thu May 19, 2022 4:48 pm Converted from your C example it would look like this:

Code: Select all

Prototype.i length_proc()

Structure my_string
	chars.s
	length.length_proc
EndStructure

Procedure.i length_()
	Protected *this.my_string
	CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
		!MOV [p.p_this],Rbp
	CompilerElse
		!MOV [p.p_this],Ebp
	CompilerEndIf
	ProcedureReturn Len(*this\chars)
EndProcedure

Define data1.my_string
data1\chars = Space(30)
data1\length = @length_()

Debug data1\length()
You forget to mention that your code is not compatible with the C backend. :wink:

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Pointers to procedures as structure fields?

Post by NicTheQuick »

Oh, didn't think of that. Haven't updated Purebasic since 6.00 beta 1 on Linux :oops:
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Pointers to procedures as structure fields?

Post by skywalk »

Yes, I backed out these ASM moves to enable the C backend+optimizer.
For the few oop codes I have, I just pass *self into the method.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply