type and static type

For other topics related to the FreeBASIC project or its community.
nastasa eodor
Posts: 131
Joined: Dec 18, 2018 16:37
Location: Germany, Hessdorf
Contact:

type and static type

Postby nastasa eodor » Feb 16, 2021 21:38

look closer to this code...

Code: Select all

type QObject extends Object
    as string ClassName
    as integer ID
end type

type QStaticObject extends QObject
    static as QObject ptr AnObject
   
    declare static function ID_get as integer
    declare static sub ID_set(as integer)
    declare static function ClassName_get as string
    declare static sub ClassName_set(as string)
   
    declare operator let(as string)
    declare operator let(as integer)
    declare operator cast as string
    declare operator cast as integer
   
    declare operator cast as any ptr
    declare operator cast as QObject
    declare constructor
end type

dim QStaticObject.AnObject as QObject ptr

constructor QStaticObject
    QStaticObject.AnObject=@this
end constructor

function QStaticObject.ID_get as integer
    return AnObject->id
end function

function QStaticObject.ClassName_get as string
    return AnObject->ClassName
end function

sub QStaticObject.ClassName_set(v as string)
    AnObject->ClassName=v
end sub

sub QStaticObject.ID_set(v as integer)
    AnObject->id=v
end sub

operator QStaticObject.let(v as string)
    classname=v
end operator

operator QStaticObject.let(v as integer)
    id=v
end operator

operator QStaticObject.cast as string
    return classname
end operator

operator QStaticObject.cast as integer
    return id
end operator

operator QStaticObject.cast as any ptr
    return @this
end operator

operator QStaticObject.cast as QObject
    return this
end operator

dim shared as QStaticObject so

? @QStaticObject.id_get,@QStaticObject.id_set
? QStaticObject.AnObject

so.classname="dummy"
so.id=1

? so.classname
? so.id

so.id_set(2)
? so.id

so.classname="dummy"
so.id=1

so.classname_set("dummy_1")
? so.classname

QStaticObject.classname_set("dummy_static")
? so.classname,QStaticObject.classname_get

dim shared as sub(as string) str_setter
str_setter=@QStaticObject.classname_set

str_setter("i've done men...")
? so.classname,QStaticObject.classname_get

so="Test_static_type"
? so

so=10
? so.id, qstaticobject.id_get, qstaticobject

sleep
dodicat
Posts: 6883
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: type and static type

Postby dodicat » Feb 17, 2021 19:30

Could use byref functions.
I have changed men to folks, there could be some ladies around.

Code: Select all

type QObject extends Object
    as string ClassName
    as integer ID
end type

type QStaticObject extends QObject
    static as QObject ptr AnObject
   
    'declare static function ID_get as integer
    'declare static sub ID_set(as integer)
   
    declare static function ID_Get_set() byref as integer
    declare static function classname_Get_set() byref as string
   
    'declare static function ClassName_get as string
    'declare static sub ClassName_set(as string)
   
    declare operator let(as string)
    declare operator let(as integer)
    declare operator cast as string
    declare operator cast as integer
   
    declare operator cast as any ptr
    declare operator cast as QObject
    declare constructor
end type

dim QStaticObject.AnObject as QObject ptr

constructor QStaticObject
    QStaticObject.AnObject=@this
end constructor

function QStaticObject.classname_get_set() byref as string
      return AnObject->ClassName
end function

function QStaticObject.ID_get_set() byref as integer
      return AnObject->id
end function
     
'function QStaticObject.ID_get as integer
    'return AnObject->id
'end function

'function QStaticObject.ClassName_get as string
    'return AnObject->ClassName
'end function

'sub QStaticObject.ClassName_set(v as string)
    'AnObject->ClassName=v
'end sub

'sub QStaticObject.ID_set(v as integer)
    'AnObject->id=v
'end sub

operator QStaticObject.let(v as string)
    classname=v
end operator

operator QStaticObject.let(v as integer)
    id=v
end operator

operator QStaticObject.cast as string
    return classname
end operator

operator QStaticObject.cast as integer
    return id
end operator

operator QStaticObject.cast as any ptr
    return @this
end operator

operator QStaticObject.cast as QObject
    return this
end operator

