summaryrefslogtreecommitdiff
path: root/pbstruct.c
blob: 34e7880247a91ea8e83a6f1e25aea9d00a6cbdc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
 * pbstream - a stream-oriented implementation of protocol buffers.
 *
 * Copyright (c) 2009 Joshua Haberman.  See LICENSE for details.
 */

#include <stddef.h>
#include "pbstruct.h"

#define alignof(t) offsetof(struct { char c; t x; }, x)
#define ALIGN_UP(p, t) (alignof(t) + ((p - 1) & ~(alignof(t) - 1)))

bool pbstruct_is_set(struct pbstruct *s, struct pbstruct_field *f)
{
  return s->data[f->isset_byte_offset] & f->isset_byte_mask;
}

void pbstruct_unset(struct pbstruct *s, struct pbstruct_field *f)
{
  s->data[f->isset_byte_offset] &= ~f->isset_byte_mask;
}

void pbstruct_set(struct pbstruct *s, struct pbstruct_field *f)
{
  s->data[f->isset_byte_offset] |= f->isset_byte_mask;
}

#define DEFINE_GETTERS(ctype, name) \
  ctype *pbstruct_get_ ## name(struct pbstruct *s, struct pbstruct_field *f) { \
    return (ctype*)(s->data + f->byte_offset); \
  }

DEFINE_GETTERS(double,   double)
DEFINE_GETTERS(float,    float)
DEFINE_GETTERS(int32_t,  int32)
DEFINE_GETTERS(int64_t,  int64)
DEFINE_GETTERS(uint32_t, uint32)
DEFINE_GETTERS(uint64_t, uint64)
DEFINE_GETTERS(bool,     bool)
DEFINE_GETTERS(struct pbstruct_delimited*, bytes)
DEFINE_GETTERS(struct pbstruct_delimited*, string)
DEFINE_GETTERS(struct pbstruct*, substruct)
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback