DCCL v4
bitset.cpp
1 // Copyright 2012-2023:
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 "bitset.h"
25 #include "codec.h"
26 
27 using namespace dccl::logger;
28 
29 dccl::Bitset dccl::Bitset::relinquish_bits(size_type num_bits, bool final_child)
30 {
31  if (final_child || this->size() < num_bits)
32  {
33  size_type num_parent_bits = (final_child) ? num_bits : num_bits - this->size();
34  if (parent_)
35  {
36  Bitset parent_bits = parent_->relinquish_bits(num_parent_bits, false);
37  append(parent_bits);
38  }
39  }
40 
41  Bitset out;
42  if (!final_child)
43  {
44  for (size_type i = 0; i < num_bits; ++i)
45  {
46  if (this->empty())
47  throw(dccl::Exception("Cannot relinquish_bits - no more bits to give up! Check "
48  "that all field codecs are always producing (encode) and "
49  "consuming (decode) the exact same number of bits."));
50 
51  out.push_back(this->front());
52  this->pop_front();
53  }
54  }
55  return out;
56 }
dccl::Exception
Exception class for DCCL.
Definition: exception.h:46
dccl::Bitset
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
Definition: bitset.h:42