google-protobuf 3.0.0.alpha.1.1 → 3.0.0.alpha.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of google-protobuf might be problematic. Click here for more details.

@@ -710,6 +710,9 @@ typedef struct {
710
710
  #define UPB_STRTABLE_INIT(count, mask, ctype, size_lg2, entries) \
711
711
  {{count, mask, ctype, size_lg2, entries}}
712
712
 
713
+ #define UPB_EMPTY_STRTABLE_INIT(ctype) \
714
+ UPB_STRTABLE_INIT(0, 0, ctype, 0, NULL)
715
+
713
716
  typedef struct {
714
717
  upb_table t; // For entries that don't fit in the array part.
715
718
  const _upb_value *array; // Array part of the table. See const note above.
@@ -1129,6 +1132,7 @@ class Def;
1129
1132
  class EnumDef;
1130
1133
  class FieldDef;
1131
1134
  class MessageDef;
1135
+ class OneofDef;
1132
1136
  }
1133
1137
  #endif
1134
1138
 
@@ -1136,6 +1140,7 @@ UPB_DECLARE_TYPE(upb::Def, upb_def);
1136
1140
  UPB_DECLARE_TYPE(upb::EnumDef, upb_enumdef);
1137
1141
  UPB_DECLARE_TYPE(upb::FieldDef, upb_fielddef);
1138
1142
  UPB_DECLARE_TYPE(upb::MessageDef, upb_msgdef);
1143
+ UPB_DECLARE_TYPE(upb::OneofDef, upb_oneofdef);
1139
1144
 
1140
1145
  // Maximum field number allowed for FieldDefs. This is an inherent limit of the
1141
1146
  // protobuf wire format.
@@ -1159,6 +1164,7 @@ typedef enum {
1159
1164
  UPB_DEF_MSG,
1160
1165
  UPB_DEF_FIELD,
1161
1166
  UPB_DEF_ENUM,
1167
+ UPB_DEF_ONEOF,
1162
1168
  UPB_DEF_SERVICE, // Not yet implemented.
1163
1169
  UPB_DEF_ANY = -1, // Wildcard for upb_symtab_get*()
1164
1170
  } upb_deftype_t;
@@ -1443,6 +1449,10 @@ UPB_DEFINE_DEF(upb::FieldDef, fielddef, FIELD,
1443
1449
  const MessageDef* containing_type() const;
1444
1450
  const char* containing_type_name();
1445
1451
 
1452
+ // The OneofDef to which this field belongs, or NULL if this field is not part
1453
+ // of a oneof.
1454
+ const OneofDef* containing_oneof() const;
1455
+
1446
1456
  // The field's type according to the enum in descriptor.proto. This is not
1447
1457
  // the same as UPB_TYPE_*, because it distinguishes between (for example)
1448
1458
  // INT32 and SINT32, whereas our "type" enum does not. This return of
@@ -1456,6 +1466,7 @@ UPB_DEFINE_DEF(upb::FieldDef, fielddef, FIELD,
1456
1466
  bool IsString() const;
1457
1467
  bool IsSequence() const;
1458
1468
  bool IsPrimitive() const;
1469
+ bool IsMap() const;
1459
1470
 
1460
1471
  // How integers are encoded. Only meaningful for integer types.
1461
1472
  // Defaults to UPB_INTFMT_VARIABLE, and is reset when "type" changes.
@@ -1616,6 +1627,7 @@ UPB_DEFINE_STRUCT(upb_fielddef, upb_def,
1616
1627
  } sub; // The msgdef or enumdef for this field, if upb_hassubdef(f).
1617
1628
  bool subdef_is_symbolic;
1618
1629
  bool msg_is_symbolic;
1630
+ const upb_oneofdef *oneof;
1619
1631
  bool default_is_string;
1620
1632
  bool type_is_set_; // False until type is explicitly set.
1621
1633
  bool is_extension_;
@@ -1631,11 +1643,11 @@ UPB_DEFINE_STRUCT(upb_fielddef, upb_def,
1631
1643
  ));
1632
1644
 
1633
1645
  #define UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy, \
1634
- packed, name, num, msgdef, subdef, selector_base, \
1646
+ packed, name, num, msgdef, subdef, selector_base, \
1635
1647
  index, defaultval, refs, ref2s) \
1636
1648
  { \
1637
1649
  UPB_DEF_INIT(name, UPB_DEF_FIELD, refs, ref2s), defaultval, {msgdef}, \
1638
- {subdef}, false, false, \
1650
+ {subdef}, NULL, false, false, \
1639
1651
  type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES, true, is_extension, \
1640
1652
  lazy, packed, intfmt, tagdelim, type, label, num, selector_base, index \
1641
1653
  }
@@ -1669,6 +1681,7 @@ bool upb_fielddef_isextension(const upb_fielddef *f);
1669
1681
  bool upb_fielddef_lazy(const upb_fielddef *f);
1670
1682
  bool upb_fielddef_packed(const upb_fielddef *f);
1671
1683
  const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f);
1684
+ const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f);
1672
1685
  upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f);
1673
1686
  const char *upb_fielddef_containingtypename(upb_fielddef *f);
1674
1687
  upb_intfmt_t upb_fielddef_intfmt(const upb_fielddef *f);
@@ -1678,6 +1691,7 @@ bool upb_fielddef_issubmsg(const upb_fielddef *f);
1678
1691
  bool upb_fielddef_isstring(const upb_fielddef *f);
1679
1692
  bool upb_fielddef_isseq(const upb_fielddef *f);
1680
1693
  bool upb_fielddef_isprimitive(const upb_fielddef *f);
1694
+ bool upb_fielddef_ismap(const upb_fielddef *f);
1681
1695
  int64_t upb_fielddef_defaultint64(const upb_fielddef *f);
1682
1696
  int32_t upb_fielddef_defaultint32(const upb_fielddef *f);
1683
1697
  uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f);
@@ -1736,7 +1750,8 @@ UPB_END_EXTERN_C // }
1736
1750
 
