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());
104 Bitset length_bits(min_size(), s.length());
106 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"DefaultStringCodec value_bits: " << value_bits
109 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"DefaultStringCodec length_bits: " << length_bits
113 for (
int i = 0, n = value_bits.size(); i < n; ++i) length_bits.push_back(value_bits[i]);
115 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"DefaultStringCodec created: " << length_bits
121std::string dccl::v2::DefaultStringCodec::decode(
Bitset* bits)
123 unsigned value_length = bits->
to_ulong();
127 unsigned header_length = min_size();
129 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"Length of string is = " << value_length
132 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"bits before get_more_bits " << *bits << std::endl;
137 dccl::dlog.
is(DEBUG2) && dccl::dlog <<
"bits after get_more_bits " << *bits << std::endl;
138 Bitset string_body_bits = *bits;
139 string_body_bits >>= header_length;
140 string_body_bits.resize(bits->size() - header_length);
150unsigned dccl::v2::DefaultStringCodec::size() {
return min_size(); }
152unsigned dccl::v2::DefaultStringCodec::size(
const std::string& wire_value)
154 return std::min(min_size() +
static_cast<unsigned>(wire_value.length() * BITS_IN_BYTE),
158unsigned dccl::v2::DefaultStringCodec::max_size()
161 return min_size() + dccl_field_options().max_length() * BITS_IN_BYTE;
164unsigned dccl::v2::DefaultStringCodec::min_size() {
return dccl::ceil_log2(MAX_STRING_LENGTH + 1); }
166void dccl::v2::DefaultStringCodec::validate()
168 require(dccl_field_options().has_max_length(),
"missing (dccl.field).max_length");
169 require(dccl_field_options().max_length() <= MAX_STRING_LENGTH,
170 "(dccl.field).max_length must be <= " +
171 std::to_string(
static_cast<int>(MAX_STRING_LENGTH)));
184 if (bits.size() > max_size() && this->strict())
187 this->this_field(), this->this_descriptor()));
189 bits.resize(max_size());
213 Bitset bytes_body_bits = *bits;
214 bytes_body_bits >>= min_size();
215 bytes_body_bits.resize(bits->size() - min_size());
232 return dccl_field_options().max_length() * BITS_IN_BYTE +
233 (use_required() ? 0 : 1);
246 require(dccl_field_options().has_max_length(),
"missing (dccl.field).max_length");
253 const google::protobuf::EnumValueDescriptor*
const& field_value)
255 return field_value->index();
258const google::protobuf::EnumValueDescriptor*
259dccl::v2::DefaultEnumCodec::post_decode(
const dccl::int32& wire_value)
261 const google::protobuf::EnumDescriptor* e = this_field()->enum_type();
263 if (wire_value < e->value_count())
265 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)