Skip to content

Commit

Permalink
Add proof-of-concept C++ wrapper header.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Haberman committed May 21, 2011
1 parent 0941664 commit 2ccebb7
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 76 deletions.
2 changes: 1 addition & 1 deletion benchmarks/parsestream.upb_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static bool initialize()

upb_handlers_init(&handlers);
// Cause all messages to be read, but do nothing when they are.
upb_handlerset hset = {NULL, NULL, value, startsubmsg, NULL};
upb_handlerset hset = {NULL, NULL, value, startsubmsg, NULL, NULL, NULL};
upb_handlers_reghandlerset(&handlers, def, &hset);
upb_decoder_init(&decoder, &handlers);
upb_stringsrc_init(&stringsrc);
Expand Down
142 changes: 142 additions & 0 deletions lang_ext/cpp/upb/handlers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* upb - a minimalist implementation of protocol buffers.
*
* Copyright (c) 2011 Google Inc. See LICENSE for details.
* Author: Josh Haberman <[email protected]>
*
* Note! This file is a proof-of-concept for C++ wrappers and does not
* yet build.
*
* upb::Handlers is a generic visitor-like interface for iterating over a
* stream of protobuf data. You can register function pointers that will be
* called for each message and/or field as the data is being parsed or iterated
* over, without having to know the source format that we are parsing from.
* This decouples the parsing logic from the processing logic.
*/

#ifndef UPB_HANDLERS_HPP
#define UPB_HANDLERS_HPP

#include "upb_handlers.h"

namespace upb {

typedef upb_flow_t Flow;

class FieldHandlers : public upb_fhandlers {
public:
typedef upb_value_handler ValueHandler;
typedef upb_startfield_handler StartFieldHandler;
typedef upb_endfield_handler EndFieldHandler;

// 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); }

// Functions to set this field's handlers.
// These return "this" so they can be conveniently chained, eg.
// message_handlers->NewField(...)
// ->SetStartSequenceHandler(&StartSequence),
// ->SetEndSequenceHandler(&EndSequence),
// ->SetValueHandler(&Value);
FieldHandlers* SetValueHandler(ValueHandler* h) {
upb_fhandlers_setvalue(this, h); return this;
}
FieldHandlers* SetStartSequenceHandler(StartFieldHandler* h) {
upb_fhandlers_setstartseq(this, h); return this;
}
FieldHandlers* SetEndSequenceHandler(EndFieldHandler* h) {
upb_fhandlers_endseq(this, h); return this;
}
FieldHandlers* SetStartSubmessageHandler(StartFieldHandler* h) {
upb_fhandlers_setstartsubmsg(this, h); return this;
}
FieldHandlers* SetEndSubmessageHandler(EndFieldHandler* h) {
upb_fhandlers_endsubmsg(this, h); return this;
}

// Get/Set the field's bound value, which will be passed to its handlers.
Value GetBoundValue() { return upb_fhandlers_getfval(this); }
FieldHandlers* SetBoundValue(Value val) {
upb_fhandlers_setfval(this, val); return this;
}

private:
FieldHandlers(); // Only created by upb::Handlers.
~FieldHandlers(); // Only destroyed by refcounting.
};


class MessageHandlers : public upb_mhandlers {
public:
typedef upb_startmsg_handler StartMessageHandler;
typedef upb_endmsg_handler EndMessageHandler;

// 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); }

// Functions to set this message's handlers.
// These return "this" so they can be conveniently chained, eg.
// handlers->NewMessage()
// ->SetStartMessageHandler(&StartMessage)
// ->SetEndMessageHandler(&EndMessage);
MessageHandlers* SetStartMessageHandler(StartMessageHandler* h) {
upb_mhandlers_setstartmsg(this, h); return this;
}
MessageHandlers* SetEndMessageHandler(EndMessageHandler* h) {
upb_mhandlers_setendmsg(this, h); return this;
}

// Functions to create new FieldHandlers for this message.
FieldHandlers* NewFieldHandlers(uint32_t fieldnum, upb_fieldtype_t type,
bool repeated) {
return upb_mhandlers_newfhandlers(this, fieldnum, type, repeated);
}
FieldHandlers* NewFieldHandlers(FieldDef* f) {
return upb_mhandlers_newfhandlers_fordef(f);
}

// 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,
MessageHandlers* subm) {
return upb_mhandlers_newsubmsgfhandlers(this, n, type, repeated, subm);
}

FieldHandlers* NewFieldHandlersForSubmessage(FieldDef* f,
MessageHandlers* subm) {
return upb_mhandlers_newsubmsgfhandlers_fordef(f);
}


private:
MessageHandlers(); // Only created by upb::Handlers.
~MessageHandlers(); // Only destroyed by refcounting.
};

