Thanks Mike
JS
Announcement
Collapse
No announcement yet.
Generic Pointers
Collapse
X
-
When you think about it, all pointers are generic.
A pointer is a pointer.. an address. How one interprets how much data at that address is application-dependent.
>I was just hoping I didn't have to take the step and explicitly create the union.
The UNION statement creates nothing at runtime; it only provides syntax for easy access of data.
An option to creating a UNION defined with PTR members is to define a UNION PTR data item, with the members of the underlying UNION structure as "themselves"
Code:UNION MyUnion DW AS DWORD sz AS ASCIIZ * (Way, way more than enough) C AS CURRENCY END UNION .... LOCAL pU AS MyUnion PTR ' only costs your program SIZEOF(pU), which is 4 bytes. pU = Function returning a "generic pointer" IF Pointer_Is_STRING_PTR THEN PRINT @pU.sz ELSEIF Pointer_is_DWORD_PTR THEN PRINT @PU.DW ELSEIF Pointer_IS_CURRENCY THEN PRINT @pu.C .......
Leave a comment:
-
-
Thanks guys
I appreciate the quick response. Hadn't considered Cheetah - I'll take a look.
Scott:
That's a good reference thread you pointed me to. Thanks. The idea was to put together a set of "pseudo-classes" where then the only thing I'd have to do is de-reference the pointer when it was time to get or put the udt into the random access file. I don't know if I explained what I wanted clear enough.
Kev:
That's pretty much what I figured I'd have to do - I was just hoping I didn't have to take the step and explicitly create the union.
Thanks again folks.
John
Leave a comment:
-
-
This is an ideal candidate for a UNION - you can define a pointer to any structure and reference that structure from the UNION member:
Code:Type data01 a As Long b As String * 10 End Type Type data02 c As Quad d As String * 30 End Type Union data_union pAny As Dword pd1 As data01 Ptr pd2 As data02 Ptr End Union Type meta filename As String * 260 filenumber As Long data_union End Type Function PBMain Dim meta01 As meta Dim meta02 As meta Dim d1 As data01 Dim d2 As data02 meta01.filename="file01.dat" meta01.filenumber=FreeFile meta01.pAny = VarPtr(d1) meta02.filename="file02.dat" meta02.filenumber=FreeFile meta02.pAny = VarPtr(d2) [email protected] = "test_meta1" [email protected] = "test_meta2" ? [email protected] ? [email protected] End Function
Even in C, using a typecast, it would still work the same as PB.
Leave a comment:
-
-
Hi John,
You can't access data files, or UDT members in the explicit manner you are attempting in code. Read the PB help file on Types and Unions. In your samples directory under DDT, you will find address.bas. It is a good starting point and shows record based file access as well. Then study up on pointers.
As to btree, here is a random sample:
http://www.powerbasic.com/support/pb...ighlight=btree
Search the forum for "BTREE" you wil find quite a few hits.
If you would prefer not to roll your own, I recommend Paul Squires Cheetah dll that works on xbase - dbase file formats. It is very fast, small and problem free. It also has numerous code samples of the various functions and a very well written help file. Recently, Paul released it as freeware. I bought my copy when it was commercial and it was worth every penny!
Get it at:
http://www.planetsquires.com/
HTH,
Scott
Leave a comment:
-
-
Generic Pointers
Hi Folks,
I'm writing a program where I'me working with several random access files (a mini database using Btree type keys).
What I'd like to do is create a generic UDT for most of what I need (filename, filenumber, etc) and then have another UDT for the specific data that would go into each one.
Ideally I would have a generic pointer in the "meta' UDT where I could store the address of the "data" udt. I know that I can do this by having a dword store the actual value and use a union with "options" for every "data" udt I need.
But that's a PITA.
If I remember my C this would be typecasting the pointer (been about 10 years...)
I'd like it to look like this:
Code:Type data01 a as long b as string * 10 End Type Type data02 c as quad d as string * 30 End Type Type meta filename as string filenumber as long GPTR as generic pointer End Type dim meta01 as meta dim meta02 as meta dim d1 as data01 dim d2 as data02 meta01.filename="file01.dat" meta01.filenumber=freefile meta01.GPTR=varptr(d1) meta02.filename="file02.dat" meta02.filenumber=freefile meta02.GPTR=varptr(d2)
JSTags: None
-
Leave a comment: