You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mini_table_accessors.h 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2009-2022, Google LLC
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of Google LLC nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
  20. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef UPB_MINI_TABLE_ACCESSORS_H_
  28. #define UPB_MINI_TABLE_ACCESSORS_H_
  29. #include "upb/array.h"
  30. #include "upb/internal/mini_table_accessors.h"
  31. #include "upb/msg_internal.h"
  32. // Must be last.
  33. #include "upb/port_def.inc"
  34. #ifdef __cplusplus
  35. extern "C"
  36. {
  37. #endif
  38. bool upb_MiniTable_HasField(const upb_Message* msg, const upb_MiniTable_Field* field);
  39. void upb_MiniTable_ClearField(upb_Message* msg, const upb_MiniTable_Field* field);
  40. UPB_INLINE bool upb_MiniTable_GetBool(const upb_Message* msg, const upb_MiniTable_Field* field)
  41. {
  42. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Bool);
  43. return *UPB_PTR_AT(msg, field->offset, bool);
  44. }
  45. UPB_INLINE void upb_MiniTable_SetBool(upb_Message* msg, const upb_MiniTable_Field* field, bool value)
  46. {
  47. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Bool);
  48. _upb_MiniTable_SetPresence(msg, field);
  49. *UPB_PTR_AT(msg, field->offset, bool) = value;
  50. }
  51. UPB_INLINE int32_t upb_MiniTable_GetInt32(const upb_Message* msg, const upb_MiniTable_Field* field)
  52. {
  53. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Int32 || field->descriptortype == kUpb_FieldType_SInt32 || field->descriptortype == kUpb_FieldType_SFixed32);
  54. return *UPB_PTR_AT(msg, field->offset, int32_t);
  55. }
  56. UPB_INLINE void upb_MiniTable_SetInt32(upb_Message* msg, const upb_MiniTable_Field* field, int32_t value)
  57. {
  58. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Int32 || field->descriptortype == kUpb_FieldType_SInt32 || field->descriptortype == kUpb_FieldType_SFixed32);
  59. _upb_MiniTable_SetPresence(msg, field);
  60. *UPB_PTR_AT(msg, field->offset, int32_t) = value;
  61. }
  62. UPB_INLINE uint32_t upb_MiniTable_GetUInt32(const upb_Message* msg, const upb_MiniTable_Field* field)
  63. {
  64. UPB_ASSERT(field->descriptortype == kUpb_FieldType_UInt32 || field->descriptortype == kUpb_FieldType_Fixed32);
  65. return *UPB_PTR_AT(msg, field->offset, uint32_t);
  66. }
  67. UPB_INLINE void upb_MiniTable_SetUInt32(upb_Message* msg, const upb_MiniTable_Field* field, uint32_t value)
  68. {
  69. UPB_ASSERT(field->descriptortype == kUpb_FieldType_UInt32 || field->descriptortype == kUpb_FieldType_Fixed32);
  70. _upb_MiniTable_SetPresence(msg, field);
  71. *UPB_PTR_AT(msg, field->offset, uint32_t) = value;
  72. }
  73. UPB_INLINE int32_t upb_MiniTable_GetEnum(const upb_Message* msg, const upb_MiniTable_Field* field)
  74. {
  75. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Enum);
  76. return *UPB_PTR_AT(msg, field->offset, int32_t);
  77. }
  78. UPB_INLINE void upb_MiniTable_SetEnum(upb_Message* msg, const upb_MiniTable_Field* field, int32_t value)
  79. {
  80. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Enum);
  81. _upb_MiniTable_SetPresence(msg, field);
  82. *UPB_PTR_AT(msg, field->offset, int32_t) = value;
  83. }
  84. UPB_INLINE int64_t upb_MiniTable_GetInt64(const upb_Message* msg, const upb_MiniTable_Field* field)
  85. {
  86. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Int64 || field->descriptortype == kUpb_FieldType_SInt64 || field->descriptortype == kUpb_FieldType_SFixed64);
  87. return *UPB_PTR_AT(msg, field->offset, int64_t);
  88. }
  89. UPB_INLINE void upb_MiniTable_SetInt64(upb_Message* msg, const upb_MiniTable_Field* field, int64_t value)
  90. {
  91. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Int64 || field->descriptortype == kUpb_FieldType_SInt64 || field->descriptortype == kUpb_FieldType_SFixed64);
  92. _upb_MiniTable_SetPresence(msg, field);
  93. *UPB_PTR_AT(msg, field->offset, int64_t) = value;
  94. }
  95. UPB_INLINE uint64_t upb_MiniTable_GetUInt64(const upb_Message* msg, const upb_MiniTable_Field* field)
  96. {
  97. UPB_ASSERT(field->descriptortype == kUpb_FieldType_UInt64 || field->descriptortype == kUpb_FieldType_Fixed64);
  98. return *UPB_PTR_AT(msg, field->offset, uint64_t);
  99. }
  100. UPB_INLINE void upb_MiniTable_SetUInt64(upb_Message* msg, const upb_MiniTable_Field* field, uint64_t value)
  101. {
  102. UPB_ASSERT(field->descriptortype == kUpb_FieldType_UInt64 || field->descriptortype == kUpb_FieldType_Fixed64);
  103. _upb_MiniTable_SetPresence(msg, field);
  104. *UPB_PTR_AT(msg, field->offset, uint64_t) = value;
  105. }
  106. UPB_INLINE float upb_MiniTable_GetFloat(const upb_Message* msg, const upb_MiniTable_Field* field)
  107. {
  108. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Float);
  109. return *UPB_PTR_AT(msg, field->offset, float);
  110. }
  111. UPB_INLINE void upb_MiniTable_SetFloat(upb_Message* msg, const upb_MiniTable_Field* field, float value)
  112. {
  113. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Float);
  114. _upb_MiniTable_SetPresence(msg, field);
  115. *UPB_PTR_AT(msg, field->offset, float) = value;
  116. }
  117. UPB_INLINE double upb_MiniTable_GetDouble(const upb_Message* msg, const upb_MiniTable_Field* field)
  118. {
  119. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Double);
  120. return *UPB_PTR_AT(msg, field->offset, double);
  121. }
  122. UPB_INLINE void upb_MiniTable_SetDouble(upb_Message* msg, const upb_MiniTable_Field* field, double value)
  123. {
  124. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Double);
  125. _upb_MiniTable_SetPresence(msg, field);
  126. *UPB_PTR_AT(msg, field->offset, double) = value;
  127. }
  128. UPB_INLINE upb_StringView upb_MiniTable_GetString(
  129. const upb_Message* msg, const upb_MiniTable_Field* field
  130. )
  131. {
  132. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Bytes || field->descriptortype == kUpb_FieldType_String);
  133. return *UPB_PTR_AT(msg, field->offset, upb_StringView);
  134. }
  135. UPB_INLINE void upb_MiniTable_SetString(upb_Message* msg, const upb_MiniTable_Field* field, upb_StringView value)
  136. {
  137. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Bytes || field->descriptortype == kUpb_FieldType_String);
  138. _upb_MiniTable_SetPresence(msg, field);
  139. *UPB_PTR_AT(msg, field->offset, upb_StringView) = value;
  140. }
  141. UPB_INLINE const upb_Message* upb_MiniTable_GetMessage(
  142. const upb_Message* msg, const upb_MiniTable_Field* field
  143. )
  144. {
  145. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Message || field->descriptortype == kUpb_FieldType_Group);
  146. return *UPB_PTR_AT(msg, field->offset, const upb_Message*);
  147. }
  148. UPB_INLINE void upb_MiniTable_SetMessage(upb_Message* msg, const upb_MiniTable_Field* field, upb_Message* sub_message)
  149. {
  150. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Message || field->descriptortype == kUpb_FieldType_Group);
  151. _upb_MiniTable_SetPresence(msg, field);
  152. *UPB_PTR_AT(msg, field->offset, const upb_Message*) = sub_message;
  153. }
  154. UPB_INLINE upb_Message* upb_MiniTable_GetMutableMessage(
  155. upb_Message* msg, const upb_MiniTable* mini_table, const upb_MiniTable_Field* field, upb_Arena* arena
  156. )
  157. {
  158. UPB_ASSERT(field->descriptortype == kUpb_FieldType_Message || field->descriptortype == kUpb_FieldType_Group);
  159. upb_Message* sub_message = *UPB_PTR_AT(msg, field->offset, upb_Message*);
  160. if (!sub_message)
  161. {
  162. sub_message =
  163. _upb_Message_New(mini_table->subs[field->submsg_index].submsg, arena);
  164. *UPB_PTR_AT(msg, field->offset, upb_Message*) = sub_message;
  165. _upb_MiniTable_SetPresence(msg, field);
  166. }
  167. return sub_message;
  168. }
  169. UPB_INLINE const upb_Array* upb_MiniTable_GetArray(
  170. const upb_Message* msg, const upb_MiniTable_Field* field
  171. )
  172. {
  173. return (const upb_Array*)*UPB_PTR_AT(msg, field->offset, upb_Array*);
  174. }
  175. UPB_INLINE upb_Array* upb_MiniTable_GetMutableArray(
  176. upb_Message* msg, const upb_MiniTable_Field* field
  177. )
  178. {
  179. return (upb_Array*)*UPB_PTR_AT(msg, field->offset, upb_Array*);
  180. }
  181. void* upb_MiniTable_ResizeArray(upb_Message* msg, const upb_MiniTable_Field* field, size_t len, upb_Arena* arena);
  182. typedef enum
  183. {
  184. kUpb_GetExtension_Ok,
  185. kUpb_GetExtension_NotPresent,
  186. kUpb_GetExtension_ParseError,
  187. kUpb_GetExtension_OutOfMemory,
  188. } upb_GetExtension_Status;
  189. typedef enum
  190. {
  191. kUpb_GetExtensionAsBytes_Ok,
  192. kUpb_GetExtensionAsBytes_NotPresent,
  193. kUpb_GetExtensionAsBytes_EncodeError,
  194. } upb_GetExtensionAsBytes_Status;
  195. // Returns a message extension or promotes an unknown field to
  196. // an extension.
  197. //
  198. // TODO(ferhat): Only supports extension fields that are messages,
  199. // expand support to include non-message types.
  200. upb_GetExtension_Status upb_MiniTable_GetOrPromoteExtension(
  201. upb_Message* msg, const upb_MiniTable_Extension* ext_table, int decode_options, upb_Arena* arena, const upb_Message_Extension** extension
  202. );
  203. // Returns a message extension or unknown field matching the extension
  204. // data as bytes.
  205. //
  206. // If an extension has already been decoded it will be re-encoded
  207. // to bytes.
  208. upb_GetExtensionAsBytes_Status upb_MiniTable_GetExtensionAsBytes(
  209. const upb_Message* msg, const upb_MiniTable_Extension* ext_table, int encode_options, upb_Arena* arena, const char** extension_data, size_t* len
  210. );
  211. #ifdef __cplusplus
  212. } /* extern "C" */
  213. #endif
  214. #include "upb/port_undef.inc"
  215. #endif // UPB_MINI_TABLE_ACCESSORS_H_