summaryrefslogtreecommitdiff
path: root/upb/pb/glue.h
blob: 716fc0e8f856ddbc2194075378bb4f3c3ef60ac9 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
** upb's core components like upb_decoder and upb_msg are carefully designed to
** avoid depending on each other for maximum orthogonality.  In other words,
** you can use a upb_decoder to decode into *any* kind of structure; upb_msg is
** just one such structure.  A upb_msg can be serialized/deserialized into any
** format, protobuf binary format is just one such format.
**
** However, for convenience we provide functions here for doing common
** operations like deserializing protobuf binary format into a upb_msg.  The
** compromise is that this file drags in almost all of upb as a dependency,
** which could be undesirable if you're trying to use a trimmed-down build of
** upb.
**
** While these routines are convenient, they do not reuse any encoding/decoding
** state.  For example, if a decoder is JIT-based, it will be re-JITted every
** time these functions are called.  For this reason, if you are parsing lots
** of data and efficiency is an issue, these may not be the best functions to
** use (though they are useful for prototyping, before optimizing).
*/

#ifndef UPB_GLUE_H
#define UPB_GLUE_H

#include <stdbool.h>
#include "upb/def.h"

#ifdef __cplusplus
#include <vector>

extern "C" {
#endif

/* Loads a binary descriptor and returns a NULL-terminated array of unfrozen
 * filedefs.  The caller owns the returned array, which must be freed with
 * upb_gfree(). */
upb_filedef **upb_loaddescriptor(const char *buf, size_t n, const void *owner,
                                 upb_status *status);

#ifdef __cplusplus
}  /* extern "C" */

namespace upb {

inline bool LoadDescriptor(const char* buf, size_t n, Status* status,
                           std::vector<reffed_ptr<FileDef> >* files) {
  FileDef** parsed_files = upb_loaddescriptor(buf, n, &parsed_files, status);

  if (parsed_files) {
    FileDef** p = parsed_files;
    while (*p) {
      files->push_back(reffed_ptr<FileDef>(*p, &parsed_files));
      ++p;
    }
    free(parsed_files);
    return true;
  } else {
    return false;
  }
}

/* Templated so it can accept both string and std::string. */
template <typename T>
bool LoadDescriptor(const T& desc, Status* status,
                    std::vector<reffed_ptr<FileDef> >* files) {
  return LoadDescriptor(desc.c_str(), desc.size(), status, files);
}

}  /* namespace upb */

#endif

#endif  /* UPB_GLUE_H */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback