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
18 // Must be last.
19 #include "upb/port/def.inc"
20
21 typedef struct upb_Array upb_Array;
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 // Creates a new array on the given arena that holds elements of this type.
28 UPB_API upb_Array* upb_Array_New(upb_Arena* a, upb_CType type);
29
30 // Returns the number of elements in the array.
upb_Array_Size(const upb_Array * arr)31 UPB_API_INLINE size_t upb_Array_Size(const upb_Array* arr) {
32 return UPB_PRIVATE(_upb_Array_Size)(arr);
33 }
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 // Changes the size of a vector. New elements are initialized to NULL/0.
67 // Returns false on allocation failure.
68 UPB_API bool upb_Array_Resize(upb_Array* array, size_t size, upb_Arena* arena);
69
70 // Returns pointer to array data.
upb_Array_DataPtr(const upb_Array * arr)71 UPB_API_INLINE const void* upb_Array_DataPtr(const upb_Array* arr) {
72 return UPB_PRIVATE(_upb_Array_DataPtr)(arr);
73 }
74
75 // Returns mutable pointer to array data.
upb_Array_MutableDataPtr(upb_Array * arr)76 UPB_API_INLINE void* upb_Array_MutableDataPtr(upb_Array* arr) {
77 return UPB_PRIVATE(_upb_Array_MutableDataPtr)(arr);
78 }
79
80 #ifdef __cplusplus
81 } /* extern "C" */
82 #endif
83
84 #include "upb/port/undef.inc"
85
86 #endif /* UPB_MESSAGE_ARRAY_H_ */
87