What seems to be the problem? I just created a simple test case that seems to work perfectly.
My proto file looks like this: <CUT HERE> message Outer { message Inner { required int32 foo = 1; } optional Inner inner = 1; } <CUT HERE> My C++ source file looks like this: <CUT HERE> #include "test.pb.h" #include <iostream> #include <google/protobuf/descriptor.h> #include <google/protobuf/message.h> using namespace google::protobuf; int main() { Outer msg; const Descriptor *outer_descriptor = msg.GetDescriptor(); const Reflection *outer_reflection = msg.GetReflection(); const FieldDescriptor *outer_field_descriptor = outer_descriptor->field(0); std::cout << "Found field " << outer_field_descriptor->name() << '\n'; std::cout << "Has field? " << outer_reflection->HasField(msg, outer_field_descriptor) << '\n'; Message *inner_msg = outer_reflection->MutableMessage(&msg, outer_field_descriptor); std::cout << "Inner message ptr is " << inner_msg << '\n'; std::cout << "Has field? " << outer_reflection->HasField(msg, outer_field_descriptor) << '\n'; std::cout << "Inner message is " << inner_msg->GetDescriptor()->name() << '\n'; std::cout << "Fields are:\n"; for (int i = 0; i < inner_msg->GetDescriptor()->field_count(); ++i) { std::cout << inner_msg->GetDescriptor()->field(i)->name() << '\n'; } return 0; } <CUT HERE> When I run it, I get this output: <CUT HERE> Found field inner Has field? 0 Inner message ptr is 0x881340 Has field? 1 Inner message is Inner Fields are: foo <CUT HERE> So obviously the inner message is getting created, and the field is getting set, and the descriptor associated with the inner Message* is the proper descriptor for the Inner message, and has the proper fields available. Chris On Mon, 10 Oct 2011 14:37:08 -0700 (PDT) EJ <j.gra...@gmail.com> wrote: > My message structure is similar to this (more complicated, but this is > the general idea) > > message MyMessage > { > > message InnerMessage > { > > message command > { > required int32 setsomething = 1; > } > > optional command SomeCommand = 1; > > } > > optional InnerMessage MessageInner = 1; > > } > > > What I want to do is be able to specify command line arguments like > this: > MessageInner SomeCommand setsomething [some number that will be the > value in setsomething field] > > I initialize the overall MyMessage successfully, then pass it to a > function, which I call recursively, that looks at each part of the > command line, looks for a field by that name, and either creates the > message or sets the field. > > But I'm having trouble. When my function sees that the field is > TYPE_MESSAGE, I want it to create that message as part of the bigger > without having to know what it is at compile time. My function takes > &somemessage as input and I get the message reflection and the field > descriptor for my field (from the command line) and then I try to > create a new mutable message from the message reflection. I checked my > code and I'm 100% sure this has to do with me not understanding how to > use protocol buffers the way I want to and is not something in my c++ > code in general. > > I guess the simple question is this: > How do I create a new message that is part of another message without > knowing what inner message I'm going to create at compile time? (I DO > know that the message field is going to be in my .proto file already > or I will send an error message) In particular, I need to know more > about message factories (can you make them relate to a specific > message?), dynamic messages, reflections? > -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to protobuf@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.