From cfdb9907cb87d15eaab72ceefbfa42fd7a4c3127 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Sat, 11 May 2013 16:45:38 -0700 Subject: Synced with 3 months of Google-internal development. Major changes: - Got rid of all bytestream interfaces in favor of using regular handlers. - new Pipeline object represents a upb pipeline, does bump allocation internally to manage memory. - proto2 support now can handle extensions. --- upb/descriptor/reader.h | 106 ++++++++++++++++++++++-------------------------- 1 file changed, 49 insertions(+), 57 deletions(-) (limited to 'upb/descriptor/reader.h') diff --git a/upb/descriptor/reader.h b/upb/descriptor/reader.h index 4312682..87f5f86 100644 --- a/upb/descriptor/reader.h +++ b/upb/descriptor/reader.h @@ -4,9 +4,8 @@ * Copyright (c) 2011 Google Inc. See LICENSE for details. * Author: Josh Haberman * - * upb_descreader provides a set of sink handlers that will build defs from a - * data source that uses the descriptor.proto schema (like a protobuf binary - * descriptor). + * upb::descriptor::Reader provides a way of building upb::Defs from + * data in descriptor.proto format. */ #ifndef UPB_DESCRIPTOR_H @@ -15,69 +14,62 @@ #include "upb/handlers.h" #ifdef __cplusplus -extern "C" { -#endif +namespace upb { +namespace descriptor { -/* upb_deflist ****************************************************************/ - -// upb_deflist is an internal-only dynamic array for storing a growing list of -// upb_defs. -typedef struct { - upb_def **defs; - size_t len; - size_t size; - bool owned; -} upb_deflist; - -void upb_deflist_init(upb_deflist *l); -void upb_deflist_uninit(upb_deflist *l); -void upb_deflist_push(upb_deflist *l, upb_def *d); - -/* upb_descreader ************************************************************/ - -// We keep a stack of all the messages scopes we are currently in, as well as -// the top-level file scope. This is necessary to correctly qualify the -// definitions that are contained inside. "name" tracks the name of the -// message or package (a bare name -- not qualified by any enclosing scopes). -typedef struct { - char *name; - // Index of the first def that is under this scope. For msgdefs, the - // msgdef itself is at start-1. - int start; -} upb_descreader_frame; - -typedef struct { - upb_deflist defs; - upb_descreader_frame stack[UPB_MAX_TYPE_DEPTH]; - int stack_len; - upb_status status; - - uint32_t number; - char *name; - bool saw_number; - bool saw_name; - - char *default_string; - - upb_fielddef *f; -} upb_descreader; - -void upb_descreader_init(upb_descreader *r); -void upb_descreader_uninit(upb_descreader *r); - -// Registers handlers that will build the defs. Pass the descreader as the -// closure. -const upb_handlers *upb_descreader_newhandlers(const void *owner); +// Frame type that accumulates defs as they are being built from a descriptor +// according to the descriptor.proto schema. +class Reader; // Gets the array of defs that have been parsed and removes them from the // descreader. Ownership of the defs is passed to the caller using the given // owner), but the ownership of the returned array is retained and is // invalidated by any other call into the descreader. The defs will not have // been resolved, and are ready to be added to a symtab. +inline upb::Def** GetDefs(Reader* r, void* owner, int* n); + +// Gets the handlers for reading a FileDescriptorSet, which builds defs and +// accumulates them in a Reader object (which the handlers use as their +// FrameType). +inline const upb::Handlers* GetReaderHandlers(const void* owner); + +} // namespace descriptor +} // namespace upb + +typedef upb::descriptor::Reader upb_descreader; + +extern "C" { +#else +struct upb_descreader; +typedef struct upb_descreader upb_descreader; +#endif + +// C API. +const upb_frametype *upb_descreader_getframetype(); upb_def **upb_descreader_getdefs(upb_descreader *r, void *owner, int *n); +const upb_handlers *upb_descreader_gethandlers(const void *owner); + + +// C++ implementation details. ///////////////////////////////////////////////// #ifdef __cplusplus -} /* extern "C" */ +} // extern "C" + +namespace upb { + +template<> inline const FrameType* GetFrameType() { + return upb_descreader_getframetype(); +} + +namespace descriptor { +inline upb::Def** GetDefs(Reader* r, void* owner, int* n) { + return upb_descreader_getdefs(r, owner, n); +} +inline const upb::Handlers* GetReaderHandlers(const void* owner) { + return upb_descreader_gethandlers(owner); +} +} // namespace descriptor +} // namespace upb #endif -#endif +#endif // UPB_DESCRIPTOR_H -- cgit v1.2.3