I currently use a logging system that uses a tag value to identify the parameter that it will store. The format we are using is the following: Tag + Time + value.
The next step is to take a logged message and send it to a server using Protocol Buffers to serialize the data.
The protocol buffer .proto file, provides a struct with all the fields each corresponding to a tag in the original logging system.
The problem comes when I need to efficiently read the tag and assign the value to a field in the protocol buffer struct. In essence I would for example like to take in tag 5 and be able to find the field 5 in the struct and write the value.
I know this could be done with switch cases, but we are using around 50 tags so I would like to avoid this solution if possible. I attach an example struct to illustrate the problem.
/* Struct definitions */
typedef struct _Profiles {
int32_t param1;
int32_t param2;
int32_t param3;
int32_t param4;
int32_t param5;
int16_t param6;
int32_t param7;
uint32_t param8;
int32_t param9;
int32_t param10;
uint32_t param11;
int32_t time;
/* @@protoc_insertion_point(struct:Profiles) */
} Profiles;
The expected result would be that I can store a logged line like the following: 5 1345643 1500 (tag, time, value)
to a protocol buffer struct:
profiles.param5 = 1500
profiles.time = 1345643
without the need of 12 switch cases (in this example). Basically, how can I access the 5th declared field of a struct using an integer/enum.
Bare in mind that each field of the struct could potentially have a different type.