1737
1751
  /* upb::MessageDef ************************************************************/
1738
1752
 
1739
- typedef upb_inttable_iter upb_msg_iter;
1753
+ typedef upb_inttable_iter upb_msg_field_iter;
1754
+ typedef upb_strtable_iter upb_msg_oneof_iter;
1740
1755
 
1741
1756
  // Structure that describes a single .proto message type.
1742
1757
  //
@@ -1766,14 +1781,37 @@ UPB_DEFINE_DEF(upb::MessageDef, msgdef, MSG, UPB_QUOTE(
1766
1781
  // The number of fields that belong to the MessageDef.
1767
1782
  int field_count() const;
1768
1783
 
1784
+ // The number of oneofs that belong to the MessageDef.
1785
+ int oneof_count() const;
1786
+
1769
1787
  // Adds a field (upb_fielddef object) to a msgdef. Requires that the msgdef
1770
1788
  // and the fielddefs are mutable. The fielddef's name and number must be
1771
1789
  // set, and the message may not already contain any field with this name or
1772
1790
  // number, and this fielddef may not be part of another message. In error
1773
1791
  // cases false is returned and the msgdef is unchanged.
1792
+ //
1793
+ // If the given field is part of a oneof, this call succeeds if and only if
1794
+ // that oneof is already part of this msgdef. (Note that adding a oneof to a
1795
+ // msgdef automatically adds all of its fields to the msgdef at the time that
1796
+ // the oneof is added, so it is usually more idiomatic to add the oneof's
1797
+ // fields first then add the oneof to the msgdef. This case is supported for
1798
+ // convenience.)
1799
+ //
1800
+ // If |f| is already part of this MessageDef, this method performs no action
1801
+ // and returns true (success). Thus, this method is idempotent.
1774
1802
  bool AddField(FieldDef* f, Status* s);
1775
1803
  bool AddField(const reffed_ptr<FieldDef>& f, Status* s);
1776
1804
 
1805
+ // Adds a oneof (upb_oneofdef object) to a msgdef. Requires that the msgdef,
1806
+ // oneof, and any fielddefs are mutable, that the fielddefs contained in the
1807
+ // oneof do not have any name or number conflicts with existing fields in the
1808
+ // msgdef, and that the oneof's name is unique among all oneofs in the msgdef.
1809
+ // If the oneof is added successfully, all of its fields will be added
1810
+ // directly to the msgdef as well. In error cases, false is returned and the
1811
+ // msgdef is unchanged.
1812
+ bool AddOneof(OneofDef* o, Status* s);
1813
+ bool AddOneof(const reffed_ptr<OneofDef>& o, Status* s);
1814
+
1777
1815
  // These return NULL if the field is not found.
1778
1816
  FieldDef* FindFieldByNumber(uint32_t number);
1779
1817
  FieldDef* FindFieldByName(const char *name, size_t len);
@@ -1797,6 +1835,25 @@ UPB_DEFINE_DEF(upb::MessageDef, msgdef, MSG, UPB_QUOTE(
1797
1835
  return FindFieldByName(str.c_str(), str.size());
1798
1836
  }
1799
1837
 
1838
+ OneofDef* FindOneofByName(const char* name, size_t len);
1839
+ const OneofDef* FindOneofByName(const char* name, size_t len) const;
1840
+
1841
+ OneofDef* FindOneofByName(const char* name) {
1842
+ return FindOneofByName(name, strlen(name));
1843
+ }
1844
+ const OneofDef* FindOneofByName(const char* name) const {
1845
+ return FindOneofByName(name, strlen(name));
1846
+ }
1847
+
1848
+ template<class T>
1849
+ OneofDef* FindOneofByName(const T& str) {
1850
+ return FindOneofByName(str.c_str(), str.size());
1851
+ }
1852
+ template<class T>
1853
+ const OneofDef* FindOneofByName(const T& str) const {
1854
+ return FindOneofByName(str.c_str(), str.size());
1855
+ }
1856
+
1800
1857
  // Returns a new msgdef that is a copy of the given msgdef (and a copy of all
1801
1858
  // the fields) but with any references to submessages broken and replaced
1802
1859
  // with just the name of the submessage. Returns NULL if memory allocation
@@ -1812,39 +1869,117 @@ UPB_DEFINE_DEF(upb::MessageDef, msgdef, MSG, UPB_QUOTE(
1812
1869
  bool mapentry() const;
1813
1870
 
1814
1871
  // Iteration over fields. The order is undefined.
1815
- class iterator : public std::iterator<std::forward_iterator_tag, FieldDef*> {
1872
+ class field_iterator
1873
+ : public std::iterator<std::forward_iterator_tag, FieldDef*> {
1816
1874
  public:
1817
- explicit iterator(MessageDef* md);
1818
- static iterator end(MessageDef* md);
1875
+ explicit field_iterator(MessageDef* md);
1876
+ static field_iterator end(MessageDef* md);
1819
1877
 
1820
1878
  void operator++();
1821
1879
  FieldDef* operator*() const;
1822
- bool operator!=(const iterator& other) const;
1823
- bool operator==(const iterator& other) const;
1880
+ bool operator!=(const field_iterator& other) const;
1881
+ bool operator==(const field_iterator& other) const;
1824
1882
 
1825
1883
  private:
1826
- upb_msg_iter iter_;
1884
+ upb_msg_field_iter iter_;
1827
1885
  };
1828
1886
 
1829
- class const_iterator
1887
+ class const_field_iterator
1830
1888
  : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
1831
1889
  public:
1832
- explicit const_iterator(const MessageDef* md);
1833
- static const_iterator end(const MessageDef* md);
1890
+ explicit const_field_iterator(const MessageDef* md);
1891
+ static const_field_iterator end(const MessageDef* md);
1834
1892
 
1835
1893
  void operator++();
1836
1894
  const FieldDef* operator*() const;
1837
- bool operator!=(const const_iterator& other) const;
1838
- bool operator==(const const_iterator& other) const;
1895
+ bool operator!=(const const_field_iterator& other) const;
1896
+ bool operator==(const const_field_iterator& other) const;
1839
1897
 
1840
1898
  private:
1841
- upb_msg_iter iter_;
1899
+ upb_msg_field_iter iter_;
1842
1900
  };
1843
1901
 
1844
- iterator begin();
1845
- iterator end();
1846
- const_iterator begin() const;
1847
- const_iterator end() const;
1902
+ // Iteration over oneofs. The order is undefined.
1903
+ class oneof_iterator
1904
+ : public std::iterator<std::forward_iterator_tag, FieldDef*> {
1905
+ public:
1906
+ explicit oneof_iterator(MessageDef* md);
1907
+ static oneof_iterator end(MessageDef* md);
1908
+
1909
+ void operator++();
1910
+ OneofDef* operator*() const;
1911
+ bool operator!=(const oneof_iterator& other) const;
1912
+ bool operator==(const oneof_iterator& other) const;
1913
+
1914
+ private:
1915
+ upb_msg_oneof_iter iter_;
1916
+ };
1917
+
1918
+ class const_oneof_iterator
1919
+ : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
1920
+ public:
1921
+ explicit const_oneof_iterator(const MessageDef* md);
1922
+ static const_oneof_iterator end(const MessageDef* md);
1923
+
1924
+ void operator++();
1925
+ const OneofDef* operator*() const;
1926
+ bool operator!=(const const_oneof_iterator& other) const;
1927
+ bool operator==(const const_oneof_iterator& other) const;
1928
+
1929
+ private:
1930
+ upb_msg_oneof_iter iter_;
1931
+ };
1932
+
1933
+ class FieldAccessor {
1934
+ public:
1935
+ explicit FieldAccessor(MessageDef* msg) : msg_(msg) {}
1936
+ field_iterator begin() { return msg_->field_begin(); }
1937
+ field_iterator end() { return msg_->field_end(); }
1938
+ private:
1939
+ MessageDef* msg_;
1940
+ };
1941
+
1942
+ class ConstFieldAccessor {
1943
+ public:
1944
+ explicit ConstFieldAccessor(const MessageDef* msg) : msg_(msg) {}
1945
+ const_field_iterator begin() { return msg_->field_begin(); }
1946
+ const_field_iterator end() { return msg_->field_end(); }
1947
+ private:
1948
+ const MessageDef* msg_;
1949
+ };
1950
+
1951
+ class OneofAccessor {
1952
+ public:
1953
+ explicit OneofAccessor(MessageDef* msg) : msg_(msg) {}
1954
+ oneof_iterator begin() { return msg_->oneof_begin(); }
1955
+ oneof_iterator end() { return msg_->oneof_end(); }
1956
+ private:
1957
+ MessageDef* msg_;
1958
+ };
1959
+
1960
+ class ConstOneofAccessor {
1961
+ public:
1962
+ explicit ConstOneofAccessor(const MessageDef* msg) : msg_(msg) {}
1963
+ const_oneof_iterator begin() { return msg_->oneof_begin(); }
1964
+ const_oneof_iterator end() { return msg_->oneof_end(); }
1965
+ private:
1966
+ const MessageDef* msg_;
1967
+ };
1968
+
1969
+ field_iterator field_begin();
1970
+ field_iterator field_end();
1971
+ const_field_iterator field_begin() const;
1972
+ const_field_iterator field_end() const;
1973
+
1974
+ oneof_iterator oneof_begin();
1975
+ oneof_iterator oneof_end();
1976
+ const_oneof_iterator oneof_begin() const;
1977
+ const_oneof_iterator oneof_end() const;
1978
+
1979
+ FieldAccessor fields() { return FieldAccessor(this); }
1980
+ ConstFieldAccessor fields() const { return ConstFieldAccessor(this); }
1981
+ OneofAccessor oneofs() { return OneofAccessor(this); }
1982
+ ConstOneofAccessor oneofs() const { return ConstOneofAccessor(this); }
1848
1983
 
1849
1984
  private:
1850
1985
  UPB_DISALLOW_POD_OPS(MessageDef, upb::MessageDef);
@@ -1857,6 +1992,9 @@ UPB_DEFINE_STRUCT(upb_msgdef, upb_def,
1857
1992
  upb_inttable itof; // int to field
1858
1993
  upb_strtable ntof; // name to field
1859
1994
 
1995
+ // Tables for looking up oneofs by name.
1996
+ upb_strtable ntoo; // name to oneof
1997
+
1860
1998
  // Is this a map-entry message?
1861
1999
  // TODO: set this flag properly for static descriptors; regenerate
1862
2000
  // descriptor.upb.c.
@@ -1865,11 +2003,14 @@ UPB_DEFINE_STRUCT(upb_msgdef, upb_def,
1865
2003
  // TODO(haberman): proper extension ranges (there can be multiple).
1866
2004
  ));
1867
2005
 
2006
+ // TODO: also support static initialization of the oneofs table. This will be
2007
+ // needed if we compile in descriptors that contain oneofs.
1868
2008
  #define UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, \
1869
2009
  refs, ref2s) \
1870
2010
  { \
1871
2011
  UPB_DEF_INIT(name, UPB_DEF_MSG, refs, ref2s), selector_count, \
1872
- submsg_field_count, itof, ntof, false \
2012
+ submsg_field_count, itof, ntof, \
2013
+ UPB_EMPTY_STRTABLE_INIT(UPB_CTYPE_PTR), false \
1873
2014
  }
1874
2015
 
1875
2016
  UPB_BEGIN_EXTERN_C // {
@@ -1893,6 +2034,8 @@ bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname, upb_status *s);
1893
2034
  upb_msgdef *upb_msgdef_dup(const upb_msgdef *m, const void *owner);
1894
2035
  bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, const void *ref_donor,
1895
2036
  upb_status *s);
2037
+ bool upb_msgdef_addoneof(upb_msgdef *m, upb_oneofdef *o, const void *ref_donor,
2038
+ upb_status *s);
1896
2039
 
1897
2040
  // Field lookup in a couple of different variations:
1898
2041
  // - itof = int to field
@@ -1917,11 +2060,38 @@ UPB_INLINE upb_fielddef *upb_msgdef_ntof_mutable(upb_msgdef *m,
1917
2060
  return (upb_fielddef *)upb_msgdef_ntof(m, name, len);
1918
2061
  }
1919
2062
 
2063
+ // Oneof lookup:
2064
+ // - ntoo = name to oneof
2065
+ // - ntooz = name to oneof, null-terminated string.
2066
+ const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
2067
+ size_t len);
2068
+ int upb_msgdef_numoneofs(const upb_msgdef *m);
2069
+
2070
+ UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m,
2071
+ const char *name) {
2072
+ return upb_msgdef_ntoo(m, name, strlen(name));
2073
+ }
2074
+
2075
+ UPB_INLINE upb_oneofdef *upb_msgdef_ntoo_mutable(upb_msgdef *m,
2076
+ const char *name, size_t len) {
2077
+ return (upb_oneofdef *)upb_msgdef_ntoo(m, name, len);
2078
+ }
2079
+
1920
2080
  void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry);
