Page 1 of 1

Examine Runtime Enumerations and Structures

Posted: Sun Sep 15, 2013 11:51 am
by Danilo
ExamineRuntimeEnumeration to get entries in named enumerations:

Code: Select all

Runtime Enumeration myEnum
    #GadgetInfo = 12
    #GadgetText
    #GadgetOK
EndEnumeration 

If ExamineRuntimeEnumeration(0, "myEnum") ; "enumerationName", "module::enumerationName"

    While NextRuntimeEnumerationEntry(0)
        
        ;
        ; Get the entry name:
        ;
        ;    return: "#Gadgetinfo", "#GadgetText", "#GadgetOK"
        ;
        Name$ = RuntimeEnumerationEntryName(0)
        
        Debug Name$ + " = " + GetRuntimeInteger(Name$) ; named enums are always integer
    Wend
    
    FinishRuntimeEnumeration(0)
    
EndIf
ExamineRuntimeStructure to get structure entries and types (for serialization: saving and loading structures/memory and structured maps/lists/arrays easily):

Code: Select all

Runtime Structure myStruct
    *p
    name.s
    street.s
    street2.s
    zip.i
    Map m.i()
    List l.i()
    Array a.i(10)
EndStructure

If ExamineRuntimeStructure(0, "myStruct") ; "structureName", "module::structureName"

    While NextRuntimeStructureEntry(0)
        
        ;
        ; Get the entry name (everything before the point):
        ;
        ;    return: "*p", "name", "street", "street2", "zip", "map m", "list l", "array a"
        ;
        Name$ = RuntimeStructureEntryName(0)
        
        ;
        ; Get the entry type (everything after the point):
        ;
        ;    return: "a", "b", "c", "w", "u", "l", "i", "q", "f", "d", "s", "s{10}", "structureName"
        ;            "a[10]", "b[2]", ...
        ;            "i(10)", "i()",  ...
        ;
        Type$ = RuntimeStructureEntryType(0)
        
        ;
        ; returns the size of the entry
        ;
        Size$ = " (Size: " + RuntimeStructureEntrySize(0) + ")"
        
        ;
        ; returns the offset of the entry within the structure
        ;
        Offset$ = " (Offset: " + RuntimeStructureEntryOffset(0) + ")"

        Debug Name$ + "."+Type$ + "; " + Size$ + Offset$
    Wend
    
    FinishRuntimeStructure(0)
    
EndIf

Another way for ExamineRuntimeStructure, maybe too complicated with maps/lists/arrays/structures:

Code: Select all

Runtime Structure Address
    *p
    name.s
    street.s
    street2.s
    zip.i
    Map m.i()
    List l.i()
    Array a.i(10)
EndStructure

If ExamineRuntimeStructure(0, "Address")

    While NextRuntimeStructureEntry(0)
        
        ;
        ; returns the entry name:
        ;    return: "*p", "name", "street", "street2", "zip", "m", "l", "a"
        ;
        Name$ = RuntimeStructureEntryName(0)
        
        ;
        ; Get the entry type (.a, .b, .c, .w, .u, .l, .i, .q, .f, .d, .s, ...)
        ;
        Select RuntimeStructureEntryType(0)
            Case #PB_Byte        : Type$ = " [Byte] "
            Case #PB_Ascii       : Type$ = " [Ascii] "
            Case #PB_Character   : Type$ = " [Character] "
            Case #PB_Word        : Type$ = " [Word] "
            Case #PB_Unicode     : Type$ = " [Unicode] "
            Case #PB_Long        : Type$ = " [Long] "
            Case #PB_Integer     : Type$ = " [Integer] "
            Case #PB_Quad        : Type$ = " [Quad] "
            Case #PB_Float       : Type$ = " [Float] "
            Case #PB_Double      : Type$ = " [Double] "
            Case #PB_String      : Type$ = " [String] "
            Case #PB_FixedString : Type$ = " [FixedString] "                   ; #PB_FixedString missing
                
            ;
            ; special types
            ;
            Case #PB_Structure
                Type$ = " [Structure "
                Type$ + RuntimeStructureEntryTypeString(0) + "] " ; get structure name
            Case #PB_FixedArray
                Type$ = " [FixedArray "
                Type$ + RuntimeStructureEntryTypeString(0) + "] " ; get fixed array part and type ( type[10] )
            Case #PB_Map
                Type$ = " [Map "
                Type$ + RuntimeStructureEntryTypeString(0) + "] " ; get map type ( s, i, structureName )
            Case #PB_List                                                      ; #PB_List missing
                Type$ = " [List "
                Type$ + RuntimeStructureEntryTypeString(0) + "] " ; get list type ( s, i, structureName )
            Case #PB_Array
                Type$ = " [Array "
                Type$ + RuntimeStructureEntryTypeString(0) + "] " ; get array type ( s, i, structureName )
        EndSelect
        
        ;
        ; returns the size of the entry
        ;
        Size$ = " (Size: " + RuntimeStructureEntrySize(0) + ")"
        
        ;
        ; returns the offset of the entry
        ;
        Offset$ = " (Offset: " + RuntimeStructureEntryOffset(0) + ")"

        Debug Name$ Type$ + Size$ + Offset$
                    
    Wend
    
    FinishDirectory(0)
    
EndIf
IsRuntimeStructureEntryUnion() to check if entry is in StructureUnion?

GetRuntimeStructureSize(name.s) like sizeOf() with string argument?