23#include "field_codec_var_bytes.h"
26using namespace dccl::logger;
32 std::string s = wire_value;
33 if (s.size() > dccl_field_options().max_length())
38 this->this_field(), this->this_descriptor()));
40 dccl::dlog.
is(DEBUG2) &&
41 dccl::dlog <<
"Bytes " << s <<
" exceeds `dccl.max_length`, truncating" << std::endl;
42 s.resize(dccl_field_options().max_length());
45 if (s.size() < dccl_field_options().min_length())
50 this->this_field(), this->this_descriptor()));
52 dccl::dlog.
is(DEBUG2) &&
53 dccl::dlog <<
"Bytes " << s <<
" is shorter than `dccl.min_length`, padding"
55 s.resize(dccl_field_options().min_length(),
'\0');
61 dccl::Bitset length_bits(presence_size() + prefix_size(),
62 s.length() - dccl_field_options().min_length());
70 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"dccl::v3::VarBytesCodec value_bits: " << value_bits
73 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"dccl::v3::VarBytesCodec length_bits: " << length_bits
77 for (
int i = 0, n = value_bits.size(); i < n; ++i) length_bits.push_back(value_bits[i]);
79 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"dccl::v3::VarBytesCodec created: " << length_bits
100 unsigned value_length = bits->
to_ulong() + dccl_field_options().min_length();
101 unsigned header_length = presence_size() + prefix_size();
103 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"Length of string is = " << value_length << std::endl;
105 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"bits before get_more_bits " << *bits << std::endl;
110 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"bits after get_more_bits " << *bits << std::endl;
112 string_body_bits >>= header_length;
113 string_body_bits.resize(bits->size() - header_length);
115 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"string_body_bits " << string_body_bits << std::endl;
125 std::max(
static_cast<unsigned>(wire_value.length()),
126 static_cast<unsigned>(dccl_field_options().min_length()));
127 return std::min(presence_size() + prefix_size() + length * dccl::BITS_IN_BYTE, max_size());
132 return presence_size() + prefix_size() + dccl_field_options().max_length() * dccl::BITS_IN_BYTE;
138 return prefix_size();
140 return presence_size();
145 require(dccl_field_options().has_max_length(),
"missing (dccl.field).max_length");
146 require(dccl_field_options().max_length() >= dccl_field_options().min_length(),
147 "(dccl.field).max_length must be >= (dccl.field).min_length");
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...
Bitset & set(size_type n, bool val=true)
Set a bit to a given value.
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.
std::string decode(dccl::Bitset *bits) override
Decode a field. If the field is empty (i.e. was encoded using the zero-argument encode()),...
unsigned max_size() override
Calculate maximum size of the field in bits.
void validate() override
Validate a field. Use require() inside your overloaded validate() to assert requirements or throw Exc...
dccl::Bitset encode() override
Encode an empty field.
unsigned min_size() override
Calculate minimum size of the field in bits.
unsigned size() override
Calculate the size (in bits) of an empty field.