DCCL v3
field_codec_var_bytes.cpp
1 #include "field_codec_var_bytes.h"
2 #include "dccl/field_codec_manager.h"
3 #include "dccl/codecs3/field_codec_default.h"
4 
5 
6 using namespace dccl::logger;
7 
8 
9 dccl::Bitset dccl::v3::VarBytesCodec::encode()
10 {
11  return dccl::Bitset(min_size());
12 }
13 
14 dccl::Bitset dccl::v3::VarBytesCodec::encode(const std::string& wire_value)
15 {
16  std::string s = wire_value;
17  if(s.size() > dccl_field_options().max_length())
18  {
19  dccl::dlog.is(DEBUG2) && dccl::dlog << "Bytes " << s << " exceeds `dccl.max_length`, truncating" << std::endl;
20  s.resize(dccl_field_options().max_length());
21  }
22 
23  dccl::Bitset value_bits;
24  value_bits.from_byte_string(s);
25 
26  dccl::Bitset length_bits(presence_size() + prefix_size(), s.length());
27 
28  if(!use_required()) // set the presence bit
29  {
30  length_bits <<= 1;
31  length_bits.set(0);
32  }
33 
34  dccl::dlog.is(DEBUG2) && dccl::dlog << "dccl::v3::VarBytesCodec value_bits: " << value_bits << std::endl;
35 
36 
37  dccl::dlog.is(DEBUG2) && dccl::dlog << "dccl::v3::VarBytesCodec length_bits: " << length_bits << std::endl;
38 
39  // adds to MSBs
40  for(int i = 0, n = value_bits.size(); i < n; ++i)
41  length_bits.push_back(value_bits[i]);
42 
43  dccl::dlog.is(DEBUG2) && dccl::dlog << "dccl::v3::VarBytesCodec created: " << length_bits << std::endl;
44 
45  return length_bits;
46 }
47 
48 std::string dccl::v3::VarBytesCodec::decode(dccl::Bitset* bits)
49 {
50  if(!use_required())
51  {
52  if(bits->to_ulong() == 0)
53  {
55  }
56  else
57  {
58  bits->get_more_bits(prefix_size());
59  (*bits) >>= 1;
60  }
61  }
62 
63 
64  unsigned value_length = bits->to_ulong();
65  unsigned header_length = presence_size() + prefix_size();
66 
67  dccl::dlog.is(DEBUG2) && dccl::dlog << "Length of string is = " << value_length << std::endl;
68 
69  dccl::dlog.is(DEBUG2) && dccl::dlog << "bits before get_more_bits " << *bits << std::endl;
70 
71  // grabs more bits to add to the MSBs of `bits`
72  bits->get_more_bits(value_length*dccl::BITS_IN_BYTE);
73 
74  dccl::dlog.is(DEBUG2) && dccl::dlog << "bits after get_more_bits " << *bits << std::endl;
75  dccl::Bitset string_body_bits = *bits;
76  string_body_bits >>= header_length;
77  string_body_bits.resize(bits->size() - header_length);
78 
79  dccl::dlog.is(DEBUG2) && dccl::dlog << "string_body_bits " << string_body_bits << std::endl;
80 
81 
82  return string_body_bits.to_byte_string();
83 }
84 
85 unsigned dccl::v3::VarBytesCodec::size()
86 {
87  return min_size();
88 }
89 
90 unsigned dccl::v3::VarBytesCodec::size(const std::string& wire_value)
91 {
92  return std::min(presence_size() + prefix_size() + static_cast<unsigned>(wire_value.length()*dccl::BITS_IN_BYTE), max_size());
93 }
94 
95 
96 unsigned dccl::v3::VarBytesCodec::max_size()
97 {
98  return presence_size() + prefix_size() + dccl_field_options().max_length() * dccl::BITS_IN_BYTE;
99 }
100 
101 unsigned dccl::v3::VarBytesCodec::min_size()
102 {
103  if(use_required())
104  return prefix_size();
105  else
106  return presence_size();
107 }
108 
109 
110 void dccl::v3::VarBytesCodec::validate()
111 {
112  require(dccl_field_options().has_max_length(), "missing (dccl.field).max_length");
113 }
unsigned long to_ulong() const
Returns the value of the Bitset as an unsigned long integer. Equivalent to to<unsigned long>()...
Definition: bitset.h:284
Exception used to signal null (non-existent) value within field codecs during decode.
Definition: exception.h:39
Bitset & set(size_type n, bool val=true)
Set a bit to a given value.
Definition: bitset.h:176
void get_more_bits(size_type num_bits)
Retrieve more bits from the parent Bitset.
Definition: bitset.h:447
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...
Definition: logger.h:145
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...
Definition: bitset.h:348
std::string to_byte_string()
Returns the value of the Bitset to a byte string, where each character represents 8 bits of the Bitse...
Definition: bitset.h:310
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy...
Definition: bitset.h:38