summaryrefslogtreecommitdiff
path: root/upb/msg.c
blob: c671b7b6c23bdb7ce9e92277e17d372ccacb3a85 (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
43
44
45
46
47
48
49
50
51
52
/*
 * upb - a minimalist implementation of protocol buffers.
 *
 * Copyright (c) 2010 Google Inc.  See LICENSE for details.
 * Author: Josh Haberman <jhaberman@gmail.com>
 *
 */

#include "upb/upb.h"
#include "upb/msg.h"

#define UPB_ACCESSOR(type, ctype)                                             \
  upb_flow_t upb_stdmsg_set ## type (void *_m, upb_value fval,                \
                                     upb_value val) {                         \
    assert(_m != NULL);                                                       \
    const upb_fielddef *f = upb_value_getfielddef(fval);                      \
    uint8_t *m = _m;                                                          \
    /* Hasbit is set automatically by the handlers. */                        \
    *(ctype*)&m[f->offset] = upb_value_get ## type(val);                      \
    return UPB_CONTINUE;                                                      \
  }                                                                           \

UPB_ACCESSOR(double, double)
UPB_ACCESSOR(float, float)
UPB_ACCESSOR(int32, int32_t)
UPB_ACCESSOR(int64, int64_t)
UPB_ACCESSOR(uint32, uint32_t)
UPB_ACCESSOR(uint64, uint64_t)
UPB_ACCESSOR(bool, bool)
UPB_ACCESSOR(ptr, void*)
#undef UPB_ACCESSORS

static void upb_accessors_onfreg(void *c, upb_fhandlers *fh,
                                 const upb_fielddef *f) {
  (void)c;
  if (f->accessor) {
    upb_fhandlers_setfval(fh, f->fval);
    if (upb_isseq(f)) {
      upb_fhandlers_setstartseq(fh, f->accessor->startseq);
      upb_fhandlers_setvalue(fh, f->accessor->append);
      upb_fhandlers_setstartsubmsg(fh, f->accessor->appendsubmsg);
    } else {
      upb_fhandlers_setvalue(fh, f->accessor->set);
      upb_fhandlers_setstartsubmsg(fh, f->accessor->startsubmsg);
      upb_fhandlers_sethasbit(fh, f->hasbit);
    }
  }
}

upb_mhandlers *upb_accessors_reghandlers(upb_handlers *h, const upb_msgdef *m) {
  return upb_handlers_regmsgdef(h, m, NULL, &upb_accessors_onfreg, NULL);
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback