No announcement yet.

Generic Pointers

  • Filter
  • Time
  • Show
Clear All
new posts

  • 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:

    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
    Is that do-able? Or am I missing something?

    John Strasser
    Phone: 480 - 273 - 8798

  • #2
    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:

    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:

    The most exasperating part of the "rat race" is how often the rats are in the lead!


    • #3
      This is an ideal candidate for a UNION - you can define a pointer to any structure and reference that structure from the UNION member:

      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
      End Type
      Function PBMain
        Dim meta01 As meta
        Dim meta02 As meta
        Dim d1 As data01
        Dim d2 As data02
        meta01.pAny = VarPtr(d1)
        meta02.pAny = VarPtr(d2)
        [email protected] = "test_meta1"
        [email protected] = "test_meta2"
        ? [email protected]
        ? [email protected]
      End Function
      The above code still involves a degree of "separation", however, as the data01 and data02 members are not aligned the same.
      Even in C, using a typecast, it would still work the same as PB. | Slam DBMS | PrpT Control | Other Downloads | Contact Me


      • #4
        Thanks guys

        I appreciate the quick response. Hadn't considered Cheetah - I'll take a look.

        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.

        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 Strasser
        Phone: 480 - 273 - 8798


        • #5
          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"

          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 
            ELSEIF Pointer_is_DWORD_PTR THEN
                  PRINT @PU.DW
            ELSEIF Pointer_IS_CURRENCY THEN 
                 PRINT @pu.C 
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]


          • #6
            Thanks Mike

            John Strasser
            Phone: 480 - 273 - 8798