diff options
author | Peter Mitrano <mitranopeter@gmail.com> | 2017-09-21 13:23:30 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2017-10-02 12:08:24 (GMT) |
commit | 1299f4cc5ee5a996b051f7767049e239092a65a0 (patch) | |
tree | d3c873f8ec327bc1d094c668a8debb555712ba8f /Tests/FindProtobuf/Test/main-desc.cxx | |
parent | 4e91be95323fa869a82ea59e733a706a5fd3867b (diff) | |
download | CMake-1299f4cc5ee5a996b051f7767049e239092a65a0.zip CMake-1299f4cc5ee5a996b051f7767049e239092a65a0.tar.gz CMake-1299f4cc5ee5a996b051f7767049e239092a65a0.tar.bz2 |
FindProtobuf: add flag to allow descriptor files to be generated
- The .desc files will be in the same folder as the generated .cc and .h files.
- Paths to generate .desc files are stored in a variable passed in
- This is only implemented for C++
- Remove legacy ARGS
- Add test that generates and uses C++ protobuf message
- Add test that checks that the generated .desc file can be instantiated
with DynamicMessageFactory
- Add Help rst for new feature
Diffstat (limited to 'Tests/FindProtobuf/Test/main-desc.cxx')
-rw-r--r-- | Tests/FindProtobuf/Test/main-desc.cxx | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/Tests/FindProtobuf/Test/main-desc.cxx b/Tests/FindProtobuf/Test/main-desc.cxx new file mode 100644 index 0000000..a26e562 --- /dev/null +++ b/Tests/FindProtobuf/Test/main-desc.cxx @@ -0,0 +1,57 @@ +#include <fstream> +#include <google/protobuf/descriptor.h> +#include <google/protobuf/descriptor.pb.h> +#include <google/protobuf/dynamic_message.h> +#include <google/protobuf/text_format.h> +#include <iostream> +#include <string> + +int main(int argc, char* argv[]) +{ + std::ifstream fs; + fs.open(argv[1], std::ifstream::in); + google::protobuf::FileDescriptorSet file_descriptor_set; + file_descriptor_set.ParseFromIstream(&fs); + + const google::protobuf::DescriptorPool* compiled_pool = + google::protobuf::DescriptorPool::generated_pool(); + + if (compiled_pool == NULL) { + std::cerr << "compiled pool is NULL." << std::endl; + return 1; + } + + google::protobuf::DescriptorPool pool(compiled_pool); + google::protobuf::DynamicMessageFactory dynamic_message_factory(&pool); + + for (const google::protobuf::FileDescriptorProto& file_descriptor_proto : + file_descriptor_set.file()) { + const google::protobuf::FileDescriptor* file_descriptor = + pool.BuildFile(file_descriptor_proto); + if (file_descriptor == NULL) { + continue; + } + + const google::protobuf::Descriptor* descriptor = + pool.FindMessageTypeByName("example.msgs.ExampleDesc"); + + if (descriptor == NULL) { + continue; + } + + google::protobuf::Message* msg = + dynamic_message_factory.GetPrototype(descriptor)->New(); + std::string data = "data: 1"; + bool success = google::protobuf::TextFormat::ParseFromString(data, msg); + + if (success) { + return 0; + } else { + std::cerr << "Failed to parse message." << std::endl; + return 2; + } + } + + std::cerr << "No matching message found." << std::endl; + return 3; +} |