Skip to content

Commit

Permalink
Repeated example.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Jan 12, 2020
1 parent 7193562 commit 618e668
Show file tree
Hide file tree
Showing 5 changed files with 385 additions and 0 deletions.
18 changes: 18 additions & 0 deletions examples/c/repeated/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
all:
gcc \
-O2 \
-Wall \
-I../../../pbtools/c_source \
-Igenerated \
generated/*.c \
../../../pbtools/c_source/pbtools.c \
main.c \
-o main
./main

generate:
rm -rf generated
mkdir -p generated
cd generated && \
env PYTHONPATH=../../../.. \
python3 -m pbtools generate_c_source ../repeated.proto
180 changes: 180 additions & 0 deletions examples/c/repeated/generated/repeated.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2019 Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* This file was generated by pbtools.
*/

#include <limits.h>
#include "repeated.h"

#if CHAR_BIT != 8
# error "Number of bits in a char must be 8."
#endif

void coordinates_init(
struct coordinates_t *self_p,
struct pbtools_heap_t *heap_p)
{
self_p->base.heap_p = heap_p;
self_p->xs.length = 0;
self_p->ys.length = 0;
}

void coordinates_encode_inner(
struct pbtools_encoder_t *encoder_p,
struct coordinates_t *self_p)
{
pbtools_encoder_write_repeated_int32(encoder_p, 2, &self_p->ys);
pbtools_encoder_write_repeated_int32(encoder_p, 1, &self_p->xs);
}

void coordinates_decode_inner(
struct pbtools_decoder_t *decoder_p,
struct coordinates_t *self_p)
{
int wire_type;
struct pbtools_repeated_info_t repeated_info_xs;
struct pbtools_repeated_info_t repeated_info_ys;

pbtools_repeated_info_init(&repeated_info_xs, 1, decoder_p);
pbtools_repeated_info_init(&repeated_info_ys, 2, decoder_p);

while (pbtools_decoder_available(decoder_p)) {
switch (pbtools_decoder_read_tag(decoder_p, &wire_type)) {

case 1:
pbtools_repeated_info_decode_int32(
&repeated_info_xs,
decoder_p,
wire_type);
break;

case 2:
pbtools_repeated_info_decode_int32(
&repeated_info_ys,
decoder_p,
wire_type);
break;

default:
pbtools_decoder_skip_field(decoder_p, wire_type);
break;
}
}

pbtools_decoder_decode_repeated_int32(
decoder_p,
&repeated_info_xs,
&self_p->xs);
pbtools_decoder_decode_repeated_int32(
decoder_p,
&repeated_info_ys,
&self_p->ys);
}

int coordinates_xs_alloc(
struct coordinates_t *self_p,
int length)
{
return (pbtools_alloc_repeated_int32(
&self_p->base,
length,
&self_p->xs));
}

int coordinates_ys_alloc(
struct coordinates_t *self_p,
int length)
{
return (pbtools_alloc_repeated_int32(
&self_p->base,
length,
&self_p->ys));
}

void coordinates_encode_repeated_inner(
struct pbtools_encoder_t *encoder_p,
int field_number,
struct coordinates_repeated_t *repeated_p)
{
pbtools_encode_repeated_inner(
encoder_p,
field_number,
(struct pbtools_repeated_message_t *)repeated_p,
sizeof(struct coordinates_t),
(pbtools_message_encode_inner_t)coordinates_encode_inner);
}

void coordinates_decode_repeated_inner(
struct pbtools_repeated_info_t *repeated_info_p,
struct pbtools_decoder_t *decoder_p,
struct coordinates_repeated_t *repeated_p)
{
pbtools_decode_repeated_inner(
repeated_info_p,
decoder_p,
(struct pbtools_repeated_message_t *)repeated_p,
sizeof(struct coordinates_t),
(pbtools_message_init_t)coordinates_init,
(pbtools_message_decode_inner_t)coordinates_decode_inner);
}

struct coordinates_t *
coordinates_new(
void *workspace_p,
size_t size)
{
return (pbtools_message_new(
workspace_p,
size,
sizeof(struct coordinates_t),
(pbtools_message_init_t)coordinates_init));
}

int coordinates_encode(
struct coordinates_t *self_p,
uint8_t *encoded_p,
size_t size)
{
return (pbtools_message_encode(
&self_p->base,
encoded_p,
size,
(pbtools_message_encode_inner_t)coordinates_encode_inner));
}

