29 #include "field_codec_default.h"
31 #if DCCL_THREAD_SUPPORT
32 std::mutex dccl::v2::TimeCodecClock::clock_mutex_;
35 std::function<
dccl::int64()> dccl::v2::TimeCodecClock::epoch_sec_func_;
37 using 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;
86 dccl::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());
102 value_bits.from_byte_string(s);
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
121 std::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;
135 bits->get_more_bits(value_length * BITS_IN_BYTE);
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);
142 return string_body_bits.to_byte_string();
146 throw NullValueException();
150 unsigned dccl::v2::DefaultStringCodec::size() {
return min_size(); }
152 unsigned dccl::v2::DefaultStringCodec::size(
const std::string& wire_value)
154 return std::min(min_size() +
static_cast<unsigned>(wire_value.length() * BITS_IN_BYTE),
158 unsigned dccl::v2::DefaultStringCodec::max_size()
161 return min_size() + dccl_field_options().max_length() * BITS_IN_BYTE;
164 unsigned dccl::v2::DefaultStringCodec::min_size() {
return dccl::ceil_log2(MAX_STRING_LENGTH + 1); }
166 void 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();
258 const google::protobuf::EnumValueDescriptor*
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);