25 #include "field_codec_default.h"
27 using namespace dccl::logger;
37 std::string s = wire_value;
38 if (s.size() > dccl_field_options().max_length())
43 this->this_field(), this->this_descriptor()));
45 dccl::dlog.
is(DEBUG2) &&
46 dccl::dlog <<
"String " << s <<
" exceeds `dccl.max_length`, truncating" << std::endl;
47 s.resize(dccl_field_options().max_length());
53 Bitset length_bits(min_size(), s.length());
55 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"DefaultStringCodec value_bits: " << value_bits
58 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"DefaultStringCodec length_bits: " << length_bits
62 for (
int i = 0, n = value_bits.size(); i < n; ++i) length_bits.push_back(value_bits[i]);
64 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"DefaultStringCodec created: " << length_bits
72 unsigned value_length = bits->
to_ulong();
76 unsigned header_length = min_size();
78 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"Length of string is = " << value_length
81 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"bits before get_more_bits " << *bits << std::endl;
86 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"bits after get_more_bits " << *bits << std::endl;
87 Bitset string_body_bits = *bits;
88 string_body_bits >>= header_length;
89 string_body_bits.resize(bits->size() - header_length);
103 return std::min(min_size() +
static_cast<unsigned>(wire_value.length() * BITS_IN_BYTE),
110 return min_size() + dccl_field_options().max_length() * BITS_IN_BYTE;
120 require(dccl_field_options().has_max_length(),
"missing (dccl.field).max_length");
126 double dccl::v3::DefaultEnumCodec::max()
128 const google::protobuf::EnumDescriptor* e = this_field()->enum_type();
130 if (dccl_field_options().packed_enum())
132 return e->value_count() - 1;
136 const google::protobuf::EnumValueDescriptor* value = e->value(0);
137 int32 maxVal = value->number();
138 for (
int i = 1; i < e->value_count(); ++i)
141 if (value->number() > maxVal)
143 maxVal = value->number();
150 double dccl::v3::DefaultEnumCodec::min()
152 if (dccl_field_options().packed_enum())
158 const google::protobuf::EnumDescriptor* e = this_field()->enum_type();
159 const google::protobuf::EnumValueDescriptor* value = e->value(0);
160 int32 minVal = value->number();
161 for (
int i = 1; i < e->value_count(); ++i)
164 if (value->number() < minVal)
166 minVal = value->number();
173 dccl::int32 dccl::v3::DefaultEnumCodec::pre_encode(
174 const google::protobuf::EnumValueDescriptor*
const& field_value)
176 if (dccl_field_options().packed_enum())
177 return field_value->index();
179 return field_value->number();
182 const google::protobuf::EnumValueDescriptor*
183 dccl::v3::DefaultEnumCodec::post_decode(
const dccl::int32& wire_value)
185 const google::protobuf::EnumDescriptor* e = this_field()->enum_type();
187 if (dccl_field_options().packed_enum())
189 if (wire_value < e->value_count())
191 const google::protobuf::EnumValueDescriptor* return_value = e->value(wire_value);
195 throw NullValueException();
199 const google::protobuf::EnumValueDescriptor* return_value =
200 e->FindValueByNumber(wire_value);
201 if (return_value !=
nullptr)
204 throw NullValueException();