dim shared as QStaticObject so

? @QStaticObject.id_get_set,@QStaticObject.id_get_set
? QStaticObject.AnObject

so.classname="dummy"
so.id=1

? so.classname
? so.id

so.id_get_set()=2
? so.id

so.classname="dummy"
so.id=1

so.classname_get_set()="dummy_1"
? so.classname

QStaticObject.classname_get_set()="dummy_static"
? so.classname,QStaticObject.classname_get_set

dim shared as function() byref as string str_setter
 str_setter=@QStaticObject.classname_get_set

str_setter()="i've done folks..."
? so.classname,QStaticObject.classname_get_set

so="Test_static_type"
? so

so=10
? so.id, qstaticobject.id_get_set, qstaticobject

sleep

You can get pointers to non static methods, but it is a bit messy.
Last edited by dodicat on Feb 17, 2021 20:03, edited 1 time in total.
fxm
Posts: 10258
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: type and static type

Postby fxm » Feb 17, 2021 19:52

@nastasa eodor

Could you explain what is the purpose of this code and what are you looking for?
nastasa eodor
Posts: 131
Joined: Dec 18, 2018 16:37
Location: Germany, Hessdorf
Contact:

Re: type and static type

Postby nastasa eodor » Feb 18, 2021 8:17

fxm wrote:@nastasa eodor

Could you explain what is the purpose of this code and what are you looking for?

yes, it is a work around for an future ide which use an property inspector, could set/get properties for an pointer type anonymus, lot of code will be saved in that case...
nastasa eodor
Posts: 131
Joined: Dec 18, 2018 16:37
Location: Germany, Hessdorf
Contact:

Re: type and static type

Postby nastasa eodor » Feb 18, 2021 8:21

dodicat wrote:Could use byref functions.
I have changed men to folks, there could be some ladies around.

Code: Select all

type QObject extends Object
    as string ClassName
    as integer ID
end type

type QStaticObject extends QObject
    static as QObject ptr AnObject
   
    'declare static function ID_get as integer
    'declare static sub ID_set(as integer)
   
    declare static function ID_Get_set() byref as integer
    declare static function classname_Get_set() byref as string
   
    'declare static function ClassName_get as string
    'declare static sub ClassName_set(as string)
   
    declare operator let(as string)
    declare operator let(as integer)
    declare operator cast as string
    declare operator cast as integer
   
    declare operator cast as any ptr
    declare operator cast as QObject
    declare constructor
end type

dim QStaticObject.AnObject as QObject ptr

constructor QStaticObject
    QStaticObject.AnObject=@this
end constructor

function QStaticObject.classname_get_set() byref as string
      return AnObject->ClassName
end function

function QStaticObject.ID_get_set() byref as integer
      return AnObject->id
end function
     
'function QStaticObject.ID_get as integer
    'return AnObject->id
'end function

'function QStaticObject.ClassName_get as string
    'return AnObject->ClassName
'end function

'sub QStaticObject.ClassName_set(v as string)
    'AnObject->ClassName=v
'end sub

'sub QStaticObject.ID_set(v as integer)
    'AnObject->id=v
'end sub

operator QStaticObject.let(v as string)
    classname=v
end operator

operator QStaticObject.let(v as integer)
    id=v
end operator

operator QStaticObject.cast as string
    return classname
end operator

operator QStaticObject.cast as integer
    return id
end operator

operator QStaticObject.cast as any ptr
    return @this
end operator

operator QStaticObject.cast as QObject
    return this
end operator

dim shared as QStaticObject so

? @QStaticObject.id_get_set,@QStaticObject.id_get_set
? QStaticObject.AnObject

so.classname="dummy"
so.id=1

? so.classname
? so.id

so.id_get_set()=2
? so.id

so.classname="dummy"
so.id=1

so.classname_get_set()="dummy_1"
? so.classname

QStaticObject.classname_get_set()="dummy_static"
? so.classname,QStaticObject.classname_get_set

dim shared as function() byref as string str_setter
 str_setter=@QStaticObject.classname_get_set

str_setter()="i've done folks..."
? so.classname,QStaticObject.classname_get_set

so="Test_static_type"
? so

so=10
? so.id, qstaticobject.id_get_set, qstaticobject

