DCCL v3
field_codec_id.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 "field_codec_id.h"
23 
24 //
25 // DefaultIdentifierCodec
26 //
27 
29 {
30  return encode(0);
31 }
32 
34 {
35  if(id <= ONE_BYTE_MAX_ID)
36  {
37  return(dccl::Bitset(this_size(id), id) << 1);
38  }
39  else
40  {
41  dccl::Bitset return_bits(this_size(id), id);
42  return_bits <<= 1;
43  // set LSB to indicate long header form
44  return_bits.set(0, true);
45 
46 
47  return return_bits;
48  }
49 }
50 
52 {
53  if(bits->test(0))
54  {
55  // long header
56  // grabs more bits to add to the MSB of `bits`
57  bits->get_more_bits((LONG_FORM_ID_BYTES - SHORT_FORM_ID_BYTES)*BITS_IN_BYTE);
58  // discard identifier
59  *(bits) >>= 1;
60  return bits->to_ulong();
61  }
62  else
63  {
64  // short header
65  *(bits) >>= 1;
66  return bits->to_ulong();
67  }
68 }
69 
71 {
72  return this_size(0);
73 }
74 
76 {
77  return this_size(id);
78 }
79 
80 unsigned dccl::DefaultIdentifierCodec::this_size(const uint32& id)
81 {
82  if(id > TWO_BYTE_MAX_ID)
83  throw(Exception("dccl.id provided (" + boost::lexical_cast<std::string>(id) + ") exceeds maximum: " + boost::lexical_cast<std::string>(int(TWO_BYTE_MAX_ID))));
84 
85  return (id <= ONE_BYTE_MAX_ID) ?
86  SHORT_FORM_ID_BYTES*BITS_IN_BYTE :
87  LONG_FORM_ID_BYTES*BITS_IN_BYTE;
88 }
89 
90 
92 {
93  return LONG_FORM_ID_BYTES * BITS_IN_BYTE;
94 }
95 
97 {
98  return SHORT_FORM_ID_BYTES * BITS_IN_BYTE;
99 }
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:284
dccl::Bitset::get_more_bits
void get_more_bits(size_type num_bits)
Retrieve more bits from the parent Bitset.
Definition: bitset.h:447
dccl::Exception
Exception class for DCCL.
Definition: exception.h:31
dccl::uint32
google::protobuf::uint32 uint32
an unsigned 32 bit integer
Definition: common.h:55
dccl::DefaultIdentifierCodec::size
virtual unsigned size()
Calculate the size (in bits) of an empty field.
Definition: field_codec_id.cpp:70
dccl::Bitset::test
bool test(size_type n) const
Test a bit (return its value)
Definition: bitset.h:230
dccl::Bitset
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
Definition: bitset.h:38
dccl::DefaultIdentifierCodec::max_size
virtual unsigned max_size()
Calculate maximum size of the field in bits.
Definition: field_codec_id.cpp:91
dccl::DefaultIdentifierCodec::encode
virtual Bitset encode()
Encode an empty field.
Definition: field_codec_id.cpp:28
dccl::DefaultIdentifierCodec::decode
virtual uint32 decode(Bitset *bits)
Decode a field. If the field is empty (i.e. was encoded using the zero-argument encode()),...
Definition: field_codec_id.cpp:51
dccl::Bitset::set
Bitset & set(size_type n, bool val=true)
Set a bit to a given value.
Definition: bitset.h:176
dccl::DefaultIdentifierCodec::min_size
virtual unsigned min_size()
Calculate minimum size of the field in bits.
Definition: field_codec_id.cpp:96