From c03802b0f18a1a654f832ddff01a5bd78efbc4d6 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 24 Aug 2011 15:45:47 -0700 Subject: Some updates to the experimental C++ wrapper. --- bindings/cpp/upb/handlers.hpp | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/bindings/cpp/upb/handlers.hpp b/bindings/cpp/upb/handlers.hpp index b083f15..e72c0a4 100644 --- a/bindings/cpp/upb/handlers.hpp +++ b/bindings/cpp/upb/handlers.hpp @@ -17,11 +17,12 @@ #ifndef UPB_HANDLERS_HPP #define UPB_HANDLERS_HPP -#include "upb_handlers.h" +#include "upb/handlers.h" namespace upb { typedef upb_flow_t Flow; +class MessageHandlers; class FieldHandlers : public upb_fhandlers { public: @@ -32,8 +33,8 @@ class FieldHandlers : public upb_fhandlers { // The FieldHandlers will live at least as long as the upb::Handlers to // which it belongs, but can be Ref'd/Unref'd to make it live longer (which // will prolong the life of the underlying upb::Handlers also). - void Ref() { upb_fhandlers_ref(this); } - void Unref() { upb_fhandlers_unref(this); } + void Ref() const { upb_fhandlers_ref(this); } + void Unref() const { upb_fhandlers_unref(this); } // Functions to set this field's handlers. // These return "this" so they can be conveniently chained, eg. @@ -58,11 +59,27 @@ class FieldHandlers : public upb_fhandlers { } // Get/Set the field's bound value, which will be passed to its handlers. - Value GetBoundValue() { return upb_fhandlers_getfval(this); } + Value GetBoundValue() const { return upb_fhandlers_getfval(this); } FieldHandlers* SetBoundValue(Value val) { upb_fhandlers_setfval(this, val); return this; } + // Returns the MessageHandlers to which we belong. + MessageHandlers* GetMessageHandlers() const { + return upb_fhandlers_msg(this); + } + + // Returns the MessageHandlers for this field's submessage (invalid to call + // unless this field's type UPB_TYPE(MESSAGE) or UPB_TYPE(GROUP). + MessageHandlers* GetSubMessageHandlers() const { + return upb_fhandlers_submsg(this); + } + + // If set to >=0, the given hasbit will be set after the value callback is + // called (relative to the current closure). + int32_t GetValueHasbit() const { return upb_fhandler_valuehasbit(this); } + void SetValueHasbit(int32_t bit) { upb_fhandler_setvaluehasbit(this, bit); } + private: FieldHandlers(); // Only created by upb::Handlers. ~FieldHandlers(); // Only destroyed by refcounting. @@ -77,8 +94,8 @@ class MessageHandlers : public upb_mhandlers { // The MessageHandlers will live at least as long as the upb::Handlers to // which it belongs, but can be Ref'd/Unref'd to make it live longer (which // will prolong the life of the underlying upb::Handlers also). - void Ref() { upb_mhandlers_ref(this); } - void Unref() { upb_mhandlers_unref(this); } + void Ref() const { upb_mhandlers_ref(this); } + void Unref() const { upb_mhandlers_unref(this); } // Functions to set this message's handlers. // These return "this" so they can be conveniently chained, eg. @@ -103,8 +120,8 @@ class MessageHandlers : public upb_mhandlers { // Like the previous but for MESSAGE or GROUP fields. For GROUP fields, the // given submessage must not have any fields with this field number. - FieldHandlers* NewFieldHandlersForSubmessage(uint32_t n, FieldType type, - bool repeated, + FieldHandlers* NewFieldHandlersForSubmessage(uint32_t n, const char *name, + FieldType type, bool repeated, MessageHandlers* subm) { return upb_mhandlers_newsubmsgfhandlers(this, n, type, repeated, subm); } @@ -130,7 +147,13 @@ class Handlers : public upb_handlers { // Returns a new MessageHandlers object. The first such message that is // obtained will be the top-level message for this Handlers object. - MessageHandlers* NewMessageHandlers() { return upb_handlers_newmhandlers(); } + MessageHandlers* NewMessageHandlers() { return upb_handlers_newmhandlers(this); } + + // Freezes the handlers against future modification. Handlers must be + // finalized before they can be passed to a data producer. After Finalize() + // has been called, you may only call const methods on the Handlers and its + // MessageHandlers/FieldHandlers. + void Finalize() { upb_handlers_finalize(this); } private: FieldHandlers(); // Only created by Handlers::New(). -- cgit v1.2.3