DCCL v4
Loading...
Searching...
No Matches
field_codec_var_bytes.h
1// Copyright 2018-2023:
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
28namespace dccl
29{
30namespace 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]
36class 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
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
Definition bitset.h:43
dccl::DCCLFieldOptions dccl_field_options() const
Get the DCCL field option extension value for the current field.
bool use_required()
Whether to use the required or optional encoding.
Base class for static-typed (no dccl::any) field encoders/decoders. Most single-valued user defined v...
std::string decode(dccl::Bitset *bits) override
Decode a field. If the field is empty (i.e. was encoded using the zero-argument encode()),...
unsigned max_size() override
Calculate maximum size of the field in bits.
void validate() override
Validate a field. Use require() inside your overloaded validate() to assert requirements or throw Exc...
dccl::Bitset encode() override
Encode an empty field.
unsigned min_size() override
Calculate minimum size of the field in bits.
unsigned size() override
Calculate the size (in bits) of an empty field.
Dynamic Compact Control Language namespace.
Definition any.h:47
unsigned ceil_log2(dccl::uint64 v)
Definition binary.h:178