1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2023 Google LLC. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 #ifndef UPB_REFLECTION_MESSAGE_H_ 9 #define UPB_REFLECTION_MESSAGE_H_ 10 11 #include <stddef.h> 12 13 #include "upb/mem/arena.h" 14 #include "upb/message/map.h" 15 #include "upb/message/message.h" 16 #include "upb/reflection/common.h" 17 18 // Must be last. 19 #include "upb/port/def.inc" 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 // Returns a mutable pointer to a map, array, or submessage value. If the given 26 // arena is non-NULL this will construct a new object if it was not previously 27 // present. May not be called for primitive fields. 28 UPB_API upb_MutableMessageValue upb_Message_Mutable(upb_Message* msg, 29 const upb_FieldDef* f, 30 upb_Arena* a); 31 32 // Returns the field that is set in the oneof, or NULL if none are set. 33 UPB_API const upb_FieldDef* upb_Message_WhichOneofByDef(const upb_Message* msg, 34 const upb_OneofDef* o); 35 36 // Clear all data and unknown fields. 37 void upb_Message_ClearByDef(upb_Message* msg, const upb_MessageDef* m); 38 39 // Clears any field presence and sets the value back to its default. 40 UPB_API void upb_Message_ClearFieldByDef(upb_Message* msg, 41 const upb_FieldDef* f); 42 43 // May only be called for fields where upb_FieldDef_HasPresence(f) == true. 44 UPB_API bool upb_Message_HasFieldByDef(const upb_Message* msg, 45 const upb_FieldDef* f); 46 47 // Returns the value in the message associated with this field def. 48 UPB_API upb_MessageValue upb_Message_GetFieldByDef(const upb_Message* msg, 49 const upb_FieldDef* f); 50 51 // Sets the given field to the given value. For a msg/array/map/string, the 52 // caller must ensure that the target data outlives |msg| (by living either in 53 // the same arena or a different arena that outlives it). 54 // 55 // Returns false if allocation fails. 56 UPB_API bool upb_Message_SetFieldByDef(upb_Message* msg, const upb_FieldDef* f, 57 upb_MessageValue val, upb_Arena* a); 58 59 // Iterate over present fields. 60 // 61 // size_t iter = kUpb_Message_Begin; 62 // const upb_FieldDef *f; 63 // upb_MessageValue val; 64 // while (upb_Message_Next(msg, m, ext_pool, &f, &val, &iter)) { 65 // process_field(f, val); 66 // } 67 // 68 // If ext_pool is NULL, no extensions will be returned. If the given symtab 69 // returns extensions that don't match what is in this message, those extensions 70 // will be skipped. 71 72 #define kUpb_Message_Begin -1 73 74 UPB_API bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m, 75 const upb_DefPool* ext_pool, 76 const upb_FieldDef** f, upb_MessageValue* val, 77 size_t* iter); 78 79 // Clears all unknown field data from this message and all submessages. 80 UPB_API bool upb_Message_DiscardUnknown(upb_Message* msg, 81 const upb_MessageDef* m, int maxdepth); 82 83 #ifdef __cplusplus 84 } /* extern "C" */ 85 #endif 86 87 #include "upb/port/undef.inc" 88 89 #endif /* UPB_REFLECTION_MESSAGE_H_ */ 90