Typeof (pointer value) ?

General FreeBASIC programming questions.
Post Reply
ppf
Posts: 88
Joined: Oct 10, 2017 6:41

Typeof (pointer value) ?

Post by ppf »

Hi,

looking on 'Typeof' page in FB manual and found a question.
Maybe stupid, maybe new feature request.
if this is allowed

Code: Select all

Dim As TypeOf(67.2) bar '' '67.2' is a literal double
what about this one ??

Code: Select all

Dim As TypeOf(*p) bar	'p is a pointer to initialized variable/UDT
This feature enabled, my life is easier ;)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Typeof (pointer value) ?

Post by MrSwiss »

ppf wrote:This feature enabled, my life is easier ;)
What makes you think so? I don't see any reason for it.

Code: Select all

'Var bar = 67.2f     ' forced to: Single
Var bar = 67.2      ' default Double

#Print TypeOf(bar)  ' see: compiler output

Print bar : Sleep
TypeOf() returns the data-type, as string at compile time.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Typeof (pointer value) ?

Post by Tourist Trap »

ppf wrote: what about this one ??

Code: Select all

Dim As TypeOf(*p) bar	'p is a pointer to initialized variable/UDT
This feature enabled, my life is easier ;)
Hello ppf,

this below works:

Code: Select all

dim as single S
dim typeOf(*(@S))   X
As far as you can say the type just by reading the code, typeOf will also be able to do so.

To test typeOf, do a #print typeOf(...) , and watch your output window (if you have an IDE with it, like FBIDE).

Just in case. I had questions times ago, that look like what you are searching for. Maybe it can help so I give you this link for example : viewtopic.php?p=224104#p224104.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Typeof (pointer value) ?

Post by fxm »

As long as 'p' is a really typed pointer ('datatype Ptr' with 'datatype' <> 'Any'), 'Typeof(* p)' returns the 'datatype' type.
Note: If 'datatype' = 'Any Ptr [Ptr [Ptr ...]]', This even works and the return type by 'Typeof()' is 'Any Ptr [Ptr [Ptr ...]]'.
Last edited by fxm on May 12, 2019 13:45, edited 2 times in total.
ppf
Posts: 88
Joined: Oct 10, 2017 6:41

Re: Typeof (pointer value) ?

Post by ppf »

@ MrSwiss
Hi,
was inspired by this https://freebasic.net/forum/viewtopic.p ... 93#p260547
In case of bigger 'if' statements field to detect datatype, it could be replaced by only one line
of thinked syntax, for any initialized variable type/udt
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Typeof (pointer value) ?

Post by Tourist Trap »

ppf wrote: In case of bigger 'if' statements field to detect datatype, it could be replaced by only one line
of thinked syntax, for any initialized variable type/udt
Why don'tyou you simply pass the size to your read function before you cast the typed variable pointer to an ANY ptr ? Would be simpler.
ppf
Posts: 88
Joined: Oct 10, 2017 6:41

Re: Typeof (pointer value) ?

Post by ppf »

Thanks, Tourist Trap,
long trying to enable shortened example to working level, clearing real program too.
Done, next step is routine.
ppf
Posts: 88
Joined: Oct 10, 2017 6:41

Re: Typeof (pointer value) ?

Post by ppf »

*** glibc detected *** ... free(): corrupted unsorted chunks
*** glibc detected *** ... malloc(): memory corruption
etc.
Endlesss errooooors.., success finally, compiled ok.Not yet routine, only example to unifying.
(My mind was mystified with 'Cast/Cptr' usage, looks like).
Example looks now (non-OOP version with pointers)

Code: Select all

#include "fbgfx.bi"
screen 20,32
 
 'UDT for 4 user datafile formats and parameters 
type fileTypeBuffer		
	indexFR as integer
	notes as string
	bufferType as string
	bufferVariable as string
	bufferPointer as any Ptr
	bufferLEN as ubyte						
	bufferSize as byte						
end type 
 
'4 custom buffer datatype here - UDTs for RANDOM file reading of user data
type udtA 
	as byte a
end type 
type udtB
	as ushort b
	as string*3 cs
end type 
type udtC
	as single a
	as ubyte c
	as integer d
	as string*8 fs
end type 
type udtD
	as long dw
	as single a
	as ubyte c
	as integer d
	as string*10 ws
end type  

const bufferTypeSsum=4
dim shared fileReadParm(1 to bufferTypeSsum) as fileTypeBuffer		
dim shared recN as integer				'record number
dim shared as integer fx(1 to 4)		'freefile

'define global buffers for reading files
Static shared buffA as udtA 
Static shared buffB as udtB
Static shared buffC as udtC
Static shared buffD as udtD

Static shared buffR as udtD

'pointers to types
Static Shared As udtA Ptr pA	
Static Shared As udtB Ptr pB	
Static Shared As udtC Ptr pC	
Static Shared As udtD Ptr pD	

Static Shared As udtD Ptr pR		

pA=0
pB=0
pC=0
pD=0



'array  of I/O  parameters 
'fill values for file format + buffer

with fileReadParm(1)
	.indexFR=1						'just info

	.bufferPointer=pA				'address const
	.bufferLEN=LEN(buffA)		'lenght of variable
	.bufferSize=Sizeof(udtA)	'size of type
end with

with fileReadParm(2)
	.indexFR=2						'just info

	.bufferPointer=pB				'address const
	.bufferLEN=LEN(buffB)		'lenght of variable
	.bufferSize=Sizeof(udtB)	'size of type
end with

with fileReadParm(3)
	.indexFR=3						'just info

	.bufferPointer=pC				'address const
	.bufferLEN=LEN(buffC)		'lenght of variable
	.bufferSize=Sizeof(udtC)	'size of type
end with

with fileReadParm(4)
	.indexFR=4						'just info

	.bufferPointer=pR				'address const
	'.bufferPointer=pD				'address const
	.bufferLEN=LEN(buffD)		'lenght of variable
	.bufferSize=Sizeof(udtD)	'size of type
end with

 'define global index of buffer type selected
dim shared iB as integer		

	'testin - print buffer members
sub printBufferMembers(byval id as integer,byval p as any ptr)
 ? " Buffer members ... buffer type id= ";id
	select case id
	case 1
		? "->a "; cast(Typeof(buffA) ptr, p)->a
	case 2
		? "->b "; cast(Typeof(buffB) ptr, p)->b
		? "->cs "; cast(Typeof(buffB) ptr, p)->cs
	case 3
		? "->a "; cast(Typeof(buffC) ptr, p)->a
		? "->c "; cast(Typeof(buffC) ptr, p)->c
		? "->d "; cast(Typeof(buffC) ptr, p)->d
		? "->fs "; cast(Typeof(buffC) ptr, p)->fs
	case 4
		? "->dw "; cast(Typeof(buffD) ptr, p)->dw
		? "->a "; cast(Typeof(buffD) ptr, p)->a
		? "->c "; cast(Typeof(buffD) ptr, p)->c
		? "->d "; cast(Typeof(buffD) ptr, p)->d
		? "->fs "; cast(Typeof(buffD) ptr, p)->ws
	end select
	
end sub	


	'simulation starts here
var aqa=1
var bbq=1
dim as any Ptr p=0


'open files
 var aqar=10					'dummy
for i as integer=1 to 4	
  fx(i)=freefile()
  aqar=fileReadParm(i).bufferSize
  open str(i)+"datatype.dat" for random as #fx(i) Len=aqar
next i


'uncomment to create files
/'
 'fill some buffer variable 
scope
'A
buffA.a=26
'B
buffB.b=12345
buffB.cs="XYZ"
'C
buffC.a=999.5
buffC.c=99
buffC.d=321000
buffC.fs="87654321"
'D
buffD.dw=123000456
buffD.a=23.4
buffD.c=101
buffD.d=23000
buffD.ws="0123456789"


'R
buffR.dw=1111111111
buffR.a=4321.0
buffR.c=55
buffR.d=20002
buffR.ws="0022433789"


? "pR ";pR	',*pr	',pR[0]

end scope

