DCCL v3
field_codec_default.cpp
1 // Copyright 2009-2017 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (for 2013-)
3 // Massachusetts Institute of Technology (for 2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Dynamic Compact Control Language Library
8 // ("DCCL").
9 //
10 // DCCL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // DCCL is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
22 #include "dccl/codecs3/field_codec_default.h"
23 
24 using namespace dccl::logger;
25 
26 //
27 // DefaultStringCodec
28 //
29 
30 dccl::Bitset dccl::v3::DefaultStringCodec::encode()
31 {
32  return Bitset(min_size());
33 }
34 
35 dccl::Bitset dccl::v3::DefaultStringCodec::encode(const std::string& wire_value)
36 {
37  std::string s = wire_value;
38  if(s.size() > dccl_field_options().max_length())
39  {
40  dccl::dlog.is(DEBUG2) && dccl::dlog << "String " << s << " exceeds `dccl.max_length`, truncating" << std::endl;
41  s.resize(dccl_field_options().max_length());
42  }
43 
44 
45  Bitset value_bits;
46  value_bits.from_byte_string(s);
47 
48  Bitset length_bits(min_size(), s.length());
49 
50  dccl::dlog.is(DEBUG2) && dccl::dlog << "DefaultStringCodec value_bits: " << value_bits << std::endl;
51 
52 
53  dccl::dlog.is(DEBUG2) && dccl::dlog << "DefaultStringCodec length_bits: " << length_bits << std::endl;
54 
55  // adds to MSBs
56  for(int i = 0, n = value_bits.size(); i < n; ++i)
57  length_bits.push_back(value_bits[i]);
58 
59  dccl::dlog.is(DEBUG2) && dccl::dlog << "DefaultStringCodec created: " << length_bits << std::endl;
60 
61 
62  return length_bits;
63 }
64 
65 std::string dccl::v3::DefaultStringCodec::decode(Bitset* bits)
66 {
67  unsigned value_length = bits->to_ulong();
68 
69  if(value_length)
70  {
71 
72  unsigned header_length = min_size();
73 
74  dccl::dlog.is(DEBUG2) && dccl::dlog << "Length of string is = " << value_length << std::endl;
75 
76  dccl::dlog.is(DEBUG2) && dccl::dlog << "bits before get_more_bits " << *bits << std::endl;
77 
78  // grabs more bits to add to the MSBs of `bits`
79  bits->get_more_bits(value_length*BITS_IN_BYTE);
80 
81 
82  dccl::dlog.is(DEBUG2) && dccl::dlog << "bits after get_more_bits " << *bits << std::endl;
83  Bitset string_body_bits = *bits;
84  string_body_bits >>= header_length;
85  string_body_bits.resize(bits->size() - header_length);
86 
87  return string_body_bits.to_byte_string();
88  }
89  else
90  {
91  throw NullValueException();
92  }
93 
94 }
95 
96 unsigned dccl::v3::DefaultStringCodec::size()
97 {
98  return min_size();
99 }
100 
101 unsigned dccl::v3::DefaultStringCodec::size(const std::string& wire_value)
102 {
103  return std::min(min_size() + static_cast<unsigned>(wire_value.length()*BITS_IN_BYTE), max_size());
104 }
105 
106 
107 unsigned dccl::v3::DefaultStringCodec::max_size()
108 {
109  // string length + actual string
110  return min_size() + dccl_field_options().max_length() * BITS_IN_BYTE;
111 }
112 
113 unsigned dccl::v3::DefaultStringCodec::min_size()
114 {
115  return dccl::ceil_log2(dccl_field_options().max_length()+1);
116 }
117 
118 
119 void dccl::v3::DefaultStringCodec::validate()
120 {
121  require(dccl_field_options().has_max_length(), "missing (dccl.field).max_length");
122 }
unsigned ceil_log2(dccl::uint64 v)
Definition: binary.h:178
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
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy...
Definition: bitset.h:38