1921
2081
  bool upb_msgdef_mapentry(const upb_msgdef *m);
1922
2082
 
1923
- // upb_msg_iter i;
1924
- // for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) {
2083
+ // Well-known field tag numbers for map-entry messages.
2084
+ #define UPB_MAPENTRY_KEY 1
2085
+ #define UPB_MAPENTRY_VALUE 2
2086
+
2087
+ const upb_oneofdef *upb_msgdef_findoneof(const upb_msgdef *m,
2088
+ const char *name);
2089
+ int upb_msgdef_numoneofs(const upb_msgdef *m);
2090
+
2091
+ // upb_msg_field_iter i;
2092
+ // for(upb_msg_field_begin(&i, m);
2093
+ // !upb_msg_field_done(&i);
2094
+ // upb_msg_field_next(&i)) {
1925
2095
  // upb_fielddef *f = upb_msg_iter_field(&i);
1926
2096
  // // ...
1927
2097
  // }
@@ -1929,11 +2099,18 @@ bool upb_msgdef_mapentry(const upb_msgdef *m);
1929
2099
  // For C we don't have separate iterators for const and non-const.
1930
2100
  // It is the caller's responsibility to cast the upb_fielddef* to
1931
2101
  // const if the upb_msgdef* is const.
1932
- void upb_msg_begin(upb_msg_iter *iter, const upb_msgdef *m);
1933
- void upb_msg_next(upb_msg_iter *iter);
1934
- bool upb_msg_done(const upb_msg_iter *iter);
1935
- upb_fielddef *upb_msg_iter_field(const upb_msg_iter *iter);
1936
- void upb_msg_iter_setdone(upb_msg_iter *iter);
2102
+ void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m);
2103
+ void upb_msg_field_next(upb_msg_field_iter *iter);
2104
+ bool upb_msg_field_done(const upb_msg_field_iter *iter);
2105
+ upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter);
2106
+ void upb_msg_field_iter_setdone(upb_msg_field_iter *iter);
2107
+
2108
+ // Similar to above, we also support iterating through the oneofs in a msgdef.
2109
+ void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m);
2110
+ void upb_msg_oneof_next(upb_msg_oneof_iter *iter);
2111
+ bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter);
2112
+ upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter);
2113
+ void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter);
1937
2114
 
