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