Conversion between C structs and google protobufs

1,131 views
Skip to first unread message

Konstantin Utkin

unread,
Oct 8, 2014, 1:51:15 AM10/8/14
to prot...@googlegroups.com
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?

Oliver Jowett

unread,
Oct 8, 2014, 6:28:46 PM10/8/14
to Konstantin Utkin, Protocol Buffers
On 8 October 2014 06:51, Konstantin Utkin
<eternalego...@gmail.com> wrote:

> 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?

If the mapping between your C structures and the messages is regular,
and you have lots of messages to map, write something to read the
message descriptor and generate the glue code for you?

If you don't have a regular mapping there's no particularly easy way -
you might need to write a little DSL to describe the mapping.

Oliver
Reply all
Reply to author
Forward
0 new messages