24 #include "field_codec_default_message.h"
31 void dccl::v2::DefaultMessageCodec::any_encode(Bitset* bits,
const dccl::any& wire_value)
33 if (is_empty(wire_value))
34 *bits = Bitset(min_size());
36 *bits = traverse_const_message<Encoder, Bitset>(wire_value);
39 unsigned dccl::v2::DefaultMessageCodec::any_size(
const dccl::any& wire_value)
41 if (is_empty(wire_value))
44 return traverse_const_message<Size, unsigned>(wire_value);
47 void dccl::v2::DefaultMessageCodec::any_decode(Bitset* bits, dccl::any* wire_value)
51 auto* msg = dccl::any_cast<google::protobuf::Message*>(*wire_value);
53 const google::protobuf::Descriptor* desc = msg->GetDescriptor();
54 const google::protobuf::Reflection* refl = msg->GetReflection();
56 for (
int i = 0, n = desc->field_count(); i < n; ++i)
58 const google::protobuf::FieldDescriptor* field_desc = desc->field(i);
60 if (!check_field(field_desc))
63 std::shared_ptr<FieldCodecBase> codec = find(field_desc);
64 std::shared_ptr<internal::FromProtoCppTypeBase> helper =
65 manager().type_helper().find(field_desc);
67 if (field_desc->is_repeated())
69 std::vector<dccl::any> wire_values;
70 if (field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE)
73 m = field_desc->options().GetExtension(dccl::field).max_repeat();
75 wire_values.emplace_back(refl->AddMessage(msg, field_desc));
77 codec->field_decode_repeated(bits, &wire_values, field_desc);
79 for (
auto& wire_value : wire_values)
81 if (is_empty(wire_value))
82 refl->RemoveLast(msg, field_desc);
88 codec->field_decode_repeated(bits, &wire_values, field_desc);
89 for (
auto& wire_value : wire_values)
90 helper->add_value(field_desc, msg, wire_value);
96 if (field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE)
99 wire_value = refl->MutableMessage(msg, field_desc);
100 codec->field_decode(bits, &wire_value, field_desc);
101 if (is_empty(wire_value))
102 refl->ClearField(msg, field_desc);
107 codec->field_decode(bits, &wire_value, field_desc);
108 helper->set_value(field_desc, msg, wire_value);
113 std::vector<const google::protobuf::FieldDescriptor*> set_fields;
114 refl->ListFields(*msg, &set_fields);
115 if (set_fields.empty() && this_field())
116 *wire_value = dccl::any();
120 catch (dccl::bad_any_cast& e)
123 "Bad type given to traverse mutable, expecting google::protobuf::Message*, got " +
124 std::string(wire_value->type().name())));
128 unsigned dccl::v2::DefaultMessageCodec::max_size()
131 traverse_descriptor<MaxSize>(&u);
135 unsigned dccl::v2::DefaultMessageCodec::min_size()
138 traverse_descriptor<MinSize>(&u);
142 void dccl::v2::DefaultMessageCodec::validate()
148 if (desc->oneof_decl_count() != 0)
149 throw(Exception(
"DCCL Codec Version 2 does not support 'oneof' declarations"), desc);
151 traverse_descriptor<Validate>(&b);
154 std::string dccl::v2::DefaultMessageCodec::info()
156 std::stringstream ss;
157 traverse_descriptor<Info>(&ss);
161 std::size_t dccl::v2::DefaultMessageCodec::hash()
163 std::size_t hash = 0;
164 traverse_descriptor<Hash>(&hash);
168 bool dccl::v2::DefaultMessageCodec::check_field(
const google::protobuf::FieldDescriptor* field)
177 if (dccl_field_options.omit())
181 else if (message_data().current_part() == UNKNOWN)
183 if (field->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE &&
184 find(field)->name() ==
185 Codec::default_codec_name())
187 else if ((part() == HEAD && !dccl_field_options.in_head()) ||
188 (part() == BODY && dccl_field_options.in_head()))
193 else if (message_data().current_part() != part())