28#include "../../codec.h"
31#include "../../binary.h"
34using dccl::operator<<;
45 unsigned size()
override {
return (
part() == dccl::HEAD) ? 0 : A_SIZE + B_SIZE; }
48 Bitset encode(
const CustomMsg& msg)
override
50 if (
part() == dccl::HEAD)
56 Bitset a(A_SIZE,
static_cast<unsigned long>(msg.a()));
57 Bitset b(B_SIZE,
static_cast<unsigned long>(msg.b()));
59 std::cout <<
"a: " << a << std::endl;
60 std::cout <<
"b: " << b << std::endl;
67 CustomMsg decode(
Bitset* bits)
override
69 if (
part() == dccl::HEAD)
88 void validate()
override {}
105 REPEAT_STORAGE_BITS = 4
109 MAX_REPEAT_SIZE = 1 << REPEAT_STORAGE_BITS
116 Bitset encode_repeated(
const std::vector<dccl::int32>& wire_values)
override
120 static_cast<int>(wire_values.size()) > max_repeat() ? max_repeat() : wire_values.size();
122 std::cout <<
"repeat size is " << repeat_size << std::endl;
124 for (
int i = 0, n = repeat_size; i < n; ++i)
128 value_bits.
append(
Bitset(singular_size(),
static_cast<unsigned long>(wire_value)));
131 std::cout << value_bits << std::endl;
132 std::cout <<
Bitset(REPEAT_STORAGE_BITS, repeat_size) << std::endl;
133 Bitset out(REPEAT_STORAGE_BITS, repeat_size);
138 std::vector<dccl::int32> decode_repeated(
Bitset* bits)
override
141 std::cout <<
"repeat size is " << repeat_size << std::endl;
145 Bitset value_bits = *bits;
146 value_bits >>= REPEAT_STORAGE_BITS;
148 std::vector<dccl::int32> out;
149 for (
int i = 0; i < repeat_size; ++i)
153 out.push_back(value);
154 value_bits >>= singular_size();
159 unsigned size_repeated(
const std::vector<dccl::int32>& field_values)
override
161 return REPEAT_STORAGE_BITS + field_values.size() * singular_size();
164 unsigned singular_size() {
return dccl::ceil_log2((max() - min()) + 1); }
166 unsigned max_size_repeated()
override
168 return REPEAT_STORAGE_BITS + max_repeat() * singular_size();
171 unsigned min_size_repeated()
override {
return REPEAT_STORAGE_BITS; }
173 void validate()
override
176 "missing (dccl.field).min");
178 "missing (dccl.field).max");
181 "(dccl.field).max_repeat must be less than " +
182 std::to_string(
static_cast<int>(MAX_REPEAT_SIZE)));
189int main(
int ,
char* [])
191 dccl::dlog.
connect(dccl::logger::ALL, &std::cerr);
197 codec.set_crypto_passphrase(
"my_passphrase!");
204 codec.info(msg_in1.GetDescriptor(), &std::cout);
205 std::cout <<
"Message in:\n" << msg_in1.DebugString() << std::endl;
206 codec.load(msg_in1.GetDescriptor());
207 std::cout <<
"Try encode..." << std::endl;
209 codec.encode(&bytes1, msg_in1);
210 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes1) << std::endl;
211 std::cout <<
"Try decode..." << std::endl;
212 std::shared_ptr<google::protobuf::Message> msg_out1 =
213 codec.decode<std::shared_ptr<google::protobuf::Message>>(bytes1);
214 std::cout <<
"... got Message out:\n" << msg_out1->DebugString() << std::endl;
215 assert(msg_in1.SerializeAsString() == msg_out1->SerializeAsString());
217 CustomMsg2 msg_in2, msg_out2;
219 msg_in2.mutable_msg()->CopyFrom(msg_in1);
223 codec.info(msg_in2.GetDescriptor(), &std::cout);
224 std::cout <<
"Message in:\n" << msg_in2.DebugString() << std::endl;
225 codec.load(msg_in2.GetDescriptor());
226 std::cout <<
"Try encode..." << std::endl;
228 codec.encode(&bytes2, msg_in2);
229 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes2) << std::endl;
230 std::cout <<
"Try decode..." << std::endl;
231 codec.decode(bytes2, &msg_out2);
232 std::cout <<
"... got Message out:\n" << msg_out2.DebugString() << std::endl;
233 assert(msg_in2.SerializeAsString() == msg_out2.SerializeAsString());
235 std::cout <<
"all tests passed" << std::endl;
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
void get_more_bits(size_type num_bits)
Retrieve more bits from the parent Bitset.
Bitset & append(const Bitset &bits)
Adds the bitset to the big end.
unsigned long to_ulong() const
Returns the value of the Bitset as an unsigned long integer. Equivalent to to<unsigned long>().
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
MessagePart part()
the part of the message currently being encoded (head or body)
void require(bool b, const std::string &description)
Essentially an assertion to be used in the validate() virtual method.
dccl::DCCLFieldOptions dccl_field_options() const
Get the DCCL field option extension value for the current field.
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar)
Exception used to signal null (non-existent) value within field codecs during decode.
Base class for "repeated" (multiple value) static-typed (no dccl::any) field encoders/decoders....
Base class for static-typed field encoders/decoders that use a fixed number of bits on the wire regar...
Dynamic Compact Control Language namespace.
google::protobuf::int32 int32
a signed 32 bit integer
void hex_encode(CharIterator begin, CharIterator end, std::string *out, bool upper_case=false)
Encodes a (little-endian) hexadecimal string from a byte string. Index 0 of begin is written to index...
unsigned ceil_log2(dccl::uint64 v)