DCCL v3
field_codec_default.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 // implements FieldCodecBase for all the basic DCCL types for version 3
23 
24 #ifndef DCCLV3FIELDCODECDEFAULT20140403H
25 #define DCCLV3FIELDCODECDEFAULT20140403H
26 
27 #include "dccl/codecs2/field_codec_default.h"
28 
29 namespace dccl
30 {
32  namespace v3
33  {
34  // all these are the same as version 2
35  template<typename WireType, typename FieldType = WireType>
36  class DefaultNumericFieldCodec : public v2::DefaultNumericFieldCodec<WireType, FieldType> { };
37 
40  // Enum Codec is identical to v2, except when packed_enum is set to false.
41 
49  : public DefaultNumericFieldCodec<int32, const google::protobuf::EnumValueDescriptor*>
50  {
51  public:
52  int32 pre_encode(const google::protobuf::EnumValueDescriptor* const& field_value);
53  const google::protobuf::EnumValueDescriptor* post_decode(const int32& wire_value);
54 
55  private:
56  void validate() { }
57 
58  double max();
59  double min();
60  };
61 
62  template<typename TimeType>
63  class TimeCodec : public v2::TimeCodecBase<TimeType, 0>
64  { BOOST_STATIC_ASSERT(sizeof(TimeCodec) == 0); };
65 
66  template<> class TimeCodec<uint64> : public v2::TimeCodecBase<uint64, 1000000> { };
67  template<> class TimeCodec<int64> : public v2::TimeCodecBase<int64, 1000000> { };
68  template<> class TimeCodec<double> : public v2::TimeCodecBase<double, 1> { };
69 
70  template<typename T>
71  class StaticCodec : public v2::StaticCodec<T>
72  { };
73 
74 
78  class DefaultStringCodec : public TypedFieldCodec<std::string>
79  {
80  private:
81  Bitset encode();
82  Bitset encode(const std::string& wire_value);
83  std::string decode(Bitset* bits);
84  unsigned size();
85  unsigned size(const std::string& wire_value);
86  unsigned max_size();
87  unsigned min_size();
88  void validate();
89  };
90 
91  }
92 }
93 
94 #endif
virtual WireType decode(Bitset *bits)
Decode a field. If the field is empty (i.e. was encoded using the zero-argument encode()), throw NullValueException to indicate this.
Bitset encode()
Encode an empty field.
Provides an fixed length byte string encoder.
virtual WireType pre_encode(const FieldType &field_value)=0
Convert from the FieldType representation (used in the Google Protobuf message) to the WireType repre...
unsigned size()
The size of the encoded message in bits. Use TypedFieldCodec if the size depends on the data...
google::protobuf::int64 int64
a signed 64 bit integer
Definition: common.h:61
Provides a basic bounded arbitrary length numeric (double, float, uint32, uint64, int32...
google::protobuf::uint64 uint64
an unsigned 64 bit integer
Definition: common.h:59
Base class for static-typed (no boost::any) field encoders/decoders. Most single-valued user defined ...
Placeholder codec that takes no space on the wire (0 bits).
virtual void validate()
Validate a field. Use require() inside your overloaded validate() to assert requirements or throw Exc...
Dynamic Compact Control Language namespace.
Provides an enum encoder. This converts the enumeration to an integer and uses DefaultNumericFieldCod...
virtual FieldType post_decode(const WireType &wire_value)=0
Convert from the WireType representation (used with encode() and decode(), i.e. "on the wire") to the...
google::protobuf::int32 int32
a signed 32 bit integer
Definition: common.h:57
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy...
Definition: bitset.h:38
Provides an variable length ASCII string encoder.
Provides a bool encoder. Uses 1 bit if field is required, 2 bits if optional