'create files of one record
 put #fx(1),,buffA		
 put #fx(2),,buffB
 put #fx(3),,buffC
 put #fx(4),,buffD
'/


'target to do
Sub randomReadRecord3(byval id as integer, byref p as any Ptr)	

  'get #ff,recN,*s,1

end sub	

'simulated flow
  var sizeX=0	'bufferSize empty
    recN=1	'first record only

'used pointer to its datatype works ok  
iB=1
  sizeX=fileReadParm(iB).bufferSize
  ? "sizeX ";sizeX,:? " pA = ";pA,
  pA = Allocate(sizeX)
  get #fx(iB),recN,*pA,1
  ? "2- pA = ";pA
  printBufferMembers(iB,pA)		'testin - print buffer members
?

iB=3
  sizeX=fileReadParm(iB).bufferSize
  ? "sizeX ";sizeX,:? " pC = ";pC,
  pC = Allocate(sizeX)
  get #fx(iB),recN,*pC,1
  ? "2- pC = ";pC
  printBufferMembers(iB,pC)		'testin - print buffer members
?  

iB=4  
  sizeX=fileReadParm(iB).bufferSize
  ? "sizeX ";sizeX,:? " pD = ";pD,
  pD = Allocate(sizeX)
  get #fx(iB),recN,*pD,1
  ? "2- pD = ";pD
  printBufferMembers(iB,pD)		'testin - print buffer members
?  
  
  deallocate (pA): pA=0

'now used pointer pA to all datatypes works too
iB=1
  sizeX=fileReadParm(iB).bufferSize  
 ? "fx(iB),recN,sizeX ";fx(iB),recN,sizeX,:? " pA = ";pA,
  pA = Allocate(sizeX)
  'get #fx(iB),recN,*Cptr(udtD ptr, pA),1
  get #fx(iB),recN,*Cast(udtA ptr, pA),1		',sizeX
  ? "2- pA = ";pA
  printBufferMembers(iB,pA)		'testin - print buffer members
deallocate (pA): pA=0
?

iB=2
  sizeX=fileReadParm(iB).bufferSize
 ? "fx(iB),recN,sizeX ";fx(iB),recN,sizeX,:? " pA = ";pA,
  pA = Allocate(sizeX)
  get #fx(iB),recN,*Cast(udtB ptr, pA),1		',sizeX
  ? "2- pA = ";pA
  printBufferMembers(iB,pA)		'testin - print buffer members
deallocate (pA): pA=0
?  
  
iB=3
  sizeX=fileReadParm(iB).bufferSize
 ? "fx(iB),recN,sizeX ";fx(iB),recN,sizeX,:? " pA = ";pA,
  pA = Allocate(sizeX)
  get #fx(iB),recN,*Cast(udtC ptr, pA),1		',sizeX
  ? "2- pA = ";pA
  printBufferMembers(iB,pA)		'testin - print buffer members
deallocate (pA): pA=0
?  

close
? "ended, press key..."
sleep
end
compiled + works ok

Now I want to change/replace this part

Code: Select all

"Cast(udtB ptr,"     'from line " get #fx(iB),recN,*Cast(udtB ptr, pA),1 "		
by used index "iB"
with keyword 'Typeof(*somePointer(iB))', assuming, to get needed routine.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Typeof (pointer value) ?

Post by Tourist Trap »

ppf wrote: Now I want to change/replace this part

Code: Select all

"Cast(udtB ptr,"     'from line " get #fx(iB),recN,*Cast(udtB ptr, pA),1 "		
by used index "iB"
with keyword 'Typeof(*somePointer(iB))', assuming, to get needed routine.
Hi ppf,

if you want an array of untyped pointers , namely somePointer(), to store all the starting addresses of your data, you lose the information on the type. TypeOf will return ANY very probably.

That's why I gave you a link to my testing of Variant type, a Variant being able to know what was its type at the initialization. But it's simply what fxm showed with Abstract stuff in a base type etc... This way you could solve your issue with an array of your base type as in the examples by fxm.

I don't see any easier way here. This doesn't mean that you couldn't find one. I don't know.
Post Reply