Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

working with structures 4

Status
Not open for further replies.

franklane

Programmer
Oct 5, 2004
12
0
0
US
Hi,

I was wondering if anyone would share a neat way to determine at run time what types a structure is composed of?

Basically I want to write a function that takes a single parameter of type void*. Where the void* would be a structure. Then I want the function to be able to take apart any structure based on type and offset. I don't know how to determine what types a structure is composed of?.

I thought about doing this with templates but don't think that would work because the structures don't have similar compositions. Does that make any sense? Maybe I'm not thinking about templates correctly.

Any help here will be greatly appreciated!

thanks,
Frank
 
What you're describing requires a language with reflection capabilities; C++ simply doesn't have that.


What's the exact problem? There may be a way to work something out using templates or something similar.
 
Hi,

Thanks!:)

We're invoking function(s) that live in different processes on different processors(they all used to be together in the same address space, now they are in different processes on physically different processors). Sometimes the parameters of the function calls are monolithic structures(i.e. there will be three or four layers of structures embedded within structures). The only way we could think of to avoid padding and boundary issues was to unroll the structures into a byte stream of scalar types(then once across the boundary populate structures native to the other architecture). To get things going I've gone in by hand and flattened all the structures and unflattened them on the "other side". I'm thinking/hoping there has to be a programmatic way to do this for the general solution.

I tried to do this with templates but it seems that a template solution required that all structures have identical member sets(at least that's the way I was interpreting the compiler's error messages). I still consider myself green with templates so I'm not surprised if I'm thinking about this all wrong. Please help straighten me out on this.

Thanks,
Frank
 
I might be able to think of a better solution if I had more than 4 hours of sleep, but to answer your first post:

You can use a dynamic_cast to try to guess at the structure type. If you guess wrong, the pointer will be NULL. If you guess right, the pointer will be valid.

However, that would be a nightmare if you have a lot of types to guess. Your structures could also include (or derive from) a class that contains an enum or string (or both) telling you exactly what kind of object it is...
 
Here is a crap way of doing it but it might be just what you need - switch on RTTI (/GR flag) and use typeid to determine what type your structure is. It doesn't work at all on byte streams - you will need some factory mechanism for that.

MFC has some mechanism for RTTI without switching on RTTI. Just check the macro definitions - it has some class name of some sort which you can use: that is, if you are using MFC.
 
I don't think the problem lies in identifying the type of the structure, but with identifying the types of the fields of the structures. The poster wants to avoid writing code to serialize each structure.


I don't have any bright ideas at the moment, but I'd suggest looking at some serialization libraries and tools. They may have solved your problem, or at least provide some tools that make it easier to do so.

My instinct tells me that you're just going to have to write the code to serialize each type.
 
Chipper is right, you can't do it, C++ doesn't have reflection. Try Java or C# :D.
 
...wanted to thank everyone for the great input...and say that i appreciate your patience and help

have a great one,
frank
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top