summaryrefslogtreecommitdiff
path: root/upb/pb
diff options
context:
space:
mode:
authorJoshua Haberman <jhaberman@gmail.com>2019-01-12 16:15:46 -0800
committerJoshua Haberman <jhaberman@gmail.com>2019-01-12 16:15:46 -0800
commitd2f9bec5c6f3c34362cf13e35e11d3dbc7888a32 (patch)
tree7a2d1f3e34ea5ad6486cbb56da8b6ed49a123690 /upb/pb
parent0553eff64a87eceff0de3b6260b4f2d45b61703a (diff)
Removed old-style C++ handlers that relied on UB in favor of more normal ones.
Diffstat (limited to 'upb/pb')
-rw-r--r--upb/pb/compile_decoder.c8
-rw-r--r--upb/pb/decoder.h221
-rw-r--r--upb/pb/encoder.c10
-rw-r--r--upb/pb/encoder.h70
-rw-r--r--upb/pb/textprinter.c6
5 files changed, 144 insertions, 171 deletions
diff --git a/upb/pb/compile_decoder.c b/upb/pb/compile_decoder.c
index 02f5179..e17ca03 100644
--- a/upb/pb/compile_decoder.c
+++ b/upb/pb/compile_decoder.c
@@ -516,7 +516,7 @@ static upb_pbdecodermethod *find_submethod(const compiler *c,
static void putsel(compiler *c, opcode op, upb_selector_t sel,
const upb_handlers *h) {
- if (upb_handlers_gethandler(h, sel)) {
+ if (upb_handlers_gethandler(h, sel, NULL)) {
putop(c, op, sel);
}
}
@@ -532,9 +532,9 @@ static bool haslazyhandlers(const upb_handlers *h, const upb_fielddef *f) {
if (!upb_fielddef_lazy(f))
return false;
- return upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_STARTSTR)) ||
- upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_STRING)) ||
- upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_ENDSTR));
+ return upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_STARTSTR), NULL) ||
+ upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_STRING), NULL) ||
+ upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_ENDSTR), NULL);
}
diff --git a/upb/pb/decoder.h b/upb/pb/decoder.h
index 1a00801..1ffcb7d 100644
--- a/upb/pb/decoder.h
+++ b/upb/pb/decoder.h
@@ -21,17 +21,13 @@
namespace upb {
namespace pb {
class CodeCache;
-class Decoder;
-class DecoderMethod;
+class DecoderPtr;
+class DecoderMethodPtr;
class DecoderMethodOptions;
} /* namespace pb */
} /* namespace upb */
#endif
-UPB_DECLARE_TYPE(upb::pb::CodeCache, upb_pbcodecache)
-UPB_DECLARE_TYPE(upb::pb::Decoder, upb_pbdecoder)
-UPB_DECLARE_TYPE(upb::pb::DecoderMethod, upb_pbdecodermethod)
-
/* The maximum number of bytes we are required to buffer internally between
* calls to the decoder. The value is 14: a 5 byte unknown tag plus ten-byte
* varint, less one because we are buffering an incomplete value.
@@ -39,54 +35,106 @@ UPB_DECLARE_TYPE(upb::pb::DecoderMethod, upb_pbdecodermethod)
* Should only be used by unit tests. */
#define UPB_DECODER_MAX_RESIDUAL_BYTES 14
+/* upb_pbdecodermethod ********************************************************/
+
+struct upb_pbdecodermethod;
+typedef struct upb_pbdecodermethod upb_pbdecodermethod;
+
+UPB_BEGIN_EXTERN_C
+
+const upb_handlers *upb_pbdecodermethod_desthandlers(
+ const upb_pbdecodermethod *m);
+const upb_byteshandler *upb_pbdecodermethod_inputhandler(
+ const upb_pbdecodermethod *m);
+bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m);
+
+UPB_END_EXTERN_C
+
#ifdef __cplusplus
/* Represents the code to parse a protobuf according to a destination
* Handlers. */
-class upb::pb::DecoderMethod {
+class upb::pb::DecoderMethodPtr {
public:
+ DecoderMethodPtr(const upb_pbdecodermethod* ptr) : ptr_(ptr) {}
+
+ const upb_pbdecodermethod* ptr() { return ptr_; }
+
/* The destination handlers that are statically bound to this method.
* This method is only capable of outputting to a sink that uses these
* handlers. */
- const Handlers* dest_handlers() const;
+ const Handlers *dest_handlers() const {
+ return upb_pbdecodermethod_desthandlers(ptr_);
+ }
/* The input handlers for this decoder method. */
- const BytesHandler* input_handler() const;
+ const BytesHandler* input_handler() const {
+ return upb_pbdecodermethod_inputhandler(ptr_);
+ }
/* Whether this method is native. */
- bool is_native() const;
+ bool is_native() const {
+ return upb_pbdecodermethod_isnative(ptr_);
+ }
private:
- UPB_DISALLOW_POD_OPS(DecoderMethod, upb::pb::DecoderMethod)
+ const upb_pbdecodermethod* ptr_;
};
#endif
+/* upb_pbdecoder **************************************************************/
+
/* Preallocation hint: decoder won't allocate more bytes than this when first
* constructed. This hint may be an overestimate for some build configurations.
* But if the decoder library is upgraded without recompiling the application,
* it may be an underestimate. */
#define UPB_PB_DECODER_SIZE 4416
+struct upb_pbdecoder;
+typedef struct upb_pbdecoder upb_pbdecoder;
+
+UPB_BEGIN_EXTERN_C
+
+upb_pbdecoder *upb_pbdecoder_create(upb_env *e,
+ const upb_pbdecodermethod *method,
+ upb_sink *output);
+const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d);
+upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d);
+uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d);
+size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d);
+bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max);
+void upb_pbdecoder_reset(upb_pbdecoder *d);
+
+UPB_END_EXTERN_C
+
#ifdef __cplusplus
/* A Decoder receives binary protobuf data on its input sink and pushes the
* decoded data to its output sink. */
-class upb::pb::Decoder {
+class upb::pb::DecoderPtr {
public:
+ DecoderPtr(upb_pbdecoder* ptr) : ptr_(ptr) {}
+
+ upb_pbdecoder* ptr() { return ptr_; }
+
/* Constructs a decoder instance for the given method, which must outlive this
* decoder. Any errors during parsing will be set on the given status, which
* must also outlive this decoder.
*
* The sink must match the given method. */
- static Decoder* Create(Environment* env, const DecoderMethod* method,
- Sink* output);
+ static DecoderPtr Create(Environment *env, DecoderMethodPtr method,
+ upb_sink *output) {
+ return DecoderPtr(upb_pbdecoder_create(env, method.ptr(), output));
+ }
/* Returns the DecoderMethod this decoder is parsing from. */
- const DecoderMethod* method() const;
+ const DecoderMethodPtr method() const {
+ return DecoderMethodPtr(upb_pbdecoder_method(ptr_));
+ }
/* The sink on which this decoder receives input. */
- BytesSink* input();
+ upb_bytessink* input() { return upb_pbdecoder_input(ptr()); }
/* Returns number of bytes successfully parsed.
*
@@ -95,7 +143,7 @@ class upb::pb::Decoder {
*
* This value may not be up-to-date when called from inside a parsing
* callback. */
- uint64_t BytesParsed() const;
+ uint64_t BytesParsed() { return upb_pbdecoder_bytesparsed(ptr()); }
/* Gets/sets the parsing nexting limit. If the total number of nested
* submessages and repeated fields hits this limit, parsing will fail. This
@@ -104,25 +152,51 @@ class upb::pb::Decoder {
*
* Setting the limit will fail if the parser is currently suspended at a depth
* greater than this, or if memory allocation of the stack fails. */
- size_t max_nesting() const;
- bool set_max_nesting(size_t max);
+ size_t max_nesting() { return upb_pbdecoder_maxnesting(ptr()); }
+ bool set_max_nesting(size_t max) { return upb_pbdecoder_maxnesting(ptr()); }
- void Reset();
+ void Reset() { upb_pbdecoder_reset(ptr()); }
static const size_t kSize = UPB_PB_DECODER_SIZE;
private:
- UPB_DISALLOW_POD_OPS(Decoder, upb::pb::Decoder)
+ upb_pbdecoder *ptr_;
};
+#endif /* __cplusplus */
+
+/* upb_pbcodecache ************************************************************/
+
+struct upb_pbcodecache;
+typedef struct upb_pbcodecache upb_pbcodecache;
+
+UPB_BEGIN_EXTERN_C
+
+upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest);
+void upb_pbcodecache_free(upb_pbcodecache *c);
+bool upb_pbcodecache_allowjit(const upb_pbcodecache *c);
+void upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow);
+void upb_pbcodecache_setlazy(upb_pbcodecache *c, bool lazy);
+const upb_pbdecodermethod *upb_pbcodecache_get(upb_pbcodecache *c,
+ const upb_msgdef *md);
+
+UPB_END_EXTERN_C
+
+#ifdef __cplusplus
+
/* A class for caching protobuf processing code, whether bytecode for the
* interpreted decoder or machine code for the JIT.
*
* This class is not thread-safe. */
class upb::pb::CodeCache {
public:
- static CodeCache* New(HandlerCache* dest);
- static void Free(CodeCache* cache);
+ CodeCache(upb::HandlerCache *dest)
+ : ptr_(upb_pbcodecache_new(dest->ptr()), upb_pbcodecache_free) {}
+ CodeCache(CodeCache&&) = default;
+ CodeCache& operator=(CodeCache&&) = default;
+
+ upb_pbcodecache* ptr() { return ptr_.get(); }
+ const upb_pbcodecache* ptr() const { return ptr_.get(); }
/* Whether the cache is allowed to generate machine code. Defaults to true.
* There is no real reason to turn it off except for testing or if you are
@@ -131,114 +205,27 @@ class upb::pb::CodeCache {
* Note that allow_jit = true does not *guarantee* that the code will be JIT
* compiled. If this platform is not supported or the JIT was not compiled
* in, the code may still be interpreted. */
- bool allow_jit() const;
+ bool allow_jit() const { return upb_pbcodecache_allowjit(ptr()); }
/* This may only be called when the object is first constructed, and prior to
* any code generation. */
- void set_allow_jit(bool allow);
+ void set_allow_jit(bool allow) { upb_pbcodecache_setallowjit(ptr(), allow); }
/* Should the decoder push submessages to lazy handlers for fields that have
* them? The caller should set this iff the lazy handlers expect data that is
* in protobuf binary format and the caller wishes to lazy parse it. */
- void set_lazy(bool lazy);
+ void set_lazy(bool lazy) { upb_pbcodecache_setlazy(ptr(), lazy); }
/* Returns a DecoderMethod that can push data to the given handlers.
* If a suitable method already exists, it will be returned from the cache. */
- const DecoderMethod *Get(const MessageDef* md);
+ const DecoderMethodPtr Get(MessageDefPtr md) {
+ return DecoderMethodPtr(upb_pbcodecache_get(ptr(), md.ptr()));
+ }
private:
- UPB_DISALLOW_POD_OPS(CodeCache, upb::pb::CodeCache)
+ std::unique_ptr<upb_pbcodecache, decltype(&upb_pbcodecache_free)> ptr_;
};
-#endif
-
-UPB_BEGIN_EXTERN_C
-
-upb_pbdecoder *upb_pbdecoder_create(upb_env *e,
- const upb_pbdecodermethod *method,
- upb_sink *output);
-const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d);
-upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d);
-uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d);
-size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d);
-bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max);
-void upb_pbdecoder_reset(upb_pbdecoder *d);
-
-
-
-const upb_handlers *upb_pbdecodermethod_desthandlers(
- const upb_pbdecodermethod *m);
-const upb_byteshandler *upb_pbdecodermethod_inputhandler(
- const upb_pbdecodermethod *m);
-bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m);
-
-upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest);
-void upb_pbcodecache_free(upb_pbcodecache *c);
-bool upb_pbcodecache_allowjit(const upb_pbcodecache *c);
-void upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow);
-void upb_pbcodecache_setlazy(upb_pbcodecache *c, bool lazy);
-const upb_pbdecodermethod *upb_pbcodecache_get(upb_pbcodecache *c,
- const upb_msgdef *md);
-
-UPB_END_EXTERN_C
-
-#ifdef __cplusplus
-
-namespace upb {
-
-namespace pb {
-
-/* static */
-inline Decoder* Decoder::Create(Environment* env, const DecoderMethod* m,
- Sink* sink) {
- return upb_pbdecoder_create(env, m, sink);
-}
-inline const DecoderMethod* Decoder::method() const {
- return upb_pbdecoder_method(this);
-}
-inline BytesSink* Decoder::input() {
- return upb_pbdecoder_input(this);
-}
-inline uint64_t Decoder::BytesParsed() const {
- return upb_pbdecoder_bytesparsed(this);
-}
-inline size_t Decoder::max_nesting() const {
- return upb_pbdecoder_maxnesting(this);
-}
-inline bool Decoder::set_max_nesting(size_t max) {
- return upb_pbdecoder_setmaxnesting(this, max);
-}
-inline void Decoder::Reset() { upb_pbdecoder_reset(this); }
-
-inline const Handlers* DecoderMethod::dest_handlers() const {
- return upb_pbdecodermethod_desthandlers(this);
-}
-inline const BytesHandler* DecoderMethod::input_handler() const {
- return upb_pbdecodermethod_inputhandler(this);
-}
-inline bool DecoderMethod::is_native() const {
- return upb_pbdecodermethod_isnative(this);
-}
-
-inline CodeCache* CodeCache::New(HandlerCache* dest) {
- return upb_pbcodecache_new(dest);
-}
-inline void CodeCache::Free(CodeCache* cache) {
- upb_pbcodecache_free(cache);
-}
-inline bool CodeCache::allow_jit() const {
- return upb_pbcodecache_allowjit(this);
-}
-inline void CodeCache::set_allow_jit(bool allow) {
- upb_pbcodecache_setallowjit(this, allow);
-}
-inline const DecoderMethod *CodeCache::Get(const MessageDef *md) {
- return upb_pbcodecache_get(this, md);
-}
-
-} /* namespace pb */
-} /* namespace upb */
-
#endif /* __cplusplus */
#endif /* UPB_DECODER_H_ */
diff --git a/upb/pb/encoder.c b/upb/pb/encoder.c
index ca3ca5c..3497007 100644
--- a/upb/pb/encoder.c
+++ b/upb/pb/encoder.c
@@ -304,8 +304,7 @@ static void new_tag(upb_handlers *h, const upb_fielddef *f, upb_wiretype_t wt,
tag_t *tag = upb_gmalloc(sizeof(tag_t));
tag->bytes = upb_vencode64((n << 3) | wt, tag->tag);
- upb_handlerattr_init(attr);
- upb_handlerattr_sethandlerdata(attr, tag);
+ attr->handler_data = tag;
upb_handlers_addcleanup(h, tag, upb_gfree);
}
@@ -451,7 +450,7 @@ static void newhandlers_callback(const void *closure, upb_handlers *h) {
const upb_fielddef *f = upb_msg_iter_field(&i);
bool packed = upb_fielddef_isseq(f) && upb_fielddef_isprimitive(f) &&
upb_fielddef_packed(f);
- upb_handlerattr attr;
+ upb_handlerattr attr = UPB_HANDLERATTR_INIT;
upb_wiretype_t wt =
packed ? UPB_WIRE_TYPE_DELIMITED
: upb_pb_native_wire_types[upb_fielddef_descriptortype(f)];
@@ -500,20 +499,17 @@ static void newhandlers_callback(const void *closure, upb_handlers *h) {
break;
case UPB_DESCRIPTOR_TYPE_GROUP: {
/* Endgroup takes a different tag (wire_type = END_GROUP). */
- upb_handlerattr attr2;
+ upb_handlerattr attr2 = UPB_HANDLERATTR_INIT;
new_tag(h, f, UPB_WIRE_TYPE_END_GROUP, &attr2);
upb_handlers_setstartsubmsg(h, f, encode_startgroup, &attr);
upb_handlers_setendsubmsg(h, f, encode_endgroup, &attr2);
- upb_handlerattr_uninit(&attr2);
break;
}
}
#undef T
-
- upb_handlerattr_uninit(&attr);
}
}
diff --git a/upb/pb/encoder.h b/upb/pb/encoder.h
index eefa385..20ce606 100644
--- a/upb/pb/encoder.h
+++ b/upb/pb/encoder.h
@@ -17,16 +17,14 @@
#ifdef __cplusplus
namespace upb {
namespace pb {
-class Encoder;
+class EncoderPtr;
} /* namespace pb */
} /* namespace upb */
#endif
-UPB_DECLARE_TYPE(upb::pb::Encoder, upb_pb_encoder)
-
#define UPB_PBENCODER_MAX_NESTING 100
-/* upb::pb::Encoder ***********************************************************/
+/* upb_pb_encoder *************************************************************/
/* Preallocation hint: decoder won't allocate more bytes than this when first
* constructed. This hint may be an overestimate for some build configurations.
@@ -34,56 +32,48 @@ UPB_DECLARE_TYPE(upb::pb::Encoder, upb_pb_encoder)
* it may be an underestimate. */
#define UPB_PB_ENCODER_SIZE 768
+struct upb_pb_encoder;
+typedef struct upb_pb_encoder upb_pb_encoder;
+
+UPB_BEGIN_EXTERN_C
+
+upb_sink *upb_pb_encoder_input(upb_pb_encoder *p);
+upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h,
+ upb_bytessink* output);
+
+upb_handlercache *upb_pb_encoder_newcache();
+
+UPB_END_EXTERN_C
+
#ifdef __cplusplus
-class upb::pb::Encoder {
+class upb::pb::EncoderPtr {
public:
+ EncoderPtr(upb_pb_encoder* ptr) : ptr_(ptr) {}
+
+ upb_pb_encoder* ptr() { return ptr_; }
+
/* Creates a new encoder in the given environment. The Handlers must have
* come from NewHandlers() below. */
- static Encoder* Create(Environment* env, const Handlers* handlers,
- BytesSink* output);
+ static EncoderPtr Create(Environment* env, const Handlers* handlers,
+ BytesSink* output) {
+ return EncoderPtr(upb_pb_encoder_create(env, handlers, output->ptr()));
+ }
/* The input to the encoder. */
- Sink* input();
+ upb_sink* input() { return upb_pb_encoder_input(ptr()); }
/* Creates a new set of handlers for this MessageDef. */
- static upb_handlercache* NewCache();
+ static HandlerCache NewCache() {
+ return HandlerCache(upb_pb_encoder_newcache());
+ }
static const size_t kSize = UPB_PB_ENCODER_SIZE;
private:
- UPB_DISALLOW_POD_OPS(Encoder, upb::pb::Encoder)
+ upb_pb_encoder* ptr_;
};
-#endif
-
-UPB_BEGIN_EXTERN_C
-
-upb_sink *upb_pb_encoder_input(upb_pb_encoder *p);
-upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h,
- upb_bytessink* output);
-
-upb_handlercache *upb_pb_encoder_newcache();
-
-UPB_END_EXTERN_C
-
-#ifdef __cplusplus
-
-namespace upb {
-namespace pb {
-inline Encoder* Encoder::Create(Environment* env, const Handlers* handlers,
- BytesSink* output) {
- return upb_pb_encoder_create(env, handlers, output);
-}
-inline Sink* Encoder::input() {
- return upb_pb_encoder_input(this);
-}
-inline upb_handlercache* Encoder::NewCache() {
- return upb_pb_encoder_newcache();
-}
-} /* namespace pb */
-} /* namespace upb */
-
-#endif
+#endif /* __cplusplus */
#endif /* UPB_ENCODER_H_ */
diff --git a/upb/pb/textprinter.c b/upb/pb/textprinter.c
index b6f8024..d1d539d 100644
--- a/upb/pb/textprinter.c
+++ b/upb/pb/textprinter.c
@@ -260,8 +260,8 @@ static void onmreg(const void *c, upb_handlers *h) {
!upb_msg_field_done(&i);
upb_msg_field_next(&i)) {
upb_fielddef *f = upb_msg_iter_field(&i);
- upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
- upb_handlerattr_sethandlerdata(&attr, f);
+ upb_handlerattr attr = UPB_HANDLERATTR_INIT;
+ attr.handler_data = f;
switch (upb_fielddef_type(f)) {
case UPB_TYPE_INT32:
upb_handlers_setint32(h, f, textprinter_putint32, &attr);
@@ -295,7 +295,7 @@ static void onmreg(const void *c, upb_handlers *h) {
upb_fielddef_descriptortype(f) == UPB_DESCRIPTOR_TYPE_GROUP
? shortname(upb_msgdef_fullname(upb_fielddef_msgsubdef(f)))
: upb_fielddef_name(f);
- upb_handlerattr_sethandlerdata(&attr, name);
+ attr.handler_data = name;
upb_handlers_setstartsubmsg(h, f, textprinter_startsubmsg, &attr);
upb_handlers_setendsubmsg(h, f, textprinter_endsubmsg, &attr);
break;
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback