Greetings! As I can see, conversion between C structures and google protobuf classes is a problem that has been avoided for a while. I've searched a bit, and have found only one way by using ugly macro. It looks like;
#define ROT_CIP_STRUCTURE_NAME t_rot_channel_init_params #define ROT_CIP_PROTOCLASS_NAME channel_manager::RotChannelInitParams #define XFIELDS_ROTCIP \ X(unsigned char, rot_idx, "%d", ROT_CIP_STRUCTURE_NAME, ROT_CIP_PROTOCLASS_NAME) \ X(unsigned short, av_num, "%d", ROT_CIP_STRUCTURE_NAME, ROT_CIP_PROTOCLASS_NAME) \ X(unsigned long, pw_min_us, "%d", ROT_CIP_STRUCTURE_NAME, ROT_CIP_PROTOCLASS_NAME) \ X(unsigned long, period_min_us, "%d", ROT_CIP_STRUCTURE_NAME, ROT_CIP_PROTOCLASS_NAME) \ X(unsigned long, period_max_us, "%d", ROT_CIP_STRUCTURE_NAME, ROT_CIP_PROTOCLASS_NAME) typedef struct { #define X(type, name, format, stype, pclass) type name; XFIELDS_ROTCIP #undef X } ROT_CIP_STRUCTURE_NAME; And using then: int extractStructureFromClass(void* dst_structure, void* src_class, t_reflected_structures entity_type) { if ((dst_structure == NULL) || (src_class == NULL)) return -EINVAL; int ret = 0; #define X(type, name, format, stype, pclass) \ if (static_cast<pclass*>(src_class)->has_##name()) static_cast<stype*>(dst_structure)->name = static_cast<pclass*>(src_class)->name(); \ else return -ENOENT; switch (entity_type) { case TRS_CHANNEL_MANAGER_INIT_PARAMS: { XFIELDS_ROTCIP break; } default: { ret = -ERANGE; break; } } #undef X return ret; } It may fit for simple structures somehow. But what if I have nested structures? Oooook... Y(ADC_CIP_SUBSTRUCT_NAME, ADC_CIP_SUBVARIABLE_NAME, ADC_CIP_SUBCLASS_NAME, unsigned short, control1, "%d", ADC_CIP_STRUCTURE_NAME, ADC_CIP_PROTOCLASS_NAME) Ugly. And what if I have multiple nesting, arrays? It leads to tons of macro with huge routine work, and It's simple to convert each C structure manually. But it is so boring! Maybe anyone has found a better way? -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+unsubscr...@googlegroups.com. To post to this group, send email to protobuf@googlegroups.com. Visit this group at http://groups.google.com/group/protobuf. For more options, visit https://groups.google.com/d/optout.