Identify type of structure?

Just starting out? Need help? Post your questions and find answers here.
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Identify type of structure?

Post by wombats »

Hi,

Is there a way to get the name of a structure after it has been created? In the following, could I somehow get "MyStructure" from *test?

Code: Select all

Structure MyStructure
  name.s
EndStructure

Define *test.MyStructure
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Identify type of structure?

Post by mk-soft »

Only over small trick.

Code: Select all

;-TOP

Structure udtMyStruct
  iVal.i
  fVal.f
  dVal.d
  sVal.s
EndStructure

Define MyStruct.udtMyStruct

If CreateXML(0)
  InsertXMLStructure(RootXMLNode(0), @MyStruct, udtMyStruct)
  FormatXML(0, #PB_XML_ReFormat)
  Debug ComposeXML(0)
EndIf

If CreateJSON(0)
  InsertJSONStructure(JSONValue(0), @MyStruct, udtMyStruct)
  Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf
Unfortunately the runtime-library does not yet work with structures.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Identify type of structure?

Post by Mijikai »

Something like this?

Code: Select all

EnableExplicit

Structure MyStructure
  name.s
EndStructure

Global *test.MyStructure

Macro mAllocMyStructure(ptr)
  ptr = AllocateStructure(MyStructure)
  ptr\name = "MyStructure"
EndMacro

mAllocMyStructure(*test)

Debug *test\name
Name has to be stored somewhere anyways...
User avatar
GedB
Addict
Addict
Posts: 1312
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Re: Identify type of structure?

Post by GedB »

wombats wrote:Hi,

Is there a way to get the name of a structure after it has been created? In the following, could I somehow get "MyStructure" from *test?

Code: Select all

Structure MyStructure
  name.s
EndStructure

Define *test.MyStructure
The only thing you can find out about a structure is it's size, using the SizeOf compiler function: https://www.purebasic.com/documentation ... tions.html

If you want to distinguish between different structures and they are of different sizes then you can do it based on Size. At a squeeze you could also consider the offset of it's fields using OffsetOf().

The thing is that Structures only exist at compile time. At runtime all that remains is a block of memory that gets allocated and relative offsets into that memory block.

Otherwise you have to add some metadata of your own, as others have explained.
Post Reply