int coordinates_decode(
struct coordinates_t *self_p,
const uint8_t *encoded_p,
size_t size)
{
return (pbtools_message_decode(
&self_p->base,
encoded_p,
size,
(pbtools_message_decode_inner_t)coordinates_decode_inner));
}
108 changes: 108 additions & 0 deletions examples/c/repeated/generated/repeated.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2019 Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* This file was generated by pbtools.
*/

#ifndef REPEATED_H
#define REPEATED_H

#ifdef __cplusplus
extern "C" {
#endif

#include "pbtools.h"

/**
* Message Coordinates.
*/
struct coordinates_repeated_t {
int length;
struct coordinates_t *items_p;
};

struct coordinates_t {
struct pbtools_message_base_t base;
struct pbtools_repeated_int32_t xs;
struct pbtools_repeated_int32_t ys;
};

int coordinates_xs_alloc(
struct coordinates_t *self_p,
int length);

int coordinates_ys_alloc(
struct coordinates_t *self_p,
int length);

/**
* Encoding and decoding of Coordinates.
*/
struct coordinates_t *
coordinates_new(
void *workspace_p,
size_t size);

int coordinates_encode(
struct coordinates_t *self_p,
uint8_t *encoded_p,
size_t size);

int coordinates_decode(
struct coordinates_t *self_p,
const uint8_t *encoded_p,
size_t size);

/* Internal functions. Do not use! */

void coordinates_init(
struct coordinates_t *self_p,
struct pbtools_heap_t *heap_p);

void coordinates_encode_inner(
struct pbtools_encoder_t *encoder_p,
struct coordinates_t *self_p);

void coordinates_decode_inner(
struct pbtools_decoder_t *decoder_p,
struct coordinates_t *self_p);

void coordinates_encode_repeated_inner(
struct pbtools_encoder_t *encoder_p,
int field_number,
struct coordinates_repeated_t *repeated_p);

void coordinates_decode_repeated_inner(
struct pbtools_repeated_info_t *repeated_info_p,
struct pbtools_decoder_t *decoder_p,
struct coordinates_repeated_t *repeated_p);

#ifdef __cplusplus
}
#endif

#endif
73 changes: 73 additions & 0 deletions examples/c/repeated/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <stdio.h>
#include "repeated.h"

static int32_t xs[] = { 1, 3, 5, 7, 9 };

int main(int argc, const char *argv[])
{
int i;
int res;
int size;
uint8_t workspace[128];
uint8_t encoded[64];
struct coordinates_t *coordinates_p;

/* Encode. */
coordinates_p = coordinates_new(&workspace[0], sizeof(workspace));

if (coordinates_p == NULL) {
return (1);
}

/* Set the length and items. */
coordinates_p->xs.length = 5;
coordinates_p->xs.items_p = &xs[0];

/* Alternatively, allocate a buffer in the workspace. */
res = coordinates_ys_alloc(coordinates_p, 5);

if (res != 0) {
return (2);
}

coordinates_p->ys.items_p[0] = 0;
coordinates_p->ys.items_p[1] = 2;
coordinates_p->ys.items_p[2] = 4;
coordinates_p->ys.items_p[3] = 6;
coordinates_p->ys.items_p[4] = 8;

size = coordinates_encode(coordinates_p, &encoded[0], sizeof(encoded));

if (size < 0) {
return (3);
}

printf("Successfully encoded Coordinates into %d bytes.\n", size);

/* Decode. */
coordinates_p = coordinates_new(&workspace[0], sizeof(workspace));

if (coordinates_p == NULL) {
return (4);
}

size = coordinates_decode(coordinates_p, &encoded[0], size);

if (size < 0) {
return (5);
}

printf("Successfully decoded %d bytes into Coordinates.\n", size);

if (coordinates_p->xs.length != coordinates_p->ys.length) {
return (6);
}

for (i = 0; i < coordinates_p->xs.length; i++) {
printf("(%d, %d)\n",
(int)coordinates_p->xs.items_p[i],
(int)coordinates_p->ys.items_p[i]);
}

return (0);
}
6 changes: 6 additions & 0 deletions examples/c/repeated/repeated.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";

message Coordinates {
repeated int32 xs = 1;
repeated int32 ys = 2;
}

0 comments on commit 618e668

Please sign in to comment.