Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-24 09:28:43

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/internal/types.h"
0017 #include "upb/message/value.h"
0018 #include "upb/mini_table/field.h"
0019 #include "upb/mini_table/message.h"
0020 
0021 // Must be last.
0022 #include "upb/port/def.inc"
0023 
0024 typedef struct upb_Map upb_Map;
0025 
0026 #ifdef __cplusplus
0027 extern "C" {
0028 #endif
0029 
0030 // Creates a new map on the given arena with the given key/value size.
0031 UPB_API upb_Map* upb_Map_New(upb_Arena* a, upb_CType key_type,
0032                              upb_CType value_type);
0033 
0034 // Returns the number of entries in the map.
0035 UPB_API size_t upb_Map_Size(const upb_Map* map);
0036 
0037 // Stores a value for the given key into |*val| (or the zero value if the key is
0038 // not present). Returns whether the key was present. The |val| pointer may be
0039 // NULL, in which case the function tests whether the given key is present.
0040 UPB_API bool upb_Map_Get(const upb_Map* map, upb_MessageValue key,
0041                          upb_MessageValue* val);
0042 
0043 // Returns a mutable pointer to the value for the given key. Returns NULL if the
0044 // key is not present.
0045 // This function is only legal to call for maps that contain messages.
0046 UPB_API struct upb_Message* upb_Map_GetMutable(upb_Map* map,
0047                                                upb_MessageValue key);
0048 
0049 // Removes all entries in the map.
0050 UPB_API void upb_Map_Clear(upb_Map* map);
0051 
0052 // Sets the given key to the given value, returning whether the key was inserted
0053 // or replaced. If the key was inserted, then any existing iterators will be
0054 // invalidated.
0055 UPB_API upb_MapInsertStatus upb_Map_Insert(upb_Map* map, upb_MessageValue key,
0056                                            upb_MessageValue val,
0057                                            upb_Arena* arena);
0058 
0059 // Sets the given key to the given value. Returns false if memory allocation
0060 // failed. If the key is newly inserted, then any existing iterators will be
0061 // invalidated.
0062 UPB_API_INLINE bool upb_Map_Set(upb_Map* map, upb_MessageValue key,
0063                                 upb_MessageValue val, upb_Arena* arena) {
0064   return upb_Map_Insert(map, key, val, arena) !=
0065          kUpb_MapInsertStatus_OutOfMemory;
0066 }
0067 
0068 // Deletes this key from the table. Returns true if the key was present.
0069 // If present and |val| is non-NULL, stores the deleted value.
0070 UPB_API bool upb_Map_Delete(upb_Map* map, upb_MessageValue key,
0071                             upb_MessageValue* val);
0072 
0073 // Map iteration:
0074 //
0075 // size_t iter = kUpb_Map_Begin;
0076 // upb_MessageValue key, val;
0077 // while (upb_Map_Next(map, &key, &val, &iter)) {
0078 //   ...
0079 // }
0080 
0081 #define kUpb_Map_Begin ((size_t)-1)
0082 
0083 // Advances to the next entry. Returns false if no more entries are present.
0084 // Otherwise returns true and populates both *key and *value.
0085 UPB_API bool upb_Map_Next(const upb_Map* map, upb_MessageValue* key,
0086                           upb_MessageValue* val, size_t* iter);
0087 
0088 // Sets the value for the entry pointed to by iter.
0089 // WARNING: this does not currently work for string values!
0090 UPB_API void upb_Map_SetEntryValue(upb_Map* map, size_t iter,
0091                                    upb_MessageValue val);
0092 
0093 // DEPRECATED iterator, slated for removal.
0094 
0095 /* Map iteration:
0096  *
0097  * size_t iter = kUpb_Map_Begin;
0098  * while (upb_MapIterator_Next(map, &iter)) {
0099  *   upb_MessageValue key = upb_MapIterator_Key(map, iter);
0100  *   upb_MessageValue val = upb_MapIterator_Value(map, iter);
0101  * }
0102  */
0103 
0104 // Advances to the next entry. Returns false if no more entries are present.
0105 UPB_API bool upb_MapIterator_Next(const upb_Map* map, size_t* iter);
0106 
0107 // Returns true if the iterator still points to a valid entry, or false if the
0108 // iterator is past the last element. It is an error to call this function with
0109 // kUpb_Map_Begin (you must call next() at least once first).
0110 UPB_API bool upb_MapIterator_Done(const upb_Map* map, size_t iter);
0111 
0112 // Returns the key and value for this entry of the map.
0113 UPB_API upb_MessageValue upb_MapIterator_Key(const upb_Map* map, size_t iter);
0114 UPB_API upb_MessageValue upb_MapIterator_Value(const upb_Map* map, size_t iter);
0115 
0116 // Mark a map and all of its descendents as frozen/immutable.
0117 // If the map values are messages then |m| must point to the minitable for
0118 // those messages. Otherwise |m| must be NULL.
0119 UPB_API void upb_Map_Freeze(upb_Map* map, const upb_MiniTable* m);
0120 
0121 // Returns whether a map has been frozen.
0122 UPB_API_INLINE bool upb_Map_IsFrozen(const upb_Map* map);
0123 
0124 #ifdef __cplusplus
0125 } /* extern "C" */
0126 #endif
0127 
0128 #include "upb/port/undef.inc"
0129 
0130 #endif /* UPB_MESSAGE_MAP_H_ */