MOS Source Code
Loading...
Searching...
No Matches
cxx_callback_datatype.cpp
Go to the documentation of this file.
1// Test wrapping of a C++ class inside struct using callback_datatype option.
2#include "message.pb.hpp"
3
4#include <pb_encode.h>
5#include <pb_decode.h>
6
7#include <algorithm>
8#include <cstdio>
9
10// See tests/alltypes_callback, tests/oneoff_callback and examples/network_server for more...
11bool TestMessage_values_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_t *field)
12{
13 if (ostream != NULL) {
14 // Encoding callback, serialize items from vector
15 const std::vector<int> &v = *(const std::vector<int> *)field->pData;
16 for (std::vector<int>::const_iterator i = v.begin(); i != v.end(); ++i) {
17 if (!pb_encode_tag_for_field(ostream, field)) {
18 return false;
19 }
20 SubMessage tmp;
21 tmp.actual_value = *i;
22 if (!pb_encode_submessage(ostream, SubMessage_fields, &tmp)) {
23 return false;
24 }
25 }
26 } else if (istream != NULL) {
27 // Decoding callback, add items to vector
28 std::vector<int> &v = *(std::vector<int> *)field->pData;
29 SubMessage tmp;
30 if (!pb_decode(istream, SubMessage_fields, &tmp)) {
31 return false;
32 }
33 v.push_back(tmp.actual_value);
34 }
35 return true;
36}
37
38extern "C"
39bool TestMessage_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_t *field)
40{
41 if (field->tag == TestMessage_values_tag) {
42 return TestMessage_values_callback(istream, ostream, field);
43 }
44 return true;
45}
46
47extern "C"
48int main() {
49 TestMessage source = TestMessage_init_zero; // Not strictly necessary to initialize, just using it to test the initializer.
50 source.values.push_back(5);
51 source.values.push_back(4);
52 source.values.push_back(3);
53 source.values.push_back(2);
54 source.values.push_back(1);
55
56
57 std::vector<uint8_t> serialized;
58 size_t size = 0;
59 pb_get_encoded_size(&size, TestMessage_fields, &source);
60 serialized.resize(size);
61
62 pb_ostream_t outstream = pb_ostream_from_buffer(&serialized.front(), serialized.size());
63 if (!pb_encode(&outstream, TestMessage_fields, &source)) {
64 fprintf(stderr, "Failed to encode: %s\n", PB_GET_ERROR(&outstream));
65 return 1;
66 }
67
68
69 TestMessage destination;
70 pb_istream_t instream = pb_istream_from_buffer(&serialized.front(), outstream.bytes_written);
71 if (!pb_decode(&instream, TestMessage_fields, &destination)) {
72 fprintf(stderr, "Failed to decode: %s\n", PB_GET_ERROR(&instream));
73 return 2;
74 }
75 if (source.values != destination.values) {
76 fprintf(stderr, "Result does not match\n");
77 fprintf(stderr, "source(%d): ", (int)source.values.size());
78 for (std::vector<int>::iterator i = source.values.begin(); i != source.values.end(); ++i)
79 {
80 fprintf(stderr, "%d, ", *i);
81 }
82 fprintf(stderr, "\nencoded(%d): ", (int)serialized.size());
83 for (unsigned i = 0; i != std::min(serialized.size(), outstream.bytes_written); ++i) {
84 fprintf(stderr, "0x%02x ", serialized[i]);
85 }
86 fprintf(stderr, "\ndestination(%d): ", (int)destination.values.size());
87 for (std::vector<int>::iterator i = destination.values.begin(); i != destination.values.end(); ++i)
88 {
89 fprintf(stderr, "%d, ", *i);
90 }
91 fprintf(stderr, "\n");
92 return 3;
93 }
94
95 return 0;
96}
#define SubMessage_fields
bool TestMessage_values_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_t *field)
bool TestMessage_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_t *field)
#define stderr
Definition mos_stdio.h:32
#define PB_GET_ERROR(stream)
Definition pb.h:891
pb_field_iter_t pb_field_t
Definition pb.h:359
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1182
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
Definition pb_decode.c:143
bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_iter_t *field)
Definition pb_encode.c:681
bool pb_encode_submessage(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
Definition pb_encode.c:724
bool pb_get_encoded_size(size_t *size, const pb_msgdesc_t *fields, const void *src_struct)
Definition pb_encode.c:557
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
Definition pb_encode.c:63
bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
Definition pb_encode.c:512
#define NULL
Definition pb_syshdr.h:46
size_t size
Definition slab.c:30