29#include "field_codec_default.h"
31#if DCCL_THREAD_SUPPORT
32std::mutex dccl::v2::TimeCodecClock::clock_mutex_;
35std::function<
dccl::int64()> dccl::v2::TimeCodecClock::epoch_sec_func_;
37using namespace dccl::logger;
47 return Bitset(size(), use_required() ? wire_value : wire_value + 1);
71 const unsigned BOOL_VALUES = 2;
73 const unsigned NULL_VALUE = use_required() ? 0 : 1;
86dccl::Bitset dccl::v2::DefaultStringCodec::encode(
const std::string& wire_value)
88 std::string s = wire_value;
89 if (s.size() > dccl_field_options().max_length())
94 this->this_field(), this->this_descriptor()));
96 dccl::dlog.
is(DEBUG2) &&
97 dccl::dlog <<
"String " << s <<
" exceeds `dccl.max_length`, truncating" << std::endl;
98 s.resize(dccl_field_options().max_length());
101 if (s.size() < dccl_field_options().min_length())
106 this->this_field(), this->this_descriptor()));
108 dccl::dlog.
is(DEBUG2) &&
109 dccl::dlog <<
"String " << s <<
" is shorter than `dccl.min_length`, padding"
111 s.resize(dccl_field_options().min_length(),
'\0');
117 Bitset length_bits(min_size(), s.length());
119 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"DefaultStringCodec value_bits: " << value_bits
122 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"DefaultStringCodec length_bits: " << length_bits
126 for (
int i = 0, n = value_bits.size(); i < n; ++i) length_bits.push_back(value_bits[i]);
128 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"DefaultStringCodec created: " << length_bits
134std::string dccl::v2::DefaultStringCodec::decode(
Bitset* bits)
136 unsigned value_length = bits->
to_ulong();
140 unsigned header_length = min_size();
142 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"Length of string is = " << value_length
145 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"bits before get_more_bits " << *bits << std::endl;
150 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"bits after get_more_bits " << *bits << std::endl;
151 Bitset string_body_bits = *bits;
152 string_body_bits >>= header_length;
153 string_body_bits.resize(bits->size() - header_length);
163unsigned dccl::v2::DefaultStringCodec::size() {
return min_size(); }
165unsigned dccl::v2::DefaultStringCodec::size(
const std::string& wire_value)
167 return std::min(min_size() +
static_cast<unsigned>(wire_value.length() * BITS_IN_BYTE),
171unsigned dccl::v2::DefaultStringCodec::max_size()
174 return min_size() + dccl_field_options().max_length() * BITS_IN_BYTE;
177unsigned dccl::v2::DefaultStringCodec::min_size() {
return dccl::ceil_log2(MAX_STRING_LENGTH + 1); }
179void dccl::v2::DefaultStringCodec::validate()
181 require(dccl_field_options().has_max_length(),
"missing (dccl.field).max_length");
182 require(dccl_field_options().max_length() <= MAX_STRING_LENGTH,
183 "(dccl.field).max_length must be <= " +
184 std::to_string(
static_cast<int>(MAX_STRING_LENGTH)));
185 require(dccl_field_options().max_length() >= dccl_field_options().min_length(),
186 "(dccl.field).max_length must be >= (dccl.field).min_length");
199 if (bits.size() > max_size() && this->strict())
202 this->this_field(), this->this_descriptor()));
204 bits.resize(max_size());
228 Bitset bytes_body_bits = *bits;
229 bytes_body_bits >>= min_size();
230 bytes_body_bits.resize(bits->size() - min_size());
247 return dccl_field_options().max_length() * BITS_IN_BYTE +
248 (use_required() ? 0 : 1);
261 require(dccl_field_options().has_max_length(),
"missing (dccl.field).max_length");
268 const google::protobuf::EnumValueDescriptor*
const& field_value)
270 return field_value->index();
273const google::protobuf::EnumValueDescriptor*
274dccl::v2::DefaultEnumCodec::post_decode(
const dccl::int32& wire_value)
276 const google::protobuf::EnumDescriptor* e = this_field()->enum_type();
278 if (wire_value < e->value_count())
280 const google::protobuf::EnumValueDescriptor* return_value = e->value(wire_value);
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.
unsigned size() override
The size of the encoded message in bits. Use TypedFieldCodec if the size depends on the data.
Bitset encode() override
Encode an empty field.
bool decode(Bitset *bits) override
Decode a field. If the field is empty (i.e. was encoded using the zero-argument encode()),...
void validate() override
Validate a field. Use require() inside your overloaded validate() to assert requirements or throw Exc...
unsigned min_size() override
Calculate minimum size of the field in bits.
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...
unsigned max_size() override
Calculate maximum 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()),...
google::protobuf::int32 int32
a signed 32 bit integer
google::protobuf::int64 int64
a signed 64 bit integer
unsigned ceil_log2(dccl::uint64 v)