|
||||
File indexing completed on 2025-01-30 10:25:23
0001 // Protocol Buffers - Google's data interchange format 0002 // Copyright 2023 Google LLC. All rights reserved. 0003 // 0004 // Use of this source code is governed by a BSD-style 0005 // license that can be found in the LICENSE file or at 0006 // https://developers.google.com/open-source/licenses/bsd 0007 0008 #ifndef UPB_MESSAGE_MAP_H_ 0009 #define UPB_MESSAGE_MAP_H_ 0010 0011 #include <stddef.h> 0012 0013 #include "upb/base/descriptor_constants.h" 0014 #include "upb/mem/arena.h" 0015 #include "upb/message/internal/map.h" 0016 #include "upb/message/value.h" 0017 #include "upb/mini_table/field.h" 0018 #include "upb/mini_table/message.h" 0019 0020 // Must be last. 0021 #include "upb/port/def.inc" 0022 0023 typedef struct upb_Map upb_Map; 0024 0025 #ifdef __cplusplus 0026 extern "C" { 0027 #endif 0028 0029 // Creates a new map on the given arena with the given key/value size. 0030 UPB_API upb_Map* upb_Map_New(upb_Arena* a, upb_CType key_type, 0031 upb_CType value_type); 0032 0033 // Returns the number of entries in the map. 0034 UPB_API size_t upb_Map_Size(const upb_Map* map); 0035 0036 // Stores a value for the given key into |*val| (or the zero value if the key is 0037 // not present). Returns whether the key was present. The |val| pointer may be 0038 // NULL, in which case the function tests whether the given key is present. 0039 UPB_API bool upb_Map_Get(const upb_Map* map, upb_MessageValue key, 0040 upb_MessageValue* val); 0041 0042 // Removes all entries in the map. 0043 UPB_API void upb_Map_Clear(upb_Map* map); 0044 0045 // Sets the given key to the given value, returning whether the key was inserted 0046 // or replaced. If the key was inserted, then any existing iterators will be 0047 // invalidated. 0048 UPB_API upb_MapInsertStatus upb_Map_Insert(upb_Map* map, upb_MessageValue key, 0049 upb_MessageValue val, 0050 upb_Arena* arena); 0051 0052 // Sets the given key to the given value. Returns false if memory allocation 0053 // failed. If the key is newly inserted, then any existing iterators will be 0054 // invalidated. 0055 UPB_API_INLINE bool upb_Map_Set(upb_Map* map, upb_MessageValue key, 0056 upb_MessageValue val, upb_Arena* arena) { 0057 return upb_Map_Insert(map, key, val, arena) != 0058 kUpb_MapInsertStatus_OutOfMemory; 0059 } 0060 0061 // Deletes this key from the table. Returns true if the key was present. 0062 // If present and |val| is non-NULL, stores the deleted value. 0063 UPB_API bool upb_Map_Delete(upb_Map* map, upb_MessageValue key, 0064 upb_MessageValue* val); 0065 0066 // Map iteration: 0067 // 0068 // size_t iter = kUpb_Map_Begin; 0069 // upb_MessageValue key, val; 0070 // while (upb_Map_Next(map, &key, &val, &iter)) { 0071 // ... 0072 // } 0073 0074 #define kUpb_Map_Begin ((size_t) - 1) 0075 0076 // Advances to the next entry. Returns false if no more entries are present. 0077 // Otherwise returns true and populates both *key and *value. 0078 UPB_API bool upb_Map_Next(const upb_Map* map, upb_MessageValue* key, 0079 upb_MessageValue* val, size_t* iter); 0080 0081 // Sets the value for the entry pointed to by iter. 0082 // WARNING: this does not currently work for string values! 0083 UPB_API void upb_Map_SetEntryValue(upb_Map* map, size_t iter, 0084 upb_MessageValue val); 0085 0086 // DEPRECATED iterator, slated for removal. 0087 0088 /* Map iteration: 0089 * 0090 * size_t iter = kUpb_Map_Begin; 0091 * while (upb_MapIterator_Next(map, &iter)) { 0092 * upb_MessageValue key = upb_MapIterator_Key(map, iter); 0093 * upb_MessageValue val = upb_MapIterator_Value(map, iter); 0094 * } 0095 */ 0096 0097 // Advances to the next entry. Returns false if no more entries are present. 0098 UPB_API bool upb_MapIterator_Next(const upb_Map* map, size_t* iter); 0099 0100 // Returns true if the iterator still points to a valid entry, or false if the 0101 // iterator is past the last element. It is an error to call this function with 0102 // kUpb_Map_Begin (you must call next() at least once first). 0103 UPB_API bool upb_MapIterator_Done(const upb_Map* map, size_t iter); 0104 0105 // Returns the key and value for this entry of the map. 0106 UPB_API upb_MessageValue upb_MapIterator_Key(const upb_Map* map, size_t iter); 0107 UPB_API upb_MessageValue upb_MapIterator_Value(const upb_Map* map, size_t iter); 0108 0109 // Mark a map and all of its descendents as frozen/immutable. 0110 // If the map values are messages then |m| must point to the minitable for 0111 // those messages. Otherwise |m| must be NULL. 0112 UPB_API void upb_Map_Freeze(upb_Map* map, const upb_MiniTable* m); 0113 0114 // Returns whether a map has been frozen. 0115 UPB_API_INLINE bool upb_Map_IsFrozen(const upb_Map* map); 0116 0117 #ifdef __cplusplus 0118 } /* extern "C" */ 0119 #endif 0120 0121 #include "upb/port/undef.inc" 0122 0123 #endif /* UPB_MESSAGE_MAP_H_ */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |