From 3bd667e95f8f00e4c62b670374dd852995c870c4 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Mon, 15 Dec 2014 13:42:24 -0800 Subject: Added msgdef flag to indicate map_entry protos. --- tests/test_def.c | 15 +++++++++++++++ upb/def.c | 11 +++++++++++ upb/def.h | 20 +++++++++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/tests/test_def.c b/tests/test_def.c index 84082df..dcf80c0 100644 --- a/tests/test_def.c +++ b/tests/test_def.c @@ -328,6 +328,20 @@ static void test_partial_freeze() { upb_msgdef_unref(m3, &m3); } +static void test_descriptor_flags() { + upb_msgdef *m = upb_msgdef_new(&m); + ASSERT(upb_msgdef_mapentry(m) == false); + upb_status s = UPB_STATUS_INIT; + upb_msgdef_setfullname(m, "TestMessage", &s); + ASSERT(upb_ok(&s)); + upb_msgdef_setmapentry(m, true); + ASSERT(upb_msgdef_mapentry(m) == true); + upb_msgdef *m2 = upb_msgdef_dup(m, &m2); + ASSERT(upb_msgdef_mapentry(m2) == true); + upb_msgdef_unref(m, &m); + upb_msgdef_unref(m2, &m2); +} + int run_tests(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "Usage: test_def \n"); @@ -343,5 +357,6 @@ int run_tests(int argc, char *argv[]) { test_freeze_free(); test_partial_freeze(); test_noreftracking(); + test_descriptor_flags(); return 0; } diff --git a/upb/def.c b/upb/def.c index aa05618..349dfd5 100644 --- a/upb/def.c +++ b/upb/def.c @@ -1270,6 +1270,7 @@ upb_msgdef *upb_msgdef_new(const void *owner) { if (!upb_def_init(UPB_UPCAST(m), UPB_DEF_MSG, &vtbl, owner)) goto err2; if (!upb_inttable_init(&m->itof, UPB_CTYPE_PTR)) goto err2; if (!upb_strtable_init(&m->ntof, UPB_CTYPE_PTR)) goto err1; + m->map_entry = false; return m; err1: @@ -1284,6 +1285,7 @@ upb_msgdef *upb_msgdef_dup(const upb_msgdef *m, const void *owner) { if (!newm) return NULL; bool ok = upb_def_setfullname(UPB_UPCAST(newm), upb_def_fullname(UPB_UPCAST(m)), NULL); + newm->map_entry = m->map_entry; UPB_ASSERT_VAR(ok, ok); upb_msg_iter i; for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) { @@ -1387,6 +1389,15 @@ int upb_msgdef_numfields(const upb_msgdef *m) { return upb_strtable_count(&m->ntof); } +void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry) { + assert(!upb_msgdef_isfrozen(m)); + m->map_entry = map_entry; +} + +bool upb_msgdef_mapentry(const upb_msgdef *m) { + return m->map_entry; +} + void upb_msg_begin(upb_msg_iter *iter, const upb_msgdef *m) { upb_inttable_begin(iter, &m->itof); } diff --git a/upb/def.h b/upb/def.h index cfa140a..ee138a0 100644 --- a/upb/def.h +++ b/upb/def.h @@ -712,6 +712,10 @@ UPB_DEFINE_DEF(upb::MessageDef, msgdef, MSG, UPB_QUOTE( // just be moved into symtab.c? MessageDef* Dup(const void* owner) const; + // Is this message a map entry? + void setmapentry(bool map_entry); + bool mapentry() const; + // Iteration over fields. The order is undefined. class iterator : public std::iterator { public: @@ -758,6 +762,11 @@ UPB_DEFINE_STRUCT(upb_msgdef, upb_def, upb_inttable itof; // int to field upb_strtable ntof; // name to field + // Is this a map-entry message? + // TODO: set this flag properly for static descriptors; regenerate + // descriptor.upb.c. + bool map_entry; + // TODO(haberman): proper extension ranges (there can be multiple). )); @@ -765,7 +774,7 @@ UPB_DEFINE_STRUCT(upb_msgdef, upb_def, refs, ref2s) \ { \ UPB_DEF_INIT(name, UPB_DEF_MSG, refs, ref2s), selector_count, \ - submsg_field_count, itof, ntof \ + submsg_field_count, itof, ntof, false \ } UPB_BEGIN_EXTERN_C // { @@ -813,6 +822,9 @@ UPB_INLINE upb_fielddef *upb_msgdef_ntof_mutable(upb_msgdef *m, return (upb_fielddef *)upb_msgdef_ntof(m, name, len); } +void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry); +bool upb_msgdef_mapentry(const upb_msgdef *m); + // upb_msg_iter i; // for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) { // upb_fielddef *f = upb_msg_iter_field(&i); @@ -1266,6 +1278,12 @@ inline const FieldDef *MessageDef::FindFieldByName(const char *name, inline MessageDef* MessageDef::Dup(const void *owner) const { return upb_msgdef_dup(this, owner); } +inline void MessageDef::setmapentry(bool map_entry) { + upb_msgdef_setmapentry(this, map_entry); +} +inline bool MessageDef::mapentry() const { + return upb_msgdef_mapentry(this); +} inline MessageDef::iterator MessageDef::begin() { return iterator(this); } inline MessageDef::iterator MessageDef::end() { return iterator::end(this); } inline MessageDef::const_iterator MessageDef::begin() const { -- cgit v1.2.3