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_MESSAGE_ARRAY_H_ 9 #define UPB_MESSAGE_ARRAY_H_ 10 11 #include <stddef.h> 12 13 #include "upb/base/descriptor_constants.h" 14 #include "upb/mem/arena.h" 15 #include "upb/message/internal/array.h" 16 #include "upb/message/value.h" 17 #include "upb/mini_table/field.h" 18 #include "upb/mini_table/message.h" 19 20 // Must be last. 21 #include "upb/port/def.inc" 22 23 typedef struct upb_Array upb_Array; 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 29 // Creates a new array on the given arena that holds elements of this type. 30 UPB_API upb_Array* upb_Array_New(upb_Arena* a, upb_CType type); 31 32 // Returns the number of elements in the array. 33 UPB_API_INLINE size_t upb_Array_Size(const upb_Array* arr); 34 35 // Returns the given element, which must be within the array's current size. 36 UPB_API upb_MessageValue upb_Array_Get(const upb_Array* arr, size_t i); 37 38 // Returns a mutating pointer to the given element, which must be within the 39 // array's current size. 40 UPB_API upb_MutableMessageValue upb_Array_GetMutable(upb_Array* arr, size_t i); 41 42 // Sets the given element, which must be within the array's current size. 43 UPB_API void upb_Array_Set(upb_Array* arr, size_t i, upb_MessageValue val); 44 45 // Appends an element to the array. Returns false on allocation failure. 46 UPB_API bool upb_Array_Append(upb_Array* array, upb_MessageValue val, 47 upb_Arena* arena); 48 49 // Moves elements within the array using memmove(). 50 // Like memmove(), the source and destination elements may be overlapping. 51 UPB_API void upb_Array_Move(upb_Array* array, size_t dst_idx, size_t src_idx, 52 size_t count); 53 54 // Inserts one or more empty elements into the array. 55 // Existing elements are shifted right. 56 // The new elements have undefined state and must be set with `upb_Array_Set()`. 57 // REQUIRES: `i <= upb_Array_Size(arr)` 58 UPB_API bool upb_Array_Insert(upb_Array* array, size_t i, size_t count, 59 upb_Arena* arena); 60 61 // Deletes one or more elements from the array. 62 // Existing elements are shifted left. 63 // REQUIRES: `i + count <= upb_Array_Size(arr)` 64 UPB_API void upb_Array_Delete(upb_Array* array, size_t i, size_t count); 65 66 // Reserves |size| elements of storage for the array. 67 UPB_API_INLINE bool upb_Array_Reserve(struct upb_Array* array, size_t size, 68 upb_Arena* arena); 69 70 // Changes the size of a vector. New elements are initialized to NULL/0. 71 // Returns false on allocation failure. 72 UPB_API bool upb_Array_Resize(upb_Array* array, size_t size, upb_Arena* arena); 73 74 // Returns pointer to array data. 75 UPB_API_INLINE const void* upb_Array_DataPtr(const upb_Array* arr); 76 77 // Returns mutable pointer to array data. 78 UPB_API_INLINE void* upb_Array_MutableDataPtr(upb_Array* arr); 79 80 // Mark an array and all of its descendents as frozen/immutable. 81 // If the array elements are messages then |m| must point to the minitable for 82 // those messages. Otherwise |m| must be NULL. 83 UPB_API void upb_Array_Freeze(upb_Array* arr, const upb_MiniTable* m); 84 85 // Returns whether an array has been frozen. 86 UPB_API_INLINE bool upb_Array_IsFrozen(const upb_Array* arr); 87 88 #ifdef __cplusplus 89 } /* extern "C" */ 90 #endif 91 92 #include "upb/port/undef.inc" 93 94 #endif /* UPB_MESSAGE_ARRAY_H_ */ 95