1938
2115
  UPB_END_EXTERN_C // }
1939
2116
 
@@ -2075,6 +2252,172 @@ int32_t upb_enum_iter_number(upb_enum_iter *iter);
2075
2252
 
2076
2253
  UPB_END_EXTERN_C // }
2077
2254
 
2255
+ /* upb::OneofDef **************************************************************/
2256
+
2257
+ typedef upb_inttable_iter upb_oneof_iter;
2258
+
2259
+ // Class that represents a oneof. Its base class is upb::Def (convert with
2260
+ // upb::upcast()).
2261
+ UPB_DEFINE_DEF(upb::OneofDef, oneofdef, ONEOF, UPB_QUOTE(
2262
+ public:
2263
+ // Returns NULL if memory allocation failed.
2264
+ static reffed_ptr<OneofDef> New();
2265
+
2266
+ // Functionality from upb::RefCounted.
2267
+ bool IsFrozen() const;
2268
+ void Ref(const void* owner) const;
2269
+ void Unref(const void* owner) const;
2270
+ void DonateRef(const void* from, const void* to) const;
2271
+ void CheckRef(const void* owner) const;
2272
+
2273
+ // Functionality from upb::Def.
2274
+ const char* full_name() const;
2275
+
2276
+ // Returns the MessageDef that owns this OneofDef.
2277
+ const MessageDef* containing_type() const;
2278
+
2279
+ // Returns the name of this oneof. This is the name used to look up the oneof
2280
+ // by name once added to a message def.
2281
+ const char* name() const;
2282
+ bool set_name(const char* name, Status* s);
2283
+
2284
+ // Returns the number of fields currently defined in the oneof.
2285
+ int field_count() const;
2286
+
2287
+ // Adds a field to the oneof. The field must not have been added to any other
2288
+ // oneof or msgdef. If the oneof is not yet part of a msgdef, then when the
2289
+ // oneof is eventually added to a msgdef, all fields added to the oneof will
2290
+ // also be added to the msgdef at that time. If the oneof is already part of a
2291
+ // msgdef, the field must either be a part of that msgdef already, or must not
2292
+ // be a part of any msgdef; in the latter case, the field is added to the
2293
+ // msgdef as a part of this operation.
2294
+ //
2295
+ // The field may only have an OPTIONAL label, never REQUIRED or REPEATED.
2296
+ //
2297
+ // If |f| is already part of this MessageDef, this method performs no action
2298
+ // and returns true (success). Thus, this method is idempotent.
2299
+ bool AddField(FieldDef* field, Status* s);
2300
+ bool AddField(const reffed_ptr<FieldDef>& field, Status* s);
2301
+
2302
+ // Looks up by name.
2303
+ const FieldDef* FindFieldByName(const char* name, size_t len) const;
2304
+ FieldDef* FindFieldByName(const char* name, size_t len);
2305
+ const FieldDef* FindFieldByName(const char* name) const {
2306
+ return FindFieldByName(name, strlen(name));
2307
+ }
2308
+ FieldDef* FindFieldByName(const char* name) {
2309
+ return FindFieldByName(name, strlen(name));
2310
+ }
2311
+
2312
+ template <class T>
2313
+ FieldDef* FindFieldByName(const T& str) {
2314
+ return FindFieldByName(str.c_str(), str.size());
2315
+ }
2316
+ template <class T>
2317
+ const FieldDef* FindFieldByName(const T& str) const {
2318
+ return FindFieldByName(str.c_str(), str.size());
2319
+ }
2320
+
2321
+ // Looks up by tag number.
2322
+ const FieldDef* FindFieldByNumber(uint32_t num) const;
2323
+
2324
+ // Returns a new OneofDef with all the same fields. The OneofDef will be owned
2325
+ // by the given owner.
2326
+ OneofDef* Dup(const void* owner) const;
2327
+
2328
+ // Iteration over fields. The order is undefined.
2329
+ class iterator : public std::iterator<std::forward_iterator_tag, FieldDef*> {
2330
+ public:
2331
+ explicit iterator(OneofDef* md);
2332
+ static iterator end(OneofDef* md);
2333
+
2334
+ void operator++();
2335
+ FieldDef* operator*() const;
2336
+ bool operator!=(const iterator& other) const;
2337
+ bool operator==(const iterator& other) const;
2338
+
2339
+ private:
2340
+ upb_oneof_iter iter_;
2341
+ };
2342
+
2343
+ class const_iterator
2344
+ : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
2345
+ public:
2346
+ explicit const_iterator(const OneofDef* md);
2347
+ static const_iterator end(const OneofDef* md);
2348
+
2349
+ void operator++();
2350
+ const FieldDef* operator*() const;
2351
+ bool operator!=(const const_iterator& other) const;
2352
+ bool operator==(const const_iterator& other) const;
2353
+
2354
+ private:
2355
+ upb_oneof_iter iter_;
2356
+ };
2357
+
2358
+ iterator begin();
2359
+ iterator end();
2360
+ const_iterator begin() const;
2361
+ const_iterator end() const;
2362
+
2363
+ private:
2364
+ UPB_DISALLOW_POD_OPS(OneofDef, upb::OneofDef);
2365
+ ),
2366
+ UPB_DEFINE_STRUCT(upb_oneofdef, upb_def,
2367
+ upb_strtable ntof;
2368
+ upb_inttable itof;
2369
+ const upb_msgdef *parent;
2370
+ ));
2371
+
2372
+ #define UPB_ONEOFDEF_INIT(name, ntof, itof, refs, ref2s) \
2373
+ { UPB_DEF_INIT(name, UPB_DEF_ENUM, refs, ref2s), ntof, itof }
2374
+
2375
+ UPB_BEGIN_EXTERN_C // {
2376
+
2377
+ // Native C API.
2378
+ upb_oneofdef *upb_oneofdef_new(const void *owner);
2379
+ upb_oneofdef *upb_oneofdef_dup(const upb_oneofdef *o, const void *owner);
2380
+
2381
+ // From upb_refcounted.
2382
+ void upb_oneofdef_unref(const upb_oneofdef *o, const void *owner);
2383
+ bool upb_oneofdef_isfrozen(const upb_oneofdef *e);
2384
+ void upb_oneofdef_ref(const upb_oneofdef *o, const void *owner);
2385
+ void upb_oneofdef_donateref(const upb_oneofdef *m, const void *from,
2386
+ const void *to);
2387
+ void upb_oneofdef_checkref(const upb_oneofdef *o, const void *owner);
2388
+
2389
+ const char *upb_oneofdef_name(const upb_oneofdef *o);
2390
+ bool upb_oneofdef_setname(upb_oneofdef *o, const char *name, upb_status *s);
2391
+
2392
+ const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o);
2393
+ int upb_oneofdef_numfields(const upb_oneofdef *o);
2394
+ bool upb_oneofdef_addfield(upb_oneofdef *o, upb_fielddef *f,
2395
+ const void *ref_donor,
2396
+ upb_status *s);
2397
+
2398
+ // Oneof lookups:
2399
+ // - ntof: look up a field by name.
2400
+ // - ntofz: look up a field by name (as a null-terminated string).
2401
+ // - itof: look up a field by number.
2402
+ const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
2403
+ const char *name, size_t length);
2404
+ UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o,
2405
+ const char *name) {
2406
+ return upb_oneofdef_ntof(o, name, strlen(name));
2407
+ }
2408
+ const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num);
2409
+
2410
+ // upb_oneof_iter i;
2411
+ // for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) {
2412
+ // // ...
2413
+ // }
2414
+ void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o);
2415
+ void upb_oneof_next(upb_oneof_iter *iter);
2416
+ bool upb_oneof_done(upb_oneof_iter *iter);
2417
+ upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter);
2418
+ void upb_oneof_iter_setdone(upb_oneof_iter *iter);
2419
+
2420
+ UPB_END_EXTERN_C // }
2078
2421
 