class Handlers : public upb_handlers {
public:
// Creates a new Handlers instance.
Handlers* New() { return static_cast<Handlers*>(upb_handlers_new()); }

void Ref() { upb_handlers_ref(this); }
void Unref() { upb_handlers_unref(this); }

// 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(); }

private:
FieldHandlers(); // Only created by Handlers::New().
~FieldHandlers(); // Only destroyed by refcounting.
};

} // namespace upb

#endif
50 changes: 26 additions & 24 deletions src/upb_def.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,20 +346,20 @@ static upb_flow_t upb_defbuilder_FileDescriptorProto_package(void *_b,

static upb_mhandlers *upb_defbuilder_register_FileDescriptorProto(
upb_handlers *h) {
upb_mhandlers *m = upb_handlers_newmsg(h);
upb_mhandlers *m = upb_handlers_newmhandlers(h);
upb_mhandlers_setstartmsg(m, &upb_defbuilder_FileDescriptorProto_startmsg);
upb_mhandlers_setendmsg(m, &upb_defbuilder_FileDescriptorProto_endmsg);

#define FNUM(field) GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ ## field ## __FIELDNUM
#define FTYPE(field) GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ ## field ## __FIELDTYPE
upb_fhandlers *f =
upb_mhandlers_newfield(m, FNUM(PACKAGE), FTYPE(PACKAGE), false);
upb_mhandlers_newfhandlers(m, FNUM(PACKAGE), FTYPE(PACKAGE), false);
upb_fhandlers_setvalue(f, &upb_defbuilder_FileDescriptorProto_package);

upb_mhandlers_newsubmsgfield(m, FNUM(MESSAGE_TYPE), FTYPE(MESSAGE_TYPE), true,
upb_msgdef_register_DescriptorProto(h));
upb_mhandlers_newsubmsgfield(m, FNUM(ENUM_TYPE), FTYPE(ENUM_TYPE), true,
upb_enumdef_register_EnumDescriptorProto(h));
upb_mhandlers_newfhandlers_subm(m, FNUM(MESSAGE_TYPE), FTYPE(MESSAGE_TYPE), true,
upb_msgdef_register_DescriptorProto(h));
upb_mhandlers_newfhandlers_subm(m, FNUM(ENUM_TYPE), FTYPE(ENUM_TYPE), true,
upb_enumdef_register_EnumDescriptorProto(h));
// TODO: services, extensions
return m;
}
Expand All @@ -379,13 +379,13 @@ static void upb_defbuilder_FileDescriptorSet_onendmsg(void *_b,
}

static upb_mhandlers *upb_defbuilder_register_FileDescriptorSet(upb_handlers *h) {
upb_mhandlers *m = upb_handlers_newmsg(h);
upb_mhandlers *m = upb_handlers_newmhandlers(h);
upb_mhandlers_setendmsg(m, upb_defbuilder_FileDescriptorSet_onendmsg);

#define FNUM(field) GOOGLE_PROTOBUF_FILEDESCRIPTORSET_ ## field ## __FIELDNUM
#define FTYPE(field) GOOGLE_PROTOBUF_FILEDESCRIPTORSET_ ## field ## __FIELDTYPE
upb_mhandlers_newsubmsgfield(m, FNUM(FILE), FTYPE(FILE), true,
upb_defbuilder_register_FileDescriptorProto(h));
upb_mhandlers_newfhandlers_subm(m, FNUM(FILE), FTYPE(FILE), true,
upb_defbuilder_register_FileDescriptorProto(h));
return m;
}
#undef FNUM
Expand Down Expand Up @@ -494,17 +494,17 @@ static void upb_enumdef_EnumValueDescriptorProto_endmsg(void *_b,

static upb_mhandlers *upb_enumdef_register_EnumValueDescriptorProto(
upb_handlers *h) {
upb_mhandlers *m = upb_handlers_newmsg(h);
upb_mhandlers *m = upb_handlers_newmhandlers(h);
upb_mhandlers_setstartmsg(m, &upb_enumdef_EnumValueDescriptorProto_startmsg);
upb_mhandlers_setendmsg(m, &upb_enumdef_EnumValueDescriptorProto_endmsg);

#define FNUM(f) GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_ ## f ## __FIELDNUM
#define FTYPE(f) GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_ ## f ## __FIELDTYPE
upb_fhandlers *f;
f = upb_mhandlers_newfield(m, FNUM(NAME), FTYPE(NAME), false);
f = upb_mhandlers_newfhandlers(m, FNUM(NAME), FTYPE(NAME), false);
upb_fhandlers_setvalue(f, &upb_enumdef_EnumValueDescriptorProto_name);

f = upb_mhandlers_newfield(m, FNUM(NUMBER), FTYPE(NUMBER), false);
f = upb_mhandlers_newfhandlers(m, FNUM(NUMBER), FTYPE(NUMBER), false);
upb_fhandlers_setvalue(f, &upb_enumdef_EnumValueDescriptorProto_number);
return m;
}
Expand Down Expand Up @@ -547,16 +547,17 @@ static upb_flow_t upb_enumdef_EnumDescriptorProto_name(void *_b,
}

static upb_mhandlers *upb_enumdef_register_EnumDescriptorProto(upb_handlers *h) {
upb_mhandlers *m = upb_handlers_newmsg(h);
upb_mhandlers *m = upb_handlers_newmhandlers(h);
upb_mhandlers_setstartmsg(m, &upb_enumdef_EnumDescriptorProto_startmsg);
upb_mhandlers_setendmsg(m, &upb_enumdef_EnumDescriptorProto_endmsg);

#define FNUM(f) GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_ ## f ## __FIELDNUM
#define FTYPE(f) GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_ ## f ## __FIELDTYPE
upb_fhandlers *f = upb_mhandlers_newfield(m, FNUM(NAME), FTYPE(NAME), false);
upb_fhandlers *f =
upb_mhandlers_newfhandlers(m, FNUM(NAME), FTYPE(NAME), false);
upb_fhandlers_setvalue(f, &upb_enumdef_EnumDescriptorProto_name);

upb_mhandlers_newsubmsgfield(m, FNUM(VALUE), FTYPE(VALUE), true,
upb_mhandlers_newfhandlers_subm(m, FNUM(VALUE), FTYPE(VALUE), true,
upb_enumdef_register_EnumValueDescriptorProto(h));
return m;
}
Expand Down Expand Up @@ -824,13 +825,13 @@ static upb_flow_t upb_fielddef_ondefaultval(void *_b, upb_value fval,

static upb_mhandlers *upb_fielddef_register_FieldDescriptorProto(
upb_handlers *h) {
upb_mhandlers *m = upb_handlers_newmsg(h);
upb_mhandlers *m = upb_handlers_newmhandlers(h);
upb_mhandlers_setstartmsg(m, &upb_fielddef_startmsg);
upb_mhandlers_setendmsg(m, &upb_fielddef_endmsg);

#define FIELD(name, handler) \
upb_fhandlers_setvalue( \
upb_mhandlers_newfield(m, \
upb_mhandlers_newfhandlers(m, \
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_ ## name ## __FIELDNUM, \
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_ ## name ## __FIELDTYPE, \
false), \
Expand Down Expand Up @@ -960,22 +961,23 @@ static upb_flow_t upb_msgdef_onname(void *_b, upb_value fval, upb_value val) {
}

static upb_mhandlers *upb_msgdef_register_DescriptorProto(upb_handlers *h) {
upb_mhandlers *m = upb_handlers_newmsg(h);
upb_mhandlers *m = upb_handlers_newmhandlers(h);
upb_mhandlers_setstartmsg(m, &upb_msgdef_startmsg);
upb_mhandlers_setendmsg(m, &upb_msgdef_endmsg);

#define FNUM(f) GOOGLE_PROTOBUF_DESCRIPTORPROTO_ ## f ## __FIELDNUM
#define FTYPE(f) GOOGLE_PROTOBUF_DESCRIPTORPROTO_ ## f ## __FIELDTYPE
upb_fhandlers *f = upb_mhandlers_newfield(m, FNUM(NAME), FTYPE(NAME), false);
upb_fhandlers *f =
upb_mhandlers_newfhandlers(m, FNUM(NAME), FTYPE(NAME), false);
upb_fhandlers_setvalue(f, &upb_msgdef_onname);

upb_mhandlers_newsubmsgfield(m, FNUM(FIELD), FTYPE(FIELD), true,
upb_fielddef_register_FieldDescriptorProto(h));
upb_mhandlers_newsubmsgfield(m, FNUM(ENUM_TYPE), FTYPE(ENUM_TYPE), true,
upb_enumdef_register_EnumDescriptorProto(h));
upb_mhandlers_newfhandlers_subm(m, FNUM(FIELD), FTYPE(FIELD), true,
upb_fielddef_register_FieldDescriptorProto(h));
upb_mhandlers_newfhandlers_subm(m, FNUM(ENUM_TYPE), FTYPE(ENUM_TYPE), true,
upb_enumdef_register_EnumDescriptorProto(h));

// DescriptorProto is self-recursive, so we must link the definition.
upb_mhandlers_newsubmsgfield(
upb_mhandlers_newfhandlers_subm(
m, FNUM(NESTED_TYPE), FTYPE(NESTED_TYPE), true, m);

// TODO: extensions.
Expand Down
Loading

0 comments on commit 2ccebb7

Please sign in to comment.