Attacking a UDT

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
fxm
Posts: 9720
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Attacking a UDT

Postby fxm » Apr 22, 2020 11:47

In FreeBASIC, pass an argument by reference or by pointer value is equivalent:

Code: Select all

type list
    as long s,f
end type

Type st
    private:
    Declare Property middle( As List) As String
    Declare Property middle( As List ,As String)
    Declare Operator Cast() As String
    Declare Constructor(As String="")
    Declare Destructor()
    #define md(s,c,d) Mid(s,c+1,d-c+1)
    as string s
End Type

'private OOP to public Procedural
'********** passing instance by address **********
declare function GetTheString alias "_ZN2ST13MIDDLE__get__ER4LIST"(as st ptr,as list) as string 'property middle as string
declare function show alias "_ZN2STcv8FBSTRINGEv"(as st ptr) as string  'cast
declare sub SetTheString alias "_ZN2ST13MIDDLE__set__ER4LISTR8FBSTRING"(as st ptr,as list, as string)'property middle
declare sub construct cdecl alias "_ZN2STC1ER8FBSTRING"(as st ptr,as string) 'constructor
declare sub destruct cdecl alias "_ZN2STD1Ev"(as st ptr) 'destructor

'==========
Constructor st(g As String)
color 5
print __function__
color 15
s=g
End Constructor

Property st.middle(n As list) As String
color 5
print __function__
color 15
Property= md(s,n.s,n.f)
End Property

Property st.middle(n As list,g As String)
color 5
print __function__
color 15
md(s,n.s,n.f)=g
End Property

Operator st.cast() As String
color 5
print __function__
color 15
Return s
End Operator

Destructor st()
End Destructor

'=========================

print "actual task:"
dim as string g="abcdefghijklmnopqrstuvwxyz"
print g
print md(g,2,7)
md(g,10,19)="STATEMENT"
print g
print
print


'use only method pointers
var GetTheStringMethod=@GetTheString
var SetTheStringMethod=@SetTheString
var ConstructorMethod=@construct
var CastMethod=@show
var DestructorMethod=@destruct

print "Task via method pointers:"
dim as st ptr y=allocate(sizeof(st))
'********** passing instance by address **********
ConstructorMethod(y,"abcdefghijklmnopqrstuvwxyz")
print CastMethod(y)
print GetTheStringMethod(y,type(2,7))
SetTheStringMethod(y,type(10,19),"STATEMENT")
print CastMethod(y)


Print
sleep

'********** passing instance by address **********
DestructorMethod(y)
deallocate y
grindstone
Posts: 737
Joined: May 05, 2015 5:35
Location: Germany

Re: Attacking a UDT

Postby grindstone » Apr 22, 2020 12:22

@UEZ: Thank you for your "silly" question (and to dodicat and fxm for their answers).
dodicat
Posts: 6557
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Attacking a UDT

Postby dodicat » Apr 22, 2020 13:56

In the .bas file using the C++ dll, I can substitute any ptr for byval as Vector parameters, except for the constructors and the ip point in segdist which must be passed byref.
So
pass an argument by value or by any pointer is equivalent: -- getting info from a dll.
Tested fb dll's also, the same rules apply.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 2 guests