Get the Symbol name of a UDT?

General FreeBASIC programming questions.
Post Reply
Michael
Posts: 61
Joined: Jul 15, 2005 21:11
Location: Oregon, USA
Contact:

Get the Symbol name of a UDT?

Post by Michael »

I need to know if its possible to get the symbol name of a udt (or any variable for that matter) during its initialization.

Example:

Code: Select all


Type TCustom
    public:
        declare function GetName() as string
    
    private:
        myname as string
End Type


function TCustom.GetName()
    return get_my_symbol_name_somehow(this)
end function


'-------------------------------

dim tcB as TCustom
dim tcA as TCustom

print tcA.GetName()    'would print tcA

I have a define that I can pass a variable name to and it will return it as a string, but that isnt quite what I want. I think I need a way to track variables even if they get reassigned to other variable names in my code. That is to say, i need to know what varaible name they were initialized with. Dont ask why, just entertain me please. Thank you in advance.

Id even be happy with a method that would allow me to put the initial symbol name in a string when the udt is initialized.
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

Why not record the variable name and it’s address when you declare it. If you pass it byref then @variable will return the original pointer so you can lookup the original name.
Michael
Posts: 61
Joined: Jul 15, 2005 21:11
Location: Oregon, USA
Contact:

Post by Michael »

Ok so its a little bit complicated but here goes. I have a callback function that gets called by an object. the callback has a single argument that it takes which is a pointer to the calling object. The calling object is part of a shared library and not part of my source. Thats why Its being referenced by pointer.

I have a UDT that acts a Class to encapsulate API calls (in custom methods) that can be used with the object stored at a given pointer (known by a variable stored in the UDT).

When the callback function is called it will receive the pointer to the outside API object, but I need to know which UDT was associated with that pointer when the callback was triggered.

Thoughts?
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

The easy way...

Code: Select all

 #define ID(name)      #name
 
 dim z as string = id(z)
 ? z
 sleep
With a hash map... VonGodric made an awesome example of registering all your symbols to a hashmap. This will make them accessible w/in a program.

http://www.freebasic.net/forum/viewtopi ... highlight=
Sisophon2001
Posts: 1706
Joined: May 27, 2005 6:34
Location: Cambodia, Thailand, Lao, Ireland etc.
Contact:

Post by Sisophon2001 »

I came across a problem like this interfacing with Lua once. I was trying to make a generic callback to handle different situations. Unless the author of you external library included some means of identifying what structure is passed to the callback, then you are stuck. Symbols do not survive compilation unless somebody deliberately writes code to make it so.

Now if you have the source of the external library?

Happy New Year.

Garvan
Michael
Posts: 61
Joined: Jul 15, 2005 21:11
Location: Oregon, USA
Contact:

Post by Michael »

Funny thing, I was searching the API and found that each object had a generic mapping that will allow me to set/get data by key. So I can assign a generic "Name" property to the object at the pointer. This should solve my problem. Thank you all for the feedback though.
Post Reply