DCCL v4
field_codec_var_bytes.h
1 // Copyright 2018:
2 // GobySoft, LLC (2013-)
3 // Community contributors (see AUTHORS file)
4 // File authors:
5 // Toby Schneider <toby@gobysoft.org>
6 //
7 //
8 // This file is part of the Dynamic Compact Control Language Library
9 // ("DCCL").
10 //
11 // DCCL is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU Lesser General Public License as published by
13 // the Free Software Foundation, either version 2.1 of the License, or
14 // (at your option) any later version.
15 //
16 // DCCL is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public License
22 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
23 #ifndef FIELD_CODEC_VAR_BYTES_20181010H
24 #define FIELD_CODEC_VAR_BYTES_20181010H
25 
26 #include "../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  public:
39  dccl::Bitset encode() override;
40  dccl::Bitset encode(const std::string& wire_value) override;
41  std::string decode(dccl::Bitset* bits) override;
42  unsigned size() override;
43  unsigned size(const std::string& wire_value) override;
44  unsigned max_size() override;
45  unsigned min_size() override;
46  void validate() override;
47 
48  private:
49  unsigned prefix_size() { return dccl::ceil_log2(dccl_field_options().max_length() + 1); }
50  unsigned presence_size() { return use_required() ? 0 : 1; }
51 };
52 } // namespace v3
53 } // namespace dccl
54 
55 #endif
dccl
Dynamic Compact Control Language namespace.
Definition: any.h:49
dccl::v3::VarBytesCodec::encode
dccl::Bitset encode() override
Encode an empty field.
Definition: field_codec_var_bytes.cpp:30
dccl::ceil_log2
unsigned ceil_log2(dccl::uint64 v)
Definition: binary.h:178
dccl::v3::VarBytesCodec::decode
std::string decode(dccl::Bitset *bits) override
Decode a field. If the field is empty (i.e. was encoded using the zero-argument encode()),...
Definition: field_codec_var_bytes.cpp:73
dccl::v3::VarBytesCodec
Definition: field_codec_var_bytes.h:36
dccl::v3::VarBytesCodec::max_size
unsigned max_size() override
Calculate maximum size of the field in bits.
Definition: field_codec_var_bytes.cpp:117
dccl::Bitset
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
Definition: bitset.h:41
dccl::TypedFieldCodec
Base class for static-typed (no dccl::any) field encoders/decoders. Most single-valued user defined v...
Definition: field_codec_typed.h:73
dccl::v3::VarBytesCodec::validate
void validate() override
Validate a field. Use require() inside your overloaded validate() to assert requirements or throw Exc...
Definition: field_codec_var_bytes.cpp:130
dccl::v3::VarBytesCodec::min_size
unsigned min_size() override
Calculate minimum size of the field in bits.
Definition: field_codec_var_bytes.cpp:122
dccl::FieldCodecBase::use_required
bool use_required()
Whether to use the required or optional encoding.
Definition: field_codec.h:386
dccl::v3::VarBytesCodec::size
unsigned size() override
Calculate the size (in bits) of an empty field.
Definition: field_codec_var_bytes.cpp:108
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:335