DCCL v4
field_codec_id.cpp
1 // Copyright 2014-2017:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
7 //
8 //
9 // This file is part of the Dynamic Compact Control Language Library
10 // ("DCCL").
11 //
12 // DCCL is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation, either version 2.1 of the License, or
15 // (at your option) any later version.
16 //
17 // DCCL is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
24 #include "field_codec_id.h"
25 
26 //
27 // DefaultIdentifierCodec
28 //
29 
31 
33 {
34  if (id <= ONE_BYTE_MAX_ID)
35  {
36  return (dccl::Bitset(this_size(id), id) << 1);
37  }
38  else
39  {
40  dccl::Bitset return_bits(this_size(id), id);
41  return_bits <<= 1;
42  // set LSB to indicate long header form
43  return_bits.set(0, true);
44 
45  return return_bits;
46  }
47 }
48 
50 {
51  if (bits->test(0))
52  {
53  // long header
54  // grabs more bits to add to the MSB of `bits`
55  bits->get_more_bits((LONG_FORM_ID_BYTES - SHORT_FORM_ID_BYTES) * BITS_IN_BYTE);
56  // discard identifier
57  *(bits) >>= 1;
58  return bits->to_ulong();
59  }
60  else
61  {
62  // short header
63  *(bits) >>= 1;
64  return bits->to_ulong();
65  }
66 }
67 
68 unsigned dccl::DefaultIdentifierCodec::size() { return this_size(0); }
69 
70 unsigned dccl::DefaultIdentifierCodec::size(const uint32& id) { return this_size(id); }
71 
72 unsigned dccl::DefaultIdentifierCodec::this_size(const uint32& id)
73 {
74  if (id > TWO_BYTE_MAX_ID)
75  throw(Exception(
76  "dccl.id provided (" + std::to_string(id) +
77  ") exceeds maximum: " + std::to_string(int(TWO_BYTE_MAX_ID))));
78 
79  return (id <= ONE_BYTE_MAX_ID) ? SHORT_FORM_ID_BYTES * BITS_IN_BYTE
80  : LONG_FORM_ID_BYTES * BITS_IN_BYTE;
81 }
82 
83 unsigned dccl::DefaultIdentifierCodec::max_size() { return LONG_FORM_ID_BYTES * BITS_IN_BYTE; }
84 
85 unsigned dccl::DefaultIdentifierCodec::min_size() { return SHORT_FORM_ID_BYTES * BITS_IN_BYTE; }
dccl::DefaultIdentifierCodec::size
unsigned size() override
Calculate the size (in bits) of an empty field.
Definition: field_codec_id.cpp:68
dccl::Bitset::to_ulong
unsigned long to_ulong() const
Returns the value of the Bitset as an unsigned long integer. Equivalent to to<unsigned long>().
Definition: bitset.h:274
dccl::Bitset::get_more_bits
void get_more_bits(size_type num_bits)
Retrieve more bits from the parent Bitset.
Definition: bitset.h:419
dccl::Exception
Exception class for DCCL.
Definition: exception.h:47
dccl::DefaultIdentifierCodec::min_size
unsigned min_size() override
Calculate minimum size of the field in bits.
Definition: field_codec_id.cpp:85
dccl::uint32
google::protobuf::uint32 uint32
an unsigned 32 bit integer
Definition: common.h:56
dccl::DefaultIdentifierCodec::max_size
unsigned max_size() override
Calculate maximum size of the field in bits.
Definition: field_codec_id.cpp:83
dccl::DefaultIdentifierCodec::decode
uint32 decode(Bitset *bits) override
Decode a field. If the field is empty (i.e. was encoded using the zero-argument encode()),...
Definition: field_codec_id.cpp:49
dccl::Bitset::test
bool test(size_type n) const
Test a bit (return its value)
Definition: bitset.h:223
dccl::DefaultIdentifierCodec::encode
Bitset encode() override
Encode an empty field.
Definition: field_codec_id.cpp:30
dccl::Bitset
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
Definition: bitset.h:41
dccl::Bitset::set
Bitset & set(size_type n, bool val=true)
Set a bit to a given value.
Definition: bitset.h:174