diff options
Diffstat (limited to 'upb/def.h')
-rw-r--r-- | upb/def.h | 973 |
1 files changed, 399 insertions, 574 deletions
@@ -38,114 +38,90 @@ class OneofDef; } #endif -UPB_DECLARE_TYPE(upb::Def, upb_def); -UPB_DECLARE_TYPE(upb::EnumDef, upb_enumdef); -UPB_DECLARE_TYPE(upb::FieldDef, upb_fielddef); -UPB_DECLARE_TYPE(upb::MessageDef, upb_msgdef); -UPB_DECLARE_TYPE(upb::OneofDef, upb_oneofdef); - -// Maximum field number allowed for FieldDefs. This is an inherent limit of the -// protobuf wire format. -#define UPB_MAX_FIELDNUMBER ((1 << 29) - 1) +UPB_DECLARE_DERIVED_TYPE(upb::Def, upb::RefCounted, upb_def, upb_refcounted) -// The maximum message depth that the type graph can have. This is a resource -// limit for the C stack since we sometimes need to recursively traverse the -// graph. Cycles are ok; the traversal will stop when it detects a cycle, but -// we must hit the cycle before the maximum depth is reached. -// -// If having a single static limit is too inflexible, we can add another variant -// of Def::Freeze that allows specifying this as a parameter. +/* The maximum message depth that the type graph can have. This is a resource + * limit for the C stack since we sometimes need to recursively traverse the + * graph. Cycles are ok; the traversal will stop when it detects a cycle, but + * we must hit the cycle before the maximum depth is reached. + * + * If having a single static limit is too inflexible, we can add another variant + * of Def::Freeze that allows specifying this as a parameter. */ #define UPB_MAX_MESSAGE_DEPTH 64 /* upb::Def: base class for defs *********************************************/ -// All the different kind of defs we support. These correspond 1:1 with -// declarations in a .proto file. +/* All the different kind of defs we support. These correspond 1:1 with + * declarations in a .proto file. */ typedef enum { UPB_DEF_MSG, UPB_DEF_FIELD, UPB_DEF_ENUM, UPB_DEF_ONEOF, - UPB_DEF_SERVICE, // Not yet implemented. - UPB_DEF_ANY = -1, // Wildcard for upb_symtab_get*() + UPB_DEF_SERVICE, /* Not yet implemented. */ + UPB_DEF_ANY = -1 /* Wildcard for upb_symtab_get*() */ } upb_deftype_t; -// The base class of all defs. Its base is upb::RefCounted (use upb::upcast() -// to convert). -UPB_DEFINE_CLASS1(upb::Def, upb::RefCounted, +#ifdef __cplusplus + +/* The base class of all defs. Its base is upb::RefCounted (use upb::upcast() + * to convert). */ +class upb::Def { public: typedef upb_deftype_t Type; Def* Dup(const void *owner) const; - // Functionality from upb::RefCounted. - bool IsFrozen() const; - void Ref(const void* owner) const; - void Unref(const void* owner) const; - void DonateRef(const void* from, const void* to) const; - void CheckRef(const void* owner) const; + /* upb::RefCounted methods like Ref()/Unref(). */ + UPB_REFCOUNTED_CPPMETHODS Type def_type() const; - // "fullname" is the def's fully-qualified name (eg. foo.bar.Message). + /* "fullname" is the def's fully-qualified name (eg. foo.bar.Message). */ const char *full_name() const; - // The def must be mutable. Caller retains ownership of fullname. Defs are - // not required to have a name; if a def has no name when it is frozen, it - // will remain an anonymous def. On failure, returns false and details in "s" - // if non-NULL. + /* The def must be mutable. Caller retains ownership of fullname. Defs are + * not required to have a name; if a def has no name when it is frozen, it + * will remain an anonymous def. On failure, returns false and details in "s" + * if non-NULL. */ bool set_full_name(const char* fullname, upb::Status* s); bool set_full_name(const std::string &fullname, upb::Status* s); - // Freezes the given defs; this validates all constraints and marks the defs - // as frozen (read-only). "defs" may not contain any fielddefs, but fields - // of any msgdefs will be frozen. - // - // Symbolic references to sub-types and enum defaults must have already been - // resolved. Any mutable defs reachable from any of "defs" must also be in - // the list; more formally, "defs" must be a transitive closure of mutable - // defs. - // - // After this operation succeeds, the finalized defs must only be accessed - // through a const pointer! + /* Freezes the given defs; this validates all constraints and marks the defs + * as frozen (read-only). "defs" may not contain any fielddefs, but fields + * of any msgdefs will be frozen. + * + * Symbolic references to sub-types and enum defaults must have already been + * resolved. Any mutable defs reachable from any of "defs" must also be in + * the list; more formally, "defs" must be a transitive closure of mutable + * defs. + * + * After this operation succeeds, the finalized defs must only be accessed + * through a const pointer! */ static bool Freeze(Def* const* defs, int n, Status* status); static bool Freeze(const std::vector<Def*>& defs, Status* status); private: - UPB_DISALLOW_POD_OPS(Def, upb::Def); -, -UPB_DEFINE_STRUCT(upb_def, upb_refcounted, - const char *fullname; - upb_deftype_t type : 8; - // Used as a flag during the def's mutable stage. Must be false unless - // it is currently being used by a function on the stack. This allows - // us to easily determine which defs were passed into the function's - // current invocation. - bool came_from_user; -)); - -#define UPB_DEF_INIT(name, type, refs, ref2s) \ - { UPB_REFCOUNT_INIT(refs, ref2s), name, type, false } - -UPB_BEGIN_EXTERN_C // { - -// Native C API. + UPB_DISALLOW_POD_OPS(Def, upb::Def) +}; + +#endif /* __cplusplus */ + +UPB_BEGIN_EXTERN_C + +/* Native C API. */ upb_def *upb_def_dup(const upb_def *def, const void *owner); -// From upb_refcounted. -bool upb_def_isfrozen(const upb_def *def); -void upb_def_ref(const upb_def *def, const void *owner); -void upb_def_unref(const upb_def *def, const void *owner); -void upb_def_donateref(const upb_def *def, const void *from, const void *to); -void upb_def_checkref(const upb_def *def, const void *owner); +/* Include upb_refcounted methods like upb_def_ref()/upb_def_unref(). */ +UPB_REFCOUNTED_CMETHODS(upb_def, upb_def_upcast) upb_deftype_t upb_def_type(const upb_def *d); const char *upb_def_fullname(const upb_def *d); bool upb_def_setfullname(upb_def *def, const char *fullname, upb_status *s); bool upb_def_freeze(upb_def *const *defs, int n, upb_status *s); -UPB_END_EXTERN_C // } +UPB_END_EXTERN_C /* upb::Def casts *************************************************************/ @@ -178,14 +154,14 @@ UPB_END_EXTERN_C // } inline const cpptype *dyn_cast<const cpptype *, Def>(Def * def) { \ return upb_dyncast_##cname(def); \ } \ - } // namespace upb + } /* namespace upb */ #else #define UPB_CPP_CASTS(cname, cpptype) -#endif +#endif /* __cplusplus */ -// Dynamic casts, for determining if a def is of a particular type at runtime. -// Downcasts, for when some wants to assert that a def is of a particular type. -// These are only checked if we are building debug. +/* Dynamic casts, for determining if a def is of a particular type at runtime. + * Downcasts, for when some wants to assert that a def is of a particular type. + * These are only checked if we are building debug. */ #define UPB_DEF_CASTS(lower, upper, cpptype) \ UPB_INLINE const upb_##lower *upb_dyncast_##lower(const upb_def *def) { \ if (upb_def_type(def) != UPB_DEF_##upper) return NULL; \ @@ -204,16 +180,30 @@ UPB_END_EXTERN_C // } UPB_CPP_CASTS(lower, cpptype) #define UPB_DEFINE_DEF(cppname, lower, upper, cppmethods, members) \ - UPB_DEFINE_CLASS2(cppname, upb::Def, upb::RefCounted, UPB_QUOTE(cppmethods), \ + UPB_DEFINE_CLASS2(cppname, upb::Def, upb::RefCounted, cppmethods, \ members) \ UPB_DEF_CASTS(lower, upper, cppname) +#define UPB_DECLARE_DEF_TYPE(cppname, lower, upper) \ + UPB_DECLARE_DERIVED_TYPE2(cppname, upb::Def, upb::RefCounted, \ + upb_ ## lower, upb_def, upb_refcounted) \ + UPB_DEF_CASTS(lower, upper, cppname) + +UPB_DECLARE_DEF_TYPE(upb::FieldDef, fielddef, FIELD) +UPB_DECLARE_DEF_TYPE(upb::MessageDef, msgdef, MSG) +UPB_DECLARE_DEF_TYPE(upb::EnumDef, enumdef, ENUM) +UPB_DECLARE_DEF_TYPE(upb::OneofDef, oneofdef, ONEOF) + +#undef UPB_DECLARE_DEF_TYPE +#undef UPB_DEF_CASTS +#undef UPB_CPP_CASTS + /* upb::FieldDef **************************************************************/ -// The types a field can have. Note that this list is not identical to the -// types defined in descriptor.proto, which gives INT32 and SINT32 separate -// types (we distinguish the two with the "integer encoding" enum below). +/* The types a field can have. Note that this list is not identical to the + * types defined in descriptor.proto, which gives INT32 and SINT32 separate + * types (we distinguish the two with the "integer encoding" enum below). */ typedef enum { UPB_TYPE_FLOAT = 1, UPB_TYPE_DOUBLE = 2, @@ -221,29 +211,29 @@ typedef enum { UPB_TYPE_STRING = 4, UPB_TYPE_BYTES = 5, UPB_TYPE_MESSAGE = 6, - UPB_TYPE_ENUM = 7, // Enum values are int32. + UPB_TYPE_ENUM = 7, /* Enum values are int32. */ UPB_TYPE_INT32 = 8, UPB_TYPE_UINT32 = 9, UPB_TYPE_INT64 = 10, - UPB_TYPE_UINT64 = 11, + UPB_TYPE_UINT64 = 11 } upb_fieldtype_t; -// The repeated-ness of each field; this matches descriptor.proto. +/* The repeated-ness of each field; this matches descriptor.proto. */ typedef enum { UPB_LABEL_OPTIONAL = 1, UPB_LABEL_REQUIRED = 2, - UPB_LABEL_REPEATED = 3, + UPB_LABEL_REPEATED = 3 } upb_label_t; -// How integers should be encoded in serializations that offer multiple -// integer encoding methods. +/* How integers should be encoded in serializations that offer multiple + * integer encoding methods. */ typedef enum { UPB_INTFMT_VARIABLE = 1, UPB_INTFMT_FIXED = 2, - UPB_INTFMT_ZIGZAG = 3, // Only for signed types (INT32/INT64). + UPB_INTFMT_ZIGZAG = 3 /* Only for signed types (INT32/INT64). */ } upb_intfmt_t; -// Descriptor types, as defined in descriptor.proto. +/* Descriptor types, as defined in descriptor.proto. */ typedef enum { UPB_DESCRIPTOR_TYPE_DOUBLE = 1, UPB_DESCRIPTOR_TYPE_FLOAT = 2, @@ -262,128 +252,129 @@ typedef enum { UPB_DESCRIPTOR_TYPE_SFIXED32 = 15, UPB_DESCRIPTOR_TYPE_SFIXED64 = 16, UPB_DESCRIPTOR_TYPE_SINT32 = 17, - UPB_DESCRIPTOR_TYPE_SINT64 = 18, + UPB_DESCRIPTOR_TYPE_SINT64 = 18 } upb_descriptortype_t; +/* Maximum field number allowed for FieldDefs. This is an inherent limit of the + * protobuf wire format. */ +#define UPB_MAX_FIELDNUMBER ((1 << 29) - 1) + +#ifdef __cplusplus -// A upb_fielddef describes a single field in a message. It is most often -// found as a part of a upb_msgdef, but can also stand alone to represent -// an extension. -// -// Its base class is upb::Def (use upb::upcast() to convert). -UPB_DEFINE_DEF(upb::FieldDef, fielddef, FIELD, +/* A upb_fielddef describes a single field in a message. It is most often + * found as a part of a upb_msgdef, but can also stand alone to represent + * an extension. + * + * Its base class is upb::Def (use upb::upcast() to convert). */ +class upb::FieldDef { public: typedef upb_fieldtype_t Type; typedef upb_label_t Label; typedef upb_intfmt_t IntegerFormat; typedef upb_descriptortype_t DescriptorType; - // These return true if the given value is a valid member of the enumeration. + /* These return true if the given value is a valid member of the enumeration. */ static bool CheckType(int32_t val); static bool CheckLabel(int32_t val); static bool CheckDescriptorType(int32_t val); static bool CheckIntegerFormat(int32_t val); - // These convert to the given enumeration; they require that the value is - // valid. + /* These convert to the given enumeration; they require that the value is + * valid. */ static Type ConvertType(int32_t val); static Label ConvertLabel(int32_t val); static DescriptorType ConvertDescriptorType(int32_t val); static IntegerFormat ConvertIntegerFormat(int32_t val); - // Returns NULL if memory allocation failed. + /* Returns NULL if memory allocation failed. */ static reffed_ptr<FieldDef> New(); - // Duplicates the given field, returning NULL if memory allocation failed. - // When a fielddef is duplicated, the subdef (if any) is made symbolic if it - // wasn't already. If the subdef is set but has no name (which is possible - // since msgdefs are not required to have a name) the new fielddef's subdef - // will be unset. + /* Duplicates the given field, returning NULL if memory allocation failed. + * When a fielddef is duplicated, the subdef (if any) is made symbolic if it + * wasn't already. If the subdef is set but has no name (which is possible + * since msgdefs are not required to have a name) the new fielddef's subdef + * will be unset. */ FieldDef* Dup(const void* owner) const; - // Functionality from upb::RefCounted. - bool IsFrozen() const; - void Ref(const void* owner) const; - void Unref(const void* owner) const; - void DonateRef(const void* from, const void* to) const; - void CheckRef(const void* owner) const; + /* upb::RefCounted methods like Ref()/Unref(). */ + UPB_REFCOUNTED_CPPMETHODS - // Functionality from upb::Def. + /* Functionality from upb::Def. */ const char* full_name() const; - bool type_is_set() const; // Whether set_[descriptor_]type() has been called. - Type type() const; // Requires that type_is_set() == true. - Label label() const; // Defaults to UPB_LABEL_OPTIONAL. - const char* name() const; // NULL if uninitialized. - uint32_t number() const; // Returns 0 if uninitialized. + bool type_is_set() const; /* set_[descriptor_]type() has been called? */ + Type type() const; /* Requires that type_is_set() == true. */ + Label label() const; /* Defaults to UPB_LABEL_OPTIONAL. */ + const char* name() const; /* NULL if uninitialized. */ + uint32_t number() const; /* Returns 0 if uninitialized. */ bool is_extension() const; - // For UPB_TYPE_MESSAGE fields only where is_tag_delimited() == false, - // indicates whether this field should have lazy parsing handlers that yield - // the unparsed string for the submessage. - // - // TODO(haberman): I think we want to move this into a FieldOptions container - // when we add support for custom options (the FieldOptions struct will - // contain both regular FieldOptions like "lazy" *and* custom options). + /* For UPB_TYPE_MESSAGE fields only where is_tag_delimited() == false, + * indicates whether this field should have lazy parsing handlers that yield + * the unparsed string for the submessage. + * + * TODO(haberman): I think we want to move this into a FieldOptions container + * when we add support for custom options (the FieldOptions struct will + * contain both regular FieldOptions like "lazy" *and* custom options). */ bool lazy() const; - // For non-string, non-submessage fields, this indicates whether binary - // protobufs are encoded in packed or non-packed format. - // - // TODO(haberman): see note above about putting options like this into a - // FieldOptions container. + /* For non-string, non-submessage fields, this indicates whether binary + * protobufs are encoded in packed or non-packed format. + * + * TODO(haberman): see note above about putting options like this into a + * FieldOptions container. */ bool packed() const; - // An integer that can be used as an index into an array of fields for - // whatever message this field belongs to. Guaranteed to be less than - // f->containing_type()->field_count(). May only be accessed once the def has - // been finalized. + /* An integer that can be used as an index into an array of fields for + * whatever message this field belongs to. Guaranteed to be less than + * f->containing_type()->field_count(). May only be accessed once the def has + * been finalized. */ int index() const; - // The MessageDef to which this field belongs. - // - // If this field has been added to a MessageDef, that message can be retrieved - // directly (this is always the case for frozen FieldDefs). - // - // If the field has not yet been added to a MessageDef, you can set the name - // of the containing type symbolically instead. This is mostly useful for - // extensions, where the extension is declared separately from the message. + /* The MessageDef to which this field belongs. + * + * If this field has been added to a MessageDef, that message can be retrieved + * directly (this is always the case for frozen FieldDefs). + * + * If the field has not yet been added to a MessageDef, you can set the name + * of the containing type symbolically instead. This is mostly useful for + * extensions, where the extension is declared separately from the message. */ const MessageDef* containing_type() const; const char* containing_type_name(); - // The OneofDef to which this field belongs, or NULL if this field is not part - // of a oneof. + /* The OneofDef to which this field belongs, or NULL if this field is not part + * of a oneof. */ const OneofDef* containing_oneof() const; - // The field's type according to the enum in descriptor.proto. This is not - // the same as UPB_TYPE_*, because it distinguishes between (for example) - // INT32 and SINT32, whereas our "type" enum does not. This return of - // descriptor_type() is a function of type(), integer_format(), and - // is_tag_delimited(). Likewise set_descriptor_type() sets all three - // appropriately. + /* The field's type according to the enum in descriptor.proto. This is not + * the same as UPB_TYPE_*, because it distinguishes between (for example) + * INT32 and SINT32, whereas our "type" enum does not. This return of + * descriptor_type() is a function of type(), integer_format(), and + * is_tag_delimited(). Likewise set_descriptor_type() sets all three + * appropriately. */ DescriptorType descriptor_type() const; - // Convenient field type tests. + /* Convenient field type tests. */ bool IsSubMessage() const; bool IsString() const; bool IsSequence() const; bool IsPrimitive() const; bool IsMap() const; - // How integers are encoded. Only meaningful for integer types. - // Defaults to UPB_INTFMT_VARIABLE, and is reset when "type" changes. + /* How integers are encoded. Only meaningful for integer types. + * Defaults to UPB_INTFMT_VARIABLE, and is reset when "type" changes. */ IntegerFormat integer_format() const; - // Whether a submessage field is tag-delimited or not (if false, then - // length-delimited). May only be set when type() == UPB_TYPE_MESSAGE. + /* Whether a submessage field is tag-delimited or not (if false, then + * length-delimited). May only be set when type() == UPB_TYPE_MESSAGE. */ bool is_tag_delimited() const; - // Returns the non-string default value for this fielddef, which may either - // be something the client set explicitly or the "default default" (0 for - // numbers, empty for strings). The field's type indicates the type of the - // returned value, except for enum fields that are still mutable. - // - // Requires that the given function matches the field's current type. + /* Returns the non-string default value for this fielddef, which may either + * be something the client set explicitly or the "default default" (0 for + * numbers, empty for strings). The field's type indicates the type of the + * returned value, except for enum fields that are still mutable. + * + * Requires that the given function matches the field's current type. */ int64_t default_int64() const; int32_t default_int32() const; uint64_t default_uint64() const; @@ -392,83 +383,81 @@ UPB_DEFINE_DEF(upb::FieldDef, fielddef, FIELD, float default_float() const; double default_double() const; - // The resulting string is always NULL-terminated. If non-NULL, the length - // will be stored in *len. + /* The resulting string is always NULL-terminated. If non-NULL, the length + * will be stored in *len. */ const char *default_string(size_t* len) const; - // For frozen UPB_TYPE_ENUM fields, enum defaults can always be read as either - // string or int32, and both of these methods will always return true. - // - // For mutable UPB_TYPE_ENUM fields, the story is a bit more complicated. - // Enum defaults are unusual. They can be specified either as string or int32, - // but to be valid the enum must have that value as a member. And if no - // default is specified, the "default default" comes from the EnumDef. - // - // We allow reading the default as either an int32 or a string, but only if - // we have a meaningful value to report. We have a meaningful value if it was - // set explicitly, or if we could get the "default default" from the EnumDef. - // Also if you explicitly set the name and we find the number in the EnumDef + /* For frozen UPB_TYPE_ENUM fields, enum defaults can always be read as either + * string or int32, and both of these methods will always return true. + * + * For mutable UPB_TYPE_ENUM fields, the story is a bit more complicated. + * Enum defaults are unusual. They can be specified either as string or int32, + * but to be valid the enum must have that value as a member. And if no + * default is specified, the "default default" comes from the EnumDef. + * + * We allow reading the default as either an int32 or a string, but only if + * we have a meaningful value to report. We have a meaningful value if it was + * set explicitly, or if we could get the "default default" from the EnumDef. + * Also if you explicitly set the name and we find the number in the EnumDef */ bool EnumHasStringDefault() const; bool EnumHasInt32Default() const; - // Submessage and enum fields must reference a "subdef", which is the - // upb::MessageDef or upb::EnumDef that defines their type. Note that when - // the FieldDef is mutable it may not have a subdef *yet*, but this function - // still returns true to indicate that the field's type requires a subdef. + /* Submessage and enum fields must reference a "subdef", which is the + * upb::MessageDef or upb::EnumDef that defines their type. Note that when + * the FieldDef is mutable it may not have a subdef *yet*, but this function + * still returns true to indicate that the field's type requires a subdef. */ bool HasSubDef() const; - // Returns the enum or submessage def for this field, if any. The field's - // type must match (ie. you may only call enum_subdef() for fields where - // type() == UPB_TYPE_ENUM). Returns NULL if the subdef has not been set or - // is currently set symbolically. + /* Returns the enum or submessage def for this field, if any. The field's + * type must match (ie. you may only call enum_subdef() for fields where + * type() == UPB_TYPE_ENUM). Returns NULL if the subdef has not been set or + * is currently set symbolically. */ const EnumDef* enum_subdef() const; const MessageDef* message_subdef() const; - // Returns the generic subdef for this field. Requires that HasSubDef() (ie. - // only works for UPB_TYPE_ENUM and UPB_TYPE_MESSAGE fields). + /* Returns the generic subdef for this field. Requires that HasSubDef() (ie. + * only works for UPB_TYPE_ENUM and UPB_TYPE_MESSAGE fields). */ const Def* subdef() const; - // Returns the symbolic name of the subdef. If the subdef is currently set - // unresolved (ie. set symbolically) returns the symbolic name. If it has - // been resolved to a specific subdef, returns the name from that subdef. + /* Returns the symbolic name of the subdef. If the subdef is currently set + * unresolved (ie. set symbolically) returns the symbolic name. If it has + * been resolved to a specific subdef, returns the name from that subdef. */ const char* subdef_name() const; - ////////////////////////////////////////////////////////////////////////////// - // Setters (non-const methods), only valid for mutable FieldDefs! - ////////////////////////////////////////////////////////////////////////////// + /* Setters (non-const methods), only valid for mutable FieldDefs! ***********/ bool set_full_name(const char* fullname, upb::Status* s); bool set_full_name(const std::string& fullname, upb::Status* s); - // This may only be called if containing_type() == NULL (ie. the field has not - // been added to a message yet). + /* This may only be called if containing_type() == NULL (ie. the field has not + * been added to a message yet). */ bool set_containing_type_name(const char *name, Status* status); bool set_containing_type_name(const std::string& name, Status* status); - // Defaults to false. When we freeze, we ensure that this can only be true - // for length-delimited message fields. Prior to freezing this can be true or - // false with no restrictions. + /* Defaults to false. When we freeze, we ensure that this can only be true + * for length-delimited message fields. Prior to freezing this can be true or + * false with no restrictions. */ void set_lazy(bool lazy); - // Defaults to true. Sets whether this field is encoded in packed format. + /* Defaults to true. Sets whether this field is encoded in packed format. */ void set_packed(bool packed); - // "type" or "descriptor_type" MUST be set explicitly before the fielddef is - // finalized. These setters require that the enum value is valid; if the - // value did not come directly from an enum constant, the caller should - // validate it first with the functions above (CheckFieldType(), etc). + /* "type" or "descriptor_type" MUST be set explicitly before the fielddef is + * finalized. These setters require that the enum value is valid; if the + * value did not come directly from an enum constant, the caller should + * validate it first with the functions above (CheckFieldType(), etc). */ void set_type(Type type); void set_label(Label label); void set_descriptor_type(DescriptorType type); void set_is_extension(bool is_extension); - // "number" and "name" must be set before the FieldDef is added to a - // MessageDef, and may not be set after that. - // - // "name" is the same as full_name()/set_full_name(), but since fielddefs - // most often use simple, non-qualified names, we provide this accessor - // also. Generally only extensions will want to think of this name as - // fully-qualified. + /* "number" and "name" must be set before the FieldDef is added to a + * MessageDef, and may not be set after that. + * + * "name" is the same as full_name()/set_full_name(), but since fielddefs + * most often use simple, non-qualified names, we provide this accessor + * also. Generally only extensions will want to think of this name as + * fully-qualified. */ bool set_number(uint32_t number, upb::Status* s); bool set_name(const char* name, upb::Status* s); bool set_name(const std::string& name, upb::Status* s); @@ -476,12 +465,12 @@ UPB_DEFINE_DEF(upb::FieldDef, fielddef, FIELD, void set_integer_format(IntegerFormat format); bool set_tag_delimited(bool tag_delimited, upb::Status* s); - // Sets default value for the field. The call must exactly match the type - // of the field. Enum fields may use either setint32 or setstring to set - // the default numerically or symbolically, respectively, but symbolic - // defaults must be resolved before finalizing (see ResolveEnumDefault()). - // - // Changing the type of a field will reset its default. + /* Sets default value for the field. The call must exactly match the type + * of the field. Enum fields may use either setint32 or setstring to set + * the default numerically or symbolically, respectively, but symbolic + * defaults must be resolved before finalizing (see ResolveEnumDefault()). + * + * Changing the type of a field will reset its default. */ void set_default_int64(int64_t val); void set_default_int32(int32_t val); void set_default_uint64(uint64_t val); @@ -493,15 +482,15 @@ UPB_DEFINE_DEF(upb::FieldDef, fielddef, FIELD, bool set_default_string(const std::string &str, Status *s); void set_default_cstr(const char *str, Status *s); - // Before a fielddef is frozen, its subdef may be set either directly (with a - // upb::Def*) or symbolically. Symbolic refs must be resolved before the - // containing msgdef can be frozen (see upb_resolve() above). upb always - // guarantees that any def reachable from a live def will also be kept alive. - // - // Both methods require that upb_hassubdef(f) (so the type must be set prior - // to calling these methods). Returns false if this is not the case, or if - // the given subdef is not of the correct type. The subdef is reset if the - // field's type is changed. The subdef can be set to NULL to clear it. + /* Before a fielddef is frozen, its subdef may be set either directly (with a + * upb::Def*) or symbolically. Symbolic refs must be resolved before the + * containing msgdef can be frozen (see upb_resolve() above). upb always + * guarantees that any def reachable from a live def will also be kept alive. + * + * Both methods require that upb_hassubdef(f) (so the type must be set prior + * to calling these methods). Returns false if this is not the case, or if + * the given subdef is not of the correct type. The subdef is reset if the + * field's type is changed. The subdef can be set to NULL to clear it. */ bool set_subdef(const Def* subdef, Status* s); bool set_enum_subdef(const EnumDef* subdef, Status* s); bool set_message_subdef(const MessageDef* subdef, Status* s); @@ -509,66 +498,21 @@ UPB_DEFINE_DEF(upb::FieldDef, fielddef, FIELD, bool set_subdef_name(const std::string &name, Status* s); private: - UPB_DISALLOW_POD_OPS(FieldDef, upb::FieldDef); -, -UPB_DEFINE_STRUCT(upb_fielddef, upb_def, - union { - int64_t sint; - uint64_t uint; - double dbl; - float flt; - void *bytes; - } defaultval; - union { - const upb_msgdef *def; // If !msg_is_symbolic. - char *name; // If msg_is_symbolic. - } msg; - union { - const upb_def *def; // If !subdef_is_symbolic. - char *name; // If subdef_is_symbolic. - } sub; // The msgdef or enumdef for this field, if upb_hassubdef(f). - bool subdef_is_symbolic; - bool msg_is_symbolic; - const upb_oneofdef *oneof; - bool default_is_string; - bool type_is_set_; // False until type is explicitly set. - bool is_extension_; - bool lazy_; - bool packed_; - upb_intfmt_t intfmt; - bool tagdelim; - upb_fieldtype_t type_; - upb_label_t label_; - uint32_t number_; - uint32_t selector_base; // Used to index into a upb::Handlers table. - uint32_t index_; -)); - -#define UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy, \ - packed, name, num, msgdef, subdef, selector_base, \ - index, defaultval, refs, ref2s) \ - { \ - UPB_DEF_INIT(name, UPB_DEF_FIELD, refs, ref2s), defaultval, {msgdef}, \ - {subdef}, NULL, false, false, \ - type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES, true, is_extension, \ - lazy, packed, intfmt, tagdelim, type, label, num, selector_base, index \ - } + UPB_DISALLOW_POD_OPS(FieldDef, upb::FieldDef) +}; + +# endif /* defined(__cplusplus) */ -UPB_BEGIN_EXTERN_C // { +UPB_BEGIN_EXTERN_C -// Native C API. +/* Native C API. */ upb_fielddef *upb_fielddef_new(const void *owner); upb_fielddef *upb_fielddef_dup(const upb_fielddef *f, const void *owner); -// From upb_refcounted. -bool upb_fielddef_isfrozen(const upb_fielddef *f); -void upb_fielddef_ref(const upb_fielddef *f, const void *owner); -void upb_fielddef_unref(const upb_fielddef *f, const void *owner); -void upb_fielddef_donateref(const upb_fielddef *f, const void *from, - const void *to); -void upb_fielddef_checkref(const upb_fielddef *f, const void *owner); +/* Include upb_refcounted methods like upb_fielddef_ref(). */ +UPB_REFCOUNTED_CMETHODS(upb_fielddef, upb_fielddef_upcast2) -// From upb_def. +/* Methods from upb_def. */ const char *upb_fielddef_fullname(const upb_fielddef *f); bool upb_fielddef_setfullname(upb_fielddef *f, const char *fullname, upb_status *s); @@ -647,7 +591,7 @@ bool upb_fielddef_checktype(int32_t type); bool upb_fielddef_checkdescriptortype(int32_t type); bool upb_fielddef_checkintfmt(int32_t fmt); -UPB_END_EXTERN_C // } +UPB_END_EXTERN_C /* upb::MessageDef ************************************************************/ @@ -655,66 +599,64 @@ UPB_END_EXTERN_C // } typedef upb_inttable_iter upb_msg_field_iter; typedef upb_strtable_iter upb_msg_oneof_iter; -// Structure that describes a single .proto message type. -// -// Its base class is upb::Def (use upb::upcast() to convert). -UPB_DEFINE_DEF(upb::MessageDef, msgdef, MSG, UPB_QUOTE( +#ifdef __cplusplus + +/* Structure that describes a single .proto message type. + * + * Its base class is upb::Def (use upb::upcast() to convert). */ +class upb::MessageDef { public: - // Returns NULL if memory allocation failed. + /* Returns NULL if memory allocation failed. */ static reffed_ptr<MessageDef> New(); - // Functionality from upb::RefCounted. - bool IsFrozen() const; - void Ref(const void* owner) const; - void Unref(const void* owner) const; - void DonateRef(const void* from, const void* to) const; - void CheckRef(const void* owner) const; + /* upb::RefCounted methods like Ref()/Unref(). */ + UPB_REFCOUNTED_CPPMETHODS - // Functionality from upb::Def. + /* Functionality from upb::Def. */ const char* full_name() const; bool set_full_name(const char* fullname, Status* s); bool set_full_name(const std::string& fullname, Status* s); - // Call to freeze this MessageDef. - // WARNING: this will fail if this message has any unfrozen submessages! - // Messages with cycles must be frozen as a batch using upb::Def::Freeze(). + /* Call to freeze this MessageDef. + * WARNING: this will fail if this message has any unfrozen submessages! + * Messages with cycles must be frozen as a batch using upb::Def::Freeze(). */ bool Freeze(Status* s); - // The number of fields that belong to the MessageDef. + /* The number of fields that belong to the MessageDef. */ int field_count() const; - // The number of oneofs that belong to the MessageDef. + /* The number of oneofs that belong to the MessageDef. */ int oneof_count() const; - // Adds a field (upb_fielddef object) to a msgdef. Requires that the msgdef - // and the fielddefs are mutable. The fielddef's name and number must be - // set, and the message may not already contain any field with this name or - // number, and this fielddef may not be part of another message. In error - // cases false is returned and the msgdef is unchanged. - // - // If the given field is part of a oneof, this call succeeds if and only if - // that oneof is already part of this msgdef. (Note that adding a oneof to a - // msgdef automatically adds all of its fields to the msgdef at the time that - // the oneof is added, so it is usually more idiomatic to add the oneof's - // fields first then add the oneof to the msgdef. This case is supported for - // convenience.) - // - // If |f| is already part of this MessageDef, this method performs no action - // and returns true (success). Thus, this method is idempotent. + /* Adds a field (upb_fielddef object) to a msgdef. Requires that the msgdef + * and the fielddefs are mutable. The fielddef's name and number must be + * set, and the message may not already contain any field with this name or + * number, and this fielddef may not be part of another message. In error + * cases false is returned and the msgdef is unchanged. + * + * If the given field is part of a oneof, this call succeeds if and only if + * that oneof is already part of this msgdef. (Note that adding a oneof to a + * msgdef automatically adds all of its fields to the msgdef at the time that + * the oneof is added, so it is usually more idiomatic to add the oneof's + * fields first then add the oneof to the msgdef. This case is supported for + * convenience.) + * + * If |f| is already part of this MessageDef, this method performs no action + * and returns true (success). Thus, this method is idempotent. */ bool AddField(FieldDef* f, Status* s); bool AddField(const reffed_ptr<FieldDef>& f, Status* s); - // Adds a oneof (upb_oneofdef object) to a msgdef. Requires that the msgdef, - // oneof, and any fielddefs are mutable, that the fielddefs contained in the - // oneof do not have any name or number conflicts with existing fields in the - // msgdef, and that the oneof's name is unique among all oneofs in the msgdef. - // If the oneof is added successfully, all of its fields will be added - // directly to the msgdef as well. In error cases, false is returned and the - // msgdef is unchanged. + /* Adds a oneof (upb_oneofdef object) to a msgdef. Requires that the msgdef, + * oneof, and any fielddefs are mutable, that the fielddefs contained in the + * oneof do not have any name or number conflicts with existing fields in the + * msgdef, and that the oneof's name is unique among all oneofs in the msgdef. + * If the oneof is added successfully, all of its fields will be added + * directly to the msgdef as well. In error cases, false is returned and the + * msgdef is unchanged. */ bool AddOneof(OneofDef* o, Status* s); bool AddOneof(const reffed_ptr<OneofDef>& o, Status* s); - // These return NULL if the field is not found. + /* These return NULL if the field is not found. */ FieldDef* FindFieldByNumber(uint32_t number); FieldDef* FindFieldByName(const char *name, size_t len); const FieldDef* FindFieldByNumber(uint32_t number) const; @@ -756,21 +698,21 @@ UPB_DEFINE_DEF(upb::MessageDef, msgdef, MSG, UPB_QUOTE( return FindOneofByName(str.c_str(), str.size()); } - // Returns a new msgdef that is a copy of the given msgdef (and a copy of all - // the fields) but with any references to submessages broken and replaced - // with just the name of the submessage. Returns NULL if memory allocation - // failed. - // - // TODO(haberman): which is more useful, keeping fields resolved or - // unresolving them? If there's no obvious answer, Should this functionality - // just be moved into symtab.c? + /* Returns a new msgdef that is a copy of the given msgdef (and a copy of all + * the fields) but with any references to submessages broken and replaced + * with just the name of the submessage. Returns NULL if memory allocation + * failed. + * + * TODO(haberman): which is more useful, keeping fields resolved or + * unresolving them? If there's no obvious answer, Should this functionality + * just be moved into symtab.c? */ MessageDef* Dup(const void* owner) const; - // Is this message a map entry? + /* Is this message a map entry? */ void setmapentry(bool map_entry); bool mapentry() const; - // Iteration over fields. The order is undefined. + /* Iteration over fields. The order is undefined. */ class field_iterator : public std::iterator<std::forward_iterator_tag, FieldDef*> { public: @@ -801,7 +743,7 @@ UPB_DEFINE_DEF(upb::MessageDef, msgdef, MSG, UPB_QUOTE( upb_msg_field_iter iter_; }; - // Iteration over oneofs. The order is undefined. + /* Iteration over oneofs. The order is undefined. */ class oneof_iterator : public std::iterator<std::forward_iterator_tag, FieldDef*> { public: @@ -884,52 +826,21 @@ UPB_DEFINE_DEF(upb::MessageDef, msgdef, MSG, UPB_QUOTE( ConstOneofAccessor oneofs() const { return ConstOneofAccessor(this); } private: - UPB_DISALLOW_POD_OPS(MessageDef, upb::MessageDef); -), -UPB_DEFINE_STRUCT(upb_msgdef, upb_def, - size_t selector_count; - uint32_t submsg_field_count; - - // Tables for looking up fields by number and name. - upb_inttable itof; // int to field - upb_strtable ntof; // name to field - - // Tables for looking up oneofs by name. - upb_strtable ntoo; // name to oneof - - // 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). -)); - -// TODO: also support static initialization of the oneofs table. This will be -// needed if we compile in descriptors that contain oneofs. -#define UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, \ - refs, ref2s) \ - { \ - UPB_DEF_INIT(name, UPB_DEF_MSG, refs, ref2s), selector_count, \ - submsg_field_count, itof, ntof, \ - UPB_EMPTY_STRTABLE_INIT(UPB_CTYPE_PTR), false \ - } + UPB_DISALLOW_POD_OPS(MessageDef, upb::MessageDef) +}; + +#endif /* __cplusplus */ -UPB_BEGIN_EXTERN_C // { +UPB_BEGIN_EXTERN_C -// Returns NULL if memory allocation failed. +/* Returns NULL if memory allocation failed. */ upb_msgdef *upb_msgdef_new(const void *owner); -// From upb_refcounted. -bool upb_msgdef_isfrozen(const upb_msgdef *m); -void upb_msgdef_ref(const upb_msgdef *m, const void *owner); -void upb_msgdef_unref(const upb_msgdef *m, const void *owner); -void upb_msgdef_donateref(const upb_msgdef *m, const void *from, - const void *to); -void upb_msgdef_checkref(const upb_msgdef *m, const void *owner); +/* Include upb_refcounted methods like upb_msgdef_ref(). */ +UPB_REFCOUNTED_CMETHODS(upb_msgdef, upb_msgdef_upcast2) + bool upb_msgdef_freeze(upb_msgdef *m, upb_status *status); -// From upb_def. const char *upb_msgdef_fullname(const upb_msgdef *m); bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname, upb_status *s); @@ -939,10 +850,10 @@ bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, const void *ref_donor, bool upb_msgdef_addoneof(upb_msgdef *m, upb_oneofdef *o, const void *ref_donor, upb_status *s); -// Field lookup in a couple of different variations: -// - itof = int to field -// - ntof = name to field -// - ntofz = name to field, null-terminated string. +/* Field lookup in a couple of different variations: + * - itof = int to field + * - ntof = name to field + * - ntofz = name to field, null-terminated string. */ const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i); const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name, size_t len); @@ -962,9 +873,9 @@ UPB_INLINE upb_fielddef *upb_msgdef_ntof_mutable(upb_msgdef *m, return (upb_fielddef *)upb_msgdef_ntof(m, name, len); } -// Oneof lookup: -// - ntoo = name to oneof -// - ntooz = name to oneof, null-terminated string. +/* Oneof lookup: + * - ntoo = name to oneof + * - ntooz = name to oneof, null-terminated string. */ const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name, size_t len); int upb_msgdef_numoneofs(const upb_msgdef *m); @@ -982,7 +893,7 @@ UPB_INLINE upb_oneofdef *upb_msgdef_ntoo_mutable(upb_msgdef *m, void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry); bool upb_msgdef_mapentry(const upb_msgdef *m); -// Well-known field tag numbers for map-entry messages. +/* Well-known field tag numbers for map-entry messages. */ #define UPB_MAPENTRY_KEY 1 #define UPB_MAPENTRY_VALUE 2 @@ -990,95 +901,94 @@ const upb_oneofdef *upb_msgdef_findoneof(const upb_msgdef *m, const char *name); int upb_msgdef_numoneofs(const upb_msgdef *m); -// upb_msg_field_iter i; -// for(upb_msg_field_begin(&i, m); -// !upb_msg_field_done(&i); -// upb_msg_field_next(&i)) { -// upb_fielddef *f = upb_msg_iter_field(&i); -// // ... -// } -// -// For C we don't have separate iterators for const and non-const. -// It is the caller's responsibility to cast the upb_fielddef* to -// const if the upb_msgdef* is const. +/* upb_msg_field_iter i; + * for(upb_msg_field_begin(&i, m); + * !upb_msg_field_done(&i); + * upb_msg_field_next(&i)) { + * upb_fielddef *f = upb_msg_iter_field(&i); + * // ... + * } + * + * For C we don't have separate iterators for const and non-const. + * It is the caller's responsibility to cast the upb_fielddef* to + * const if the upb_msgdef* is const. */ void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m); void upb_msg_field_next(upb_msg_field_iter *iter); bool upb_msg_field_done(const upb_msg_field_iter *iter); upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter); void upb_msg_field_iter_setdone(upb_msg_field_iter *iter); -// Similar to above, we also support iterating through the oneofs in a msgdef. +/* Similar to above, we also support iterating through the oneofs in a + * msgdef. */ void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m); void upb_msg_oneof_next(upb_msg_oneof_iter *iter); bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter); upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter); void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter); -UPB_END_EXTERN_C // } +UPB_END_EXTERN_C /* upb::EnumDef ***************************************************************/ typedef upb_strtable_iter upb_enum_iter; -// Class that represents an enum. Its base class is upb::Def (convert with -// upb::upcast()). -UPB_DEFINE_DEF(upb::EnumDef, enumdef, ENUM, +#ifdef __cplusplus + +/* Class that represents an enum. Its base class is upb::Def (convert with + * upb::upcast()). */ +class upb::EnumDef { public: - // Returns NULL if memory allocation failed. + /* Returns NULL if memory allocation failed. */ static reffed_ptr<EnumDef> New(); - // Functionality from upb::RefCounted. - bool IsFrozen() const; - void Ref(const void* owner) const; - void Unref(const void* owner) const; - void DonateRef(const void* from, const void* to) const; - void CheckRef(const void* owner) const; + /* upb::RefCounted methods like Ref()/Unref(). */ + UPB_REFCOUNTED_CPPMETHODS - // Functionality from upb::Def. + /* Functionality from upb::Def. */ const char* full_name() const; bool set_full_name(const char* fullname, Status* s); bool set_full_name(const std::string& fullname, Status* s); - // Call to freeze this EnumDef. + /* Call to freeze this EnumDef. */ bool Freeze(Status* s); - // The value that is used as the default when no field default is specified. - // If not set explicitly, the first value that was added will be used. - // The default value must be a member of the enum. - // Requires that value_count() > 0. + /* The value that is used as the default when no field default is specified. + * If not set explicitly, the first value that was added will be used. + * The default value must be a member of the enum. + * Requires that value_count() > 0. */ int32_t default_value() const; - // Sets the default value. If this value is not valid, returns false and an - // error message in status. + /* Sets the default value. If this value is not valid, returns false and an + * error message in status. */ bool set_default_value(int32_t val, Status* status); - // Returns the number of values currently defined in the enum. Note that - // multiple names can refer to the same number, so this may be greater than - // the total number of unique numbers. + /* Returns the number of values currently defined in the enum. Note that + * multiple names can refer to the same number, so this may be greater than + * the total number of unique numbers. */ int value_count() const; - // Adds a single name/number pair to the enum. Fails if this name has - // already been used by another value. + /* Adds a single name/number pair to the enum. Fails if this name has + * already been used by another value. */ bool AddValue(const char* name, int32_t num, Status* status); bool AddValue(const std::string& name, int32_t num, Status* status); - // Lookups from name to integer, returning true if found. + /* Lookups from name to integer, returning true if found. */ bool FindValueByName(const char* name, int32_t* num) const; - // Finds the name corresponding to the given number, or NULL if none was - // found. If more than one name corresponds to this number, returns the - // first one that was added. + /* Finds the name corresponding to the given number, or NULL if none was + * found. If more than one name corresponds to this number, returns the + * first one that was added. */ const char* FindValueByNumber(int32_t num) const; - // Returns a new EnumDef with all the same values. The new EnumDef will be - // owned by the given owner. + /* Returns a new EnumDef with all the same values. The new EnumDef will be + * owned by the given owner. */ EnumDef* Dup(const void* owner) const; - // Iteration over name/value pairs. The order is undefined. - // Adding an enum val invalidates any iterators. - // - // TODO: make compatible with range-for, with elements as pairs? + /* Iteration over name/value pairs. The order is undefined. + * Adding an enum val invalidates any iterators. + * + * TODO: make compatible with range-for, with elements as pairs? */ class Iterator { public: explicit Iterator(const EnumDef*); @@ -1093,33 +1003,23 @@ UPB_DEFINE_DEF(upb::EnumDef, enumdef, ENUM, }; private: - UPB_DISALLOW_POD_OPS(EnumDef, upb::EnumDef); -, -UPB_DEFINE_STRUCT(upb_enumdef, upb_def, - upb_strtable ntoi; - upb_inttable iton; - int32_t defaultval; -)); + UPB_DISALLOW_POD_OPS(EnumDef, upb::EnumDef) +}; -#define UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval, refs, ref2s) \ - { UPB_DEF_INIT(name, UPB_DEF_ENUM, refs, ref2s), ntoi, iton, defaultval } +#endif /* __cplusplus */ -UPB_BEGIN_EXTERN_C // { +UPB_BEGIN_EXTERN_C -// Native C API. +/* Native C API. */ upb_enumdef *upb_enumdef_new(const void *owner); upb_enumdef *upb_enumdef_dup(const upb_enumdef *e, const void *owner); -// From upb_refcounted. -void upb_enumdef_unref(const upb_enumdef *e, const void *owner); -bool upb_enumdef_isfrozen(const upb_enumdef *e); -void upb_enumdef_ref(const upb_enumdef *e, const void *owner); -void upb_enumdef_donateref(const upb_enumdef *m, const void *from, - const void *to); -void upb_enumdef_checkref(const upb_enumdef *e, const void *owner); +/* Include upb_refcounted methods like upb_enumdef_ref(). */ +UPB_REFCOUNTED_CMETHODS(upb_enumdef, upb_enumdef_upcast2) + bool upb_enumdef_freeze(upb_enumdef *e, upb_status *status); -// From upb_def. +/* From upb_def. */ const char *upb_enumdef_fullname(const upb_enumdef *e); bool upb_enumdef_setfullname(upb_enumdef *e, const char *fullname, upb_status *s); @@ -1130,10 +1030,11 @@ int upb_enumdef_numvals(const upb_enumdef *e); bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num, upb_status *status); -// Enum lookups: -// - ntoi: look up a name with specified length. -// - ntoiz: look up a name provided as a null-terminated string. -// - iton: look up an integer, returning the name as a null-terminated string. +/* Enum lookups: + * - ntoi: look up a name with specified length. + * - ntoiz: look up a name provided as a null-terminated string. + * - iton: look up an integer, returning the name as a null-terminated + * string. */ bool upb_enumdef_ntoi(const upb_enumdef *e, const char *name, size_t len, int32_t *num); UPB_INLINE bool upb_enumdef_ntoiz(const upb_enumdef *e, @@ -1142,66 +1043,65 @@ UPB_INLINE bool upb_enumdef_ntoiz(const upb_enumdef *e, } const char *upb_enumdef_iton(const upb_enumdef *e, int32_t num); -// upb_enum_iter i; -// for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) { -// // ... -// } +/* upb_enum_iter i; + * for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) { + * // ... + * } + */ void upb_enum_begin(upb_enum_iter *iter, const upb_enumdef *e); void upb_enum_next(upb_enum_iter *iter); bool upb_enum_done(upb_enum_iter *iter); const char *upb_enum_iter_name(upb_enum_iter *iter); int32_t upb_enum_iter_number(upb_enum_iter *iter); -UPB_END_EXTERN_C // } +UPB_END_EXTERN_C /* upb::OneofDef **************************************************************/ typedef upb_inttable_iter upb_oneof_iter; -// Class that represents a oneof. Its base class is upb::Def (convert with -// upb::upcast()). -UPB_DEFINE_DEF(upb::OneofDef, oneofdef, ONEOF, UPB_QUOTE( +#ifdef __cplusplus + +/* Class that represents a oneof. Its base class is upb::Def (convert with + * upb::upcast()). */ +class upb::OneofDef { public: - // Returns NULL if memory allocation failed. + /* Returns NULL if memory allocation failed. */ static reffed_ptr<OneofDef> New(); - // Functionality from upb::RefCounted. - bool IsFrozen() const; - void Ref(const void* owner) const; - void Unref(const void* owner) const; - void DonateRef(const void* from, const void* to) const; - void CheckRef(const void* owner) const; + /* upb::RefCounted methods like Ref()/Unref(). */ + UPB_REFCOUNTED_CPPMETHODS - // Functionality from upb::Def. + /* Functionality from upb::Def. */ const char* full_name() const; - // Returns the MessageDef that owns this OneofDef. + /* Returns the MessageDef that owns this OneofDef. */ const MessageDef* containing_type() const; - // Returns the name of this oneof. This is the name used to look up the oneof - // by name once added to a message def. + /* Returns the name of this oneof. This is the name used to look up the oneof + * by name once added to a message def. */ const char* name() const; bool set_name(const char* name, Status* s); - // Returns the number of fields currently defined in the oneof. + /* Returns the number of fields currently defined in the oneof. */ int field_count() const; - // Adds a field to the oneof. The field must not have been added to any other - // oneof or msgdef. If the oneof is not yet part of a msgdef, then when the - // oneof is eventually added to a msgdef, all fields added to the oneof will - // also be added to the msgdef at that time. If the oneof is already part of a - // msgdef, the field must either be a part of that msgdef already, or must not - // be a part of any msgdef; in the latter case, the field is added to the - // msgdef as a part of this operation. - // - // The field may only have an OPTIONAL label, never REQUIRED or REPEATED. - // - // If |f| is already part of this MessageDef, this method performs no action - // and returns true (success). Thus, this method is idempotent. + /* Adds a field to the oneof. The field must not have been added to any other + * oneof or msgdef. If the oneof is not yet part of a msgdef, then when the + * oneof is eventually added to a msgdef, all fields added to the oneof will + * also be added to the msgdef at that time. If the oneof is already part of a + * msgdef, the field must either be a part of that msgdef already, or must not + * be a part of any msgdef; in the latter case, the field is added to the + * msgdef as a part of this operation. + * + * The field may only have an OPTIONAL label, never REQUIRED or REPEATED. + * + * If |f| is already part of this MessageDef, this method performs no action + * and returns true (success). Thus, this method is idempotent. */ bool AddField(FieldDef* field, Status* s); bool AddField(const reffed_ptr<FieldDef>& field, Status* s); - // Looks up by name. + /* Looks up by name. */ const FieldDef* FindFieldByName(const char* name, size_t len) const; FieldDef* FindFieldByName(const char* name, size_t len); const FieldDef* FindFieldByName(const char* name) const { @@ -1220,14 +1120,14 @@ UPB_DEFINE_DEF(upb::OneofDef, oneofdef, ONEOF, UPB_QUOTE( return FindFieldByName(str.c_str(), str.size()); } - // Looks up by tag number. + /* Looks up by tag number. */ const FieldDef* FindFieldByNumber(uint32_t num) const; - // Returns a new OneofDef with all the same fields. The OneofDef will be owned - // by the given owner. + /* Returns a new OneofDef with all the same fields. The OneofDef will be owned + * by the given owner. */ OneofDef* Dup(const void* owner) const; - // Iteration over fields. The order is undefined. + /* Iteration over fields. The order is undefined. */ class iterator : public std::iterator<std::forward_iterator_tag, FieldDef*> { public: explicit iterator(OneofDef* md); @@ -1263,30 +1163,19 @@ UPB_DEFINE_DEF(upb::OneofDef, oneofdef, ONEOF, UPB_QUOTE( const_iterator end() const; private: - UPB_DISALLOW_POD_OPS(OneofDef, upb::OneofDef); -), -UPB_DEFINE_STRUCT(upb_oneofdef, upb_def, - upb_strtable ntof; - upb_inttable itof; - const upb_msgdef *parent; -)); + UPB_DISALLOW_POD_OPS(OneofDef, upb::OneofDef) +}; -#define UPB_ONEOFDEF_INIT(name, ntof, itof, refs, ref2s) \ - { UPB_DEF_INIT(name, UPB_DEF_ENUM, refs, ref2s), ntof, itof } +#endif /* __cplusplus */ -UPB_BEGIN_EXTERN_C // { +UPB_BEGIN_EXTERN_C -// Native C API. +/* Native C API. */ upb_oneofdef *upb_oneofdef_new(const void *owner); upb_oneofdef *upb_oneofdef_dup(const upb_oneofdef *o, const void *owner); -// From upb_refcounted. -void upb_oneofdef_unref(const upb_oneofdef *o, const void *owner); -bool upb_oneofdef_isfrozen(const upb_oneofdef *e); -void upb_oneofdef_ref(const upb_oneofdef *o, const void *owner); -void upb_oneofdef_donateref(const upb_oneofdef *m, const void *from, - const void *to); -void upb_oneofdef_checkref(const upb_oneofdef *o, const void *owner); +/* Include upb_refcounted methods like upb_oneofdef_ref(). */ +UPB_REFCOUNTED_CMETHODS(upb_oneofdef, upb_oneofdef_upcast2) const char *upb_oneofdef_name(const upb_oneofdef *o); bool upb_oneofdef_setname(upb_oneofdef *o, const char *name, upb_status *s); @@ -1297,10 +1186,10 @@ bool upb_oneofdef_addfield(upb_oneofdef *o, upb_fielddef *f, const void *ref_donor, upb_status *s); -// Oneof lookups: -// - ntof: look up a field by name. -// - ntofz: look up a field by name (as a null-terminated string). -// - itof: look up a field by number. +/* Oneof lookups: + * - ntof: look up a field by name. + * - ntofz: look up a field by name (as a null-terminated string). + * - itof: look up a field by number. */ const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o, const char *name, size_t length); UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o, @@ -1309,17 +1198,18 @@ UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o, } const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num); -// upb_oneof_iter i; -// for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) { -// // ... -// } +/* upb_oneof_iter i; + * for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) { + * // ... + * } + */ void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o); void upb_oneof_next(upb_oneof_iter *iter); bool upb_oneof_done(upb_oneof_iter *iter); upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter); void upb_oneof_iter_setdone(upb_oneof_iter *iter); -UPB_END_EXTERN_C // } +UPB_END_EXTERN_C #ifdef __cplusplus @@ -1328,21 +1218,12 @@ UPB_INLINE const char* upb_safecstr(const std::string& str) { return str.c_str(); } -// Inline C++ wrappers. +/* Inline C++ wrappers. */ namespace upb { inline Def* Def::Dup(const void* owner) const { return upb_def_dup(this, owner); } -inline bool Def::IsFrozen() const { return upb_def_isfrozen(this); } -inline void Def::Ref(const void* owner) const { upb_def_ref(this, owner); } -inline void Def::Unref(const void* owner) const { upb_def_unref(this, owner); } -inline void Def::DonateRef(const void* from, const void* to) const { - upb_def_donateref(this, from, to); -} -inline void Def::CheckRef(const void* owner) const { - upb_def_checkref(this, owner); -} inline Def::Type Def::def_type() const { return upb_def_type(this); } inline const char* Def::full_name() const { return upb_def_fullname(this); } inline bool Def::set_full_name(const char* fullname, Status* s) { @@ -1394,19 +1275,6 @@ inline reffed_ptr<FieldDef> FieldDef::New() { inline FieldDef* FieldDef::Dup(const void* owner) const { return upb_fielddef_dup(this, owner); } -inline bool FieldDef::IsFrozen() const { return upb_fielddef_isfrozen(this); } -inline void FieldDef::Ref(const void* owner) const { - upb_fielddef_ref(this, owner); -} -inline void FieldDef::Unref(const void* owner) const { - upb_fielddef_unref(this, owner); -} -inline void FieldDef::DonateRef(const void* from, const void* to) const { - upb_fielddef_donateref(this, from, to); -} -inline void FieldDef::CheckRef(const void* owner) const { - upb_fielddef_checkref(this, owner); -} inline const char* FieldDef::full_name() const { return upb_fielddef_fullname(this); } @@ -1572,19 +1440,6 @@ inline reffed_ptr<MessageDef> MessageDef::New() { upb_msgdef *m = upb_msgdef_new(&m); return reffed_ptr<MessageDef>(m, &m); } -inline bool MessageDef::IsFrozen() const { return upb_msgdef_isfrozen(this); } -inline void MessageDef::Ref(const void* owner) const { - return upb_msgdef_ref(this, owner); -} -inline void MessageDef::Unref(const void* owner) const { - return upb_msgdef_unref(this, owner); -} -inline void MessageDef::DonateRef(const void* from, const void* to) const { - return upb_msgdef_donateref(this, from, to); -} -inline void MessageDef::CheckRef(const void* owner) const { - return upb_msgdef_checkref(this, owner); -} inline const char *MessageDef::full_name() const { return upb_msgdef_fullname(this); } @@ -1772,19 +1627,6 @@ inline reffed_ptr<EnumDef> EnumDef::New() { upb_enumdef *e = upb_enumdef_new(&e); return reffed_ptr<EnumDef>(e, &e); } -inline bool EnumDef::IsFrozen() const { return upb_enumdef_isfrozen(this); } -inline void EnumDef::Ref(const void* owner) const { - return upb_enumdef_ref(this, owner); -} -inline void EnumDef::Unref(const void* owner) const { - return upb_enumdef_unref(this, owner); -} -inline void EnumDef::DonateRef(const void* from, const void* to) const { - return upb_enumdef_donateref(this, from, to); -} -inline void EnumDef::CheckRef(const void* owner) const { - return upb_enumdef_checkref(this, owner); -} inline const char* EnumDef::full_name() const { return upb_enumdef_fullname(this); } @@ -1837,19 +1679,6 @@ inline reffed_ptr<OneofDef> OneofDef::New() { upb_oneofdef *o = upb_oneofdef_new(&o); return reffed_ptr<OneofDef>(o, &o); } -inline bool OneofDef::IsFrozen() const { return upb_oneofdef_isfrozen(this); } -inline void OneofDef::Ref(const void* owner) const { - return upb_oneofdef_ref(this, owner); -} -inline void OneofDef::Unref(const void* owner) const { - return upb_oneofdef_unref(this, owner); -} -inline void OneofDef::DonateRef(const void* from, const void* to) const { - return upb_oneofdef_donateref(this, from, to); -} -inline void OneofDef::CheckRef(const void* owner) const { - return upb_oneofdef_checkref(this, owner); -} inline const char* OneofDef::full_name() const { return upb_oneofdef_name(this); } @@ -1931,11 +1760,7 @@ inline bool OneofDef::const_iterator::operator!=( return !(*this == other); } -} // namespace upb +} /* namespace upb */ #endif -#undef UPB_DEFINE_DEF -#undef UPB_DEF_CASTS -#undef UPB_CPP_CASTS - #endif /* UPB_DEF_H_ */ |