18#include "google/protobuf/descriptor.h"
19#include "google/protobuf/message.h"
29 Request->GetReflection()->SetInt32(
35 Request->GetReflection()->SetInt64(
41 Request->GetReflection()->SetFloat(
47 Request->GetReflection()->SetDouble(
52 const std::string value) {
53 Request->GetReflection()->SetString(
59 Request->GetReflection()->SetBool(
64 const std::vector<int> &value) {
65 auto ref =
Request->GetReflection()->MutableRepeatedField<
int>(
67 ref->Add(value.begin(), value.end());
71 const std::vector<long> &value) {
72 auto ref =
Request->GetReflection()->MutableRepeatedField<
long>(
74 ref->Add(value.begin(), value.end());
78 const std::vector<float> &value) {
79 auto ref =
Request->GetReflection()->MutableRepeatedField<
float>(
81 ref->Add(value.begin(), value.end());
85 const std::vector<double> &value) {
86 auto ref =
Request->GetReflection()->MutableRepeatedField<
double>(
88 ref->Add(value.begin(), value.end());
92 const std::vector<std::string> &value) {
93 auto reflection =
Request->GetReflection();
94 auto descriptor =
Request->GetDescriptor();
95 auto field = descriptor->FindFieldByName(name);
96 for (
auto &v : value) {
97 reflection->AddString(
Request, field, v);
102 const std::vector<bool> &value) {
103 auto ref =
Request->GetReflection()->MutableRepeatedField<
bool>(
105 ref->Add(value.begin(), value.end());
109 std::string *data =
new std::string();
110 Request->SerializeToString(data);
116 const google::protobuf::Message *value) {
117 auto reflection =
Request->GetReflection();
118 auto descriptor =
Request->GetDescriptor();
119 auto field = descriptor->FindFieldByName(name);
120 reflection->MutableMessage(
Request, field)->CopyFrom(*value);
124 const std::string &name,
125 const std::vector<google::protobuf::Message *> &value) {
127 auto reflection =
Request->GetReflection();
128 auto descriptor =
Request->GetDescriptor();
129 auto field = descriptor->FindFieldByName(name);
130 for (
auto &v : value) {
131 reflection->AddMessage(
Request, field)->CopyFrom(*v);
136 this->Request =
reinterpret_cast<Message *
>(
Request);
140 this->Response =
reinterpret_cast<Message *
>(
Response);
146 Response =
reinterpret_cast<Message *
>(data);
148 const Descriptor *descriptor =
Response->GetDescriptor();
149 const Reflection *reflection =
Response->GetReflection();
150 const FieldDescriptor *field = descriptor->field(0);
152 if (field->label() == FieldDescriptor::LABEL_REPEATED) {
153 if (field->type() == FieldDescriptor::Type::TYPE_INT32) {
154 auto &ref = reflection->GetRepeatedField<int32_t>(*
Response, field);
155 std::vector<int> *ret =
new std::vector<int>(ref.begin(), ref.end());
159 if (field->type() == FieldDescriptor::Type::TYPE_INT64) {
160 auto &ref = reflection->GetRepeatedField<int64_t>(*
Response, field);
161 std::vector<int64_t> *ret =
162 new std::vector<int64_t>(ref.begin(), ref.end());
166 if (field->type() == FieldDescriptor::Type::TYPE_FLOAT) {
167 auto ref = reflection->GetRepeatedField<
float>(*
Response, field);
168 std::vector<float> *ret =
new std::vector<float>(ref.begin(), ref.end());
172 if (field->type() == FieldDescriptor::Type::TYPE_DOUBLE) {
173 auto ref = reflection->GetRepeatedField<
double>(*
Response, field);
174 std::vector<double> *ret =
175 new std::vector<double>(ref.begin(), ref.end());
179 if (field->type() == FieldDescriptor::Type::TYPE_STRING) {
181 std::cerr <<
"vector<string> deserialization yet to be done\n";
183 std::vector<std::string> *ptr =
new std::vector<std::string>();
195 if (field->type() == FieldDescriptor::Type::TYPE_BOOL) {
196 auto ref = reflection->GetRepeatedField<
bool>(*
Response, field);
197 std::vector<bool> *ptr =
new std::vector<bool>(
198 ref.mutable_data(), ref.mutable_data() + ref.size());
203 if (field->type() == FieldDescriptor::Type::TYPE_INT32) {
204 int32_t value = reflection->GetInt32(*
Response, field);
205 int32_t *ptr =
new int32_t(value);
209 if (field->type() == FieldDescriptor::Type::TYPE_INT64) {
210 int64_t value = reflection->GetInt64(*
Response, field);
211 int64_t *ptr =
new int64_t(value);
215 if (field->type() == FieldDescriptor::Type::TYPE_FLOAT) {
216 float value = reflection->GetFloat(*
Response, field);
217 float *ptr =
new float(value);
221 if (field->type() == FieldDescriptor::Type::TYPE_DOUBLE) {
222 double value = reflection->GetDouble(*
Response, field);
223 double *ptr =
new double(value);
227 if (field->type() == FieldDescriptor::Type::TYPE_STRING) {
228 std::string value = reflection->GetString(*
Response, field);
229 std::string *ptr =
new std::string(value);
233 if (field->type() == FieldDescriptor::Type::TYPE_BOOL) {
234 bool value = reflection->GetBool(*
Response, field);
235 bool *ptr =
new bool(value);
240 std::cerr <<
"Unknown type in protobuf serializer\n";
void setFeature(const std::string &name, const google::protobuf::Message *value) override
void setResponse(void *Response) override
void setRequest(void *Request) override
void cleanDataStructures() override
void * deserializeUntyped(void *data) override
void * getSerializedData() override
Protobuf Serialization/Deserialization to support gRPC communication.