sleep

You can get pointers to non static methods, but it is a bit messy.

okay...no problem, yes i know it, but is a lot of work and "easy" way is to use EXPORT directive or export a address table..
see it on http://rqwork.de/forum/Upload/showthread.php?tid=224
fxm
Posts: 10258
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: type and static type

Postby fxm » Feb 18, 2021 9:07

This kind of 'QStaticObject' Type cannot work with multiple instances simultaneously.
dodicat
Posts: 6883
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: type and static type

Postby dodicat » Feb 18, 2021 10:38

Byref functions cannot wholly substitute properties, so I wouldn't rely on them in a big project.
Static methods are very restrictive, they are a pseudo step up from straight procedures IMO.
To get pointers to non static member procedures is very messy, you have to use the mangled names from the assembler output and add an extra parameter for an instance of the udt.
example

Code: Select all


type udt
    as double y
    declare function plus(as double)as double
    declare function minus(as double)as double
end type

function udt.plus(x as double)as double
    print __function__
    return y+x
end function

function udt.minus(x as double)as double
     print __function__
    return y-x
end function

declare function plus alias "_ZN3UDT4PLUSEd"(u as udt,d as double) as double
declare function minus alias "_ZN3UDT5MINUSEd"(u as udt,d as double) as double

dim as udt a=(5)

dim as function(as udt, as double) as double f(1 to 2)={@plus,@minus}

print f(1)(a,3.6):print f(2)(a,3.6)
sleep

 
fxm
Posts: 10258
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: type and static type

Postby fxm » Feb 18, 2021 12:45

A little simpler.

By using virtual procedures, it is possible to access their static pointers in the vtable (virtual procedure static pointers ordered in the vtable like the order of the virtual procedure declarations in the Type):

Code: Select all

type udt extends object
    as double y
    declare virtual function plus(byval as double) as double
    declare virtual function minus(byval as double) as double
end type

function udt.plus(byval x as double) as double
    print __function__
    return y+x
end function

function udt.minus(byval x as double) as double
     print __function__
    return y-x
end function


dim as function(byref as udt, byval as double) as double f(1 to 2)={Cptr(Any Ptr Ptr Ptr, @udt)[0][0], Cptr(Any Ptr Ptr Ptr, @udt)[0][1]}

dim as udt a
a.y = 5

print f(1)(a,3.6):print f(2)(a,3.6)

sleep
fxm
Posts: 10258
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: type and static type

Postby fxm » Feb 18, 2021 12:53

Same example, but with an un-typed pointer of instance:

Code: Select all

type udt extends object
    as double y
    declare virtual function plus(byval as double) as double
    declare virtual function minus(byval as double) as double
end type

function udt.plus(byval x as double) as double
    print __function__
    return y+x
end function

function udt.minus(byval x as double) as double
     print __function__
    return y-x
end function


dim as function(byref as udt, byval as double) as double f(1 to 2)={Cptr(Any Ptr Ptr Ptr, @udt)[0][0], Cptr(Any Ptr Ptr Ptr, @udt)[0][1]}

dim as udt a
a.y = 5

Dim As Any Ptr p = @a

print f(1)(byval p,3.6):print f(2)(byval p,3.6)

sleep
nastasa eodor
Posts: 131
Joined: Dec 18, 2018 16:37
Location: Germany, Hessdorf
Contact:

Re: type and static type

Postby nastasa eodor » Feb 20, 2021 6:58

fxm wrote:Same example, but with an un-typed pointer of instance:

Code: Select all

type udt extends object
    as double y
    declare virtual function plus(byval as double) as double
    declare virtual function minus(byval as double) as double
end type

function udt.plus(byval x as double) as double
    print __function__
    return y+x
end function

function udt.minus(byval x as double) as double
     print __function__
    return y-x
end function


dim as function(byref as udt, byval as double) as double f(1 to 2)={Cptr(Any Ptr Ptr Ptr, @udt)[0][0], Cptr(Any Ptr Ptr Ptr, @udt)[0][1]}

dim as udt a
a.y = 5

Dim As Any Ptr p = @a

print f(1)(byval p,3.6):print f(2)(byval p,3.6)

sleep


