23#include "field_codec_var_bytes.h"
24#include "../field_codec_manager.h"
25#include "field_codec_default.h"
27using namespace dccl::logger;
33 std::string s = wire_value;
34 if (s.size() > dccl_field_options().max_length())
39 this->this_field(), this->this_descriptor()));
41 dccl::dlog.
is(DEBUG2) &&
42 dccl::dlog <<
"Bytes " << s <<
" exceeds `dccl.max_length`, truncating" << std::endl;
43 s.resize(dccl_field_options().max_length());
49 dccl::Bitset length_bits(presence_size() + prefix_size(), s.length());
57 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"dccl::v3::VarBytesCodec value_bits: " << value_bits
60 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"dccl::v3::VarBytesCodec length_bits: " << length_bits
64 for (
int i = 0, n = value_bits.size(); i < n; ++i) length_bits.push_back(value_bits[i]);
66 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"dccl::v3::VarBytesCodec created: " << length_bits
87 unsigned value_length = bits->
to_ulong();
88 unsigned header_length = presence_size() + prefix_size();
90 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"Length of string is = " << value_length << std::endl;
92 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"bits before get_more_bits " << *bits << std::endl;
97 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"bits after get_more_bits " << *bits << std::endl;
99 string_body_bits >>= header_length;
100 string_body_bits.resize(bits->size() - header_length);
102 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"string_body_bits " << string_body_bits << std::endl;
111 return std::min(presence_size() + prefix_size() +
112 static_cast<unsigned>(wire_value.length() * dccl::BITS_IN_BYTE),
118 return presence_size() + prefix_size() + dccl_field_options().max_length() * dccl::BITS_IN_BYTE;
124 return prefix_size();
126 return presence_size();
131 require(dccl_field_options().has_max_length(),
"missing (dccl.field).max_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.