24 #include "field_codec_default_message.h"
31 void dccl::v3::DefaultMessageCodec::any_encode(Bitset* bits,
const dccl::any& wire_value)
33 if (is_empty(wire_value))
35 *bits = Bitset(min_size());
39 *bits = traverse_const_message<Encoder, Bitset>(wire_value);
42 bits->push_front(
true);
46 unsigned dccl::v3::DefaultMessageCodec::any_size(
const dccl::any& wire_value)
48 if (is_empty(wire_value))
54 unsigned size = traverse_const_message<Size, unsigned>(wire_value);
57 const unsigned presence_bit = 1;
65 void dccl::v3::DefaultMessageCodec::any_decode(Bitset* bits, dccl::any* wire_value)
69 auto* msg = dccl::any_cast<google::protobuf::Message*>(*wire_value);
73 if (!bits->to_ulong())
75 *wire_value = dccl::any();
84 const google::protobuf::Descriptor* desc = msg->GetDescriptor();
85 const google::protobuf::Reflection* refl = msg->GetReflection();
87 for (
int i = 0, n = desc->field_count(); i < n; ++i)
89 const google::protobuf::FieldDescriptor* field_desc = desc->field(i);
91 if (!check_field(field_desc))
94 std::shared_ptr<FieldCodecBase> codec = find(field_desc);
95 std::shared_ptr<internal::FromProtoCppTypeBase> helper =
96 manager().type_helper().find(field_desc);
98 if (field_desc->is_repeated())
100 std::vector<dccl::any> field_values;
101 if (field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE)
103 unsigned max_repeat =
104 field_desc->options().GetExtension(dccl::field).max_repeat();
105 for (
unsigned j = 0, m = max_repeat; j < m; ++j)
106 field_values.emplace_back(refl->AddMessage(msg, field_desc));
108 codec->field_decode_repeated(bits, &field_values, field_desc);
111 for (
int j = field_values.size(), m = max_repeat; j < m; ++j)
113 refl->RemoveLast(msg, field_desc);
119 codec->field_decode_repeated(bits, &field_values, field_desc);
120 for (
auto& field_value : field_values)
121 helper->add_value(field_desc, msg, field_value);
127 DynamicConditions& dc = dynamic_conditions(field_desc);
128 if (dc.has_omit_if())
131 dc.regenerate(this_message(), root_message());
136 dccl::any field_value;
137 if (field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE)
140 field_value = refl->MutableMessage(msg, field_desc);
141 codec->field_decode(bits, &field_value, field_desc);
142 if (is_empty(field_value))
143 refl->ClearField(msg, field_desc);
148 codec->field_decode(bits, &field_value, field_desc);
149 helper->set_value(field_desc, msg, field_value);
154 std::vector<const google::protobuf::FieldDescriptor*> set_fields;
155 refl->ListFields(*msg, &set_fields);
158 catch (dccl::bad_any_cast& e)
161 "Bad type given to traverse mutable, expecting google::protobuf::Message*, got " +
162 std::string(wire_value->type().name())));
166 unsigned dccl::v3::DefaultMessageCodec::max_size()
169 traverse_descriptor<MaxSize>(&u);
173 const unsigned presence_bit = 1;
180 unsigned dccl::v3::DefaultMessageCodec::min_size()
184 const unsigned presence_bit = 1;
190 traverse_descriptor<MinSize>(&u);
195 void dccl::v3::DefaultMessageCodec::validate()
201 if (desc->oneof_decl_count() != 0)
202 throw(Exception(
"DCCL Codec Version 3 does not support 'oneof' declarations"), desc);
204 traverse_descriptor<Validate>(&b);
207 std::string dccl::v3::DefaultMessageCodec::info()
209 std::stringstream ss;
210 traverse_descriptor<Info>(&ss);
214 std::size_t dccl::v3::DefaultMessageCodec::hash()
216 std::size_t hash = 0;
217 traverse_descriptor<Hash>(&hash);
221 bool dccl::v3::DefaultMessageCodec::check_field(
const google::protobuf::FieldDescriptor* field)
230 if (dccl_field_options.omit())
234 else if (message_data().current_part() == UNKNOWN)
236 if ((part() == HEAD && !dccl_field_options.in_head()) ||
237 (part() == BODY && dccl_field_options.in_head()))
242 else if (message_data().current_part() != part())