25#include "field_codec_default.h"
27using 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());
50 if (s.size() < dccl_field_options().min_length())
55 this->this_field(), this->this_descriptor()));
57 dccl::dlog.
is(DEBUG2) &&
58 dccl::dlog <<
"String " << s <<
" is shorter than `dccl.min_length`, padding"
60 s.resize(dccl_field_options().min_length(),
'\0');
66 Bitset length_bits(min_size(), s.length());
68 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"DefaultStringCodec value_bits: " << value_bits
71 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"DefaultStringCodec length_bits: " << length_bits
75 for (
int i = 0, n = value_bits.size(); i < n; ++i) length_bits.push_back(value_bits[i]);
77 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"DefaultStringCodec created: " << length_bits
85 unsigned value_length = bits->
to_ulong();
89 unsigned header_length = min_size();
91 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"Length of string is = " << value_length
94 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"bits before get_more_bits " << *bits << std::endl;
99 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"bits after get_more_bits " << *bits << std::endl;
100 Bitset string_body_bits = *bits;
101 string_body_bits >>= header_length;
102 string_body_bits.resize(bits->size() - header_length);
116 return std::min(min_size() +
static_cast<unsigned>(wire_value.length() * BITS_IN_BYTE),
123 return min_size() + dccl_field_options().max_length() * BITS_IN_BYTE;
133 require(dccl_field_options().has_max_length(),
"missing (dccl.field).max_length");
134 require(dccl_field_options().max_length() >= dccl_field_options().min_length(),
135 "(dccl.field).max_length must be >= (dccl.field).min_length");
141double dccl::v3::DefaultEnumCodec::max()
143 const google::protobuf::EnumDescriptor* e = this_field()->enum_type();
145 if (dccl_field_options().packed_enum())
147 return e->value_count() - 1;
151 const google::protobuf::EnumValueDescriptor* value = e->value(0);
152 int32 maxVal = value->number();
153 for (
int i = 1; i < e->value_count(); ++i)
156 if (value->number() > maxVal)
158 maxVal = value->number();
165double dccl::v3::DefaultEnumCodec::min()
167 if (dccl_field_options().packed_enum())
173 const google::protobuf::EnumDescriptor* e = this_field()->enum_type();
174 const google::protobuf::EnumValueDescriptor* value = e->value(0);
175 int32 minVal = value->number();
176 for (
int i = 1; i < e->value_count(); ++i)
179 if (value->number() < minVal)
181 minVal = value->number();
189 const google::protobuf::EnumValueDescriptor*
const& field_value)
191 if (dccl_field_options().packed_enum())
192 return field_value->index();
194 return field_value->number();
197const google::protobuf::EnumValueDescriptor*
198dccl::v3::DefaultEnumCodec::post_decode(
const dccl::int32& wire_value)
200 const google::protobuf::EnumDescriptor* e = this_field()->enum_type();
202 if (dccl_field_options().packed_enum())
204 if (wire_value < e->value_count())
206 const google::protobuf::EnumValueDescriptor* return_value = e->value(wire_value);
214 const google::protobuf::EnumValueDescriptor* return_value =
215 e->FindValueByNumber(wire_value);
216 if (return_value !=
nullptr)
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
void get_more_bits(size_type num_bits)
Retrieve more bits from the parent Bitset.
unsigned long to_ulong() const
Returns the value of the Bitset as an unsigned long integer. Equivalent to to<unsigned long>().
void from_byte_string(const std::string &s)
Sets the value of the Bitset to the contents of a byte string, where each character represents 8 bits...
std::string to_byte_string()
Returns the value of the Bitset to a byte string, where each character represents 8 bits of the Bitse...
const google::protobuf::FieldDescriptor * this_field() const
Returns the FieldDescriptor (field schema meta-data) for this field.
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
Exception used to signal null (non-existent) value within field codecs during decode.
unsigned max_size() override
Calculate maximum size of the field in bits.
unsigned min_size() override
Calculate minimum size of the field in bits.
std::string decode(Bitset *bits) override
Decode a field. If the field is empty (i.e. was encoded using the zero-argument encode()),...
Bitset encode() override
Encode an empty field.
unsigned size() override
Calculate the size (in bits) of an empty field.
void validate() override
Validate a field. Use require() inside your overloaded validate() to assert requirements or throw Exc...
google::protobuf::int32 int32
a signed 32 bit integer
unsigned ceil_log2(dccl::uint64 v)