1 /* 2 * Copyright (c) 2014-2019 Pavel Kalvoda <me@pavelkalvoda.com> 3 * 4 * libcbor is free software; you can redistribute it and/or modify 5 * it under the terms of the MIT license. See LICENSE for details. 6 */ 7 8 #ifndef LIBCBOR_MAPS_H 9 #define LIBCBOR_MAPS_H 10 11 #include "cbor/common.h" 12 13 #ifdef __cplusplus 14 extern "C" { 15 #endif 16 17 /* 18 * ============================================================================ 19 * Map manipulation 20 * ============================================================================ 21 */ 22 23 /** Get the number of pairs 24 * 25 * @param item[borrow] A map 26 * @return The number of pairs 27 */ 28 size_t cbor_map_size(const cbor_item_t *item); 29 30 /** Get the size of the allocated storage 31 * 32 * @param item[borrow] A map 33 * @return Allocated storage size (as the number of #cbor_pair items) 34 */ 35 size_t cbor_map_allocated(const cbor_item_t *item); 36 37 /** Create a new definite map 38 * 39 * @param size The number of slots to preallocate 40 * @return **new** definite map. `NULL` on malloc failure. 41 */ 42 cbor_item_t *cbor_new_definite_map(size_t size); 43 44 /** Create a new indefinite map 45 * 46 * @param size The number of slots to preallocate 47 * @return **new** definite map. `NULL` on malloc failure. 48 */ 49 cbor_item_t *cbor_new_indefinite_map(); 50 51 /** Add a pair to the map 52 * 53 * For definite maps, items can only be added to the preallocated space. For 54 * indefinite maps, the storage will be expanded as needed 55 * 56 * @param item[borrow] A map 57 * @param pair[incref] The key-value pair to add (incref is member-wise) 58 * @return `true` on success, `false` if either reallocation failed or the 59 * preallcoated storage is full 60 */ 61 bool cbor_map_add(cbor_item_t *item, struct cbor_pair pair); 62 63 /** Add a key to the map 64 * 65 * Sets the value to `NULL`. Internal API. 66 * 67 * @param item[borrow] A map 68 * @param key[incref] The key 69 * @return `true` on success, `false` if either reallocation failed or the 70 * preallcoated storage is full 71 */ 72 bool _cbor_map_add_key(cbor_item_t *item, cbor_item_t *key); 73 74 /** Add a value to the map 75 * 76 * Assumes that #_cbor_map_add_key has been called. Internal API. 77 * 78 * @param item[borrow] A map 79 * @param key[incref] The value 80 * @return `true` on success, `false` if either reallocation failed or the 81 * preallcoated storage is full 82 */ 83 bool _cbor_map_add_value(cbor_item_t *item, cbor_item_t *value); 84 85 /** Is this map definite? 86 * 87 * @param item[borrow] A map 88 * @return Is this map definite? 89 */ 90 bool cbor_map_is_definite(const cbor_item_t *item); 91 92 /** Is this map indefinite? 93 * 94 * @param item[borrow] A map 95 * @return Is this map indefinite? 96 */ 97 bool cbor_map_is_indefinite(const cbor_item_t *item); 98 99 /** Get the pairs storage 100 * 101 * @param item[borrow] A map 102 * @return Array of #cbor_map_size pairs. Manipulation is possible as long as 103 * references remain valid. 104 */ 105 struct cbor_pair *cbor_map_handle(const cbor_item_t *item); 106 107 #ifdef __cplusplus 108 } 109 #endif 110 111 #endif // LIBCBOR_MAPS_H 112