How to identify the type of a struct?

Posted By: Clemens

How to identify the type of a struct? - 09/12/11 14:15

Hi there,
I'm looking for a way to find out which struct-type has been passed:

Code:
function draw_object(... void* Object ...) {
     if (Object.type==PANEL)   // ??? WHAT'S THE CORRECT CODE?
          // code for PANEL-position change
     else
          // code for TEXT((/ViewENTITY))-position change
     while (...) {
          draw_obj(Object);
          wait(1);
     }
}


Posted By: 3dgs_snake

Re: How to identify the type of a struct? - 09/12/11 15:43

I don't kow if there is a function to what you want to do but I think that overloading draw_object would be a solution.

Best regards.
Posted By: Myrkling

Re: How to identify the type of a struct? - 09/12/11 15:53

"The handle can be used to identify the type of the object."
See here: http://www.conitec.net/beta/ain-handle.htm
Posted By: TechMuc

Re: How to identify the type of a struct? - 09/12/11 16:11

though handle does ofc only work for engine objects.
dynamic_cast is not available for lite-c (or any other c language wink )
Posted By: Clemens

Re: How to identify the type of a struct? - 09/12/11 19:11

Thanks for the manual hint - didn't know that handle is good for that.

But cause I'm far away from beeing a struct/pointer/bit-shifting expert... there are two problems appearing for me:

1) How has the code to look like, when I want to
-> [converting the handle to a var that contains the index number,] right shift it by 24, then left shift the resulting int by 10?
(something I probably would be able to find out with enough time...)

2) And for my function above I forget the problem of the following compiler error:
'pos_x': is not a member of 'VOID'
Object.pos_x = PositionX;

(something I don't know where to start finding the solution...)
Posted By: WretchedSid

Re: How to identify the type of a struct? - 09/12/11 20:24

Originally Posted By: Clemens
1) How has the code to look like, when I want to
-> [converting the handle to a var that contains the index number,] right shift it by 24, then left shift the resulting int by 10?
[/quote]
Code:
int type = (int)(((var)handle(myObject)) >> 24);
type = type << 10;

if(type == HBYTE_PANEL)
{
   ...
}
etc..



Originally Posted By: Clemens
2) And for my function above I forget the problem of the following compiler error:
'pos_x': is not a member of 'VOID'
Object.pos_x = PositionX;

(something I don't know where to start finding the solution...)

Short answer: Typecast it to the appropriate type
Long answer: Void is a typeless type without any informations, the compiler doesn't know where the offset to the member with the name .x is. You have to tell the compiler what type you are working with so the compiler knows where the offset is.
Posted By: Clemens

Re: How to identify the type of a struct? - 09/12/11 21:04

1) -> Sid, thx for that piece... but when I change my code into:
Code:
function draw_object(... void* Object ...) {
	int Object_type = (int)(((var)handle(Object)) >> 24);
	Object_type = Object_type << 10;
	if (Object_type==HBYTE_PANEL)
		printf("panel! with %.2f",(int)Object_type);
	else if (Object_type==HBYTE_TEXT)
		printf("text! with %.2f",(int)Object_type);
	else
		printf("nothing! with %.2f",(int)Object_type);
	...
}


-> always the "nothing"-message (with 0.00) is appearing even when I pass a PANEL or TEXT object...


2) -> your long answer is the explanation of the error, which I already did understand - the short answer is the really interesting part: a solution. But a too short solution wink how do I tell the compiler what type I'm using -> how to typecast ?


Posted By: WretchedSid

Re: How to identify the type of a struct? - 09/12/11 22:16

Thats strange, it shifts everything like the manual says. I have nothing to test it here, so maybe someone else can help you. Btw, you do already some, wrong, typecasting in your printing function, the format specifier %f specifies a floating point variable to be printed, you pass an integer.

And while we are at typecasting and stuff: http://lmgtfy.com/?q=C+style+typecast wink
Posted By: Clemens

handle function -> HBYTE-type -> how to?? - 09/13/11 00:37

Hehe, it's really time for me to read a c(++) book from begin to end! wink
Type casting is in fact a nice - less simple as it looks in code - thing!!

But it surprises me, that it doesn't matter if I use ((TEXT*)Object).pos_x = PositionX; or ((PANEL*)Object).pos_x = PositionX; in my code above. Both works independet from which type is passed... (?)

Problem1 -> getting the right HBYTE-type
I couldn't solve it on my own, yet. Has anybody an idea what's wrong with Sid's piece of code?
Posted By: Myrkling

Re: handle function -> HBYTE-type -> how to?? - 09/13/11 08:22

If I remember well, there was something special about the typecasting bit shifting thing.

I can't test it right now, but you could try something like this:
int type = (int)(handle(Object) >> 14);
Posted By: Clemens

Re: handle function -> HBYTE-type -> how to?? - 09/13/11 11:17

Great, this works! Thanks guys...
(even if I wonder about the working solutions a little bit)
Posted By: TechMuc

Re: handle function -> HBYTE-type -> how to?? - 09/13/11 11:28

fyi
Quote:
But it surprises me, that it doesn't matter if I use ((TEXT*)Object).pos_x = PositionX; or ((PANEL*)Object).pos_x = PositionX; in my code above. Both works independet from which type is passed... (?)


After the typecasting the compiler knows the type, which is in the first example "TEXT*". It doesn't really matter which object "Object" really is. The compiler thinks it's type is "TEXT*". The "." is reinterpreteted as "->". The "->" leads to a resolving of the pointer (so access the memory area behind the pointer). The "pos_x" tells the compiler the offset to the value of the pointer "Object". The type of "pos_x" tells the compiler how many bytes he has to erase / overwrite with PositionX.

If now PANEL* and TEXT* "pos_x/y" parameters have the same offset, and the parameters have the same type, the code will work identically for the compiler.

That's by the way, the way handle works. Handle() just reads out the index in the C_LINK structure in the engine object. As you see in ANY engine object, the C_LINK structure is the FIRST parameter (offset 0).
So actually handle does nothing but:

ENTITY* obj = ...;
int handle_of = ((int)*((void*)obj)));

This code just interpretes the first 4 bytes of the ENTITY object as integer. As you can see in the struct the first 4 bytes of any engine-object (STRING,PANEL etc.) is filled with the C_LINK struct. The first 4 Bytes of the C_LINK struct is filled with the index of an object.

That's why: handle is extremly fast (ptr_for_handle is not that fast as it has to iterate over a linked list, with a complexity of O(N), which is never that fast - though as you know, the index of an object also contains the type, which makes things a little bit slower).
Posted By: Clemens

Re: handle function -> HBYTE-type -> how to?? - 09/13/11 15:57

Hey, that's a detailed and well developed description - thank you
© 2024 lite-C Forums