25 #ifndef DCCLFIELDCODECDEFAULTMESSAGEV320140421H
26 #define DCCLFIELDCODECDEFAULTMESSAGEV320140421H
28 #include "../field_codec.h"
29 #include "../field_codec_manager.h"
31 #include "dccl/option_extensions.pb.h"
41 void any_encode(
Bitset* bits,
const dccl::any& wire_value)
override;
42 void any_decode(
Bitset* bits, dccl::any* wire_value)
override;
43 unsigned max_size()
override;
44 unsigned min_size()
override;
45 unsigned any_size(
const dccl::any& wire_value)
override;
47 std::shared_ptr<FieldCodecBase> find(
const google::protobuf::FieldDescriptor* field_desc)
49 return manager().
find(field_desc, this->codec_version(), has_codec_group(), codec_group());
54 void validate()
override;
55 std::string info()
override;
56 std::size_t hash()
override;
57 bool check_field(
const google::protobuf::FieldDescriptor* field);
61 static void repeated(std::shared_ptr<FieldCodecBase> codec,
unsigned* return_value,
62 const std::vector<dccl::any>& field_values,
63 const google::protobuf::FieldDescriptor* field_desc)
65 codec->field_size_repeated(return_value, field_values, field_desc);
68 static void single(std::shared_ptr<FieldCodecBase> codec,
unsigned* return_value,
69 const dccl::any& field_value,
70 const google::protobuf::FieldDescriptor* field_desc)
72 codec->field_size(return_value, field_value, field_desc);
78 static void repeated(std::shared_ptr<FieldCodecBase> codec,
Bitset* return_value,
79 const std::vector<dccl::any>& field_values,
80 const google::protobuf::FieldDescriptor* field_desc)
82 codec->field_encode_repeated(return_value, field_values, field_desc);
85 static void single(std::shared_ptr<FieldCodecBase> codec,
Bitset* return_value,
86 const dccl::any& field_value,
87 const google::protobuf::FieldDescriptor* field_desc)
89 codec->field_encode(return_value, field_value, field_desc);
95 static void field(std::shared_ptr<FieldCodecBase> codec,
unsigned* return_value,
96 const google::protobuf::FieldDescriptor* field_desc)
98 codec->field_max_size(return_value, field_desc);
104 static void field(std::shared_ptr<FieldCodecBase> codec,
unsigned* return_value,
105 const google::protobuf::FieldDescriptor* field_desc)
111 if (dc.has_omit_if() || dc.has_required_if())
114 codec->field_min_size(return_value, field_desc);
120 static void field(std::shared_ptr<FieldCodecBase> codec,
bool* return_value,
121 const google::protobuf::FieldDescriptor* field_desc)
123 codec->field_validate(return_value, field_desc);
129 static void field(std::shared_ptr<FieldCodecBase> codec, std::stringstream* return_value,
130 const google::protobuf::FieldDescriptor* field_desc)
132 codec->field_info(return_value, field_desc);
138 static void field(std::shared_ptr<FieldCodecBase> codec, std::size_t* return_value,
139 const google::protobuf::FieldDescriptor* field_desc)
141 codec->field_hash(return_value, field_desc);
145 template <
typename Action,
typename ReturnType>
146 void traverse_descriptor(ReturnType* return_value)
150 for (
int i = 0, n = desc->field_count(); i < n; ++i)
152 const google::protobuf::FieldDescriptor* field_desc = desc->field(i);
154 if (!check_field(field_desc))
157 Action::field(find(field_desc), return_value, field_desc);
161 template <
typename Action,
typename ReturnType>
162 ReturnType traverse_const_message(
const dccl::any& wire_value)
166 ReturnType return_value = ReturnType();
168 const auto* msg = dccl::any_cast<const google::protobuf::Message*>(wire_value);
169 const google::protobuf::Descriptor* desc = msg->GetDescriptor();
170 const google::protobuf::Reflection* refl = msg->GetReflection();
172 for (
int i = 0, n = desc->field_count(); i < n; ++i)
174 const google::protobuf::FieldDescriptor* field_desc = desc->field(i);
176 if (!check_field(field_desc))
179 std::shared_ptr<FieldCodecBase> codec = find(field_desc);
180 std::shared_ptr<internal::FromProtoCppTypeBase> helper =
181 manager().type_helper().find(field_desc);
183 if (field_desc->is_repeated())
185 std::vector<dccl::any> field_values;
186 for (
int j = 0, m = refl->FieldSize(*msg, field_desc); j < m; ++j)
187 field_values.push_back(helper->get_repeated_value(field_desc, *msg, j));
189 Action::repeated(codec, &return_value, field_values, field_desc);
195 if (dc.has_omit_if())
198 dc.regenerate(this_message(), root_message());
203 Action::single(codec, &return_value, helper->get_value(field_desc, *msg),
209 catch (dccl::bad_any_cast& e)
211 throw(
Exception(
"Bad type given to traverse const, expecting const "
212 "google::protobuf::Message*, got " +
213 std::string(wire_value.type().name())));