using google::protobuf;
class MyErrorCollector : public protobuf::compiler::MultiFileErrorCollector {
// Implementation of MultiFileErrorCollector interface.
// Should probably just print errors to stderr.
};
// Set up the importer.
protobuf::compiler::DiskSourceTree source_tree;
source_tree.MapPath("", "path/to/src");
MyErrorCollector error_collector;
protobuf::compiler::Importer importer(&source_tree, &error_collector);
// Parse "path/to/src/foo/bar/baz.proto" and get the type "MyMessageType"
// defined within it.
const protobuf::FileDescriptor* file = importer.Import("foo/bar/baz.proto");
if (file == NULL) {
// Error should have been reported to error_collector.
return false;
}
const protobuf::Descriptor* type = file->FindMessageTypeByName("MyMessageType");
if (type == NULL) {
cerr << "No such type: MyMessageType" << endl;
return false;
}
// Get message prototype.
protobuf::DynamicMessageFactory factory;
const protobuf::Message* prototype = factory.GetPrototype(type);
// Make a message.
protobuf::Message* message = prototype->New();
// Let the user edit the message. Uses reflection interface.
// See text_format.cc for an example of using reflection.
RunEditorUI(message);
// Serialize and write.
std::string data = message->SerializeAsString();
WriteData(data);