okay, is very simple one.
is any way here, to get the count of virtual functions, in runtime?
fxm
Posts: 10258
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: type and static type

Postby fxm » Feb 20, 2021 9:00

nastasa eodor wrote:is any way here, to get the count of virtual functions, in runtime?

To my knowledge no.
I think the compiler fills in a new vtable for each new class (type with virtual procedures) on the fly, but probably Jeff or dkl can answer better than me.

Everything I know about the vptr / vtable structure is summarized in this diagram:

Code: Select all

                                   vtbl (vtable)
                               .-------------------.
                           [-2]|   reserved (0)    |               RTTI info            Mangled Typename
                               |-------------------|       .-----------------------.       .--------.
      Instance of UDT      [-1]| Ptr to RTTI info  |--->[0]|     reserved (0)      |       | Length |
   .-------------------.       |-------------------|       |-----------------------|       | (ASCII)|
[0]| vptr: Ptr to vtbl |--->[0]|Ptr to virt proc #1|   [+1]|Ptr to Mangled Typename|--->[0]|    &   |
   |-------------------|       |-------------------|       |-----------------------|       |Typename|
   |UDT member field #a|   [+1]|Ptr to virt proc #2|   [+2]| Ptr to Base RTTI info |---.   | (ASCII)|
   |-------------------|       |-------------------|       |_______________________|   |   |________|
   |UDT member field #b|   [+2]|Ptr to virt proc #3|   ________________________________|
   |-------------------|       :- - - - - - - - - -:  |
   |UDT member field #c|       :                   :  |             Base RTTI info
   :- - - - - - - - - -:       :                   :  |       .----------------------------.
   :                   :       |___________________|  '--->[0]|        reserved (0)        |
   :                   :                                      |----------------------------|
   |___________________|                                  [+1]|Ptr to Mangled Base Typename|--->
                                                              |----------------------------|
                                                          [+2]| Ptr to Base.Base RTTI info |---.
                                                              |____________________________|   |
                                                                                               |
                                                                                               V
dodicat
Posts: 6883
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: type and static type

Postby dodicat » Feb 20, 2021 10:56

I suppose adding extends object then virtual to every method, and thus creating the vtable does no harm even if polymorphism is not needed.
Certainly it has no effect on the mangled names of the methods.
The vtable itself is named separately.
Maybe a slight overhead, but for an IDE speed is not critical I don't think.
nastasa eodor
Posts: 131
Joined: Dec 18, 2018 16:37
Location: Germany, Hessdorf
Contact:

Re: type and static type

Postby nastasa eodor » Feb 20, 2021 20:08

fxm wrote:
nastasa eodor wrote:is any way here, to get the count of virtual functions, in runtime?

To my knowledge no.
I think the compiler fills in a new vtable for each new class (type with virtual procedures) on the fly, but probably Jeff or dkl can answer better than me.

Everything I know about the vptr / vtable structure is summarized in this diagram:

Code: Select all

                                   vtbl (vtable)
                               .-------------------.
                           [-2]|   reserved (0)    |               RTTI info            Mangled Typename
                               |-------------------|       .-----------------------.       .--------.
      Instance of UDT      [-1]| Ptr to RTTI info  |--->[0]|     reserved (0)      |       | Length |
   .-------------------.       |-------------------|       |-----------------------|       | (ASCII)|
[0]| vptr: Ptr to vtbl |--->[0]|Ptr to virt proc #1|   [+1]|Ptr to Mangled Typename|--->[0]|    &   |
   |-------------------|       |-------------------|       |-----------------------|       |Typename|
   |UDT member field #a|   [+1]|Ptr to virt proc #2|   [+2]| Ptr to Base RTTI info |---.   | (ASCII)|
   |-------------------|       |-------------------|       |_______________________|   |   |________|
   |UDT member field #b|   [+2]|Ptr to virt proc #3|   ________________________________|
   |-------------------|       :- - - - - - - - - -:  |
   |UDT member field #c|       :                   :  |             Base RTTI info
   :- - - - - - - - - -:       :                   :  |       .----------------------------.
   :                   :       |___________________|  '--->[0]|        reserved (0)        |
   :                   :                                      |----------------------------|
   |___________________|                                  [+1]|Ptr to Mangled Base Typename|--->
                                                              |----------------------------|
                                                          [+2]| Ptr to Base.Base RTTI info |---.
                                                              |____________________________|   |
                                                                                               |
                                                                                               V

okay, man. now is clear to me, how the rtti freebasic mechanism work.
thank you.

p.s.is look like Delphi one...well, is there some differences, but the core idea is like Delphi.
Last edited by nastasa eodor on Feb 20, 2021 20:10, edited 1 time in total.
nastasa eodor
Posts: 131
Joined: Dec 18, 2018 16:37
Location: Germany, Hessdorf
Contact:

Re: type and static type

Postby nastasa eodor » Feb 20, 2021 20:09

dodicat wrote:I suppose adding extends object then virtual to every method, and thus creating the vtable does no harm even if polymorphism is not needed.
Certainly it has no effect on the mangled names of the methods.
The vtable itself is named separately.
Maybe a slight overhead, but for an IDE speed is not critical I don't think.


well, i think no harm to ide speed...
dodicat
Posts: 6883
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: type and static type

Postby dodicat » Feb 21, 2021 1:02

For fun your first post without static methods.

Code: Select all

type QObject extends Object
    as string ClassName
    as integer ID
end type

type QStaticObject extends QObject
    static as QObject ptr AnObject
   
    declare  function ID_get as integer
    declare  sub ID_set(as integer)
    declare  function ClassName_get as string
    declare  sub ClassName_set(as string)
   
    declare operator let(as string)
    declare operator let(as integer)
    declare operator cast as string
    declare operator cast as integer
   
    declare operator cast as any ptr
    declare operator cast as QObject
    declare constructor
end type


declare function Id_get alias "_ZN13QSTATICOBJECT6ID_GETEv"(as QStaticObject)  as integer
#ifdef __FB_64BIT__
declare sub ID_set alias "_ZN13QSTATICOBJECT6ID_SETEu7INTEGER"(as QStaticObject,as integer)
#else
declare sub ID_set alias"_ZN13QSTATICOBJECT6ID_SETEl"(as QStaticObject,as integer)
#endif
declare  function ClassName_get alias "_ZN13QSTATICOBJECT13CLASSNAME_GETEv"(as QStaticObject)as string
declare  sub ClassName_set alias "_ZN13QSTATICOBJECT13CLASSNAME_SETER8FBSTRING"(as QStaticObject,as string)

dim QStaticObject.AnObject as QObject ptr

constructor QStaticObject
    QStaticObject.AnObject=@this
end constructor

function QStaticObject.ID_get as integer
    return AnObject->id
end function

function QStaticObject.ClassName_get as string
    return AnObject->ClassName
end function

sub QStaticObject.ClassName_set(v as string)
    AnObject->ClassName=v
end sub

sub QStaticObject.ID_set(v as integer)
    AnObject->id=v
end sub


operator QStaticObject.let(v as string)
    classname=v
end operator

operator QStaticObject.let(v as integer)
    id=v
end operator

operator QStaticObject.cast as string
    return classname
end operator

operator QStaticObject.cast as integer
    return id
end operator

operator QStaticObject.cast as any ptr
    return @this
end operator

operator QStaticObject.cast as QObject
    return this
end operator

dim shared as QStaticObject so

? @id_get,@id_set
? QStaticObject.AnObject

so.classname="dummy"
so.id=1

? so.classname
? so.id

so.id_set(2)
? so.id

so.classname="dummy"
so.id=1

so.classname_set("dummy_1")
? so.classname

classname_set(so,"dummy_static")
? so.classname,classname_get(so)

dim shared as sub(as QStaticObject,as string) str_setter
 str_setter=@classname_set

str_setter(so,"i've done folks...")
? so.classname,classname_get(so)

so="Test_static_type"
? so

so=10
? so.id, id_get(so), qstaticobject

sleep
 


The help file says for extern....end extern
Extern "C++" blocks are exactly like Extern "C" blocks but they also mangle the names declared within them in a way compatible to that of g++-4.x.
But using the C++ demangle :
#define demangle(s) shell "C++filt "+(s)
The demangled functions do not work in an extern "C++" block in fb.

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 7 guests