Complicated C++ Question

jgbishop

Senior member
May 29, 2003
521
0
0
I am currently working with a program here at work that collects hardware information from a computer and reports it back. The API of this program is similar to the following:

1. I create a callback function to "do something" with the data that gets returned.
2. I then call an "enumerateInstances()" method for each hardware item I am interested in, passing in the callback function as a parameter. This function populates a structure with all of the obtained information.

The problem I have is that there are nearly 600 separate classes to deal with (each representing a various area of the computer hardware). I'd rather not write 600 functions to print each struct (each of which is different, by the way). Is there a way to write one function that prints an arbitrary structure in a humane manner? Debuggers seem to be able to do this kind of thing - how do they do it?
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
Depends what exactly you're using. What you need is something called reflection (java has that, pure c++ doesnt). If you're using MFC framework it does provide a limited reflection functionality, however, I don't think it's powerful enough to do what you're asking for.
 

jgbishop

Senior member
May 29, 2003
521
0
0
Thanks for the tip. I doubt I'll be making use of MFC in this project, so it looks like I'm stuck. There are a number of other fundamental problems I'm encountering, so maybe I'll end up heading in a completely different direction with this project.

Again, thanks. Now I know what to search for (I had forgotten about the term 'reflection').
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
your best bet maybe to write a macro that you can feed a class/sturct name that looks at the declaration of a class and writes out a print() or toString() function.....
Should not be too difficult esp. if you are using a powerfule IDE like visual studio that has *huge* macro support. Also, I believe debuggers are able to examine objects because special information is left about objects

describing them when you compile with debug enabled.... however i have no idea how/if you can access that data......
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Try to build a base class that can contain information regarding how the parent/derived class/structure is then defined.

Then your processing method can get that information, parse and output it accordingly.

Try to think of it conceptually as a printf format statement that is held in the base class and prepared in the derived calss.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: statik213
Isn't his problem that these classes/structs are pre-defined?

If he has access to the code, then my solution could work.

Otherwise, he will have to folowing one of the other solutions and become very frustrated.

 

jgbishop

Senior member
May 29, 2003
521
0
0
Yes, the structs are predefined. In fact, we have a program here in house that generates the .cpp and .h files for us (setting up the enumerateInstances() functions and the structs themselves). I just spoke with a co-worker and it looks like I might not have to print out the data myself. I'll let whoever is asking for the data take care of that. This project is still in its infancy, so we have the luxury of changing stuff like that.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: jgbishop
Yes, the structs are predefined. In fact, we have a program here in house that generates the .cpp and .h files for us (setting up the enumerateInstances() functions and the structs themselves). I just spoke with a co-worker and it looks like I might not have to print out the data myself. I'll let whoever is asking for the data take care of that. This project is still in its infancy, so we have the luxury of changing stuff like that.

You may wish to forward my solution on to the group that controls the code generation.

They can easily then insert a print control structure as the first parameter within the existing structure or if using classes just derive the current class from the generic print structure formatting class.

 

oog

Golden Member
Feb 14, 2002
1,721
0
0
for now you can do a raw dump of the struct, which may at least minimally useful to someone. it may be possible to supplement this with debug information built into the executable if you're willing to go that route.