DCCL v3
field_codec_var_bytes.h
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 
23 #ifndef FIELD_CODEC_VAR_BYTES_20181010H
24 #define FIELD_CODEC_VAR_BYTES_20181010H
25 
26 #include "dccl/field_codec_typed.h"
27 
28 namespace dccl
29 {
30  namespace v3
31  {
32  // Size (in bits) for "required/optional/repeated bytes foo":
33  // if optional: [1 bit - has_foo()?][N bits - prefix with length of byte string][string bytes]
34  // if required: [N bits - prefix with length of string][string bytes]
35  // if repeated: [M bits - prefix with the number of repeated values][same as "required" for value with index 0][same as "required" for index = 1]...[same as required for last index]
36  class VarBytesCodec : public dccl::TypedFieldCodec<std::string>
37  {
38  private:
39  dccl::Bitset encode();
40  dccl::Bitset encode(const std::string& wire_value);
41  std::string decode(dccl::Bitset* bits);
42  unsigned size();
43  unsigned size(const std::string& wire_value);
44  unsigned max_size();
45  unsigned min_size();
46  void validate();
47 
48  private:
49  unsigned prefix_size()
50  { return dccl::ceil_log2(dccl_field_options().max_length()+1); }
51  unsigned presence_size()
52  { return use_required() ? 0 : 1; }
53 
54  };
55  }
56 }
57 
58 #endif
dccl
Dynamic Compact Control Language namespace.
Definition: gen_units_class_plugin.h:49
dccl::ceil_log2
unsigned ceil_log2(dccl::uint64 v)
Definition: binary.h:180
dccl::v3::VarBytesCodec
Definition: field_codec_var_bytes.h:36
dccl::Bitset
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
Definition: bitset.h:38
dccl::TypedFieldCodec
Base class for static-typed (no boost::any) field encoders/decoders. Most single-valued user defined ...
Definition: field_codec_typed.h:82
dccl::FieldCodecBase::use_required
bool use_required()
Whether to use the required or optional encoding.
Definition: field_codec.h:353
dccl::FieldCodecBase::dccl_field_options
dccl::DCCLFieldOptions dccl_field_options() const
Get the DCCL field option extension value for the current field.
Definition: field_codec.h:326