25 #ifndef DCCLFIELDCODECDEFAULTMESSAGE20110510H
26 #define DCCLFIELDCODECDEFAULTMESSAGE20110510H
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());
52 void validate()
override;
53 std::string info()
override;
54 std::size_t hash()
override;
55 bool check_field(
const google::protobuf::FieldDescriptor* field);
59 static void repeated(std::shared_ptr<FieldCodecBase> codec,
unsigned* return_value,
60 const std::vector<dccl::any>& field_values,
61 const google::protobuf::FieldDescriptor* field_desc)
63 codec->field_size_repeated(return_value, field_values, field_desc);
66 static void single(std::shared_ptr<FieldCodecBase> codec,
unsigned* return_value,
67 const dccl::any& field_value,
68 const google::protobuf::FieldDescriptor* field_desc)
70 codec->field_size(return_value, field_value, field_desc);
76 static void repeated(std::shared_ptr<FieldCodecBase> codec,
Bitset* return_value,
77 const std::vector<dccl::any>& field_values,
78 const google::protobuf::FieldDescriptor* field_desc)
80 codec->field_encode_repeated(return_value, field_values, field_desc);
83 static void single(std::shared_ptr<FieldCodecBase> codec,
Bitset* return_value,
84 const dccl::any& field_value,
85 const google::protobuf::FieldDescriptor* field_desc)
87 codec->field_encode(return_value, field_value, field_desc);
93 static void field(std::shared_ptr<FieldCodecBase> codec,
unsigned* return_value,
94 const google::protobuf::FieldDescriptor* field_desc)
96 codec->field_max_size(return_value, field_desc);
102 static void field(std::shared_ptr<FieldCodecBase> codec,
unsigned* return_value,
103 const google::protobuf::FieldDescriptor* field_desc)
105 codec->field_min_size(return_value, field_desc);
111 static void field(std::shared_ptr<FieldCodecBase> codec,
bool* return_value,
112 const google::protobuf::FieldDescriptor* field_desc)
114 codec->field_validate(return_value, field_desc);
120 static void field(std::shared_ptr<FieldCodecBase> codec, std::stringstream* return_value,
121 const google::protobuf::FieldDescriptor* field_desc)
123 codec->field_info(return_value, field_desc);
129 static void field(std::shared_ptr<FieldCodecBase> codec, std::size_t* return_value,
130 const google::protobuf::FieldDescriptor* field_desc)
132 codec->field_hash(return_value, field_desc);
136 template <
typename Action,
typename ReturnType>
137 void traverse_descriptor(ReturnType* return_value)
140 for (
int i = 0, n = desc->field_count(); i < n; ++i)
142 const google::protobuf::FieldDescriptor* field_desc = desc->field(i);
144 if (!check_field(field_desc))
147 Action::field(find(field_desc), return_value, field_desc);
151 template <
typename Action,
typename ReturnType>
152 ReturnType traverse_const_message(
const dccl::any& wire_value)
156 ReturnType return_value = ReturnType();
158 const auto* msg = dccl::any_cast<const google::protobuf::Message*>(wire_value);
159 const google::protobuf::Descriptor* desc = msg->GetDescriptor();
160 const google::protobuf::Reflection* refl = msg->GetReflection();
161 for (
int i = 0, n = desc->field_count(); i < n; ++i)
163 const google::protobuf::FieldDescriptor* field_desc = desc->field(i);
165 if (!check_field(field_desc))
168 std::shared_ptr<FieldCodecBase> codec = find(field_desc);
169 std::shared_ptr<internal::FromProtoCppTypeBase> helper =
170 manager().type_helper().find(field_desc);
172 if (field_desc->is_repeated())
174 std::vector<dccl::any> field_values;
175 for (
int j = 0, m = refl->FieldSize(*msg, field_desc); j < m; ++j)
176 field_values.push_back(helper->get_repeated_value(field_desc, *msg, j));
178 Action::repeated(codec, &return_value, field_values, field_desc);
182 Action::single(codec, &return_value, helper->get_value(field_desc, *msg),
188 catch (dccl::bad_any_cast& e)
190 throw(
Exception(
"Bad type given to traverse const, expecting const "
191 "google::protobuf::Message*, got " +
192 std::string(wire_value.type().name())));