Dynamically enumerating the members of a C++ struct


 
Thread Tools Search this Thread
Top Forums Programming Dynamically enumerating the members of a C++ struct
# 1  
Old 05-22-2013
Dynamically enumerating the members of a C++ struct

In C++ there is a struct with the following structure:

Code:
typedef struct myStruct
{
    string s_date;
    float fA;
    float fB;
    float fC;
    .....
    float fZ;
} myData;

After some computations on the values of the struct members are inserted into a database table:

Code:
myData ind;
string sqlInsert = "INSERT INTO calculations (date, A, B, C, ..., Z) 
VALUES ('"+ind.s_date+"','"+ftostr(ind.fA)+"','"+ftostr(ind.fB)+"','"+ftostr(ind.fC)+"','"+ ... +"','"+ftostr(ind.fZ)+"')";

Given that the members of the struct can change, as well as currently close to 100 members, we are looking for a way to loop over the members in a dynamic fashion, without having to spell out each member individually.

Therefore the question is, is there a way to loop over the struct members so that the SQL that comes after the VALUES clause is updated based on however many struct members there are?
# 2  
Old 05-22-2013
You can't do that sadly. What I usually see done when this is needed is one of two things:

* Some sort of dynamic structure, like a map or hash or list, without predefined contents.

OR

* Use a script to build the structure out of some sort of data file, and also build an array which describes its contents.
# 3  
Old 05-23-2013
Thank you for your response. Do you have examples of C++ maps and/or C++ hash lists? And what exactly do you mean by "without predefined contents"?
# 4  
Old 05-23-2013
Instead of having one big structure with hardcoded members like x.ytypeofvalue, have a container holding many structures with two members, name and value. You can store this in many kinds of data structures like lists, maps, vectors, arrays, most anything iterable.

Code:
#include <map>
#include <string>

#define foreach(var, container)                 \
                for(typeof((container).begin()) var = (container).begin(); \
                var != (container).end(); \
                ++var)

int main(void)
{
        std::map<std::string,std::string> mydata;

        mydata["a"]="b";
        mydata["c"]="d";

        foreach(x, mydata)
        {
                cout << (*x).first <<" "<<(*x).second<<"\b";
        }
}

# 5  
Old 05-24-2013
Was not aware of this structure, but have read up more on it in the meantime. Thank you for your guidance.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Get struct definition

I have many headers with huge amount of structures in them, typical one looks like this: $ cat a.h struct Rec1 { int f1; int f2; }; struct Rec2 { char r1; char r2; }; struct Rec3 { int f1; float k1; float ... (6 Replies)
Discussion started by: migurus
6 Replies

2. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies

3. Programming

Using pointers to struct members as args to functions

In a well-known book on the C language, there is an example of an efficient method for using a struct member as an argument to a function. (I'm a C noob, but I believe the correct terminology might be: use call-by-reference instead of call-by-value.) The function is printf. Anyway, here's a... (5 Replies)
Discussion started by: uiop44
5 Replies

4. UNIX for Dummies Questions & Answers

struct winsize

what is struct winsize used for? i tried looking it up, but no luck. (0 Replies)
Discussion started by: l flipboi l
0 Replies

5. UNIX for Dummies Questions & Answers

How to access a struct within a struct?

Can someone tell me how to do this? Just a thought that entered my mind when learning about structs. First thought was: struct one { struct two; } struct two { three; } one->two->three would this be how you would access "three"? (1 Reply)
Discussion started by: unbelievable21
1 Replies

6. Linux

GCOV : struct bb

Hi, I am working on gcov.Meaning, analysing the functionality of gcov. There is one structure called "struct bb". I am not sure, how struct bb members are getting assigned values. If anyone knows how it is happening pls let me know. Thanks in advance. --Vichu (0 Replies)
Discussion started by: Vichu
0 Replies

7. Programming

Enumerating hardware

I am trying to enumerate all hardware (plugged in devices) and I am using lshal source code from here: Cross Reference: /onnv/onnv-gate/usr/src/cmd/hal/tools/lshal.c and is missing information about plugged in monitors, and mouse and houppgae TV card. They came out in gnome device manager,... (0 Replies)
Discussion started by: gyula
0 Replies

8. Programming

struct tm problem

I receive an integer as argument for a function. within function definition i want it to be of type struct tm. eg.. main() { int a; ...... } function(...,..,a,..) int a; { struct tm tm; if(!a) ^ time(&a); ^ ... (4 Replies)
Discussion started by: bankpro
4 Replies

9. Programming

save a struct

hi all , can i save a structure in c in a file? how ? help me , thx. :) (2 Replies)
Discussion started by: kall_ANSI
2 Replies
Login or Register to Ask a Question