25 #include "dccl/codec.h"
29 #include "dccl/binary.h"
32 using dccl::operator<<;
43 unsigned size() {
return (part() == dccl::HEAD) ? 0 : A_SIZE + B_SIZE; }
46 Bitset encode(
const CustomMsg& msg)
48 if(part() == dccl::HEAD)
52 Bitset a(A_SIZE,
static_cast<unsigned long>(msg.a()));
53 Bitset b(B_SIZE,
static_cast<unsigned long>(msg.b()));
55 std::cout <<
"a: " << a << std::endl;
56 std::cout <<
"b: " << b << std::endl;
63 CustomMsg decode(
Bitset* bits)
65 if(part() == dccl::HEAD)
93 enum { REPEAT_STORAGE_BITS = 4 };
94 enum { MAX_REPEAT_SIZE = 1 << REPEAT_STORAGE_BITS };
100 Bitset encode_repeated(
const std::vector<dccl::int32>& wire_values)
103 int repeat_size =
static_cast<int>(wire_values.size()) > max_repeat() ?
107 std::cout <<
"repeat size is " << repeat_size << std::endl;
109 for(
int i = 0, n = repeat_size; i < n; ++i)
113 value_bits.
append(
Bitset(singular_size(),
static_cast<unsigned long>(wire_value)));
116 std::cout << value_bits << std::endl;
117 std::cout <<
Bitset(REPEAT_STORAGE_BITS, repeat_size) << std::endl;
118 Bitset out(REPEAT_STORAGE_BITS, repeat_size);
123 std::vector<dccl::int32> decode_repeated(
Bitset* bits)
126 std::cout <<
"repeat size is " << repeat_size << std::endl;
130 Bitset value_bits = *bits;
131 value_bits >>= REPEAT_STORAGE_BITS;
133 std::vector<dccl::int32> out;
134 for(
int i = 0; i < repeat_size; ++i)
138 out.push_back(value);
139 value_bits >>= singular_size();
144 unsigned size_repeated(
const std::vector<dccl::int32>& field_values)
146 return REPEAT_STORAGE_BITS + field_values.size()*singular_size();
149 unsigned singular_size()
152 unsigned max_size_repeated()
154 return REPEAT_STORAGE_BITS + max_repeat()*singular_size();
157 unsigned min_size_repeated()
159 return REPEAT_STORAGE_BITS;
177 int main(
int argc,
char* argv[])
179 dccl::dlog.
connect(dccl::logger::ALL, &std::cerr);
182 dccl::FieldCodecManager::add<dccl::test::CustomCodec>(
"custom_codec");
183 dccl::FieldCodecManager::add<dccl::test::Int32RepeatedCodec>(
"int32_test_codec");
185 codec.set_crypto_passphrase(
"my_passphrase!");
192 codec.info(msg_in1.GetDescriptor(), &std::cout);
193 std::cout <<
"Message in:\n" << msg_in1.DebugString() << std::endl;
194 codec.load(msg_in1.GetDescriptor());
195 std::cout <<
"Try encode..." << std::endl;
197 codec.encode(&bytes1, msg_in1);
198 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes1) << std::endl;
199 std::cout <<
"Try decode..." << std::endl;
200 boost::shared_ptr<google::protobuf::Message> msg_out1 = codec.decode<boost::shared_ptr<google::protobuf::Message> >(bytes1);
201 std::cout <<
"... got Message out:\n" << msg_out1->DebugString() << std::endl;
202 assert(msg_in1.SerializeAsString() == msg_out1->SerializeAsString());
205 CustomMsg2 msg_in2, msg_out2;
207 msg_in2.mutable_msg()->CopyFrom(msg_in1);
211 codec.info(msg_in2.GetDescriptor(), &std::cout);
212 std::cout <<
"Message in:\n" << msg_in2.DebugString() << std::endl;
213 codec.load(msg_in2.GetDescriptor());
214 std::cout <<
"Try encode..." << std::endl;
216 codec.encode(&bytes2, msg_in2);
217 std::cout <<
"... got bytes (hex): " <<
dccl::hex_encode(bytes2) << std::endl;
218 std::cout <<
"Try decode..." << std::endl;
219 codec.decode(bytes2, &msg_out2);
220 std::cout <<
"... got Message out:\n" << msg_out2.DebugString() << std::endl;
221 assert(msg_in2.SerializeAsString() == msg_out2.SerializeAsString());
223 std::cout <<
"all tests passed" << std::endl;