Attacking a UDT

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

Re: Attacking a UDT

Post by fxm »

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: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Attacking a UDT

Post by grindstone »

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

Re: Attacking a UDT

Post by dodicat »

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.
Post Reply