2079
2422
  #ifdef __cplusplus
2080
2423
 
@@ -2201,6 +2544,9 @@ inline void FieldDef::set_packed(bool packed) {
2201
2544
  inline const MessageDef* FieldDef::containing_type() const {
2202
2545
  return upb_fielddef_containingtype(this);
2203
2546
  }
2547
+ inline const OneofDef* FieldDef::containing_oneof() const {
2548
+ return upb_fielddef_containingoneof(this);
2549
+ }
2204
2550
  inline const char* FieldDef::containing_type_name() {
2205
2551
  return upb_fielddef_containingtypename(this);
2206
2552
  }
@@ -2237,6 +2583,7 @@ inline bool FieldDef::IsSubMessage() const {
2237
2583
  }
2238
2584
  inline bool FieldDef::IsString() const { return upb_fielddef_isstring(this); }
2239
2585
  inline bool FieldDef::IsSequence() const { return upb_fielddef_isseq(this); }
2586
+ inline bool FieldDef::IsMap() const { return upb_fielddef_ismap(this); }
2240
2587
  inline int64_t FieldDef::default_int64() const {
2241
2588
  return upb_fielddef_defaultint64(this);
2242
2589
  }
@@ -2351,12 +2698,21 @@ inline bool MessageDef::Freeze(Status* status) {
2351
2698
  inline int MessageDef::field_count() const {
2352
2699
  return upb_msgdef_numfields(this);
2353
2700
  }
2701
+ inline int MessageDef::oneof_count() const {
2702
+ return upb_msgdef_numoneofs(this);
2703
+ }
2354
2704
  inline bool MessageDef::AddField(upb_fielddef* f, Status* s) {
2355
2705
  return upb_msgdef_addfield(this, f, NULL, s);
2356
2706
  }
2357
2707
  inline bool MessageDef::AddField(const reffed_ptr<FieldDef>& f, Status* s) {
2358
2708
  return upb_msgdef_addfield(this, f.get(), NULL, s);
2359
2709
  }
2710
+ inline bool MessageDef::AddOneof(upb_oneofdef* o, Status* s) {
2711
+ return upb_msgdef_addoneof(this, o, NULL, s);
2712
+ }
2713
+ inline bool MessageDef::AddOneof(const reffed_ptr<OneofDef>& o, Status* s) {
2714
+ return upb_msgdef_addoneof(this, o.get(), NULL, s);
2715
+ }
2360
2716
  inline FieldDef* MessageDef::FindFieldByNumber(uint32_t number) {
2361
2717
  return upb_msgdef_itof_mutable(this, number);
2362
2718
  }
@@ -2370,6 +2726,13 @@ inline const FieldDef *MessageDef::FindFieldByName(const char *name,
2370
2726
  size_t len) const {
2371
2727
  return upb_msgdef_ntof(this, name, len);
2372
2728
  }
2729
+ inline OneofDef* MessageDef::FindOneofByName(const char* name, size_t len) {
2730
+ return upb_msgdef_ntoo_mutable(this, name, len);
2731
+ }
2732
+ inline const OneofDef* MessageDef::FindOneofByName(const char* name,
2733
+ size_t len) const {
2734
+ return upb_msgdef_ntoo(this, name, len);
2735
+ }
2373
2736
  inline MessageDef* MessageDef::Dup(const void *owner) const {
2374
2737
  return upb_msgdef_dup(this, owner);
2375
2738
  }
@@ -2379,55 +2742,127 @@ inline void MessageDef::setmapentry(bool map_entry) {
2379
2742
  inline bool MessageDef::mapentry() const {
2380
2743
  return upb_msgdef_mapentry(this);
2381
2744
  }
2382
- inline MessageDef::iterator MessageDef::begin() { return iterator(this); }
2383
- inline MessageDef::iterator MessageDef::end() { return iterator::end(this); }
2384
- inline MessageDef::const_iterator MessageDef::begin() const {
2385
- return const_iterator(this);
2745
+ inline MessageDef::field_iterator MessageDef::field_begin() {
2746
+ return field_iterator(this);
2386
2747
  }
2387
- inline MessageDef::const_iterator MessageDef::end() const {
2388
- return const_iterator::end(this);
2748
+ inline MessageDef::field_iterator MessageDef::field_end() {
2749
+ return field_iterator::end(this);
2750
+ }
2751
+ inline MessageDef::const_field_iterator MessageDef::field_begin() const {
2752
+ return const_field_iterator(this);
2753
+ }
2754
+ inline MessageDef::const_field_iterator MessageDef::field_end() const {
2755
+ return const_field_iterator::end(this);
2389
2756
  }
2390
2757
 
2391
- inline MessageDef::iterator::iterator(MessageDef* md) {
2392
- upb_msg_begin(&iter_, md);
2758
+ inline MessageDef::oneof_iterator MessageDef::oneof_begin() {
2759
+ return oneof_iterator(this);
2760
+ }
2761
+ inline MessageDef::oneof_iterator MessageDef::oneof_end() {
2762
+ return oneof_iterator::end(this);
2763
+ }
2764
+ inline MessageDef::const_oneof_iterator MessageDef::oneof_begin() const {
2765
+ return const_oneof_iterator(this);
2766
+ }
2767
+ inline MessageDef::const_oneof_iterator MessageDef::oneof_end() const {
2768
+ return const_oneof_iterator::end(this);
2393
2769
  }
2394
- inline MessageDef::iterator MessageDef::iterator::end(MessageDef* md) {
2395
- MessageDef::iterator iter(md);
2396
- upb_msg_iter_setdone(&iter.iter_);
2770
+
2771
+ inline MessageDef::field_iterator::field_iterator(MessageDef* md) {
2772
+ upb_msg_field_begin(&iter_, md);
2773
+ }
2774
+ inline MessageDef::field_iterator MessageDef::field_iterator::end(
2775
+ MessageDef* md) {
2776
+ MessageDef::field_iterator iter(md);
2777
+ upb_msg_field_iter_setdone(&iter.iter_);
2397
2778
  return iter;
2398
2779
  }
2399
- inline FieldDef* MessageDef::iterator::operator*() const {
2780
+ inline FieldDef* MessageDef::field_iterator::operator*() const {
2400
2781
  return upb_msg_iter_field(&iter_);
2401
2782
  }
2402
- inline void MessageDef::iterator::operator++() { return upb_msg_next(&iter_); }
2403
- inline bool MessageDef::iterator::operator==(const iterator &other) const {
2783
+ inline void MessageDef::field_iterator::operator++() {
2784
+ return upb_msg_field_next(&iter_);
2785
+ }
2786
+ inline bool MessageDef::field_iterator::operator==(
2787
+ const field_iterator &other) const {
2404
2788
  return upb_inttable_iter_isequal(&iter_, &other.iter_);
2405
2789
  }
2406
- inline bool MessageDef::iterator::operator!=(const iterator &other) const {
2790
+ inline bool MessageDef::field_iterator::operator!=(
2791
+ const field_iterator &other) const {
2407
2792
  return !(*this == other);
2408
2793
  }
2409
2794
 
2410
- inline MessageDef::const_iterator::const_iterator(const MessageDef* md) {
2411
- upb_msg_begin(&iter_, md);
2795
+ inline MessageDef::const_field_iterator::const_field_iterator(
2796
+ const MessageDef* md) {
2797
+ upb_msg_field_begin(&iter_, md);
2412
2798
  }
2413
- inline MessageDef::const_iterator MessageDef::const_iterator::end(
2799
+ inline MessageDef::const_field_iterator MessageDef::const_field_iterator::end(
2414
2800
  const MessageDef *md) {
2415
- MessageDef::const_iterator iter(md);
2416
- upb_msg_iter_setdone(&iter.iter_);
2801
+ MessageDef::const_field_iterator iter(md);
2802
+ upb_msg_field_iter_setdone(&iter.iter_);
2417
2803
  return iter;
2418
2804
  }
2419
- inline const FieldDef* MessageDef::const_iterator::operator*() const {
2805
+ inline const FieldDef* MessageDef::const_field_iterator::operator*() const {
2420
2806
  return upb_msg_iter_field(&iter_);
2421
2807
  }
2422
- inline void MessageDef::const_iterator::operator++() {
2423
- return upb_msg_next(&iter_);
2808
+ inline void MessageDef::const_field_iterator::operator++() {
2809
+ return upb_msg_field_next(&iter_);
2424
2810
  }
2425
- inline bool MessageDef::const_iterator::operator==(
2426
- const const_iterator &other) const {
2811
+ inline bool MessageDef::const_field_iterator::operator==(
2812
+ const const_field_iterator &other) const {
2427
2813
  return upb_inttable_iter_isequal(&iter_, &other.iter_);
2428
2814
  }
2429
- inline bool MessageDef::const_iterator::operator!=(
2430
- const const_iterator &other) const {
2815
+ inline bool MessageDef::const_field_iterator::operator!=(
2816
+ const const_field_iterator &other) const {
2817
+ return !(*this == other);
2818
+ }
2819
+
2820
+ inline MessageDef::oneof_iterator::oneof_iterator(MessageDef* md) {
2821
+ upb_msg_oneof_begin(&iter_, md);
2822
+ }
2823
+ inline MessageDef::oneof_iterator MessageDef::oneof_iterator::end(
2824
+ MessageDef* md) {
2825
+ MessageDef::oneof_iterator iter(md);
2826
+ upb_msg_oneof_iter_setdone(&iter.iter_);
2827
+ return iter;
2828
+ }
2829
+ inline OneofDef* MessageDef::oneof_iterator::operator*() const {
2830
+ return upb_msg_iter_oneof(&iter_);
2831
+ }
2832
+ inline void MessageDef::oneof_iterator::operator++() {
2833
+ return upb_msg_oneof_next(&iter_);
2834
+ }
2835
+ inline bool MessageDef::oneof_iterator::operator==(
2836
+ const oneof_iterator &other) const {
2837
+ return upb_strtable_iter_isequal(&iter_, &other.iter_);
2838
+ }
2839
+ inline bool MessageDef::oneof_iterator::operator!=(
2840
+ const oneof_iterator &other) const {
2841
+ return !(*this == other);
2842
+ }
2843
+
2844
+ inline MessageDef::const_oneof_iterator::const_oneof_iterator(
2845
+ const MessageDef* md) {
2846
+ upb_msg_oneof_begin(&iter_, md);
2847
+ }
2848
+ inline MessageDef::const_oneof_iterator MessageDef::const_oneof_iterator::end(
2849
+ const MessageDef *md) {
2850
+ MessageDef::const_oneof_iterator iter(md);
2851
+ upb_msg_oneof_iter_setdone(&iter.iter_);
2852
+ return iter;
2853
+ }
2854
+ inline const OneofDef* MessageDef::const_oneof_iterator::operator*() const {
2855
+ return upb_msg_iter_oneof(&iter_);
2856
+ }
2857
+ inline void MessageDef::const_oneof_iterator::operator++() {
2858
+ return upb_msg_oneof_next(&iter_);
2859
+ }
2860
+ inline bool MessageDef::const_oneof_iterator::operator==(
2861
+ const const_oneof_iterator &other) const {
2862
+ return upb_strtable_iter_isequal(&iter_, &other.iter_);
2863
+ }
2864
+ inline bool MessageDef::const_oneof_iterator::operator!=(
2865
+ const const_oneof_iterator &other) const {
2431
2866
  return !(*this == other);
2432
2867
  }
2433
2868
 
@@ -2495,6 +2930,105 @@ inline const char* EnumDef::Iterator::name() {
2495
2930
  }
2496
2931
  inline bool EnumDef::Iterator::Done() { return upb_enum_done(&iter_); }
2497
2932
  inline void EnumDef::Iterator::Next() { return upb_enum_next(&iter_); }
2933
+
2934
+ inline reffed_ptr<OneofDef> OneofDef::New() {
2935
+ upb_oneofdef *o = upb_oneofdef_new(&o);
2936
+ return reffed_ptr<OneofDef>(o, &o);
2937
+ }
2938
+ inline bool OneofDef::IsFrozen() const { return upb_oneofdef_isfrozen(this); }
2939
+ inline void OneofDef::Ref(const void* owner) const {
2940
+ return upb_oneofdef_ref(this, owner);
2941
+ }
2942
+ inline void OneofDef::Unref(const void* owner) const {
2943
+ return upb_oneofdef_unref(this, owner);
2944
+ }
2945
+ inline void OneofDef::DonateRef(const void* from, const void* to) const {
2946
+ return upb_oneofdef_donateref(this, from, to);
2947
+ }
2948
+ inline void OneofDef::CheckRef(const void* owner) const {
2949
+ return upb_oneofdef_checkref(this, owner);
2950
+ }
2951
+ inline const char* OneofDef::full_name() const {
2952
+ return upb_oneofdef_name(this);
2953
+ }
2954
+
2955
+ inline const MessageDef* OneofDef::containing_type() const {
2956
+ return upb_oneofdef_containingtype(this);
2957
+ }
2958
+ inline const char* OneofDef::name() const {
2959
+ return upb_oneofdef_name(this);
2960
+ }
2961
+ inline bool OneofDef::set_name(const char* name, Status* s) {
2962
+ return upb_oneofdef_setname(this, name, s);
2963
+ }
2964
+ inline int OneofDef::field_count() const {
2965
+ return upb_oneofdef_numfields(this);
2966
+ }
2967
+ inline bool OneofDef::AddField(FieldDef* field, Status* s) {
2968
+ return upb_oneofdef_addfield(this, field, NULL, s);
2969
+ }
2970
+ inline bool OneofDef::AddField(const reffed_ptr<FieldDef>& field, Status* s) {
2971
+ return upb_oneofdef_addfield(this, field.get(), NULL, s);
2972
+ }
2973
+ inline const FieldDef* OneofDef::FindFieldByName(const char* name,
2974
+ size_t len) const {
2975
+ return upb_oneofdef_ntof(this, name, len);
2976
+ }
2977
+ inline const FieldDef* OneofDef::FindFieldByNumber(uint32_t num) const {
2978
+ return upb_oneofdef_itof(this, num);
2979
+ }
2980
+ inline OneofDef::iterator OneofDef::begin() { return iterator(this); }
2981
+ inline OneofDef::iterator OneofDef::end() { return iterator::end(this); }
2982
+ inline OneofDef::const_iterator OneofDef::begin() const {
2983
+ return const_iterator(this);
2984
+ }
2985
+ inline OneofDef::const_iterator OneofDef::end() const {
2986
+ return const_iterator::end(this);
2987
+ }
2988
+
2989
+ inline OneofDef::iterator::iterator(OneofDef* o) {
2990
+ upb_oneof_begin(&iter_, o);
2991
+ }
2992
+ inline OneofDef::iterator OneofDef::iterator::end(OneofDef* o) {
2993
+ OneofDef::iterator iter(o);
2994
+ upb_oneof_iter_setdone(&iter.iter_);
2995
+ return iter;
2996
+ }
2997
+ inline FieldDef* OneofDef::iterator::operator*() const {
2998
+ return upb_oneof_iter_field(&iter_);
2999
+ }
3000
+ inline void OneofDef::iterator::operator++() { return upb_oneof_next(&iter_); }
3001
+ inline bool OneofDef::iterator::operator==(const iterator &other) const {
3002
+ return upb_inttable_iter_isequal(&iter_, &other.iter_);
3003
+ }
3004
+ inline bool OneofDef::iterator::operator!=(const iterator &other) const {
3005
+ return !(*this == other);
3006
+ }
3007
+
3008
+ inline OneofDef::const_iterator::const_iterator(const OneofDef* md) {
3009
+ upb_oneof_begin(&iter_, md);
3010
+ }
3011
+ inline OneofDef::const_iterator OneofDef::const_iterator::end(
3012
+ const OneofDef *md) {
3013
+ OneofDef::const_iterator iter(md);
3014
+ upb_oneof_iter_setdone(&iter.iter_);
3015
+ return iter;
3016
+ }
3017
+ inline const FieldDef* OneofDef::const_iterator::operator*() const {
3018
+ return upb_msg_iter_field(&iter_);
3019
+ }
3020
+ inline void OneofDef::const_iterator::operator++() {
3021
+ return upb_oneof_next(&iter_);
3022
+ }
3023
+ inline bool OneofDef::const_iterator::operator==(
3024
+ const const_iterator &other) const {
3025
+ return upb_inttable_iter_isequal(&iter_, &other.iter_);
3026
+ }
3027
+ inline bool OneofDef::const_iterator::operator!=(
3028
+ const const_iterator &other) const {
3029
+ return !(*this == other);
3030
+ }
3031
+
2498
3032
  } // namespace upb
2499
3033
  #endif
2500
3034
 
@@ -7295,12 +7829,30 @@ class Parser;
7295
7829
 
7296
7830
  UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser);
7297
7831
 
7298
- // Internal-only struct used by the parser.
7832
+ // Internal-only struct used by the parser. A parser frame corresponds
7833
+ // one-to-one with a handler (sink) frame.
7299
7834
  typedef struct {
7300
7835
  UPB_PRIVATE_FOR_CPP
7301
7836
  upb_sink sink;
7837
+ // The current message in which we're parsing, and the field whose value we're
7838
+ // expecting next.
7302
7839
  const upb_msgdef *m;
7303
7840
  const upb_fielddef *f;
7841
+
7842
+ // We are in a repeated-field context, ready to emit mapentries as
7843
+ // submessages. This flag alters the start-of-object (open-brace) behavior to
7844
+ // begin a sequence of mapentry messages rather than a single submessage.
7845
+ bool is_map;
7846
+ // We are in a map-entry message context. This flag is set when parsing the
7847
+ // value field of a single map entry and indicates to all value-field parsers
7848
+ // (subobjects, strings, numbers, and bools) that the map-entry submessage
7849
+ // should end as soon as the value is parsed.
7850
+ bool is_mapentry;
7851
+ // If |is_map| or |is_mapentry| is true, |mapfield| refers to the parent
7852
+ // message's map field that we're currently parsing. This differs from |f|
7853
+ // because |f| is the field in the *current* message (i.e., the map-entry
7854
+ // message itself), not the parent's field that leads to this map.
7855
+ const upb_fielddef *mapfield;
7304
7856
  } upb_jsonparser_frame;
7305
7857
 
7306
7858