DCCL v4
field_codec_default.h
1 // Copyright 2014-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 // Kyle Guilbert <kguilbert@aphysci.com>
8 // Chris Murphy <cmurphy@aphysci.com>
9 //
10 //
11 // This file is part of the Dynamic Compact Control Language Library
12 // ("DCCL").
13 //
14 // DCCL is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU Lesser General Public License as published by
16 // the Free Software Foundation, either version 2.1 of the License, or
17 // (at your option) any later version.
18 //
19 // DCCL is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU Lesser General Public License for more details.
23 //
24 // You should have received a copy of the GNU Lesser General Public License
25 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
26 // implements FieldCodecBase for all the basic DCCL types for version 3
27 
28 #ifndef DCCLV3FIELDCODECDEFAULT20140403H
29 #define DCCLV3FIELDCODECDEFAULT20140403H
30 
31 #include "../codecs2/field_codec_default.h"
32 
33 namespace dccl
34 {
36 namespace v3
37 {
38 // all these are the same as version 2
39 template <typename WireType, typename FieldType = WireType>
41 
42 template <typename TimeType> using TimeCodec = v2::TimeCodec<TimeType>;
43 template <typename T> using StaticCodec = v2::StaticCodec<T>;
44 
47 // Enum Codec is identical to v2, except when packed_enum is set to false.
48 
56  : public DefaultNumericFieldCodec<int32, const google::protobuf::EnumValueDescriptor*>
57 {
58  public:
59  int32 pre_encode(const google::protobuf::EnumValueDescriptor* const& field_value) override;
60  const google::protobuf::EnumValueDescriptor* post_decode(const int32& wire_value) override;
61  void validate() override {}
62  std::size_t hash() override
63  {
64  return std::hash<std::string>{}(this_field()->enum_type()->DebugString());
65  }
66 
67  private:
68  double max() override;
69  double min() override;
70 };
71 
75 class DefaultStringCodec : public TypedFieldCodec<std::string>
76 {
77  public:
78  void validate() override;
79  Bitset encode() override;
80  Bitset encode(const std::string& wire_value) override;
81  std::string decode(Bitset* bits) override;
82  unsigned size() override;
83  unsigned size(const std::string& wire_value) override;
84  unsigned max_size() override;
85  unsigned min_size() override;
86 };
87 
88 } // namespace v3
89 } // namespace dccl
90 
91 #endif
dccl::v2::StaticCodec
Placeholder codec that takes no space on the wire (0 bits).
Definition: field_codec_default.h:451
dccl
Dynamic Compact Control Language namespace.
Definition: any.h:46
dccl::FieldCodecBase::this_field
const google::protobuf::FieldDescriptor * this_field() const
Returns the FieldDescriptor (field schema meta-data) for this field.
Definition: field_codec.cpp:652
dccl::v3::DefaultStringCodec::validate
void validate() override
Validate a field. Use require() inside your overloaded validate() to assert requirements or throw Exc...
Definition: field_codec_default.cpp:118
dccl::v2::TimeCodec
Definition: field_codec_default.h:435
dccl::v3::DefaultStringCodec::min_size
unsigned min_size() override
Calculate minimum size of the field in bits.
Definition: field_codec_default.cpp:113
dccl::v3::DefaultStringCodec
Provides an variable length ASCII string encoder.
Definition: field_codec_default.h:75
dccl::Bitset
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
Definition: bitset.h:42
dccl::v2::DefaultBoolCodec
Provides a bool encoder. Uses 1 bit if field is required, 2 bits if optional
Definition: field_codec_default.h:253
dccl::v3::DefaultEnumCodec::hash
std::size_t hash() override
Generate a field specific hash to be combined with the descriptor hash.
Definition: field_codec_default.h:62
dccl::v3::DefaultStringCodec::size
unsigned size() override
Calculate the size (in bits) of an empty field.
Definition: field_codec_default.cpp:99
dccl::TypedFieldCodec
Base class for static-typed (no dccl::any) field encoders/decoders. Most single-valued user defined v...
Definition: field_codec_typed.h:72
dccl::v3::DefaultEnumCodec
Provides an enum encoder. This converts the enumeration to an integer and uses DefaultNumericFieldCod...
Definition: field_codec_default.h:55
dccl::v2::DefaultBytesCodec
Provides an fixed length byte string encoder.
Definition: field_codec_default.h:287
dccl::v3::DefaultEnumCodec::validate
void validate() override
Validate a field. Use require() inside your overloaded validate() to assert requirements or throw Exc...
Definition: field_codec_default.h:61
dccl::int32
google::protobuf::int32 int32
a signed 32 bit integer
Definition: common.h:58
dccl::v3::DefaultStringCodec::decode
std::string decode(Bitset *bits) override
Decode a field. If the field is empty (i.e. was encoded using the zero-argument encode()),...
Definition: field_codec_default.cpp:70
dccl::v2::DefaultNumericFieldCodec
Provides a basic bounded arbitrary length numeric (double, float, uint32, uint64, int32,...
Definition: field_codec_default.h:51
dccl::v3::DefaultStringCodec::max_size
unsigned max_size() override
Calculate maximum size of the field in bits.
Definition: field_codec_default.cpp:107
dccl::v3::DefaultStringCodec::encode
Bitset encode() override
Encode an empty field.
Definition: field_codec_default.cpp:33