protoc-c: add use_oneof_field_name option

Allows for generic oneof offset lookups based on the name of the oneof,
instead of having to know the field name. All oneof fields should have
the same offset, since they are members of a union.

Fixes #204
parent dfb6e300
......@@ -226,6 +226,23 @@ t_version_version_SOURCES = \
t_version_version_LDADD = \
protobuf-c/libprotobuf-c.la
# Issue #204
check_PROGRAMS += \
t/issue204/issue204
TESTS += \
t/issue204/issue204
t_issue204_issue204_SOURCES = \
t/issue204/issue204.c \
t/issue204/issue204.pb-c.c
t_issue204_issue204_LDADD = \
protobuf-c/libprotobuf-c.la
t/issue204/issue204.pb-c.c t/issue204/issue204.pb-c.h: $(top_builddir)/protoc-c/protoc-gen-c$(EXEEXT) $(top_srcdir)/t/issue204/issue204.proto
$(AM_V_GEN)@PROTOC@ --plugin=protoc-gen-c=$(top_builddir)/protoc-c/protoc-gen-c$(EXEEXT) -I$(top_srcdir) --c_out=$(top_builddir) $(top_srcdir)/t/issue204/issue204.proto
BUILT_SOURCES += \
t/issue204/issue204.pb-c.c t/issue204/issue204.pb-c.h
EXTRA_DIST += \
t/issue204/issue204.proto
# Issue #220
check_PROGRAMS += \
t/issue220/issue220
......
......@@ -48,6 +48,10 @@ message ProtobufCFileOptions {
// Use const char * instead of char * for string fields
optional bool const_strings = 4 [default = false];
// For oneof fields, set ProtobufCFieldDescriptor name field to the
// name of the containing oneof, instead of the field name
optional bool use_oneof_field_name = 5 [default = false];
}
extend google.protobuf.FileOptions {
......
......@@ -107,13 +107,17 @@ void FieldGenerator::GenerateDescriptorInitializerGeneric(io::Printer* printer,
const std::string &descriptor_addr) const
{
std::map<std::string, std::string> variables;
const OneofDescriptor *oneof = descriptor_->containing_oneof();
const ProtobufCFileOptions opt = descriptor_->file()->options().GetExtension(pb_c_file);
variables["TYPE"] = type_macro;
variables["classname"] = FullNameToC(FieldScope(descriptor_)->full_name());
variables["name"] = FieldName(descriptor_);
if (opt.use_oneof_field_name())
variables["proto_name"] = oneof->name();
else
variables["proto_name"] = descriptor_->name();
variables["descriptor_addr"] = descriptor_addr;
variables["value"] = SimpleItoa(descriptor_->number());
const OneofDescriptor *oneof = descriptor_->containing_oneof();
if (oneof != NULL)
variables["oneofname"] = FullNameToLower(oneof->name());
......
#include <stdlib.h>
#include "t/issue251/issue251.pb-c.h"
int main(void)
{
TwoOneofs msg = TWO_ONEOFS__INIT;
const ProtobufCFieldDescriptor *field;
unsigned off1, off2, off_name;
field = protobuf_c_message_descriptor_get_field_by_name(
msg.base.descriptor,
"first_oneof");
assert (field);
off_name = field->offset;
field = protobuf_c_message_descriptor_get_field(
msg.base.descriptor,
10);
assert (field);
off1 = field->offset;
field = protobuf_c_message_descriptor_get_field(
msg.base.descriptor,
11);
assert (field);
off2 = field->offset;
assert (off_name == off1);
assert (off1 == off2);
field = protobuf_c_message_descriptor_get_field_by_name(
msg.base.descriptor,
"second_oneof");
assert (field);
off_name = field->offset;
field = protobuf_c_message_descriptor_get_field(
msg.base.descriptor,
20);
assert (field);
off1 = field->offset;
field = protobuf_c_message_descriptor_get_field(
msg.base.descriptor,
21);
assert (field);
off2 = field->offset;
assert (off_name == off1);
assert (off1 == off2);
return EXIT_SUCCESS;
}
import "protobuf-c/protobuf-c.proto";
option (pb_c_file).use_oneof_field_name = true;
message two_oneofs {
oneof first_oneof {
bool a = 10;
bool b = 11;
}
oneof second_oneof {
bool c = 20;
bool d = 21;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment