1 // Mock of the play store functionality that creates a fidelity parameter set
2 // for an experiment.
3 // Assume we don't include the user's proto, but do have access to the file at
4 // runtime
5
6 #include "dynamicproto.h"
7
8 #include <iostream>
9
10 using namespace google::protobuf;
11
usage()12 void usage() {
13 std::cerr << "Usage: play <protofile>" << std::endl;
14 }
15
main(int argc,char * argv[])16 int main(int argc, char *argv[]) {
17 GOOGLE_PROTOBUF_VERIFY_VERSION;
18
19 if(argc<2) {
20 usage();
21 return -1;
22 }
23 dynamicproto::init({".", "proto"});
24 auto descs = dynamicproto::fileDescriptors(argv[1]);
25
26 if(descs.size()<2) {
27 std::cerr << "The proto file has errors" << std::endl;
28 return -2;
29 }
30
31 std::vector<const FieldDescriptor*> exts;
32 const std::string FIDELITYPARAMS = "com.google.tuningfork.FidelityParams";
33 dynamicproto::extensionsOf(descs.back(), FIDELITYPARAMS, exts);
34 std::cerr << "Found " << exts.size() << " extensions of " << FIDELITYPARAMS
35 << " in " << descs.back()->name() << " and its dependencies"
36 << std::endl;
37
38 auto m = dynamicproto::newMessage(descs.front(), "FidelityParams");
39 if(m) {
40 const Reflection* r = m->GetReflection();
41 if(r) {
42 std::cerr << "Enter values for FidelityParams:\n";
43 for(auto e: exts) {
44 std::cerr << "Name: " << e->name() << " type: ";
45 switch(e->type()) {
46 case FieldDescriptor::TYPE_ENUM:
47 {
48 dynamicproto::print(e->enum_type(), std::cerr);
49 std::cerr << "\n";
50 int val;
51 std::cin >> val;
52 r->SetEnumValue(m, e, val);
53 }
54 break;
55 case FieldDescriptor::TYPE_INT32:
56 std::cerr << e->type_name() << "\n";
57 int val;
58 std::cin >> val;
59 r->SetInt32(m, e, val);
60 break;
61 default:
62 std::cerr << e->type_name() << " *ERROR* not supported\n";
63 }
64 }
65
66 m->SerializeToOstream(&std::cout);
67 }
68 }
69
70 // This would be something similar if we had the user's header at compile time
71 // FidelityParams p;
72 // p.SetExtension(lod, (::LOD)((rand()%4)+1));
73 // p.SerializeToOstream(&std::cout);
74 }
75