24 #ifndef DCCLPROTOBUFCPPTYPEHELPERS20110323H
25 #define DCCLPROTOBUFCPPTYPEHELPERS20110323H
29 #include <google/protobuf/descriptor.h>
30 #include <google/protobuf/descriptor.pb.h>
31 #include <google/protobuf/message.h>
43 virtual std::string
as_str() {
return "TYPE_UNKNOWN"; }
51 return google::protobuf::FieldDescriptorProto::Type_Name(
52 static_cast<google::protobuf::FieldDescriptorProto::Type
>(t));
63 virtual std::string
as_str() {
return "CPPTYPE_UNKNOWN"; }
70 dccl::any
get_value(
const google::protobuf::FieldDescriptor* field,
73 const google::protobuf::Reflection* refl = msg.GetReflection();
74 if (!refl->HasField(msg, field))
77 return _get_value(field, msg);
92 return _get_repeated_value(field, msg, index);
106 _set_value(field, msg, value);
115 _set_value(
nullptr, msg, value);
128 _add_value(field, msg, value);
131 virtual void _set_value(
const google::protobuf::FieldDescriptor* ,
137 virtual void _add_value(
const google::protobuf::FieldDescriptor* ,
143 virtual dccl::any _get_repeated_value(
const google::protobuf::FieldDescriptor* ,
149 virtual dccl::any _get_value(
const google::protobuf::FieldDescriptor* ,
165 std::string
as_str()
override {
return "CPPTYPE_DOUBLE"; }
168 dccl::any _get_value(
const google::protobuf::FieldDescriptor* field,
171 return msg.GetReflection()->GetDouble(msg, field);
173 dccl::any _get_repeated_value(
const google::protobuf::FieldDescriptor* field,
176 return msg.GetReflection()->GetRepeatedDouble(msg, field, index);
179 dccl::any value)
override
181 msg->GetReflection()->SetDouble(msg, field, dccl::any_cast<type>(value));
184 dccl::any value)
override
186 msg->GetReflection()->AddDouble(msg, field, dccl::any_cast<type>(value));
196 std::string
as_str()
override {
return "CPPTYPE_FLOAT"; }
199 dccl::any _get_value(
const google::protobuf::FieldDescriptor* field,
202 return msg.GetReflection()->GetFloat(msg, field);
204 dccl::any _get_repeated_value(
const google::protobuf::FieldDescriptor* field,
207 return msg.GetReflection()->GetRepeatedFloat(msg, field, index);
210 dccl::any value)
override
212 msg->GetReflection()->SetFloat(msg, field, dccl::any_cast<type>(value));
215 dccl::any value)
override
217 msg->GetReflection()->AddFloat(msg, field, dccl::any_cast<type>(value));
225 using type = google::protobuf::int32;
226 std::string
as_str()
override {
return "CPPTYPE_INT32"; }
229 dccl::any _get_value(
const google::protobuf::FieldDescriptor* field,
232 return msg.GetReflection()->GetInt32(msg, field);
234 dccl::any _get_repeated_value(
const google::protobuf::FieldDescriptor* field,
237 return msg.GetReflection()->GetRepeatedInt32(msg, field, index);
240 dccl::any value)
override
242 msg->GetReflection()->SetInt32(msg, field, dccl::any_cast<type>(value));
245 dccl::any value)
override
247 msg->GetReflection()->AddInt32(msg, field, dccl::any_cast<type>(value));
255 using type = google::protobuf::int64;
256 std::string
as_str()
override {
return "CPPTYPE_INT64"; }
259 dccl::any _get_value(
const google::protobuf::FieldDescriptor* field,
262 return msg.GetReflection()->GetInt64(msg, field);
264 dccl::any _get_repeated_value(
const google::protobuf::FieldDescriptor* field,
267 return msg.GetReflection()->GetRepeatedInt64(msg, field, index);
270 dccl::any value)
override
272 msg->GetReflection()->SetInt64(msg, field, dccl::any_cast<type>(value));
275 dccl::any value)
override
277 msg->GetReflection()->AddInt64(msg, field, dccl::any_cast<type>(value));
285 using type = google::protobuf::uint32;
286 std::string
as_str()
override {
return "CPPTYPE_UINT32"; }
289 dccl::any _get_value(
const google::protobuf::FieldDescriptor* field,
292 return msg.GetReflection()->GetUInt32(msg, field);
294 dccl::any _get_repeated_value(
const google::protobuf::FieldDescriptor* field,
297 return msg.GetReflection()->GetRepeatedUInt32(msg, field, index);
300 dccl::any value)
override
302 msg->GetReflection()->SetUInt32(msg, field, dccl::any_cast<type>(value));
305 dccl::any value)
override
307 msg->GetReflection()->AddUInt32(msg, field, dccl::any_cast<type>(value));
315 using type = google::protobuf::uint64;
317 std::string
as_str()
override {
return "CPPTYPE_UINT64"; }
320 dccl::any _get_value(
const google::protobuf::FieldDescriptor* field,
323 return msg.GetReflection()->GetUInt64(msg, field);
325 dccl::any _get_repeated_value(
const google::protobuf::FieldDescriptor* field,
328 return msg.GetReflection()->GetRepeatedUInt64(msg, field, index);
331 dccl::any value)
override
333 msg->GetReflection()->SetUInt64(msg, field, dccl::any_cast<type>(value));
336 dccl::any value)
override
338 msg->GetReflection()->AddUInt64(msg, field, dccl::any_cast<type>(value));
347 std::string
as_str()
override {
return "CPPTYPE_BOOL"; }
350 dccl::any _get_value(
const google::protobuf::FieldDescriptor* field,
353 return msg.GetReflection()->GetBool(msg, field);
355 dccl::any _get_repeated_value(
const google::protobuf::FieldDescriptor* field,
358 return msg.GetReflection()->GetRepeatedBool(msg, field, index);
361 dccl::any value)
override
363 msg->GetReflection()->SetBool(msg, field, dccl::any_cast<type>(value));
366 dccl::any value)
override
368 msg->GetReflection()->AddBool(msg, field, dccl::any_cast<type>(value));
376 using type = std::string;
377 std::string
as_str()
override {
return "CPPTYPE_STRING"; }
380 dccl::any _get_value(
const google::protobuf::FieldDescriptor* field,
383 return msg.GetReflection()->GetString(msg, field);
385 dccl::any _get_repeated_value(
const google::protobuf::FieldDescriptor* field,
388 return msg.GetReflection()->GetRepeatedString(msg, field, index);
391 dccl::any value)
override
393 msg->GetReflection()->SetString(msg, field, dccl::any_cast<type>(value));
396 dccl::any value)
override
398 msg->GetReflection()->AddString(msg, field, dccl::any_cast<type>(value));
407 using const_type =
const google::protobuf::EnumValueDescriptor*;
408 using mutable_type = google::protobuf::EnumValueDescriptor*;
410 std::string
as_str()
override {
return "CPPTYPE_ENUM"; }
413 dccl::any _get_value(
const google::protobuf::FieldDescriptor* field,
416 return msg.GetReflection()->GetEnum(msg, field);
418 dccl::any _get_repeated_value(
const google::protobuf::FieldDescriptor* field,
421 return msg.GetReflection()->GetRepeatedEnum(msg, field, index);
424 dccl::any value)
override
426 msg->GetReflection()->SetEnum(msg, field, dccl::any_cast<const_type>(value));
429 dccl::any value)
override
431 msg->GetReflection()->AddEnum(msg, field, dccl::any_cast<const_type>(value));
443 std::string
as_str()
override {
return "CPPTYPE_MESSAGE"; }
446 dccl::any _get_value(
const google::protobuf::FieldDescriptor* field,
450 return &(msg.GetReflection()->GetMessage(msg, field));
455 dccl::any _get_repeated_value(
const google::protobuf::FieldDescriptor* field,
458 return &(msg.GetReflection()->GetRepeatedMessage(msg, field, index));
462 dccl::any value)
override
466 auto p = dccl::any_cast<const_type>(value);
469 msg->GetReflection()->MutableMessage(msg, field)->MergeFrom(*p);
473 catch (dccl::bad_any_cast& e)
475 auto p = dccl::any_cast<mutable_type>(value);
477 msg->GetReflection()->MutableMessage(msg, field)->MergeFrom(*p);
483 dccl::any value)
override
487 auto p = dccl::any_cast<const_type>(value);
488 msg->GetReflection()->AddMessage(msg, field)->MergeFrom(*p);
490 catch (dccl::bad_any_cast& e)
492 auto p = dccl::any_cast<mutable_type>(value);
493 msg->GetReflection()->AddMessage(msg, field)->MergeFrom(*p);
499 template <
typename CustomMessage>
501 :
public FromProtoCppType<google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE>
504 using type = CustomMessage;
509 dccl::any _get_value(
const google::protobuf::FieldDescriptor* field,
512 auto p = dccl::any_cast<Parent::const_type>(Parent::_get_value(field, msg));
518 dccl::any _get_repeated_value(
const google::protobuf::FieldDescriptor* field,
521 auto p = dccl::any_cast<Parent::const_type>(Parent::_get_repeated_value(field, msg, index));
528 dccl::any value)
override
530 type v = dccl::any_cast<type>(value);
531 Parent::const_type p = &v;
532 Parent::_set_value(field, msg, p);
535 dccl::any value)
override
537 type v = dccl::any_cast<type>(value);
538 Parent::const_type p = &v;
539 Parent::_add_value(field, msg, p);
549 static constexpr google::protobuf::FieldDescriptor::CppType as_enum()
551 return google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE;
557 static constexpr google::protobuf::FieldDescriptor::CppType as_enum()
559 return google::protobuf::FieldDescriptor::CPPTYPE_FLOAT;
565 static constexpr google::protobuf::FieldDescriptor::CppType as_enum()
567 return google::protobuf::FieldDescriptor::CPPTYPE_INT32;
573 static constexpr google::protobuf::FieldDescriptor::CppType as_enum()
575 return google::protobuf::FieldDescriptor::CPPTYPE_UINT32;
581 static constexpr google::protobuf::FieldDescriptor::CppType as_enum()
583 return google::protobuf::FieldDescriptor::CPPTYPE_INT64;
589 static constexpr google::protobuf::FieldDescriptor::CppType as_enum()
591 return google::protobuf::FieldDescriptor::CPPTYPE_UINT64;
597 static constexpr google::protobuf::FieldDescriptor::CppType as_enum()
599 return google::protobuf::FieldDescriptor::CPPTYPE_STRING;
605 static constexpr google::protobuf::FieldDescriptor::CppType as_enum()
607 return google::protobuf::FieldDescriptor::CPPTYPE_ENUM;
614 static constexpr google::protobuf::FieldDescriptor::CppType as_enum()
616 return google::protobuf::FieldDescriptor::CPPTYPE_BOOL;
623 static constexpr google::protobuf::FieldDescriptor::CppType as_enum()
625 return google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE;